This is my patch patch.1n for perl5.001.
[p5sagit/p5-mst-13.2.git] / ext / Socket / Socket.pm
1 package Socket;
2
3 =head1 NAME
4
5 Socket - load the C socket.h defines and structure manipulators
6
7 =head1 SYNOPSIS
8
9     use Socket;
10
11     $proto = (getprotobyname('udp'))[2];         
12     socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
13     $sockaddr_in = pack_sockaddr_in(AF_INET,7,inet_aton("localhost"));
14     $sockaddr_in = pack_sockaddr_in(AF_INET,7,INADDR_LOOPBACK);
15     connect(Socket_Handle,$sockaddr_in);
16     $peer = inet_ntoa((unpack_sockaddr_in(getpeername(Socket_Handle)))[2]);
17
18
19 =head1 DESCRIPTION
20
21 This module is just a translation of the C F<socket.h> file.
22 Unlike the old mechanism of requiring a translated F<socket.ph>
23 file, this uses the B<h2xs> program (see the Perl source distribution)
24 and your native C compiler.  This means that it has a 
25 far more likely chance of getting the numbers right.
26
27 In addition, some structure manipulation functions are available:
28
29 =item inet_aton HOSTNAME
30
31 Takes a string giving the name of a host, and translates that
32 to the 4-byte string (structure). Takes arguments of both
33 the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
34 cannot be resolved, returns undef.
35
36 =item inet_ntoa IP_ADDRESS
37
38 Takes a four byte ip address (as returned by inet_aton())
39 and translates it into a string of the form 'd.d.d.d'
40 where the 'd's are numbers less than 256 (the normal
41 readable four dotted number notation for internet addresses).
42
43 =item INADDR_ANY
44
45 Note - does not return a number.
46
47 Returns the 4-byte wildcard ip address which specifies any
48 of the hosts ip addresses. (A particular machine can have
49 more than one ip address, each address corresponding to
50 a particular network interface. This wildcard address
51 allows you to bind to all of them simultaneously.)
52 Normally equivalent to inet_aton('0.0.0.0').
53
54 =item INADDR_LOOPBACK
55
56 Note - does not return a number.
57
58 Returns the 4-byte loopback address. Normally equivalent
59 to inet_aton('localhost').
60
61 =item INADDR_NONE
62
63 Note - does not return a number.
64
65 Returns the 4-byte invalid ip address. Normally equivalent
66 to inet_aton('255.255.255.255').
67
68 =item pack_sockaddr_in FAMILY, PORT, IP_ADDRESS
69
70 Takes three arguments, an address family (normally AF_INET),
71 a port number, and a 4 byte IP_ADDRESS (as returned by
72 inet_aton()). Returns the sockaddr_in structure with those
73 arguments packed in. For internet domain sockets, this structure
74 is normally what you need for the arguments in bind(), connect(),
75 and send(), and is also returned by getpeername(), getsockname()
76 and recv().
77
78 =item unpack_sockaddr_in SOCKADDR_IN
79
80 Takes a sockaddr_in structure (as returned by pack_sockaddr_in())
81 and returns an array of three elements: the address family,
82 the port, and the 4-byte ip-address.
83
84 =cut
85
86 use Carp;
87
88 require Exporter;
89 use AutoLoader;
90 require DynaLoader;
91 @ISA = qw(Exporter DynaLoader);
92 @EXPORT = qw(
93         inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
94         INADDR_ANY INADDR_LOOPBACK INADDR_NONE
95         AF_802
96         AF_APPLETALK
97         AF_CCITT
98         AF_CHAOS
99         AF_DATAKIT
100         AF_DECnet
101         AF_DLI
102         AF_ECMA
103         AF_GOSIP
104         AF_HYLINK
105         AF_IMPLINK
106         AF_INET
107         AF_LAT
108         AF_MAX
109         AF_NBS
110         AF_NIT
111         AF_NS
112         AF_OSI
113         AF_OSINET
114         AF_PUP
115         AF_SNA
116         AF_UNIX
117         AF_UNSPEC
118         AF_X25
119         MSG_DONTROUTE
120         MSG_MAXIOVLEN
121         MSG_OOB
122         MSG_PEEK
123         PF_802
124         PF_APPLETALK
125         PF_CCITT
126         PF_CHAOS
127         PF_DATAKIT
128         PF_DECnet
129         PF_DLI
130         PF_ECMA
131         PF_GOSIP
132         PF_HYLINK
133         PF_IMPLINK
134         PF_INET
135         PF_LAT
136         PF_MAX
137         PF_NBS
138         PF_NIT
139         PF_NS
140         PF_OSI
141         PF_OSINET
142         PF_PUP
143         PF_SNA
144         PF_UNIX
145         PF_UNSPEC
146         PF_X25
147         SOCK_DGRAM
148         SOCK_RAW
149         SOCK_RDM
150         SOCK_SEQPACKET
151         SOCK_STREAM
152         SOL_SOCKET
153         SOMAXCONN
154         SO_ACCEPTCONN
155         SO_BROADCAST
156         SO_DEBUG
157         SO_DONTLINGER
158         SO_DONTROUTE
159         SO_ERROR
160         SO_KEEPALIVE
161         SO_LINGER
162         SO_OOBINLINE
163         SO_RCVBUF
164         SO_RCVLOWAT
165         SO_RCVTIMEO
166         SO_REUSEADDR
167         SO_SNDBUF
168         SO_SNDLOWAT
169         SO_SNDTIMEO
170         SO_TYPE
171         SO_USELOOPBACK
172 );
173
174 sub AUTOLOAD {
175     local($constname);
176     ($constname = $AUTOLOAD) =~ s/.*:://;
177     $val = constant($constname, @_ ? $_[0] : 0);
178     if ($! != 0) {
179         if ($! =~ /Invalid/) {
180             $AutoLoader::AUTOLOAD = $AUTOLOAD;
181             goto &AutoLoader::AUTOLOAD;
182         }
183         else {
184             ($pack,$file,$line) = caller;
185             croak "Your vendor has not defined Socket macro $constname, used";
186         }
187     }
188     eval "sub $AUTOLOAD { $val }";
189     goto &$AUTOLOAD;
190 }
191
192 bootstrap Socket;
193
194 # Preloaded methods go here.  Autoload methods go after __END__, and are
195 # processed by the autosplit program.
196
197 1;
198 __END__