final touches for lexical warnings (from Paul Marquess)
[p5sagit/p5-mst-13.2.git] / ext / Socket / Socket.pm
CommitLineData
a0d0e21e 1package Socket;
73c78b0a 2
17f410f9 3our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
d3a7d8c7 4$VERSION = "1.72";
3b35bae3 5
6=head1 NAME
7
cb1a09d0 8Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
3b35bae3 9
10=head1 SYNOPSIS
11
12 use Socket;
13
4633a7c4 14 $proto = getprotobyname('udp');
8e07c86e 15 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
4633a7c4 16 $iaddr = gethostbyname('hishost.com');
17 $port = getservbyname('time', 'udp');
18 $sin = sockaddr_in($port, $iaddr);
19 send(Socket_Handle, 0, 0, $sin);
20
21 $proto = getprotobyname('tcp');
22 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
67d7c167 23 $port = getservbyname('smtp', 'tcp');
4633a7c4 24 $sin = sockaddr_in($port,inet_aton("127.1"));
25 $sin = sockaddr_in(7,inet_aton("localhost"));
26 $sin = sockaddr_in(7,INADDR_LOOPBACK);
27 connect(Socket_Handle,$sin);
28
29 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
30 $peer_host = gethostbyaddr($iaddr, AF_INET);
31 $peer_addr = inet_ntoa($iaddr);
32
33 $proto = getprotobyname('tcp');
34 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
35 unlink('/tmp/usock');
36 $sun = sockaddr_un('/tmp/usock');
37 connect(Socket_Handle,$sun);
3b35bae3 38
39=head1 DESCRIPTION
40
41This module is just a translation of the C F<socket.h> file.
42Unlike the old mechanism of requiring a translated F<socket.ph>
43file, this uses the B<h2xs> program (see the Perl source distribution)
44and your native C compiler. This means that it has a
4633a7c4 45far more likely chance of getting the numbers right. This includes
46all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
3b35bae3 47
fdb41d65 48Also, some common socket "newline" constants are provided: the
49constants C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and
50C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do
51not want to use the literal characters in your programs, then use
52the constants provided here. They are not exported by default, but can
53be imported individually, and with the C<:crlf> export tag:
54
55 use Socket qw(:DEFAULT :crlf);
56
8e07c86e 57In addition, some structure manipulation functions are available:
58
2ae324a7 59=over
60
8e07c86e 61=item inet_aton HOSTNAME
62
63Takes a string giving the name of a host, and translates that
64to the 4-byte string (structure). Takes arguments of both
65the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
7e1af8bc 66cannot be resolved, returns undef. For multi-homed hosts (hosts
67with more than one address), the first address found is returned.
8e07c86e 68
69=item inet_ntoa IP_ADDRESS
70
71Takes a four byte ip address (as returned by inet_aton())
72and translates it into a string of the form 'd.d.d.d'
73where the 'd's are numbers less than 256 (the normal
74readable four dotted number notation for internet addresses).
75
76=item INADDR_ANY
77
4633a7c4 78Note: does not return a number, but a packed string.
8e07c86e 79
80Returns the 4-byte wildcard ip address which specifies any
81of the hosts ip addresses. (A particular machine can have
82more than one ip address, each address corresponding to
83a particular network interface. This wildcard address
84allows you to bind to all of them simultaneously.)
85Normally equivalent to inet_aton('0.0.0.0').
86
7e1af8bc 87=item INADDR_BROADCAST
88
89Note: does not return a number, but a packed string.
90
91Returns the 4-byte 'this-lan' ip broadcast address.
92This can be useful for some protocols to solicit information
93from all servers on the same LAN cable.
94Normally equivalent to inet_aton('255.255.255.255').
95
8e07c86e 96=item INADDR_LOOPBACK
97
98Note - does not return a number.
99
100Returns the 4-byte loopback address. Normally equivalent
101to inet_aton('localhost').
3b35bae3 102
8e07c86e 103=item INADDR_NONE
104
105Note - does not return a number.
106
7e1af8bc 107Returns the 4-byte 'invalid' ip address. Normally equivalent
8e07c86e 108to inet_aton('255.255.255.255').
109
4633a7c4 110=item sockaddr_in PORT, ADDRESS
111
112=item sockaddr_in SOCKADDR_IN
113
114In an array context, unpacks its SOCKADDR_IN argument and returns an array
115consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
116ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
117use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
118
119=item pack_sockaddr_in PORT, IP_ADDRESS
8e07c86e 120
4633a7c4 121Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
122inet_aton()). Returns the sockaddr_in structure with those arguments
123packed in with AF_INET filled in. For internet domain sockets, this
124structure is normally what you need for the arguments in bind(),
125connect(), and send(), and is also returned by getpeername(),
126getsockname() and recv().
8e07c86e 127
128=item unpack_sockaddr_in SOCKADDR_IN
129
4633a7c4 130Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
131returns an array of two elements: the port and the 4-byte ip-address.
132Will croak if the structure does not have AF_INET in the right place.
133
134=item sockaddr_un PATHNAME
135
136=item sockaddr_un SOCKADDR_UN
137
138In an array context, unpacks its SOCKADDR_UN argument and returns an array
1fef88e7 139consisting of (PATHNAME). In a scalar context, packs its PATHNAME
4633a7c4 140arguments as a SOCKADDR_UN and returns it. If this is confusing, use
141pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
1fef88e7 142These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
4633a7c4 143
144=item pack_sockaddr_un PATH
145
146Takes one argument, a pathname. Returns the sockaddr_un structure with
147that path packed in with AF_UNIX filled in. For unix domain sockets, this
148structure is normally what you need for the arguments in bind(),
149connect(), and send(), and is also returned by getpeername(),
150getsockname() and recv().
151
152=item unpack_sockaddr_un SOCKADDR_UN
153
154Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
155and returns the pathname. Will croak if the structure does not
156have AF_UNIX in the right place.
3b35bae3 157
2ae324a7 158=back
159
3b35bae3 160=cut
161
a0d0e21e 162use Carp;
d3a7d8c7 163use warnings::register;
a0d0e21e 164
165require Exporter;
9426adcd 166use XSLoader ();
167@ISA = qw(Exporter);
a0d0e21e 168@EXPORT = qw(
8e07c86e 169 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
4633a7c4 170 pack_sockaddr_un unpack_sockaddr_un
171 sockaddr_in sockaddr_un
7e1af8bc 172 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
a0d0e21e 173 AF_802
174 AF_APPLETALK
175 AF_CCITT
176 AF_CHAOS
177 AF_DATAKIT
178 AF_DECnet
179 AF_DLI
180 AF_ECMA
181 AF_GOSIP
182 AF_HYLINK
183 AF_IMPLINK
184 AF_INET
185 AF_LAT
186 AF_MAX
187 AF_NBS
188 AF_NIT
189 AF_NS
190 AF_OSI
191 AF_OSINET
192 AF_PUP
193 AF_SNA
194 AF_UNIX
195 AF_UNSPEC
196 AF_X25
6b1016b5 197 IOV_MAX
198 MSG_BCAST
de4597cb 199 MSG_CTLFLAGS
200 MSG_CTLIGNORE
201 MSG_CTRUNC
a0d0e21e 202 MSG_DONTROUTE
de4597cb 203 MSG_DONTWAIT
204 MSG_EOF
205 MSG_EOR
206 MSG_ERRQUEUE
207 MSG_FIN
a0d0e21e 208 MSG_MAXIOVLEN
6b1016b5 209 MSG_MCAST
de4597cb 210 MSG_NOSIGNAL
a0d0e21e 211 MSG_OOB
212 MSG_PEEK
de4597cb 213 MSG_PROXY
214 MSG_RST
215 MSG_SYN
216 MSG_TRUNC
217 MSG_URG
218 MSG_WAITALL
a0d0e21e 219 PF_802
220 PF_APPLETALK
221 PF_CCITT
222 PF_CHAOS
223 PF_DATAKIT
224 PF_DECnet
225 PF_DLI
226 PF_ECMA
227 PF_GOSIP
228 PF_HYLINK
229 PF_IMPLINK
230 PF_INET
231 PF_LAT
232 PF_MAX
233 PF_NBS
234 PF_NIT
235 PF_NS
236 PF_OSI
237 PF_OSINET
238 PF_PUP
239 PF_SNA
240 PF_UNIX
241 PF_UNSPEC
242 PF_X25
de4597cb 243 SCM_CONNECT
244 SCM_CREDENTIALS
245 SCM_CREDS
246 SCM_RIGHTS
247 SCM_TIMESTAMP
ca6e1c26 248 SHUT_RD
249 SHUT_RDWR
250 SHUT_WR
a0d0e21e 251 SOCK_DGRAM
252 SOCK_RAW
253 SOCK_RDM
254 SOCK_SEQPACKET
255 SOCK_STREAM
256 SOL_SOCKET
257 SOMAXCONN
258 SO_ACCEPTCONN
259 SO_BROADCAST
260 SO_DEBUG
261 SO_DONTLINGER
262 SO_DONTROUTE
263 SO_ERROR
264 SO_KEEPALIVE
265 SO_LINGER
266 SO_OOBINLINE
267 SO_RCVBUF
268 SO_RCVLOWAT
269 SO_RCVTIMEO
270 SO_REUSEADDR
271 SO_SNDBUF
272 SO_SNDLOWAT
273 SO_SNDTIMEO
274 SO_TYPE
275 SO_USELOOPBACK
6b1016b5 276 UIO_MAXIOV
a0d0e21e 277);
278
1494e794 279@EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
280
281 IPPROTO_TCP
282 TCP_KEEPALIVE
283 TCP_MAXRT
284 TCP_MAXSEG
285 TCP_NODELAY
286 TCP_STDURG);
fdb41d65 287
288%EXPORT_TAGS = (
dde527fc 289 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
fdb41d65 290 all => [@EXPORT, @EXPORT_OK],
291);
292
293BEGIN {
294 sub CR () {"\015"}
295 sub LF () {"\012"}
296 sub CRLF () {"\015\012"}
297}
298
299*CR = \CR();
300*LF = \LF();
301*CRLF = \CRLF();
302
4633a7c4 303sub sockaddr_in {
304 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
305 my($af, $port, @quad) = @_;
d3a7d8c7 306 warnings::warn "6-ARG sockaddr_in call is deprecated"
307 if warnings::enabled();
4633a7c4 308 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
309 } elsif (wantarray) {
310 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
311 unpack_sockaddr_in(@_);
312 } else {
313 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
314 pack_sockaddr_in(@_);
315 }
316}
317
318sub sockaddr_un {
319 if (wantarray) {
320 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
321 unpack_sockaddr_un(@_);
322 } else {
37120919 323 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
324 pack_sockaddr_un(@_);
4633a7c4 325 }
326}
327
ca6e1c26 328sub INADDR_ANY ();
329sub INADDR_BROADCAST ();
330sub INADDR_LOOPBACK ();
331sub INADDR_LOOPBACK ();
332
333sub AF_802 ();
334sub AF_APPLETALK ();
335sub AF_CCITT ();
336sub AF_CHAOS ();
337sub AF_DATAKIT ();
338sub AF_DECnet ();
339sub AF_DLI ();
340sub AF_ECMA ();
341sub AF_GOSIP ();
342sub AF_HYLINK ();
343sub AF_IMPLINK ();
344sub AF_INET ();
345sub AF_LAT ();
346sub AF_MAX ();
347sub AF_NBS ();
348sub AF_NIT ();
349sub AF_NS ();
350sub AF_OSI ();
351sub AF_OSINET ();
352sub AF_PUP ();
353sub AF_SNA ();
354sub AF_UNIX ();
355sub AF_UNSPEC ();
356sub AF_X25 ();
357sub IOV_MAX ();
358sub MSG_BCAST ();
359sub MSG_CTLFLAGS ();
360sub MSG_CTLIGNORE ();
361sub MSG_CTRUNC ();
362sub MSG_DONTROUTE ();
363sub MSG_DONTWAIT ();
364sub MSG_EOF ();
365sub MSG_EOR ();
366sub MSG_ERRQUEUE ();
367sub MSG_FIN ();
368sub MSG_MAXIOVLEN ();
369sub MSG_MCAST ();
370sub MSG_NOSIGNAL ();
371sub MSG_OOB ();
372sub MSG_PEEK ();
373sub MSG_PROXY ();
374sub MSG_RST ();
375sub MSG_SYN ();
376sub MSG_TRUNC ();
377sub MSG_URG ();
378sub MSG_WAITALL ();
379sub PF_802 ();
380sub PF_APPLETALK ();
381sub PF_CCITT ();
382sub PF_CHAOS ();
383sub PF_DATAKIT ();
384sub PF_DECnet ();
385sub PF_DLI ();
386sub PF_ECMA ();
387sub PF_GOSIP ();
388sub PF_HYLINK ();
389sub PF_IMPLINK ();
390sub PF_INET ();
391sub PF_LAT ();
392sub PF_MAX ();
393sub PF_NBS ();
394sub PF_NIT ();
395sub PF_NS ();
396sub PF_OSI ();
397sub PF_OSINET ();
398sub PF_PUP ();
399sub PF_SNA ();
400sub PF_UNIX ();
401sub PF_UNSPEC ();
402sub PF_X25 ();
403sub SCM_CONNECT ();
404sub SCM_CREDENTIALS ();
405sub SCM_CREDS ();
406sub SCM_RIGHTS ();
407sub SCM_TIMESTAMP ();
408sub SHUT_RD ();
409sub SHUT_RDWR ();
410sub SHUT_WR ();
411sub SOCK_DGRAM ();
412sub SOCK_RAW ();
413sub SOCK_RDM ();
414sub SOCK_SEQPACKET ();
415sub SOCK_STREAM ();
416sub SOL_SOCKET ();
417sub SOMAXCONN ();
418sub SO_ACCEPTCONN ();
419sub SO_BROADCAST ();
420sub SO_DEBUG ();
421sub SO_DONTLINGER ();
422sub SO_DONTROUTE ();
423sub SO_ERROR ();
424sub SO_KEEPALIVE ();
425sub SO_LINGER ();
426sub SO_OOBINLINE ();
427sub SO_RCVBUF ();
428sub SO_RCVLOWAT ();
429sub SO_RCVTIMEO ();
430sub SO_REUSEADDR ();
431sub SO_SNDBUF ();
432sub SO_SNDLOWAT ();
433sub SO_SNDTIMEO ();
434sub SO_TYPE ();
435sub SO_USELOOPBACK ();
436sub UIO_MAXIOV ();
4633a7c4 437
a0d0e21e 438sub AUTOLOAD {
73c78b0a 439 my($constname);
a0d0e21e 440 ($constname = $AUTOLOAD) =~ s/.*:://;
73c78b0a 441 my $val = constant($constname, @_ ? $_[0] : 0);
a0d0e21e 442 if ($! != 0) {
676d8cc7 443 my ($pack,$file,$line) = caller;
444 croak "Your vendor has not defined Socket macro $constname, used";
a0d0e21e 445 }
ca6e1c26 446 eval "sub $AUTOLOAD () { $val }";
a0d0e21e 447 goto &$AUTOLOAD;
448}
449
9426adcd 450XSLoader::load 'Socket', $VERSION;
a0d0e21e 451
a0d0e21e 4521;