This is patch.2b1g to perl5.002beta1.
[p5sagit/p5-mst-13.2.git] / ext / Socket / Socket.pm
CommitLineData
a0d0e21e 1package Socket;
25f94b33 2$VERSION = 1.5;
3b35bae3 3
4=head1 NAME
5
cb1a09d0 6Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
3b35bae3 7
8=head1 SYNOPSIS
9
10 use Socket;
11
4633a7c4 12 $proto = getprotobyname('udp');
8e07c86e 13 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
4633a7c4 14 $iaddr = gethostbyname('hishost.com');
15 $port = getservbyname('time', 'udp');
16 $sin = sockaddr_in($port, $iaddr);
17 send(Socket_Handle, 0, 0, $sin);
18
19 $proto = getprotobyname('tcp');
20 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
21 $port = getservbyname('smtp');
22 $sin = sockaddr_in($port,inet_aton("127.1"));
23 $sin = sockaddr_in(7,inet_aton("localhost"));
24 $sin = sockaddr_in(7,INADDR_LOOPBACK);
25 connect(Socket_Handle,$sin);
26
27 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
28 $peer_host = gethostbyaddr($iaddr, AF_INET);
29 $peer_addr = inet_ntoa($iaddr);
30
31 $proto = getprotobyname('tcp');
32 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
33 unlink('/tmp/usock');
34 $sun = sockaddr_un('/tmp/usock');
35 connect(Socket_Handle,$sun);
3b35bae3 36
37=head1 DESCRIPTION
38
39This module is just a translation of the C F<socket.h> file.
40Unlike the old mechanism of requiring a translated F<socket.ph>
41file, this uses the B<h2xs> program (see the Perl source distribution)
42and your native C compiler. This means that it has a
4633a7c4 43far more likely chance of getting the numbers right. This includes
44all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
3b35bae3 45
8e07c86e 46In addition, some structure manipulation functions are available:
47
48=item inet_aton HOSTNAME
49
50Takes a string giving the name of a host, and translates that
51to the 4-byte string (structure). Takes arguments of both
52the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
53cannot be resolved, returns undef.
54
55=item inet_ntoa IP_ADDRESS
56
57Takes a four byte ip address (as returned by inet_aton())
58and translates it into a string of the form 'd.d.d.d'
59where the 'd's are numbers less than 256 (the normal
60readable four dotted number notation for internet addresses).
61
62=item INADDR_ANY
63
4633a7c4 64Note: does not return a number, but a packed string.
8e07c86e 65
66Returns the 4-byte wildcard ip address which specifies any
67of the hosts ip addresses. (A particular machine can have
68more than one ip address, each address corresponding to
69a particular network interface. This wildcard address
70allows you to bind to all of them simultaneously.)
71Normally equivalent to inet_aton('0.0.0.0').
72
73=item INADDR_LOOPBACK
74
75Note - does not return a number.
76
77Returns the 4-byte loopback address. Normally equivalent
78to inet_aton('localhost').
3b35bae3 79
8e07c86e 80=item INADDR_NONE
81
82Note - does not return a number.
83
84Returns the 4-byte invalid ip address. Normally equivalent
85to inet_aton('255.255.255.255').
86
4633a7c4 87=item sockaddr_in PORT, ADDRESS
88
89=item sockaddr_in SOCKADDR_IN
90
91In an array context, unpacks its SOCKADDR_IN argument and returns an array
92consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
93ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
94use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
95
96=item pack_sockaddr_in PORT, IP_ADDRESS
8e07c86e 97
4633a7c4 98Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
99inet_aton()). Returns the sockaddr_in structure with those arguments
100packed in with AF_INET filled in. For internet domain sockets, this
101structure is normally what you need for the arguments in bind(),
102connect(), and send(), and is also returned by getpeername(),
103getsockname() and recv().
8e07c86e 104
105=item unpack_sockaddr_in SOCKADDR_IN
106
4633a7c4 107Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
108returns an array of two elements: the port and the 4-byte ip-address.
109Will croak if the structure does not have AF_INET in the right place.
110
111=item sockaddr_un PATHNAME
112
113=item sockaddr_un SOCKADDR_UN
114
115In an array context, unpacks its SOCKADDR_UN argument and returns an array
116consisting of (PATHNAME). In a scalar context, packs its PATHANE
117arguments as a SOCKADDR_UN and returns it. If this is confusing, use
118pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
25f94b33 119These are only supported if your system has <sys/un.h>.
4633a7c4 120
121=item pack_sockaddr_un PATH
122
123Takes one argument, a pathname. Returns the sockaddr_un structure with
124that path packed in with AF_UNIX filled in. For unix domain sockets, this
125structure is normally what you need for the arguments in bind(),
126connect(), and send(), and is also returned by getpeername(),
127getsockname() and recv().
128
129=item unpack_sockaddr_un SOCKADDR_UN
130
131Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
132and returns the pathname. Will croak if the structure does not
133have AF_UNIX in the right place.
3b35bae3 134
135=cut
136
a0d0e21e 137use Carp;
138
139require Exporter;
3b35bae3 140use AutoLoader;
a0d0e21e 141require DynaLoader;
fec02dd3 142@ISA = qw(Exporter DynaLoader);
a0d0e21e 143@EXPORT = qw(
8e07c86e 144 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
4633a7c4 145 pack_sockaddr_un unpack_sockaddr_un
146 sockaddr_in sockaddr_un
8e07c86e 147 INADDR_ANY INADDR_LOOPBACK INADDR_NONE
a0d0e21e 148 AF_802
149 AF_APPLETALK
150 AF_CCITT
151 AF_CHAOS
152 AF_DATAKIT
153 AF_DECnet
154 AF_DLI
155 AF_ECMA
156 AF_GOSIP
157 AF_HYLINK
158 AF_IMPLINK
159 AF_INET
160 AF_LAT
161 AF_MAX
162 AF_NBS
163 AF_NIT
164 AF_NS
165 AF_OSI
166 AF_OSINET
167 AF_PUP
168 AF_SNA
169 AF_UNIX
170 AF_UNSPEC
171 AF_X25
172 MSG_DONTROUTE
173 MSG_MAXIOVLEN
174 MSG_OOB
175 MSG_PEEK
176 PF_802
177 PF_APPLETALK
178 PF_CCITT
179 PF_CHAOS
180 PF_DATAKIT
181 PF_DECnet
182 PF_DLI
183 PF_ECMA
184 PF_GOSIP
185 PF_HYLINK
186 PF_IMPLINK
187 PF_INET
188 PF_LAT
189 PF_MAX
190 PF_NBS
191 PF_NIT
192 PF_NS
193 PF_OSI
194 PF_OSINET
195 PF_PUP
196 PF_SNA
197 PF_UNIX
198 PF_UNSPEC
199 PF_X25
200 SOCK_DGRAM
201 SOCK_RAW
202 SOCK_RDM
203 SOCK_SEQPACKET
204 SOCK_STREAM
205 SOL_SOCKET
206 SOMAXCONN
207 SO_ACCEPTCONN
208 SO_BROADCAST
209 SO_DEBUG
210 SO_DONTLINGER
211 SO_DONTROUTE
212 SO_ERROR
213 SO_KEEPALIVE
214 SO_LINGER
215 SO_OOBINLINE
216 SO_RCVBUF
217 SO_RCVLOWAT
218 SO_RCVTIMEO
219 SO_REUSEADDR
220 SO_SNDBUF
221 SO_SNDLOWAT
222 SO_SNDTIMEO
223 SO_TYPE
224 SO_USELOOPBACK
225);
226
4633a7c4 227sub sockaddr_in {
228 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
229 my($af, $port, @quad) = @_;
230 carp "6-ARG sockaddr_in call is deprecated" if $^W;
231 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
232 } elsif (wantarray) {
233 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
234 unpack_sockaddr_in(@_);
235 } else {
236 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
237 pack_sockaddr_in(@_);
238 }
239}
240
241sub sockaddr_un {
242 if (wantarray) {
243 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
244 unpack_sockaddr_un(@_);
245 } else {
37120919 246 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
247 pack_sockaddr_un(@_);
4633a7c4 248 }
249}
250
251
a0d0e21e 252sub AUTOLOAD {
a0d0e21e 253 local($constname);
254 ($constname = $AUTOLOAD) =~ s/.*:://;
255 $val = constant($constname, @_ ? $_[0] : 0);
256 if ($! != 0) {
257 if ($! =~ /Invalid/) {
258 $AutoLoader::AUTOLOAD = $AUTOLOAD;
259 goto &AutoLoader::AUTOLOAD;
260 }
261 else {
262 ($pack,$file,$line) = caller;
263 croak "Your vendor has not defined Socket macro $constname, used";
264 }
265 }
266 eval "sub $AUTOLOAD { $val }";
267 goto &$AUTOLOAD;
268}
269
270bootstrap Socket;
271
272# Preloaded methods go here. Autoload methods go after __END__, and are
273# processed by the autosplit program.
274
2751;
276__END__