What am I thinking? Just bump the version.
[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);
e9872d04 4$VERSION = "1.82";
3b35bae3 5
6=head1 NAME
7
036d8bd4 8Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton, inet_ntop - 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);
2359510d 35 unlink('/var/run/usock');
36 $sun = sockaddr_un('/var/run/usock');
4633a7c4 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
bbc7dcd2 59=over 4
2ae324a7 60
8e07c86e 61=item inet_aton HOSTNAME
62
6fe628c6 63Takes a string giving the name of a host, and translates that to an
64opaque string (if programming in C, struct in_addr). Takes arguments
65of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
66cannot be resolved, returns undef. For multi-homed hosts (hosts with
67more than one address), the first address found is returned.
8e07c86e 68
2528d3bc 69For portability do not assume that the result of inet_aton() is 32
6fe628c6 70bits wide, in other words, that it would contain only the IPv4 address
71in network order.
2528d3bc 72
8e07c86e 73=item inet_ntoa IP_ADDRESS
74
6fe628c6 75Takes a string (an opaque string as returned by inet_aton(),
76or a v-string representing the four octets of the IPv4 address in
2528d3bc 77network order) and translates it into a string of the form 'd.d.d.d'
6fe628c6 78where the 'd's are numbers less than 256 (the normal human-readable
79four dotted number notation for Internet addresses).
8e07c86e 80
81=item INADDR_ANY
82
4633a7c4 83Note: does not return a number, but a packed string.
8e07c86e 84
85Returns the 4-byte wildcard ip address which specifies any
6fe628c6 86of the hosts ip addresses. (A particular machine can have
8e07c86e 87more than one ip address, each address corresponding to
88a particular network interface. This wildcard address
89allows you to bind to all of them simultaneously.)
90Normally equivalent to inet_aton('0.0.0.0').
91
7e1af8bc 92=item INADDR_BROADCAST
93
94Note: does not return a number, but a packed string.
95
96Returns the 4-byte 'this-lan' ip broadcast address.
97This can be useful for some protocols to solicit information
98from all servers on the same LAN cable.
99Normally equivalent to inet_aton('255.255.255.255').
100
8e07c86e 101=item INADDR_LOOPBACK
102
103Note - does not return a number.
104
6fe628c6 105Returns the 4-byte loopback address. Normally equivalent
8e07c86e 106to inet_aton('localhost').
3b35bae3 107
8e07c86e 108=item INADDR_NONE
109
110Note - does not return a number.
111
6fe628c6 112Returns the 4-byte 'invalid' ip address. Normally equivalent
8e07c86e 113to inet_aton('255.255.255.255').
114
2a84dff3 115=item sockaddr_family SOCKADDR
116
117Takes a sockaddr structure (as returned by pack_sockaddr_in(),
118pack_sockaddr_un() or the perl builtin functions getsockname() and
119getpeername()) and returns the address family tag. It will match the
120constant AF_INET for a sockaddr_in and AF_UNIX for a sockaddr_un. It
121can be used to figure out what unpacker to use for a sockaddr of
122unknown type.
123
4633a7c4 124=item sockaddr_in PORT, ADDRESS
125
126=item sockaddr_in SOCKADDR_IN
127
91e74348 128In a list context, unpacks its SOCKADDR_IN argument and returns an array
4633a7c4 129consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
130ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
131use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
132
133=item pack_sockaddr_in PORT, IP_ADDRESS
8e07c86e 134
6fe628c6 135Takes two arguments, a port number and an opaque string, IP_ADDRESS
136(as returned by inet_aton(), or a v-string). Returns the sockaddr_in
137structure with those arguments packed in with AF_INET filled in. For
138Internet domain sockets, this structure is normally what you need for
139the arguments in bind(), connect(), and send(), and is also returned
140by getpeername(), getsockname() and recv().
8e07c86e 141
142=item unpack_sockaddr_in SOCKADDR_IN
143
4633a7c4 144Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
6fe628c6 145returns an array of two elements: the port and an opaque string
146representing the IP address (you can use inet_ntoa() to convert the
147address to the four-dotted numeric format). Will croak if the
148structure does not have AF_INET in the right place.
4633a7c4 149
150=item sockaddr_un PATHNAME
151
152=item sockaddr_un SOCKADDR_UN
153
91e74348 154In a list context, unpacks its SOCKADDR_UN argument and returns an array
1fef88e7 155consisting of (PATHNAME). In a scalar context, packs its PATHNAME
4633a7c4 156arguments as a SOCKADDR_UN and returns it. If this is confusing, use
157pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
1fef88e7 158These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
4633a7c4 159
160=item pack_sockaddr_un PATH
161
162Takes one argument, a pathname. Returns the sockaddr_un structure with
163that path packed in with AF_UNIX filled in. For unix domain sockets, this
164structure is normally what you need for the arguments in bind(),
165connect(), and send(), and is also returned by getpeername(),
166getsockname() and recv().
167
168=item unpack_sockaddr_un SOCKADDR_UN
169
170Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
171and returns the pathname. Will croak if the structure does not
172have AF_UNIX in the right place.
3b35bae3 173
036d8bd4 174=item inet_pton ADDRESS_FAMILY, HOSTNAME
175
176Takes an address family, either AF_INET or AF_INET6, and a string giving
177the name of a host, and translates that to an opaque string
178(if programming in C, struct in_addr or struct in6_addr depending on the
179address family passed in). The host string may be a string hostname, such
180as 'www.perl.org', or an IP address. If using an IP address, the type of
181IP address must be consistant with the address family passed into the function.
182
183=item inet_ntop ADDRESS_FAMILY, IP_ADDRESS
184
185Takes an address family, either AF_INET or AF_INET6, and a string
186(an opaque string as returned by inet_aton() or inet_pton()) and
187translates it to an IPv4 or IPv6 address string.
188
2ae324a7 189=back
190
3b35bae3 191=cut
192
a0d0e21e 193use Carp;
d3a7d8c7 194use warnings::register;
a0d0e21e 195
196require Exporter;
9426adcd 197use XSLoader ();
198@ISA = qw(Exporter);
a0d0e21e 199@EXPORT = qw(
2a84dff3 200 inet_aton inet_ntoa
201 sockaddr_family
202 pack_sockaddr_in unpack_sockaddr_in
4633a7c4 203 pack_sockaddr_un unpack_sockaddr_un
204 sockaddr_in sockaddr_un
7e1af8bc 205 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
a0d0e21e 206 AF_802
7198d2fd 207 AF_AAL
a0d0e21e 208 AF_APPLETALK
209 AF_CCITT
210 AF_CHAOS
7198d2fd 211 AF_CTF
a0d0e21e 212 AF_DATAKIT
213 AF_DECnet
214 AF_DLI
215 AF_ECMA
216 AF_GOSIP
217 AF_HYLINK
218 AF_IMPLINK
219 AF_INET
7198d2fd 220 AF_INET6
221 AF_ISO
222 AF_KEY
223 AF_LAST
a0d0e21e 224 AF_LAT
7198d2fd 225 AF_LINK
a0d0e21e 226 AF_MAX
227 AF_NBS
228 AF_NIT
229 AF_NS
230 AF_OSI
231 AF_OSINET
232 AF_PUP
7198d2fd 233 AF_ROUTE
a0d0e21e 234 AF_SNA
235 AF_UNIX
236 AF_UNSPEC
7198d2fd 237 AF_USER
238 AF_WAN
a0d0e21e 239 AF_X25
6b1016b5 240 IOV_MAX
131c565a 241 IP_OPTIONS
242 IP_HDRINCL
243 IP_TOS
244 IP_TTL
245 IP_RECVOPTS
246 IP_RECVRETOPTS
247 IP_RETOPTS
6b1016b5 248 MSG_BCAST
7198d2fd 249 MSG_BTAG
de4597cb 250 MSG_CTLFLAGS
251 MSG_CTLIGNORE
252 MSG_CTRUNC
a0d0e21e 253 MSG_DONTROUTE
de4597cb 254 MSG_DONTWAIT
255 MSG_EOF
256 MSG_EOR
257 MSG_ERRQUEUE
7198d2fd 258 MSG_ETAG
de4597cb 259 MSG_FIN
a0d0e21e 260 MSG_MAXIOVLEN
6b1016b5 261 MSG_MCAST
de4597cb 262 MSG_NOSIGNAL
a0d0e21e 263 MSG_OOB
264 MSG_PEEK
de4597cb 265 MSG_PROXY
266 MSG_RST
267 MSG_SYN
268 MSG_TRUNC
269 MSG_URG
270 MSG_WAITALL
7198d2fd 271 MSG_WIRE
a0d0e21e 272 PF_802
7198d2fd 273 PF_AAL
a0d0e21e 274 PF_APPLETALK
275 PF_CCITT
276 PF_CHAOS
7198d2fd 277 PF_CTF
a0d0e21e 278 PF_DATAKIT
279 PF_DECnet
280 PF_DLI
281 PF_ECMA
282 PF_GOSIP
283 PF_HYLINK
284 PF_IMPLINK
285 PF_INET
7198d2fd 286 PF_INET6
287 PF_ISO
288 PF_KEY
289 PF_LAST
a0d0e21e 290 PF_LAT
7198d2fd 291 PF_LINK
a0d0e21e 292 PF_MAX
293 PF_NBS
294 PF_NIT
295 PF_NS
296 PF_OSI
297 PF_OSINET
298 PF_PUP
7198d2fd 299 PF_ROUTE
a0d0e21e 300 PF_SNA
301 PF_UNIX
302 PF_UNSPEC
7198d2fd 303 PF_USER
304 PF_WAN
a0d0e21e 305 PF_X25
de4597cb 306 SCM_CONNECT
307 SCM_CREDENTIALS
308 SCM_CREDS
309 SCM_RIGHTS
310 SCM_TIMESTAMP
ca6e1c26 311 SHUT_RD
312 SHUT_RDWR
313 SHUT_WR
a0d0e21e 314 SOCK_DGRAM
315 SOCK_RAW
316 SOCK_RDM
317 SOCK_SEQPACKET
318 SOCK_STREAM
319 SOL_SOCKET
320 SOMAXCONN
321 SO_ACCEPTCONN
7198d2fd 322 SO_ATTACH_FILTER
323 SO_BACKLOG
a0d0e21e 324 SO_BROADCAST
7198d2fd 325 SO_CHAMELEON
a0d0e21e 326 SO_DEBUG
7198d2fd 327 SO_DETACH_FILTER
328 SO_DGRAM_ERRIND
a0d0e21e 329 SO_DONTLINGER
330 SO_DONTROUTE
331 SO_ERROR
7198d2fd 332 SO_FAMILY
a0d0e21e 333 SO_KEEPALIVE
334 SO_LINGER
335 SO_OOBINLINE
7198d2fd 336 SO_PASSCRED
337 SO_PASSIFNAME
338 SO_PEERCRED
339 SO_PROTOCOL
340 SO_PROTOTYPE
a0d0e21e 341 SO_RCVBUF
342 SO_RCVLOWAT
343 SO_RCVTIMEO
344 SO_REUSEADDR
b2f54bf3 345 SO_REUSEPORT
7198d2fd 346 SO_SECURITY_AUTHENTICATION
347 SO_SECURITY_ENCRYPTION_NETWORK
348 SO_SECURITY_ENCRYPTION_TRANSPORT
a0d0e21e 349 SO_SNDBUF
350 SO_SNDLOWAT
351 SO_SNDTIMEO
7198d2fd 352 SO_STATE
a0d0e21e 353 SO_TYPE
354 SO_USELOOPBACK
7198d2fd 355 SO_XOPEN
356 SO_XSE
6b1016b5 357 UIO_MAXIOV
a0d0e21e 358);
359
1494e794 360@EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
361
131c565a 362 IPPROTO_IP
363 IPPROTO_IPV6
364 IPPROTO_RAW
91c526f1 365 IPPROTO_ICMP
1494e794 366 IPPROTO_TCP
91c526f1 367 IPPROTO_UDP
368
1494e794 369 TCP_KEEPALIVE
370 TCP_MAXRT
371 TCP_MAXSEG
372 TCP_NODELAY
373 TCP_STDURG);
fdb41d65 374
375%EXPORT_TAGS = (
dde527fc 376 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
fdb41d65 377 all => [@EXPORT, @EXPORT_OK],
378);
379
380BEGIN {
381 sub CR () {"\015"}
382 sub LF () {"\012"}
383 sub CRLF () {"\015\012"}
384}
385
386*CR = \CR();
387*LF = \LF();
388*CRLF = \CRLF();
389
4633a7c4 390sub sockaddr_in {
391 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
392 my($af, $port, @quad) = @_;
d3a7d8c7 393 warnings::warn "6-ARG sockaddr_in call is deprecated"
394 if warnings::enabled();
4633a7c4 395 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
396 } elsif (wantarray) {
397 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
398 unpack_sockaddr_in(@_);
399 } else {
400 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
401 pack_sockaddr_in(@_);
402 }
403}
404
405sub sockaddr_un {
406 if (wantarray) {
407 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
408 unpack_sockaddr_un(@_);
409 } else {
37120919 410 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
411 pack_sockaddr_un(@_);
4633a7c4 412 }
413}
414
a0d0e21e 415sub AUTOLOAD {
73c78b0a 416 my($constname);
a0d0e21e 417 ($constname = $AUTOLOAD) =~ s/.*:://;
b903fcff 418 croak "&Socket::constant not defined" if $constname eq 'constant';
419 my ($error, $val) = constant($constname);
420 if ($error) {
421 croak $error;
a0d0e21e 422 }
cea00dc5 423 *$AUTOLOAD = sub { $val };
a0d0e21e 424 goto &$AUTOLOAD;
425}
426
9426adcd 427XSLoader::load 'Socket', $VERSION;
a0d0e21e 428
a0d0e21e 4291;