6 Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C
7 socket.h defines and structure manipulators
13 $proto = getprotobyname('udp');
14 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
15 $iaddr = gethostbyname('hishost.com');
16 $port = getservbyname('time', 'udp');
17 $sin = sockaddr_in($port, $iaddr);
18 send(Socket_Handle, 0, 0, $sin);
20 $proto = getprotobyname('tcp');
21 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
22 $port = getservbyname('smtp');
23 $sin = sockaddr_in($port,inet_aton("127.1"));
24 $sin = sockaddr_in(7,inet_aton("localhost"));
25 $sin = sockaddr_in(7,INADDR_LOOPBACK);
26 connect(Socket_Handle,$sin);
28 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
29 $peer_host = gethostbyaddr($iaddr, AF_INET);
30 $peer_addr = inet_ntoa($iaddr);
32 $proto = getprotobyname('tcp');
33 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
35 $sun = sockaddr_un('/tmp/usock');
36 connect(Socket_Handle,$sun);
40 This module is just a translation of the C F<socket.h> file.
41 Unlike the old mechanism of requiring a translated F<socket.ph>
42 file, this uses the B<h2xs> program (see the Perl source distribution)
43 and your native C compiler. This means that it has a
44 far more likely chance of getting the numbers right. This includes
45 all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
47 In addition, some structure manipulation functions are available:
49 =item inet_aton HOSTNAME
51 Takes a string giving the name of a host, and translates that
52 to the 4-byte string (structure). Takes arguments of both
53 the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
54 cannot be resolved, returns undef.
56 =item inet_ntoa IP_ADDRESS
58 Takes a four byte ip address (as returned by inet_aton())
59 and translates it into a string of the form 'd.d.d.d'
60 where the 'd's are numbers less than 256 (the normal
61 readable four dotted number notation for internet addresses).
65 Note: does not return a number, but a packed string.
67 Returns the 4-byte wildcard ip address which specifies any
68 of the hosts ip addresses. (A particular machine can have
69 more than one ip address, each address corresponding to
70 a particular network interface. This wildcard address
71 allows you to bind to all of them simultaneously.)
72 Normally equivalent to inet_aton('0.0.0.0').
76 Note - does not return a number.
78 Returns the 4-byte loopback address. Normally equivalent
79 to inet_aton('localhost').
83 Note - does not return a number.
85 Returns the 4-byte invalid ip address. Normally equivalent
86 to inet_aton('255.255.255.255').
88 =item sockaddr_in PORT, ADDRESS
90 =item sockaddr_in SOCKADDR_IN
92 In an array context, unpacks its SOCKADDR_IN argument and returns an array
93 consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
94 ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
95 use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
97 =item pack_sockaddr_in PORT, IP_ADDRESS
99 Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
100 inet_aton()). Returns the sockaddr_in structure with those arguments
101 packed in with AF_INET filled in. For internet domain sockets, this
102 structure is normally what you need for the arguments in bind(),
103 connect(), and send(), and is also returned by getpeername(),
104 getsockname() and recv().
106 =item unpack_sockaddr_in SOCKADDR_IN
108 Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
109 returns an array of two elements: the port and the 4-byte ip-address.
110 Will croak if the structure does not have AF_INET in the right place.
112 =item sockaddr_un PATHNAME
114 =item sockaddr_un SOCKADDR_UN
116 In an array context, unpacks its SOCKADDR_UN argument and returns an array
117 consisting of (PATHNAME). In a scalar context, packs its PATHANE
118 arguments as a SOCKADDR_UN and returns it. If this is confusing, use
119 pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
120 These are only supported if your system has <sys/un.h>.
122 =item pack_sockaddr_un PATH
124 Takes one argument, a pathname. Returns the sockaddr_un structure with
125 that path packed in with AF_UNIX filled in. For unix domain sockets, this
126 structure is normally what you need for the arguments in bind(),
127 connect(), and send(), and is also returned by getpeername(),
128 getsockname() and recv().
130 =item unpack_sockaddr_un SOCKADDR_UN
132 Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
133 and returns the pathname. Will croak if the structure does not
134 have AF_UNIX in the right place.
143 @ISA = qw(Exporter DynaLoader);
145 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
146 pack_sockaddr_un unpack_sockaddr_un
147 sockaddr_in sockaddr_un
148 INADDR_ANY INADDR_LOOPBACK INADDR_NONE
229 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
230 my($af, $port, @quad) = @_;
231 carp "6-ARG sockaddr_in call is deprecated" if $^W;
232 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
233 } elsif (wantarray) {
234 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
235 unpack_sockaddr_in(@_);
237 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
238 pack_sockaddr_in(@_);
244 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
245 unpack_sockaddr_un(@_);
247 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
248 pack_sockaddr_un(@_);
255 ($constname = $AUTOLOAD) =~ s/.*:://;
256 $val = constant($constname, @_ ? $_[0] : 0);
258 if ($! =~ /Invalid/) {
259 $AutoLoader::AUTOLOAD = $AUTOLOAD;
260 goto &AutoLoader::AUTOLOAD;
263 ($pack,$file,$line) = caller;
264 croak "Your vendor has not defined Socket macro $constname, used";
267 eval "sub $AUTOLOAD { $val }";
273 # Preloaded methods go here. Autoload methods go after __END__, and are
274 # processed by the autosplit program.