Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / msys / Socket.pm
blobd89b2f66b37898f8ab0d95758cb1e1c6e5b494d7
1 package Socket;
3 our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
4 $VERSION = "1.72";
6 =head1 NAME
8 Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
10 =head1 SYNOPSIS
12 use Socket;
14 $proto = getprotobyname('udp');
15 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
16 $iaddr = gethostbyname('hishost.com');
17 $port = getservbyname('time', 'udp');
18 $sin = sockaddr_in($port, $iaddr);
19 send(Socket_Handle, 0, 0, $sin);
21 $proto = getprotobyname('tcp');
22 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
23 $port = getservbyname('smtp', 'tcp');
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);
29 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
30 $peer_host = gethostbyaddr($iaddr, AF_INET);
31 $peer_addr = inet_ntoa($iaddr);
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);
39 =head1 DESCRIPTION
41 This module is just a translation of the C F<socket.h> file.
42 Unlike the old mechanism of requiring a translated F<socket.ph>
43 file, this uses the B<h2xs> program (see the Perl source distribution)
44 and your native C compiler. This means that it has a
45 far more likely chance of getting the numbers right. This includes
46 all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
48 Also, some common socket "newline" constants are provided: the
49 constants C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and
50 C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do
51 not want to use the literal characters in your programs, then use
52 the constants provided here. They are not exported by default, but can
53 be imported individually, and with the C<:crlf> export tag:
55 use Socket qw(:DEFAULT :crlf);
57 In addition, some structure manipulation functions are available:
59 =over
61 =item inet_aton HOSTNAME
63 Takes a string giving the name of a host, and translates that
64 to the 4-byte string (structure). Takes arguments of both
65 the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
66 cannot be resolved, returns undef. For multi-homed hosts (hosts
67 with more than one address), the first address found is returned.
69 =item inet_ntoa IP_ADDRESS
71 Takes a four byte ip address (as returned by inet_aton())
72 and translates it into a string of the form 'd.d.d.d'
73 where the 'd's are numbers less than 256 (the normal
74 readable four dotted number notation for internet addresses).
76 =item INADDR_ANY
78 Note: does not return a number, but a packed string.
80 Returns the 4-byte wildcard ip address which specifies any
81 of the hosts ip addresses. (A particular machine can have
82 more than one ip address, each address corresponding to
83 a particular network interface. This wildcard address
84 allows you to bind to all of them simultaneously.)
85 Normally equivalent to inet_aton('0.0.0.0').
87 =item INADDR_BROADCAST
89 Note: does not return a number, but a packed string.
91 Returns the 4-byte 'this-lan' ip broadcast address.
92 This can be useful for some protocols to solicit information
93 from all servers on the same LAN cable.
94 Normally equivalent to inet_aton('255.255.255.255').
96 =item INADDR_LOOPBACK
98 Note - does not return a number.
100 Returns the 4-byte loopback address. Normally equivalent
101 to inet_aton('localhost').
103 =item INADDR_NONE
105 Note - does not return a number.
107 Returns the 4-byte 'invalid' ip address. Normally equivalent
108 to inet_aton('255.255.255.255').
110 =item sockaddr_in PORT, ADDRESS
112 =item sockaddr_in SOCKADDR_IN
114 In a list context, unpacks its SOCKADDR_IN argument and returns an array
115 consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
116 ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
117 use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
119 =item pack_sockaddr_in PORT, IP_ADDRESS
121 Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
122 inet_aton()). Returns the sockaddr_in structure with those arguments
123 packed in with AF_INET filled in. For internet domain sockets, this
124 structure is normally what you need for the arguments in bind(),
125 connect(), and send(), and is also returned by getpeername(),
126 getsockname() and recv().
128 =item unpack_sockaddr_in SOCKADDR_IN
130 Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
131 returns an array of two elements: the port and the 4-byte ip-address.
132 Will croak if the structure does not have AF_INET in the right place.
134 =item sockaddr_un PATHNAME
136 =item sockaddr_un SOCKADDR_UN
138 In a list context, unpacks its SOCKADDR_UN argument and returns an array
139 consisting of (PATHNAME). In a scalar context, packs its PATHNAME
140 arguments as a SOCKADDR_UN and returns it. If this is confusing, use
141 pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
142 These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
144 =item pack_sockaddr_un PATH
146 Takes one argument, a pathname. Returns the sockaddr_un structure with
147 that path packed in with AF_UNIX filled in. For unix domain sockets, this
148 structure is normally what you need for the arguments in bind(),
149 connect(), and send(), and is also returned by getpeername(),
150 getsockname() and recv().
152 =item unpack_sockaddr_un SOCKADDR_UN
154 Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
155 and returns the pathname. Will croak if the structure does not
156 have AF_UNIX in the right place.
158 =back
160 =cut
162 use Carp;
163 use warnings::register;
165 require Exporter;
166 use XSLoader ();
167 @ISA = qw(Exporter);
168 @EXPORT = qw(
169 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
170 pack_sockaddr_un unpack_sockaddr_un
171 sockaddr_in sockaddr_un
172 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
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
197 IOV_MAX
198 MSG_BCAST
199 MSG_CTLFLAGS
200 MSG_CTLIGNORE
201 MSG_CTRUNC
202 MSG_DONTROUTE
203 MSG_DONTWAIT
204 MSG_EOF
205 MSG_EOR
206 MSG_ERRQUEUE
207 MSG_FIN
208 MSG_MAXIOVLEN
209 MSG_MCAST
210 MSG_NOSIGNAL
211 MSG_OOB
212 MSG_PEEK
213 MSG_PROXY
214 MSG_RST
215 MSG_SYN
216 MSG_TRUNC
217 MSG_URG
218 MSG_WAITALL
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
243 SCM_CONNECT
244 SCM_CREDENTIALS
245 SCM_CREDS
246 SCM_RIGHTS
247 SCM_TIMESTAMP
248 SHUT_RD
249 SHUT_RDWR
250 SHUT_WR
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_REUSEPORT
272 SO_SNDBUF
273 SO_SNDLOWAT
274 SO_SNDTIMEO
275 SO_TYPE
276 SO_USELOOPBACK
277 UIO_MAXIOV
280 @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
282 IPPROTO_TCP
283 TCP_KEEPALIVE
284 TCP_MAXRT
285 TCP_MAXSEG
286 TCP_NODELAY
287 TCP_STDURG);
289 %EXPORT_TAGS = (
290 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
291 all => [@EXPORT, @EXPORT_OK],
294 BEGIN {
295 sub CR () {"\015"}
296 sub LF () {"\012"}
297 sub CRLF () {"\015\012"}
300 *CR = \CR();
301 *LF = \LF();
302 *CRLF = \CRLF();
304 sub sockaddr_in {
305 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
306 my($af, $port, @quad) = @_;
307 warnings::warn "6-ARG sockaddr_in call is deprecated"
308 if warnings::enabled();
309 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
310 } elsif (wantarray) {
311 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
312 unpack_sockaddr_in(@_);
313 } else {
314 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
315 pack_sockaddr_in(@_);
319 sub sockaddr_un {
320 if (wantarray) {
321 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
322 unpack_sockaddr_un(@_);
323 } else {
324 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
325 pack_sockaddr_un(@_);
329 sub INADDR_ANY ();
330 sub INADDR_BROADCAST ();
331 sub INADDR_LOOPBACK ();
332 sub INADDR_LOOPBACK ();
334 sub AF_802 ();
335 sub AF_APPLETALK ();
336 sub AF_CCITT ();
337 sub AF_CHAOS ();
338 sub AF_DATAKIT ();
339 sub AF_DECnet ();
340 sub AF_DLI ();
341 sub AF_ECMA ();
342 sub AF_GOSIP ();
343 sub AF_HYLINK ();
344 sub AF_IMPLINK ();
345 sub AF_INET ();
346 sub AF_LAT ();
347 sub AF_MAX ();
348 sub AF_NBS ();
349 sub AF_NIT ();
350 sub AF_NS ();
351 sub AF_OSI ();
352 sub AF_OSINET ();
353 sub AF_PUP ();
354 sub AF_SNA ();
355 sub AF_UNIX ();
356 sub AF_UNSPEC ();
357 sub AF_X25 ();
358 sub IOV_MAX ();
359 sub MSG_BCAST ();
360 sub MSG_CTLFLAGS ();
361 sub MSG_CTLIGNORE ();
362 sub MSG_CTRUNC ();
363 sub MSG_DONTROUTE ();
364 sub MSG_DONTWAIT ();
365 sub MSG_EOF ();
366 sub MSG_EOR ();
367 sub MSG_ERRQUEUE ();
368 sub MSG_FIN ();
369 sub MSG_MAXIOVLEN ();
370 sub MSG_MCAST ();
371 sub MSG_NOSIGNAL ();
372 sub MSG_OOB ();
373 sub MSG_PEEK ();
374 sub MSG_PROXY ();
375 sub MSG_RST ();
376 sub MSG_SYN ();
377 sub MSG_TRUNC ();
378 sub MSG_URG ();
379 sub MSG_WAITALL ();
380 sub PF_802 ();
381 sub PF_APPLETALK ();
382 sub PF_CCITT ();
383 sub PF_CHAOS ();
384 sub PF_DATAKIT ();
385 sub PF_DECnet ();
386 sub PF_DLI ();
387 sub PF_ECMA ();
388 sub PF_GOSIP ();
389 sub PF_HYLINK ();
390 sub PF_IMPLINK ();
391 sub PF_INET ();
392 sub PF_LAT ();
393 sub PF_MAX ();
394 sub PF_NBS ();
395 sub PF_NIT ();
396 sub PF_NS ();
397 sub PF_OSI ();
398 sub PF_OSINET ();
399 sub PF_PUP ();
400 sub PF_SNA ();
401 sub PF_UNIX ();
402 sub PF_UNSPEC ();
403 sub PF_X25 ();
404 sub SCM_CONNECT ();
405 sub SCM_CREDENTIALS ();
406 sub SCM_CREDS ();
407 sub SCM_RIGHTS ();
408 sub SCM_TIMESTAMP ();
409 sub SHUT_RD ();
410 sub SHUT_RDWR ();
411 sub SHUT_WR ();
412 sub SOCK_DGRAM ();
413 sub SOCK_RAW ();
414 sub SOCK_RDM ();
415 sub SOCK_SEQPACKET ();
416 sub SOCK_STREAM ();
417 sub SOL_SOCKET ();
418 sub SOMAXCONN ();
419 sub SO_ACCEPTCONN ();
420 sub SO_BROADCAST ();
421 sub SO_DEBUG ();
422 sub SO_DONTLINGER ();
423 sub SO_DONTROUTE ();
424 sub SO_ERROR ();
425 sub SO_KEEPALIVE ();
426 sub SO_LINGER ();
427 sub SO_OOBINLINE ();
428 sub SO_RCVBUF ();
429 sub SO_RCVLOWAT ();
430 sub SO_RCVTIMEO ();
431 sub SO_REUSEADDR ();
432 sub SO_SNDBUF ();
433 sub SO_SNDLOWAT ();
434 sub SO_SNDTIMEO ();
435 sub SO_TYPE ();
436 sub SO_USELOOPBACK ();
437 sub UIO_MAXIOV ();
439 sub AUTOLOAD {
440 my($constname);
441 ($constname = $AUTOLOAD) =~ s/.*:://;
442 my $val = constant($constname, @_ ? $_[0] : 0);
443 if ($! != 0) {
444 my ($pack,$file,$line) = caller;
445 croak "Your vendor has not defined Socket macro $constname, used";
447 eval "sub $AUTOLOAD () { $val }";
448 goto &$AUTOLOAD;
451 XSLoader::load 'Socket', $VERSION;