3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
7 package IO::Socket::INET;
17 @ISA = qw(IO::Socket);
20 my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1;
22 IO::Socket::INET->register_domain( AF_INET );
24 my %socket_type = ( tcp => SOCK_STREAM,
29 $proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP;
30 $proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP;
31 $proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP;
32 my %proto_name = reverse %proto_number;
36 unshift(@_, "PeerAddr") if @_ == 1;
37 return $class->SUPER::new(@_);
42 for (map lc($_), $proto[0], split(' ', $proto[1])) {
43 $proto_number{$_} = $proto[2];
45 $proto_name{$proto[2]} = $proto[0];
48 sub _get_proto_number {
50 return undef unless defined $name;
51 return $proto_number{$name} if exists $proto_number{$name};
53 my @proto = getprotobyname($name);
54 return undef unless @proto;
62 return undef unless defined $num;
63 return $proto_name{$num} if exists $proto_name{$num};
65 my @proto = getprotobynumber($num);
66 return undef unless @proto;
73 my($addr,$port,$proto) = @_;
78 if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,);
80 if(defined $proto && $proto =~ /\D/) {
81 my $num = _get_proto_number($proto);
82 unless (defined $num) {
83 $@ = "Bad protocol '$proto'";
90 my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef;
91 my $pnum = ($port =~ m,^(\d+)$,)[0];
93 @serv = getservbyname($port, _get_proto_name($proto) || "")
96 $port = $serv[2] || $defport || $pnum;
97 unless (defined $port) {
98 $@ = "Bad service '$origport'";
102 $proto = _get_proto_number($serv[3]) if @serv && !$proto;
105 return ($addr || undef,
116 my $title = ref($sock).": ";
117 $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_);
119 if(defined fileno($sock));
126 my($sock,$addr_str, $multi) = @_;
128 if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
129 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
131 my $h = inet_aton($addr_str);
132 push(@addr, $h) if defined $h;
139 my($lport,$rport,$laddr,$raddr,$proto,$type);
142 $arg->{LocalAddr} = $arg->{LocalHost}
143 if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
145 ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
148 or return _error($sock, $!, $@);
150 $laddr = defined $laddr ? inet_aton($laddr)
153 return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'")
154 unless(defined $laddr);
156 $arg->{PeerAddr} = $arg->{PeerHost}
157 if exists $arg->{PeerHost} && !exists $arg->{PeerAddr};
159 unless(exists $arg->{Listen}) {
160 ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
163 or return _error($sock, $!, $@);
166 $proto ||= _get_proto_number('tcp');
168 $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)};
173 @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed});
174 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
180 $sock->socket(AF_INET, $type, $proto) or
181 return _error($sock, $!, "$!");
183 if (defined $arg->{Blocking}) {
184 defined $sock->blocking($arg->{Blocking})
185 or return _error($sock, $!, "$!");
188 if ($arg->{Reuse} || $arg->{ReuseAddr}) {
189 $sock->sockopt(SO_REUSEADDR,1) or
190 return _error($sock, $!, "$!");
193 if ($arg->{ReusePort}) {
194 $sock->sockopt(SO_REUSEPORT,1) or
195 return _error($sock, $!, "$!");
198 if ($arg->{Broadcast}) {
199 $sock->sockopt(SO_BROADCAST,1) or
200 return _error($sock, $!, "$!");
203 if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
204 $sock->bind($lport || 0, $laddr) or
205 return _error($sock, $!, "$!");
208 if(exists $arg->{Listen}) {
209 $sock->listen($arg->{Listen} || 5) or
210 return _error($sock, $!, "$!");
214 # don't try to connect unless we're given a PeerAddr
215 last unless exists($arg->{PeerAddr});
217 $raddr = shift @raddr;
219 return _error($sock, $EINVAL, 'Cannot determine remote port')
220 unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW);
223 unless($type == SOCK_STREAM || defined $raddr);
225 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
226 unless defined $raddr;
228 # my $timeout = ${*$sock}{'io_socket_timeout'};
229 # my $before = time() if $timeout;
232 if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
233 # ${*$sock}{'io_socket_timeout'} = $timeout;
237 return _error($sock, $!, $@ || "Timeout")
241 # my $new_timeout = $timeout - (time() - $before);
242 # return _error($sock,
243 # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
244 # "Timeout") if $new_timeout <= 0;
245 # ${*$sock}{'io_socket_timeout'} = $new_timeout;
254 @_ == 2 || @_ == 3 or
255 croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
257 return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_));
261 @_ == 2 || @_ == 3 or
262 croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
264 return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_))
268 @_ == 1 or croak 'usage: $sock->sockaddr()';
270 my $name = $sock->sockname;
271 $name ? (sockaddr_in($name))[1] : undef;
275 @_ == 1 or croak 'usage: $sock->sockport()';
277 my $name = $sock->sockname;
278 $name ? (sockaddr_in($name))[0] : undef;
282 @_ == 1 or croak 'usage: $sock->sockhost()';
284 my $addr = $sock->sockaddr;
285 $addr ? inet_ntoa($addr) : undef;
289 @_ == 1 or croak 'usage: $sock->peeraddr()';
291 my $name = $sock->peername;
292 $name ? (sockaddr_in($name))[1] : undef;
296 @_ == 1 or croak 'usage: $sock->peerport()';
298 my $name = $sock->peername;
299 $name ? (sockaddr_in($name))[0] : undef;
303 @_ == 1 or croak 'usage: $sock->peerhost()';
305 my $addr = $sock->peeraddr;
306 $addr ? inet_ntoa($addr) : undef;
315 IO::Socket::INET - Object interface for AF_INET domain sockets
319 use IO::Socket::INET;
323 C<IO::Socket::INET> provides an object interface to creating and using sockets
324 in the AF_INET domain. It is built upon the L<IO::Socket> interface and
325 inherits all the methods defined by L<IO::Socket>.
333 Creates an C<IO::Socket::INET> object, which is a reference to a
334 newly created symbol (see the C<Symbol> package). C<new>
335 optionally takes arguments, these arguments are in key-value pairs.
337 In addition to the key-value pairs accepted by L<IO::Socket>,
338 C<IO::Socket::INET> provides.
341 PeerAddr Remote host address <hostname>[:<port>]
342 PeerHost Synonym for PeerAddr
343 PeerPort Remote port or service <service>[(<no>)] | <no>
344 LocalAddr Local host bind address hostname[:port]
345 LocalHost Synonym for LocalAddr
346 LocalPort Local host bind port <service>[(<no>)] | <no>
347 Proto Protocol name (or number) "tcp" | "udp" | ...
348 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
349 Listen Queue size for listen
350 ReuseAddr Set SO_REUSEADDR before binding
351 Reuse Set SO_REUSEADDR before binding (deprecated,
353 ReusePort Set SO_REUSEPORT before binding
354 Broadcast Set SO_BROADCAST before binding
355 Timeout Timeout value for various operations
356 MultiHomed Try all addresses for multi-homed hosts
357 Blocking Determine if connection will be blocking mode
359 If C<Listen> is defined then a listen socket is created, else if the
360 socket type, which is derived from the protocol, is SOCK_STREAM then
363 Although it is not illegal, the use of C<MultiHomed> on a socket
364 which is in non-blocking mode is of little use. This is because the
365 first connect will never fail with a timeout as the connect call
368 The C<PeerAddr> can be a hostname or the IP-address on the
369 "xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic
370 service name. The service name might be followed by a number in
371 parenthesis which is used if the service is not known by the system.
372 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
373 by preceding it with a ":".
375 If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
376 then the constructor will try to derive C<Proto> from the service
377 name. As a last resort C<Proto> "tcp" is assumed. The C<Type>
378 parameter will be deduced from C<Proto> if not specified.
380 If the constructor is only passed a single argument, it is assumed to
381 be a C<PeerAddr> specification.
383 If C<Blocking> is set to 0, the connection will be in nonblocking mode.
384 If not specified it defaults to 1 (blocking mode).
388 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
389 PeerPort => 'http(80)',
392 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
394 $sock = IO::Socket::INET->new(Listen => 5,
395 LocalAddr => 'localhost',
399 $sock = IO::Socket::INET->new('127.0.0.1:25');
401 $sock = IO::Socket::INET->new(
403 PeerAddr => inet_ntoa(INADDR_BROADCAST),
405 LocalAddr => 'localhost',
407 or die "Can't bind : $@\n";
409 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
411 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
412 by default. This was not the case with earlier releases.
414 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
424 Return the address part of the sockaddr structure for the socket
428 Return the port number that the socket is using on the local host
432 Return the address part of the sockaddr structure for the socket in a
433 text form xx.xx.xx.xx
437 Return the address part of the sockaddr structure for the socket on
442 Return the port number for the socket on the peer host.
446 Return the address part of the sockaddr structure for the socket on the
447 peer host in a text form xx.xx.xx.xx
453 L<Socket>, L<IO::Socket>
457 Graham Barr. Currently maintained by the Perl Porters. Please report all
458 bugs to <perl5-porters@perl.org>.
462 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
463 This program is free software; you can redistribute it and/or
464 modify it under the same terms as Perl itself.