3 our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
8 Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
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);
36 $sun = sockaddr_un('/tmp/usock');
37 connect(Socket_Handle,$sun);
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:
61 =item inet_aton HOSTNAME
63 Takes a string giving the name of a host, and translates that
64 to an opaque string (struct in_adrr). 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 For portability do not assume that the result of inet_aton() is 32
70 bits wide, that it would contain only the IPv4 address in network
73 =item inet_ntoa IP_ADDRESS
75 Takes a string (an opaque string as returned by inet_aton(), or
76 a v-string representing the four octets of the IPv4 address in
77 network order) and translates it into a string of the form 'd.d.d.d'
78 where the 'd's are numbers less than 256 (the normal readable four
79 dotted number notation for internet addresses).
83 Note: does not return a number, but a packed string.
85 Returns the 4-byte wildcard ip address which specifies any
86 of the hosts ip addresses. (A particular machine can have
87 more than one ip address, each address corresponding to
88 a particular network interface. This wildcard address
89 allows you to bind to all of them simultaneously.)
90 Normally equivalent to inet_aton('0.0.0.0').
92 =item INADDR_BROADCAST
94 Note: does not return a number, but a packed string.
96 Returns the 4-byte 'this-lan' ip broadcast address.
97 This can be useful for some protocols to solicit information
98 from all servers on the same LAN cable.
99 Normally equivalent to inet_aton('255.255.255.255').
101 =item INADDR_LOOPBACK
103 Note - does not return a number.
105 Returns the 4-byte loopback address. Normally equivalent
106 to inet_aton('localhost').
110 Note - does not return a number.
112 Returns the 4-byte 'invalid' ip address. Normally equivalent
113 to inet_aton('255.255.255.255').
115 =item sockaddr_in PORT, ADDRESS
117 =item sockaddr_in SOCKADDR_IN
119 In a list context, unpacks its SOCKADDR_IN argument and returns an array
120 consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
121 ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
122 use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
124 =item pack_sockaddr_in PORT, IP_ADDRESS
126 Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
127 inet_aton()). Returns the sockaddr_in structure with those arguments
128 packed in with AF_INET filled in. For internet domain sockets, this
129 structure is normally what you need for the arguments in bind(),
130 connect(), and send(), and is also returned by getpeername(),
131 getsockname() and recv().
133 =item unpack_sockaddr_in SOCKADDR_IN
135 Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
136 returns an array of two elements: the port and the 4-byte ip-address.
137 Will croak if the structure does not have AF_INET in the right place.
139 =item sockaddr_un PATHNAME
141 =item sockaddr_un SOCKADDR_UN
143 In a list context, unpacks its SOCKADDR_UN argument and returns an array
144 consisting of (PATHNAME). In a scalar context, packs its PATHNAME
145 arguments as a SOCKADDR_UN and returns it. If this is confusing, use
146 pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
147 These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
149 =item pack_sockaddr_un PATH
151 Takes one argument, a pathname. Returns the sockaddr_un structure with
152 that path packed in with AF_UNIX filled in. For unix domain sockets, this
153 structure is normally what you need for the arguments in bind(),
154 connect(), and send(), and is also returned by getpeername(),
155 getsockname() and recv().
157 =item unpack_sockaddr_un SOCKADDR_UN
159 Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
160 and returns the pathname. Will croak if the structure does not
161 have AF_UNIX in the right place.
168 use warnings::register;
174 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
175 pack_sockaddr_un unpack_sockaddr_un
176 sockaddr_in sockaddr_un
177 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
285 @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
295 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
296 all => [@EXPORT, @EXPORT_OK],
302 sub CRLF () {"\015\012"}
310 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
311 my($af, $port, @quad) = @_;
312 warnings::warn "6-ARG sockaddr_in call is deprecated"
313 if warnings::enabled();
314 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
315 } elsif (wantarray) {
316 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
317 unpack_sockaddr_in(@_);
319 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
320 pack_sockaddr_in(@_);
326 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
327 unpack_sockaddr_un(@_);
329 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
330 pack_sockaddr_un(@_);
336 ($constname = $AUTOLOAD) =~ s/.*:://;
337 croak "&Socket::constant not defined" if $constname eq 'constant';
338 my ($error, $val) = constant($constname);
342 *$AUTOLOAD = sub { $val };
346 XSLoader::load 'Socket', $VERSION;