This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Socket to 2.004
[perl5.git] / cpan / Socket / Socket.pm
CommitLineData
a0d0e21e 1package Socket;
73c78b0a 2
e71d7b85 3use strict;
9dea6244 4{ use 5.006001; }
e71d7b85 5
b43fc6a1 6our $VERSION = '2.004';
3b35bae3
AD
7
8=head1 NAME
9
c2654555 10C<Socket> - networking constants and support functions
3b35bae3
AD
11
12=head1 SYNOPSIS
13
c2654555
CBW
14C<Socket> a low-level module used by, among other things, the L<IO::Socket>
15family of modules. The following examples demonstrate some low-level uses but
16a practical program would likely use the higher-level API provided by
17C<IO::Socket> or similar instead.
18
19 use Socket qw(PF_INET SOCK_STREAM pack_sockaddr_in inet_aton);
20
21 socket(my $socket, PF_INET, SOCK_STREAM, 0)
22 or die "socket: $!";
23
24 my $port = getservbyname "echo", "tcp";
25 connect($socket, pack_sockaddr_in($port, inet_aton("localhost")))
26 or die "connect: $!";
27
28 print $socket "Hello, world!\n";
29 print <$socket>;
30
31See also the L</EXAMPLES> section.
3b35bae3
AD
32
33=head1 DESCRIPTION
34
c2654555
CBW
35This module provides a variety of constants, structure manipulators and other
36functions related to socket-based networking. The values and functions
37provided are useful when used in conjunction with Perl core functions such as
38socket(), setsockopt() and bind(). It also provides several other support
39functions, mostly for dealing with conversions of network addresses between
40human-readable and native binary forms, and for hostname resolver operations.
3b35bae3 41
c2654555
CBW
42Some constants and functions are exported by default by this module; but for
43backward-compatibility any recently-added symbols are not exported by default
44and must be requested explicitly. When an import list is provided to the
45C<use Socket> line, the default exports are not automatically imported. It is
46therefore best practice to always to explicitly list all the symbols required.
fdb41d65 47
c2654555
CBW
48Also, some common socket "newline" constants are provided: the constants
49C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and C<$CRLF>, which map
50to C<\015>, C<\012>, and C<\015\012>. If you do not want to use the literal
51characters in your programs, then use the constants provided here. They are
52not exported by default, but can be imported individually, and with the
53C<:crlf> export tag:
fdb41d65 54
c2654555 55 use Socket qw(:DEFAULT :crlf);
8e07c86e 56
c2654555 57 $sock->print("GET / HTTP/1.0$CRLF");
2ae324a7 58
c2654555
CBW
59The entire getaddrinfo() subsystem can be exported using the tag C<:addrinfo>;
60this exports the getaddrinfo() and getnameinfo() functions, and all the
61C<AI_*>, C<NI_*>, C<NIx_*> and C<EAI_*> constants.
8e07c86e 62
c2654555 63=cut
8e07c86e 64
c2654555 65=head1 CONSTANTS
2528d3bc 66
c2654555
CBW
67In each of the following groups, there may be many more constants provided
68than just the ones given as examples in the section heading. If the heading
69ends C<...> then this means there are likely more; the exact constants
70provided will depend on the OS and headers found at compile-time.
8e07c86e 71
c2654555 72=cut
8e07c86e 73
c2654555 74=head2 PF_INET, PF_INET6, PF_UNIX, ...
8e07c86e 75
c2654555 76Protocol family constants to use as the first argument to socket() or the
eabcd9c8 77value of the C<SO_DOMAIN> or C<SO_FAMILY> socket option.
8e07c86e 78
c2654555 79=head2 AF_INET, AF_INET6, AF_UNIX, ...
8e07c86e 80
c2654555
CBW
81Address family constants used by the socket address structures, to pass to
82such functions as inet_pton() or getaddrinfo(), or are returned by such
83functions as sockaddr_family().
7e1af8bc 84
c2654555 85=head2 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ...
7e1af8bc 86
c2654555
CBW
87Socket type constants to use as the second argument to socket(), or the value
88of the C<SO_TYPE> socket option.
7e1af8bc 89
3be135d8
TC
90=head2 SOCK_NONBLOCK. SOCK_CLOEXEC
91
92Linux-specific shortcuts to specify the C<O_NONBLOCK> and C<FD_CLOEXEC> flags
93during a C<socket(2)> call.
94
95 socket( my $sockh, PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, 0 )
96
c2654555 97=head2 SOL_SOCKET
8e07c86e 98
c2654555 99Socket option level constant for setsockopt() and getsockopt().
8e07c86e 100
c2654555 101=head2 SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, ...
3b35bae3 102
c2654555
CBW
103Socket option name constants for setsockopt() and getsockopt() at the
104C<SOL_SOCKET> level.
8e07c86e 105
c2654555 106=head2 IP_OPTIONS, IP_TOS, IP_TTL, ...
8e07c86e 107
c2654555
CBW
108Socket option name constants for IPv4 socket options at the C<IPPROTO_IP>
109level.
8e07c86e 110
c2654555 111=head2 MSG_BCAST, MSG_OOB, MSG_TRUNC, ...
d3f248d3 112
c2654555 113Message flag constants for send() and recv().
d3f248d3 114
c2654555 115=head2 SHUT_RD, SHUT_RDWR, SHUT_WR
d3f248d3 116
c2654555 117Direction constants for shutdown().
d3f248d3 118
c2654555 119=head2 INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE
2a84dff3 120
c2654555
CBW
121Constants giving the special C<AF_INET> addresses for wildcard, broadcast,
122local loopback, and invalid addresses.
123
124Normally equivalent to inet_aton('0.0.0.0'), inet_aton('255.255.255.255'),
125inet_aton('localhost') and inet_aton('255.255.255.255') respectively.
126
127=head2 IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, ...
128
129IP protocol constants to use as the third argument to socket(), the level
130argument to getsockopt() or setsockopt(), or the value of the C<SO_PROTOCOL>
131socket option.
132
133=head2 TCP_CORK, TCP_KEEPALIVE, TCP_NODELAY, ...
134
135Socket option name constants for TCP socket options at the C<IPPROTO_TCP>
136level.
137
138=head2 IN6ADDR_ANY, IN6ADDR_LOOPBACK
139
140Constants giving the special C<AF_INET6> addresses for wildcard and local
141loopback.
142
143Normally equivalent to inet_pton(AF_INET6, "::") and
144inet_pton(AF_INET6, "::1") respectively.
145
146=head2 IPV6_ADD_MEMBERSHIP, IPV6_MTU, IPV6_V6ONLY, ...
147
148Socket option name constants for IPv6 socket options at the C<IPPROTO_IPV6>
149level.
150
151=cut
152
153# Still undocumented: SCM_*, SOMAXCONN, IOV_MAX, UIO_MAXIOV
154
155=head1 STRUCTURE MANIPULATORS
156
157The following functions convert between lists of Perl values and packed binary
158strings representing structures.
159
160=cut
2a84dff3 161
c2654555 162=head2 $family = sockaddr_family $sockaddr
4633a7c4 163
c2654555
CBW
164Takes a packed socket address (as returned by pack_sockaddr_in(),
165pack_sockaddr_un() or the perl builtin functions getsockname() and
166getpeername()). Returns the address family tag. This will be one of the
167C<AF_*> constants, such as C<AF_INET> for a C<sockaddr_in> addresses or
168C<AF_UNIX> for a C<sockaddr_un>. It can be used to figure out what unpack to
169use for a sockaddr of unknown type.
4633a7c4 170
c2654555 171=head2 $sockaddr = pack_sockaddr_in $port, $ip_address
4633a7c4 172
c2654555
CBW
173Takes two arguments, a port number and an opaque string (as returned by
174inet_aton(), or a v-string). Returns the C<sockaddr_in> structure with those
175arguments packed in and C<AF_INET> filled in. For Internet domain sockets,
176this structure is normally what you need for the arguments in bind(),
177connect(), and send().
8e07c86e 178
c2654555 179=head2 ($port, $ip_address) = unpack_sockaddr_in $sockaddr
8e07c86e 180
c2654555
CBW
181Takes a C<sockaddr_in> structure (as returned by pack_sockaddr_in(),
182getpeername() or recv()). Returns a list of two elements: the port and an
183opaque string representing the IP address (you can use inet_ntoa() to convert
184the address to the four-dotted numeric format). Will croak if the structure
185does not represent an C<AF_INET> address.
8e07c86e 186
c2654555 187=head2 $sockaddr = sockaddr_in $port, $ip_address
4633a7c4 188
c2654555 189=head2 ($port, $ip_address) = sockaddr_in $sockaddr
4bc6101e 190
c2654555
CBW
191A wrapper of pack_sockaddr_in() or unpack_sockaddr_in(). In list context,
192unpacks its argument and returns a list consisting of the port and IP address.
193In scalar context, packs its port and IP address arguments as a C<sockaddr_in>
194and returns it.
4bc6101e 195
c2654555
CBW
196Provided largely for legacy compatibility; it is better to use
197pack_sockaddr_in() or unpack_sockaddr_in() explicitly.
4bc6101e 198
c2654555 199=head2 $sockaddr = pack_sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]
c73aa44c
PLE
200
201Takes two to four arguments, a port number, an opaque string (as returned by
202inet_pton()), optionally a scope ID number, and optionally a flow label
c2654555
CBW
203number. Returns the C<sockaddr_in6> structure with those arguments packed in
204and C<AF_INET6> filled in. IPv6 equivalent of pack_sockaddr_in().
205
206=head2 ($port, $ip6_address, $scope_id, $flowinfo) = unpack_sockaddr_in6 $sockaddr
207
208Takes a C<sockaddr_in6> structure. Returns a list of four elements: the port
209number, an opaque string representing the IPv6 address, the scope ID, and the
210flow label. (You can use inet_ntop() to convert the address to the usual
211string format). Will croak if the structure does not represent an C<AF_INET6>
212address.
213
214=head2 $sockaddr = sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]
215
216=head2 ($port, $ip6_address, $scope_id, $flowinfo) = sockaddr_in6 $sockaddr
217
218A wrapper of pack_sockaddr_in6() or unpack_sockaddr_in6(). In list context,
219unpacks its argument according to unpack_sockaddr_in6(). In scalar context,
220packs its arguments according to pack_sockaddr_in6().
221
222Provided largely for legacy compatibility; it is better to use
223pack_sockaddr_in6() or unpack_sockaddr_in6() explicitly.
224
225=head2 $sockaddr = pack_sockaddr_un $path
226
227Takes one argument, a pathname. Returns the C<sockaddr_un> structure with that
228path packed in with C<AF_UNIX> filled in. For C<PF_UNIX> sockets, this
229structure is normally what you need for the arguments in bind(), connect(),
230and send().
231
232=head2 ($path) = unpack_sockaddr_un $sockaddr
233
234Takes a C<sockaddr_un> structure (as returned by pack_sockaddr_un(),
235getpeername() or recv()). Returns a list of one element: the pathname. Will
236croak if the structure does not represent an C<AF_UNIX> address.
c73aa44c 237
c2654555 238=head2 $sockaddr = sockaddr_un $path
c73aa44c 239
c2654555 240=head2 ($path) = sockaddr_un $sockaddr
c73aa44c 241
c2654555
CBW
242A wrapper of pack_sockaddr_un() or unpack_sockaddr_un(). In a list context,
243unpacks its argument and returns a list consisting of the pathname. In a
244scalar context, packs its pathname as a C<sockaddr_un> and returns it.
4633a7c4 245
c2654555
CBW
246Provided largely for legacy compatibility; it is better to use
247pack_sockaddr_un() or unpack_sockaddr_un() explicitly.
4633a7c4 248
1fef88e7 249These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
4633a7c4 250
3be135d8
TC
251=head2 $ip_mreq = pack_ip_mreq $multiaddr, $interface
252
253Takes an IPv4 multicast address and optionally an interface address (or
254C<INADDR_ANY>). Returns the C<ip_mreq> structure with those arguments packed
255in. Suitable for use with the C<IP_ADD_MEMBERSHIP> and C<IP_DROP_MEMBERSHIP>
256sockopts.
257
258=head2 ($multiaddr, $interface) = unpack_ip_mreq $ip_mreq
c2654555 259
3be135d8
TC
260Takes an C<ip_mreq> structure. Returns a list of two elements; the IPv4
261multicast address and interface address.
c2654555 262
d4ada64a
SH
263=head2 $ip_mreq_source = pack_ip_mreq_source $multiaddr, $source, $interface
264
265Takes an IPv4 multicast address, source address, and optionally an interface
266address (or C<INADDR_ANY>). Returns the C<ip_mreq_source> structure with those
267arguments packed in. Suitable for use with the C<IP_ADD_SOURCE_MEMBERSHIP>
268and C<IP_DROP_SOURCE_MEMBERSHIP> sockopts.
269
270=head2 ($multiaddr, $source, $interface) = unpack_ip_mreq_source $ip_mreq
271
272Takes an C<ip_mreq_source> structure. Returns a list of three elements; the
273IPv4 multicast address, source address and interface address.
274
3be135d8
TC
275=head2 $ipv6_mreq = pack_ipv6_mreq $multiaddr6, $ifindex
276
277Takes an IPv6 multicast address and an interface number. Returns the
278C<ipv6_mreq> structure with those arguments packed in. Suitable for use with
279the C<IPV6_ADD_MEMBERSHIP> and C<IPV6_DROP_MEMBERSHIP> sockopts.
280
281=head2 ($multiaddr6, $ifindex) = unpack_ipv6_mreq $ipv6_mreq
c2654555
CBW
282
283Takes an C<ipv6_mreq> structure. Returns a list of two elements; the IPv6
284address and an interface number.
285
286=cut
287
288=head1 FUNCTIONS
289
290=cut
291
292=head2 $ip_address = inet_aton $string
293
294Takes a string giving the name of a host, or a textual representation of an IP
295address and translates that to an packed binary address structure suitable to
296pass to pack_sockaddr_in(). If passed a hostname that cannot be resolved,
297returns C<undef>. For multi-homed hosts (hosts with more than one address),
298the first address found is returned.
4633a7c4 299
c2654555
CBW
300For portability do not assume that the result of inet_aton() is 32 bits wide,
301in other words, that it would contain only the IPv4 address in network order.
4633a7c4 302
c2654555
CBW
303This IPv4-only function is provided largely for legacy reasons. Newly-written
304code should use getaddrinfo() or inet_pton() instead for IPv6 support.
4633a7c4 305
c2654555 306=head2 $string = inet_ntoa $ip_address
3b35bae3 307
c2654555
CBW
308Takes a packed binary address structure such as returned by
309unpack_sockaddr_in() (or a v-string representing the four octets of the IPv4
310address in network order) and translates it into a string of the form
311C<d.d.d.d> where the C<d>s are numbers less than 256 (the normal
312human-readable four dotted number notation for Internet addresses).
036d8bd4 313
c2654555
CBW
314This IPv4-only function is provided largely for legacy reasons. Newly-written
315code should use getnameinfo() or inet_ntop() instead for IPv6 support.
036d8bd4 316
c2654555 317=head2 $address = inet_pton $family, $string
d6896be3 318
c2654555 319Takes an address family (such as C<AF_INET> or C<AF_INET6>) and a string
23296748 320containing a textual representation of an address in that family and
c2654555 321translates that to an packed binary address structure.
036d8bd4 322
c2654555
CBW
323See also getaddrinfo() for a more powerful and flexible function to look up
324socket addresses given hostnames or textual addresses.
036d8bd4 325
c2654555 326=head2 $string = inet_ntop $family, $address
d6896be3 327
c2654555
CBW
328Takes an address family and a packed binary address structure and translates
329it into a human-readable textual representation of the address; typically in
330C<d.d.d.d> form for C<AF_INET> or C<hhhh:hhhh::hhhh> form for C<AF_INET6>.
e57a891d 331
c2654555
CBW
332See also getnameinfo() for a more powerful and flexible function to turn
333socket addresses into human-readable textual representations.
e57a891d 334
c2654555
CBW
335=head2 ($err, @result) = getaddrinfo $host, $service, [$hints]
336
337Given both a hostname and service name, this function attempts to resolve the
338host name into a list of network addresses, and the service name into a
339protocol and port number, and then returns a list of address structures
340suitable to connect() to it.
341
342Given just a host name, this function attempts to resolve it to a list of
343network addresses, and then returns a list of address structures giving these
344addresses.
345
346Given just a service name, this function attempts to resolve it to a protocol
347and port number, and then returns a list of address structures that represent
348it suitable to bind() to. This use should be combined with the C<AI_PASSIVE>
349flag; see below.
350
351Given neither name, it generates an error.
352
353If present, $hints should be a reference to a hash, where the following keys
354are recognised:
355
356=over 4
e57a891d
PLE
357
358=item flags => INT
359
c2654555 360A bitfield containing C<AI_*> constants; see below.
e57a891d
PLE
361
362=item family => INT
363
364Restrict to only generating addresses in this address family
365
366=item socktype => INT
367
368Restrict to only generating addresses of this socket type
369
370=item protocol => INT
371
372Restrict to only generating addresses for this protocol
373
374=back
375
376The return value will be a list; the first value being an error indication,
c2654555 377followed by a list of address structures (if no error occurred).
e57a891d
PLE
378
379The error value will be a dualvar; comparable to the C<EI_*> error constants,
c2654555
CBW
380or printable as a human-readable error message string. If no error occurred it
381will be zero numerically and an empty string.
382
383Each value in the results list will be a hash reference containing the following
384fields:
e57a891d 385
c2654555 386=over 4
e57a891d
PLE
387
388=item family => INT
389
c2654555 390The address family (e.g. C<AF_INET>)
e57a891d
PLE
391
392=item socktype => INT
393
c2654555 394The socket type (e.g. C<SOCK_STREAM>)
e57a891d
PLE
395
396=item protocol => INT
397
c2654555 398The protocol (e.g. C<IPPROTO_TCP>)
e57a891d
PLE
399
400=item addr => STRING
401
c2654555
CBW
402The address in a packed string (such as would be returned by
403pack_sockaddr_in())
e57a891d
PLE
404
405=item canonname => STRING
406
407The canonical name for the host if the C<AI_CANONNAME> flag was provided, or
0cd14f2e
PLE
408C<undef> otherwise. This field will only be present on the first returned
409address.
e57a891d
PLE
410
411=back
412
c2654555
CBW
413The following flag constants are recognised in the $hints hash. Other flag
414constants may exist as provided by the OS.
415
416=over 4
417
418=item AI_PASSIVE
e57a891d 419
c2654555
CBW
420Indicates that this resolution is for a local bind() for a passive (i.e.
421listening) socket, rather than an active (i.e. connecting) socket.
422
423=item AI_CANONNAME
424
425Indicates that the caller wishes the canonical hostname (C<canonname>) field
426of the result to be filled in.
427
428=item AI_NUMERICHOST
429
430Indicates that the caller will pass a numeric address, rather than a hostname,
431and that getaddrinfo() must not perform a resolve operation on this name. This
432flag will prevent a possibly-slow network lookup operation, and instead return
433an error if a hostname is passed.
434
435=back
436
437=head2 ($err, $hostname, $servicename) = getnameinfo $sockaddr, [$flags, [$xflags]]
438
439Given a packed socket address (such as from getsockname(), getpeername(), or
440returned by getaddrinfo() in a C<addr> field), returns the hostname and
441symbolic service name it represents. $flags may be a bitmask of C<NI_*>
e57a891d
PLE
442constants, or defaults to 0 if unspecified.
443
444The return value will be a list; the first value being an error condition,
445followed by the hostname and service name.
446
e57a891d
PLE
447The error value will be a dualvar; comparable to the C<EI_*> error constants,
448or printable as a human-readable error message string. The host and service
449names will be plain strings.
450
c2654555
CBW
451The following flag constants are recognised as $flags. Other flag constants may
452exist as provided by the OS.
453
454=over 4
455
456=item NI_NUMERICHOST
457
458Requests that a human-readable string representation of the numeric address be
459returned directly, rather than performing a name resolve operation that may
460convert it into a hostname. This will also avoid potentially-blocking network
461IO.
462
463=item NI_NUMERICSERV
464
465Requests that the port number be returned directly as a number representation
466rather than performing a name resolve operation that may convert it into a
467service name.
468
469=item NI_NAMEREQD
470
471If a name resolve operation fails to provide a name, then this flag will cause
472getnameinfo() to indicate an error, rather than returning the numeric
473representation as a human-readable string.
474
475=item NI_DGRAM
476
477Indicates that the socket address relates to a C<SOCK_DGRAM> socket, for the
478services whose name differs between TCP and UDP protocols.
479
2ae324a7
PP
480=back
481
c2654555
CBW
482The following constants may be supplied as $xflags.
483
484=over 4
4f5bb87f 485
c2654555 486=item NIx_NOHOST
4f5bb87f 487
c2654555
CBW
488Indicates that the caller is not interested in the hostname of the result, so
489it does not have to be converted. C<undef> will be returned as the hostname.
4f5bb87f 490
c2654555 491=item NIx_NOSERV
4f5bb87f 492
c2654555
CBW
493Indicates that the caller is not interested in the service name of the result,
494so it does not have to be converted. C<undef> will be returned as the service
495name.
496
497=back
498
499=head1 getaddrinfo() / getnameinfo() ERROR CONSTANTS
500
501The following constants may be returned by getaddrinfo() or getnameinfo().
502Others may be provided by the OS.
503
504=over 4
505
506=item EAI_AGAIN
507
508A temporary failure occurred during name resolution. The operation may be
509successful if it is retried later.
510
511=item EAI_BADFLAGS
512
513The value of the C<flags> hint to getaddrinfo(), or the $flags parameter to
514getnameinfo() contains unrecognised flags.
515
516=item EAI_FAMILY
517
518The C<family> hint to getaddrinfo(), or the family of the socket address
519passed to getnameinfo() is not supported.
520
521=item EAI_NODATA
522
523The host name supplied to getaddrinfo() did not provide any usable address
524data.
525
526=item EAI_NONAME
527
528The host name supplied to getaddrinfo() does not exist, or the address
529supplied to getnameinfo() is not associated with a host name and the
530C<NI_NAMEREQD> flag was supplied.
531
532=item EAI_SERVICE
533
534The service name supplied to getaddrinfo() is not available for the socket
535type given in the $hints.
4f5bb87f
PLE
536
537=back
538
3b35bae3
AD
539=cut
540
c2654555
CBW
541=head1 EXAMPLES
542
543=head2 Lookup for connect()
544
545The getaddrinfo() function converts a hostname and a service name into a list
546of structures, each containing a potential way to connect() to the named
547service on the named host.
548
549 use IO::Socket;
550 use Socket qw(SOCK_STREAM getaddrinfo);
551
552 my %hints = (socktype => SOCK_STREAM);
553 my ($err, @res) = getaddrinfo("localhost", "echo", \%hints);
554 die "Cannot getaddrinfo - $err" if $err;
555
556 my $sock;
557
558 foreach my $ai (@res) {
559 my $candidate = IO::Socket->new();
560
561 $candidate->socket($ai->{family}, $ai->{socktype}, $ai->{protocol})
562 or next;
563
564 $candidate->connect($ai->{addr})
565 or next;
566
567 $sock = $candidate;
568 last;
569 }
570
571 die "Cannot connect to localhost:echo" unless $sock;
572
573 $sock->print("Hello, world!\n");
574 print <$sock>;
575
576Because a list of potential candidates is returned, the C<while> loop tries
577each in turn until it it finds one that succeeds both the socket() and
578connect() calls.
579
580This function performs the work of the legacy functions gethostbyname(),
581getservbyname(), inet_aton() and pack_sockaddr_in().
582
583In practice this logic is better performed by L<IO::Socket::IP>.
584
585=head2 Making a human-readable string out of an address
586
587The getnameinfo() function converts a socket address, such as returned by
588getsockname() or getpeername(), into a pair of human-readable strings
589representing the address and service name.
590
591 use IO::Socket::IP;
592 use Socket qw(getnameinfo);
593
594 my $server = IO::Socket::IP->new(LocalPort => 12345, Listen => 1) or
595 die "Cannot listen - $@";
596
597 my $socket = $server->accept or die "accept: $!";
598
599 my ($err, $hostname, $servicename) = getnameinfo($socket->peername);
600 die "Cannot getnameinfo - $err" if $err;
601
602 print "The peer is connected from $hostname\n";
603
604Since in this example only the hostname was used, the redundant conversion of
605the port number into a service name may be omitted by passing the
606C<NIx_NOSERV> flag.
607
608 use Socket qw(getnameinfo NIx_NOSERV);
609
610 my ($err, $hostname) = getnameinfo($socket->peername, 0, NIx_NOSERV);
611
612This function performs the work of the legacy functions unpack_sockaddr_in(),
613inet_ntoa(), gethostbyaddr() and getservbyport().
614
615In practice this logic is better performed by L<IO::Socket::IP>.
616
617=head2 Resolving hostnames into IP addresses
618
619To turn a hostname into a human-readable plain IP address use getaddrinfo()
620to turn the hostname into a list of socket structures, then getnameinfo() on
621each one to make it a readable IP address again.
622
623 use Socket qw(:addrinfo SOCK_RAW);
624
625 my ($err, @res) = getaddrinfo($hostname, "", {socktype => SOCK_RAW});
626 die "Cannot getaddrinfo - $err" if $err;
627
628 while( my $ai = shift @res ) {
629 my ($err, $ipaddr) = getnameinfo($ai->{addr}, NI_NUMERICHOST, NIx_NOSERV);
630 die "Cannot getnameinfo - $err" if $err;
631
632 print "$ipaddr\n";
633 }
634
635The C<socktype> hint to getaddrinfo() filters the results to only include one
636socket type and protocol. Without this most OSes return three combinations,
637for C<SOCK_STREAM>, C<SOCK_DGRAM> and C<SOCK_RAW>, resulting in triplicate
638output of addresses. The C<NI_NUMERICHOST> flag to getnameinfo() causes it to
639return a string-formatted plain IP address, rather than reverse resolving it
640back into a hostname.
641
642This combination performs the work of the legacy functions gethostbyname()
643and inet_ntoa().
644
645=head2 Accessing socket options
646
647The many C<SO_*> and other constants provide the socket option names for
648getsockopt() and setsockopt().
649
650 use IO::Socket::INET;
651 use Socket qw(SOL_SOCKET SO_RCVBUF IPPROTO_IP IP_TTL);
652
653 my $socket = IO::Socket::INET->new(LocalPort => 0, Proto => 'udp')
654 or die "Cannot create socket: $@";
655
656 $socket->setsockopt(SOL_SOCKET, SO_RCVBUF, 64*1024) or
657 die "setsockopt: $!";
658
659 print "Receive buffer is ", $socket->getsockopt(SOL_SOCKET, SO_RCVBUF),
660 " bytes\n";
661
662 print "IP TTL is ", $socket->getsockopt(IPPROTO_IP, IP_TTL), "\n";
663
664As a convenience, L<IO::Socket>'s setsockopt() method will convert a number
665into a packed byte buffer, and getsockopt() will unpack a byte buffer of the
666correct size back into a number.
667
668=cut
669
670=head1 AUTHOR
671
672This module was originally maintained in Perl core by the Perl 5 Porters.
673
674It was extracted to dual-life on CPAN at version 1.95 by
675Paul Evans <leonerd@leonerd.org.uk>
676
677=cut
678
a0d0e21e 679use Carp;
d3a7d8c7 680use warnings::register;
a0d0e21e
LW
681
682require Exporter;
da4061d3 683require XSLoader;
c2654555 684our @ISA = qw(Exporter);
80531fbd
LB
685
686# <@Nicholas> you can't change @EXPORT without breaking the implicit API
687# Please put any new constants in @EXPORT_OK!
c2654555
CBW
688
689# List re-ordered to match documentation above. Try to keep the ordering
690# consistent so it's easier to see which ones are or aren't documented.
691our @EXPORT = qw(
692 PF_802 PF_AAL PF_APPLETALK PF_CCITT PF_CHAOS PF_CTF PF_DATAKIT
693 PF_DECnet PF_DLI PF_ECMA PF_GOSIP PF_HYLINK PF_IMPLINK PF_INET PF_INET6
694 PF_ISO PF_KEY PF_LAST PF_LAT PF_LINK PF_MAX PF_NBS PF_NIT PF_NS PF_OSI
695 PF_OSINET PF_PUP PF_ROUTE PF_SNA PF_UNIX PF_UNSPEC PF_USER PF_WAN
a0d0e21e 696 PF_X25
c2654555
CBW
697
698 AF_802 AF_AAL AF_APPLETALK AF_CCITT AF_CHAOS AF_CTF AF_DATAKIT
699 AF_DECnet AF_DLI AF_ECMA AF_GOSIP AF_HYLINK AF_IMPLINK AF_INET AF_INET6
700 AF_ISO AF_KEY AF_LAST AF_LAT AF_LINK AF_MAX AF_NBS AF_NIT AF_NS AF_OSI
701 AF_OSINET AF_PUP AF_ROUTE AF_SNA AF_UNIX AF_UNSPEC AF_USER AF_WAN
702 AF_X25
703
704 SOCK_DGRAM SOCK_RAW SOCK_RDM SOCK_SEQPACKET SOCK_STREAM
705
a0d0e21e 706 SOL_SOCKET
c2654555
CBW
707
708 SO_ACCEPTCONN SO_ATTACH_FILTER SO_BACKLOG SO_BROADCAST SO_CHAMELEON
eabcd9c8
A
709 SO_DEBUG SO_DETACH_FILTER SO_DGRAM_ERRIND SO_DOMAIN SO_DONTLINGER
710 SO_DONTROUTE SO_ERROR SO_FAMILY SO_KEEPALIVE SO_LINGER SO_OOBINLINE
711 SO_PASSCRED SO_PASSIFNAME SO_PEERCRED SO_PROTOCOL SO_PROTOTYPE
712 SO_RCVBUF SO_RCVLOWAT SO_RCVTIMEO SO_REUSEADDR SO_REUSEPORT
c2654555
CBW
713 SO_SECURITY_AUTHENTICATION SO_SECURITY_ENCRYPTION_NETWORK
714 SO_SECURITY_ENCRYPTION_TRANSPORT SO_SNDBUF SO_SNDLOWAT SO_SNDTIMEO
715 SO_STATE SO_TYPE SO_USELOOPBACK SO_XOPEN SO_XSE
716
717 IP_OPTIONS IP_HDRINCL IP_TOS IP_TTL IP_RECVOPTS IP_RECVRETOPTS
718 IP_RETOPTS
719
720 MSG_BCAST MSG_BTAG MSG_CTLFLAGS MSG_CTLIGNORE MSG_CTRUNC MSG_DONTROUTE
721 MSG_DONTWAIT MSG_EOF MSG_EOR MSG_ERRQUEUE MSG_ETAG MSG_FIN
722 MSG_MAXIOVLEN MSG_MCAST MSG_NOSIGNAL MSG_OOB MSG_PEEK MSG_PROXY MSG_RST
723 MSG_SYN MSG_TRUNC MSG_URG MSG_WAITALL MSG_WIRE
724
725 SHUT_RD SHUT_RDWR SHUT_WR
726
727 INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
728
729 SCM_CONNECT SCM_CREDENTIALS SCM_CREDS SCM_RIGHTS SCM_TIMESTAMP
730
a0d0e21e 731 SOMAXCONN
c2654555
CBW
732
733 IOV_MAX
6b1016b5 734 UIO_MAXIOV
c2654555
CBW
735
736 sockaddr_family
737 pack_sockaddr_in unpack_sockaddr_in sockaddr_in
738 pack_sockaddr_in6 unpack_sockaddr_in6 sockaddr_in6
739 pack_sockaddr_un unpack_sockaddr_un sockaddr_un
740
741 inet_aton inet_ntoa
742);
743
744# List re-ordered to match documentation above. Try to keep the ordering
745# consistent so it's easier to see which ones are or aren't documented.
746our @EXPORT_OK = qw(
747 CR LF CRLF $CR $LF $CRLF
748
3be135d8
TC
749 SOCK_NONBLOCK SOCK_CLOEXEC
750
d4ada64a
SH
751 IP_ADD_MEMBERSHIP IP_ADD_SOURCE_MEMBERSHIP IP_DROP_MEMBERSHIP
752 IP_DROP_SOURCE_MEMBERSHIP IP_MULTICAST_IF IP_MULTICAST_LOOP
753 IP_MULTICAST_TTL
3be135d8 754
c2654555
CBW
755 IPPROTO_IP IPPROTO_IPV6 IPPROTO_RAW IPPROTO_ICMP IPPROTO_TCP
756 IPPROTO_UDP
757
d4ada64a
SH
758 TCP_CONGESTION TCP_CONNECTIONTIMEOUT TCP_CORK TCP_DEFER_ACCEPT TCP_INFO
759 TCP_INIT_CWND TCP_KEEPALIVE TCP_KEEPCNT TCP_KEEPIDLE TCP_KEEPINTVL
760 TCP_LINGER2 TCP_MAXRT TCP_MAXSEG TCP_MD5SIG TCP_NODELAY TCP_NOOPT
761 TCP_NOPUSH TCP_QUICKACK TCP_SACK_ENABLE TCP_STDURG TCP_SYNCNT
762 TCP_WINDOW_CLAMP
c2654555
CBW
763
764 IN6ADDR_ANY IN6ADDR_LOOPBACK
765
d4ada64a
SH
766 IPV6_ADD_MEMBERSHIP IPV6_DROP_MEMBERSHIP IPV6_JOIN_GROUP
767 IPV6_LEAVE_GROUP IPV6_MTU IPV6_MTU_DISCOVER IPV6_MULTICAST_HOPS
768 IPV6_MULTICAST_IF IPV6_MULTICAST_LOOP IPV6_UNICAST_HOPS IPV6_V6ONLY
c2654555 769
d4ada64a 770 pack_ip_mreq unpack_ip_mreq pack_ip_mreq_source unpack_ip_mreq_source
3be135d8 771
c2654555
CBW
772 pack_ipv6_mreq unpack_ipv6_mreq
773
774 inet_pton inet_ntop
775
776 getaddrinfo getnameinfo
777
778 AI_ADDRCONFIG AI_ALL AI_CANONIDN AI_CANONNAME AI_IDN
779 AI_IDN_ALLOW_UNASSIGNED AI_IDN_USE_STD3_ASCII_RULES AI_NUMERICHOST
780 AI_NUMERICSERV AI_PASSIVE AI_V4MAPPED
781
782 NI_DGRAM NI_IDN NI_IDN_ALLOW_UNASSIGNED NI_IDN_USE_STD3_ASCII_RULES
783 NI_NAMEREQD NI_NOFQDN NI_NUMERICHOST NI_NUMERICSERV
784
785 NIx_NOHOST NIx_NOSERV
786
787 EAI_ADDRFAMILY EAI_AGAIN EAI_BADFLAGS EAI_BADHINTS EAI_FAIL EAI_FAMILY
788 EAI_NODATA EAI_NONAME EAI_PROTOCOL EAI_SERVICE EAI_SOCKTYPE EAI_SYSTEM
a0d0e21e
LW
789);
790
c2654555
CBW
791our %EXPORT_TAGS = (
792 crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
793 addrinfo => [qw(getaddrinfo getnameinfo), grep m/^(?:AI|NI|NIx|EAI)_/, @EXPORT_OK],
794 all => [@EXPORT, @EXPORT_OK],
fdb41d65
CN
795);
796
797BEGIN {
798 sub CR () {"\015"}
799 sub LF () {"\012"}
800 sub CRLF () {"\015\012"}
c2654555
CBW
801
802 # These are not gni() constants; they're extensions for the perl API
803 # The definitions in Socket.pm and Socket.xs must match
804 sub NIx_NOHOST() {1 << 0}
805 sub NIx_NOSERV() {1 << 1}
fdb41d65
CN
806}
807
808*CR = \CR();
809*LF = \LF();
810*CRLF = \CRLF();
811
4633a7c4
LW
812sub sockaddr_in {
813 if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
814 my($af, $port, @quad) = @_;
c2654555 815 warnings::warn "6-ARG sockaddr_in call is deprecated"
d3a7d8c7 816 if warnings::enabled();
4633a7c4
LW
817 pack_sockaddr_in($port, inet_aton(join('.', @quad)));
818 } elsif (wantarray) {
819 croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
820 unpack_sockaddr_in(@_);
821 } else {
822 croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
823 pack_sockaddr_in(@_);
824 }
825}
826
4bc6101e
PLE
827sub sockaddr_in6 {
828 if (wantarray) {
829 croak "usage: (port,in6addr,scope_id,flowinfo) = sockaddr_in6(sin6_sv)" unless @_ == 1;
830 unpack_sockaddr_in6(@_);
831 }
832 else {
833 croak "usage: sin6_sv = sockaddr_in6(port,in6addr,[scope_id,[flowinfo]])" unless @_ >= 2 and @_ <= 4;
834 pack_sockaddr_in6(@_);
835 }
836}
837
4633a7c4
LW
838sub sockaddr_un {
839 if (wantarray) {
840 croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
841 unpack_sockaddr_un(@_);
842 } else {
37120919
AD
843 croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
844 pack_sockaddr_un(@_);
4633a7c4
LW
845 }
846}
847
4c3d8e12 848XSLoader::load(__PACKAGE__, $VERSION);
a0d0e21e 849
c2219427
PLE
850my %errstr;
851
32b40adf
NC
852if( defined &getaddrinfo ) {
853 # These are not part of the API, nothing uses them, and deleting them
854 # reduces the size of %Socket:: by about 12K
855 delete $Socket::{fake_getaddrinfo};
856 delete $Socket::{fake_getnameinfo};
857} else {
0f41f5e2
PLE
858 require Scalar::Util;
859
860 *getaddrinfo = \&fake_getaddrinfo;
861 *getnameinfo = \&fake_getnameinfo;
862
863 # These numbers borrowed from GNU libc's implementation, but since
864 # they're only used by our emulation, it doesn't matter if the real
865 # platform's values differ
866 my %constants = (
867 AI_PASSIVE => 1,
868 AI_CANONNAME => 2,
869 AI_NUMERICHOST => 4,
0369a5a5
PLE
870 AI_V4MAPPED => 8,
871 AI_ALL => 16,
872 AI_ADDRCONFIG => 32,
0f41f5e2
PLE
873 # RFC 2553 doesn't define this but Linux does - lets be nice and
874 # provide it since we can
875 AI_NUMERICSERV => 1024,
876
877 EAI_BADFLAGS => -1,
878 EAI_NONAME => -2,
879 EAI_NODATA => -5,
880 EAI_FAMILY => -6,
881 EAI_SERVICE => -8,
882
883 NI_NUMERICHOST => 1,
884 NI_NUMERICSERV => 2,
0369a5a5 885 NI_NOFQDN => 4,
0f41f5e2
PLE
886 NI_NAMEREQD => 8,
887 NI_DGRAM => 16,
0369a5a5
PLE
888
889 # Constants we don't support. Export them, but croak if anyone tries to
890 # use them
891 AI_IDN => 64,
892 AI_CANONIDN => 128,
893 AI_IDN_ALLOW_UNASSIGNED => 256,
894 AI_IDN_USE_STD3_ASCII_RULES => 512,
895 NI_IDN => 32,
896 NI_IDN_ALLOW_UNASSIGNED => 64,
897 NI_IDN_USE_STD3_ASCII_RULES => 128,
898
899 # Error constants we'll never return, so it doesn't matter what value
900 # these have, nor that we don't provide strings for them
901 EAI_SYSTEM => -11,
902 EAI_BADHINTS => -1000,
903 EAI_PROTOCOL => -1001
0f41f5e2
PLE
904 );
905
906 foreach my $name ( keys %constants ) {
907 my $value = $constants{$name};
908
909 no strict 'refs';
910 defined &$name or *$name = sub () { $value };
911 }
912
913 %errstr = (
914 # These strings from RFC 2553
915 EAI_BADFLAGS() => "invalid value for ai_flags",
916 EAI_NONAME() => "nodename nor servname provided, or not known",
917 EAI_NODATA() => "no address associated with nodename",
918 EAI_FAMILY() => "ai_family not supported",
919 EAI_SERVICE() => "servname not supported for ai_socktype",
920 );
c2219427
PLE
921}
922
923# The following functions are used if the system does not have a
924# getaddrinfo(3) function in libc; and are used to emulate it for the AF_INET
925# family
926
c2219427
PLE
927# Borrowed from Regexp::Common::net
928my $REGEXP_IPv4_DECIMAL = qr/25[0-5]|2[0-4][0-9]|1?[0-9][0-9]{1,2}/;
929my $REGEXP_IPv4_DOTTEDQUAD = qr/$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL\.$REGEXP_IPv4_DECIMAL/;
930
931sub fake_makeerr
932{
0f41f5e2
PLE
933 my ( $errno ) = @_;
934 my $errstr = $errno == 0 ? "" : ( $errstr{$errno} || $errno );
935 return Scalar::Util::dualvar( $errno, $errstr );
c2219427
PLE
936}
937
938sub fake_getaddrinfo
939{
0f41f5e2
PLE
940 my ( $node, $service, $hints ) = @_;
941
942 $node = "" unless defined $node;
943
944 $service = "" unless defined $service;
945
946 my ( $family, $socktype, $protocol, $flags ) = @$hints{qw( family socktype protocol flags )};
947
948 $family ||= Socket::AF_INET(); # 0 == AF_UNSPEC, which we want too
949 $family == Socket::AF_INET() or return fake_makeerr( EAI_FAMILY() );
950
951 $socktype ||= 0;
952
953 $protocol ||= 0;
954
955 $flags ||= 0;
956
957 my $flag_passive = $flags & AI_PASSIVE(); $flags &= ~AI_PASSIVE();
958 my $flag_canonname = $flags & AI_CANONNAME(); $flags &= ~AI_CANONNAME();
959 my $flag_numerichost = $flags & AI_NUMERICHOST(); $flags &= ~AI_NUMERICHOST();
960 my $flag_numericserv = $flags & AI_NUMERICSERV(); $flags &= ~AI_NUMERICSERV();
961
0369a5a5
PLE
962 # These constants don't apply to AF_INET-only lookups, so we might as well
963 # just ignore them. For AI_ADDRCONFIG we just presume the host has ability
964 # to talk AF_INET. If not we'd have to return no addresses at all. :)
965 $flags &= ~(AI_V4MAPPED()|AI_ALL()|AI_ADDRCONFIG());
966
967 $flags & (AI_IDN()|AI_CANONIDN()|AI_IDN_ALLOW_UNASSIGNED()|AI_IDN_USE_STD3_ASCII_RULES()) and
968 croak "Socket::getaddrinfo() does not support IDN";
969
0f41f5e2
PLE
970 $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
971
972 $node eq "" and $service eq "" and return fake_makeerr( EAI_NONAME() );
973
974 my $canonname;
975 my @addrs;
976 if( $node ne "" ) {
977 return fake_makeerr( EAI_NONAME() ) if( $flag_numerichost and $node !~ m/^$REGEXP_IPv4_DOTTEDQUAD$/ );
978 ( $canonname, undef, undef, undef, @addrs ) = gethostbyname( $node );
979 defined $canonname or return fake_makeerr( EAI_NONAME() );
980
981 undef $canonname unless $flag_canonname;
982 }
983 else {
984 $addrs[0] = $flag_passive ? Socket::inet_aton( "0.0.0.0" )
985 : Socket::inet_aton( "127.0.0.1" );
986 }
987
988 my @ports; # Actually ARRAYrefs of [ socktype, protocol, port ]
989 my $protname = "";
990 if( $protocol ) {
991 $protname = getprotobynumber( $protocol );
992 }
993
994 if( $service ne "" and $service !~ m/^\d+$/ ) {
995 return fake_makeerr( EAI_NONAME() ) if( $flag_numericserv );
996 getservbyname( $service, $protname ) or return fake_makeerr( EAI_SERVICE() );
997 }
998
999 foreach my $this_socktype ( Socket::SOCK_STREAM(), Socket::SOCK_DGRAM(), Socket::SOCK_RAW() ) {
1000 next if $socktype and $this_socktype != $socktype;
1001
1002 my $this_protname = "raw";
1003 $this_socktype == Socket::SOCK_STREAM() and $this_protname = "tcp";
1004 $this_socktype == Socket::SOCK_DGRAM() and $this_protname = "udp";
1005
1006 next if $protname and $this_protname ne $protname;
1007
1008 my $port;
1009 if( $service ne "" ) {
1010 if( $service =~ m/^\d+$/ ) {
1011 $port = "$service";
1012 }
1013 else {
1014 ( undef, undef, $port, $this_protname ) = getservbyname( $service, $this_protname );
1015 next unless defined $port;
1016 }
1017 }
1018 else {
1019 $port = 0;
1020 }
1021
1022 push @ports, [ $this_socktype, scalar getprotobyname( $this_protname ) || 0, $port ];
1023 }
1024
1025 my @ret;
1026 foreach my $addr ( @addrs ) {
1027 foreach my $portspec ( @ports ) {
1028 my ( $socktype, $protocol, $port ) = @$portspec;
1029 push @ret, {
1030 family => $family,
1031 socktype => $socktype,
1032 protocol => $protocol,
1033 addr => Socket::pack_sockaddr_in( $port, $addr ),
0cd14f2e 1034 canonname => undef,
0f41f5e2
PLE
1035 };
1036 }
1037 }
1038
0cd14f2e
PLE
1039 # Only supply canonname for the first result
1040 if( defined $canonname ) {
1041 $ret[0]->{canonname} = $canonname;
1042 }
1043
0f41f5e2 1044 return ( fake_makeerr( 0 ), @ret );
c2219427
PLE
1045}
1046
1047sub fake_getnameinfo
1048{
c2654555 1049 my ( $addr, $flags, $xflags ) = @_;
0f41f5e2
PLE
1050
1051 my ( $port, $inetaddr );
1052 eval { ( $port, $inetaddr ) = Socket::unpack_sockaddr_in( $addr ) }
1053 or return fake_makeerr( EAI_FAMILY() );
1054
1055 my $family = Socket::AF_INET();
1056
1057 $flags ||= 0;
1058
1059 my $flag_numerichost = $flags & NI_NUMERICHOST(); $flags &= ~NI_NUMERICHOST();
1060 my $flag_numericserv = $flags & NI_NUMERICSERV(); $flags &= ~NI_NUMERICSERV();
0369a5a5 1061 my $flag_nofqdn = $flags & NI_NOFQDN(); $flags &= ~NI_NOFQDN();
0f41f5e2
PLE
1062 my $flag_namereqd = $flags & NI_NAMEREQD(); $flags &= ~NI_NAMEREQD();
1063 my $flag_dgram = $flags & NI_DGRAM() ; $flags &= ~NI_DGRAM();
1064
0369a5a5
PLE
1065 $flags & (NI_IDN()|NI_IDN_ALLOW_UNASSIGNED()|NI_IDN_USE_STD3_ASCII_RULES()) and
1066 croak "Socket::getnameinfo() does not support IDN";
1067
0f41f5e2
PLE
1068 $flags == 0 or return fake_makeerr( EAI_BADFLAGS() );
1069
c2654555
CBW
1070 $xflags ||= 0;
1071
0f41f5e2 1072 my $node;
c2654555
CBW
1073 if( $xflags & NIx_NOHOST ) {
1074 $node = undef;
1075 }
1076 elsif( $flag_numerichost ) {
0f41f5e2
PLE
1077 $node = Socket::inet_ntoa( $inetaddr );
1078 }
1079 else {
1080 $node = gethostbyaddr( $inetaddr, $family );
1081 if( !defined $node ) {
1082 return fake_makeerr( EAI_NONAME() ) if $flag_namereqd;
1083 $node = Socket::inet_ntoa( $inetaddr );
1084 }
0369a5a5
PLE
1085 elsif( $flag_nofqdn ) {
1086 my ( $shortname ) = split m/\./, $node;
1087 my ( $fqdn ) = gethostbyname $shortname;
1088 $node = $shortname if defined $fqdn and $fqdn eq $node;
1089 }
0f41f5e2
PLE
1090 }
1091
1092 my $service;
c2654555
CBW
1093 if( $xflags & NIx_NOSERV ) {
1094 $service = undef;
1095 }
1096 elsif( $flag_numericserv ) {
0f41f5e2
PLE
1097 $service = "$port";
1098 }
1099 else {
1100 my $protname = $flag_dgram ? "udp" : "";
1101 $service = getservbyport( $port, $protname );
1102 if( !defined $service ) {
1103 $service = "$port";
1104 }
1105 }
1106
1107 return ( fake_makeerr( 0 ), $node, $service );
c2219427
PLE
1108}
1109
a0d0e21e 11101;