6 Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
12 $proto = getprotobyname('udp');
13 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
14 $iaddr = gethostbyname('hishost.com');
15 $port = getservbyname('time', 'udp');
16 $sin = sockaddr_in($port, $iaddr);
17 send(Socket_Handle, 0, 0, $sin);
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);
27 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
28 $peer_host = gethostbyaddr($iaddr, AF_INET);
29 $peer_addr = inet_ntoa($iaddr);
31 $proto = getprotobyname('tcp');
32 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
34 $sun = sockaddr_un('/tmp/usock');
35 connect(Socket_Handle,$sun);
39 This module is just a translation of the C F<socket.h> file.
40 Unlike the old mechanism of requiring a translated F<socket.ph>
41 file, this uses the B<h2xs> program (see the Perl source distribution)
42 and your native C compiler. This means that it has a
43 far more likely chance of getting the numbers right. This includes
44 all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
46 In addition, some structure manipulation functions are available:
48 =item inet_aton HOSTNAME
50 Takes a string giving the name of a host, and translates that
51 to the 4-byte string (structure). Takes arguments of both
52 the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
53 cannot be resolved, returns undef.
55 =item inet_ntoa IP_ADDRESS
57 Takes a four byte ip address (as returned by inet_aton())
58 and translates it into a string of the form 'd.d.d.d'
59 where the 'd's are numbers less than 256 (the normal
60 readable four dotted number notation for internet addresses).
64 Note: does not return a number, but a packed string.
66 Returns the 4-byte wildcard ip address which specifies any
67 of the hosts ip addresses. (A particular machine can have
68 more than one ip address, each address corresponding to
69 a particular network interface. This wildcard address
70 allows you to bind to all of them simultaneously.)
71 Normally equivalent to inet_aton('0.0.0.0').
75 Note - does not return a number.
77 Returns the 4-byte loopback address. Normally equivalent
78 to inet_aton('localhost').
82 Note - does not return a number.
84 Returns the 4-byte invalid ip address. Normally equivalent
85 to inet_aton('255.255.255.255').
87 =item sockaddr_in PORT, ADDRESS
89 =item sockaddr_in SOCKADDR_IN
91 In an array context, unpacks its SOCKADDR_IN argument and returns an array
92 consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
93 ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
94 use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
96 =item pack_sockaddr_in PORT, IP_ADDRESS
98 Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
99 inet_aton()). Returns the sockaddr_in structure with those arguments
100 packed in with AF_INET filled in. For internet domain sockets, this
101 structure is normally what you need for the arguments in bind(),
102 connect(), and send(), and is also returned by getpeername(),
103 getsockname() and recv().
105 =item unpack_sockaddr_in SOCKADDR_IN
107 Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
108 returns an array of two elements: the port and the 4-byte ip-address.
109 Will croak if the structure does not have AF_INET in the right place.
111 =item sockaddr_un PATHNAME
113 =item sockaddr_un SOCKADDR_UN
115 In an array context, unpacks its SOCKADDR_UN argument and returns an array
116 consisting of (PATHNAME). In a scalar context, packs its PATHANE
117 arguments as a SOCKADDR_UN and returns it. If this is confusing, use
118 pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
119 These are only supported if your system has <sys/un.h>.
121 =item pack_sockaddr_un PATH
123 Takes one argument, a pathname. Returns the sockaddr_un structure with
124 that path packed in with AF_UNIX filled in. For unix domain sockets, this
125 structure is normally what you need for the arguments in bind(),
126 connect(), and send(), and is also returned by getpeername(),
127 getsockname() and recv().
129 =item unpack_sockaddr_un SOCKADDR_UN
131 Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
132 and returns the pathname. Will croak if the structure does not
133 have AF_UNIX in the right place.
142 @ISA = qw(Exporter DynaLoader);
144 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
145 pack_sockaddr_un unpack_sockaddr_un
146 sockaddr_in sockaddr_un
147 INADDR_ANY INADDR_LOOPBACK INADDR_NONE
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(@_);
236 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
237 pack_sockaddr_in(@_);
243 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
244 unpack_sockaddr_un(@_);
246 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
247 pack_sockaddr_un(@_);
254 ($constname = $AUTOLOAD) =~ s/.*:://;
255 $val = constant($constname, @_ ? $_[0] : 0);
257 if ($! =~ /Invalid/) {
258 $AutoLoader::AUTOLOAD = $AUTOLOAD;
259 goto &AutoLoader::AUTOLOAD;
262 ($pack,$file,$line) = caller;
263 croak "Your vendor has not defined Socket macro $constname, used";
266 eval "sub $AUTOLOAD { $val }";
272 # Preloaded methods go here. Autoload methods go after __END__, and are
273 # processed by the autosplit program.