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
,
31 unshift(@_, "PeerAddr") if @_ == 1;
32 return $class->SUPER::new
(@_);
36 my($addr,$port,$proto) = @_;
42 if(defined $addr && $addr =~ s
,:([\w\
(\
)/]+)$,,);
45 if (@proto = ( $proto =~ m
,\D
,
46 ?
getprotobyname($proto)
47 : getprotobynumber($proto))
49 $proto = $proto[2] || undef;
52 $@
= "Bad protocol '$proto'";
58 $port =~ s
,\
((\d
+)\
)$,,;
60 my $defport = $1 || undef;
61 my $pnum = ($port =~ m
,^(\d
+)$,)[0];
63 @serv = getservbyname($port, $proto[0] || "")
66 $port = $pnum || $serv[2] || $defport || undef;
67 unless (defined $port) {
68 $@
= "Bad service '$origport'";
72 $proto = (getprotobyname($serv[3]))[2] || undef
76 return ($addr || undef,
87 $@
= join("",ref($sock),": ",@_);
89 if(defined fileno($sock));
96 my($sock,$addr_str, $multi) = @_;
98 if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
99 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
101 my $h = inet_aton
($addr_str);
102 push(@addr, $h) if defined $h;
109 my($lport,$rport,$laddr,$raddr,$proto,$type);
112 $arg->{LocalAddr
} = $arg->{LocalHost
}
113 if exists $arg->{LocalHost
} && !exists $arg->{LocalAddr
};
115 ($laddr,$lport,$proto) = _sock_info
($arg->{LocalAddr
},
118 or return _error
($sock, $!, $@
);
120 $laddr = defined $laddr ? inet_aton
($laddr)
123 return _error
($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr
},"'")
124 unless(defined $laddr);
126 $arg->{PeerAddr
} = $arg->{PeerHost
}
127 if exists $arg->{PeerHost
} && !exists $arg->{PeerAddr
};
129 unless(exists $arg->{Listen
}) {
130 ($raddr,$rport,$proto) = _sock_info
($arg->{PeerAddr
},
133 or return _error
($sock, $!, $@
);
136 $proto ||= (getprotobyname('tcp'))[2];
138 my $pname = (getprotobynumber($proto))[0];
139 $type = $arg->{Type
} || $socket_type{$pname};
144 @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed
});
145 return _error
($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr
},"'")
151 $sock->socket(AF_INET
, $type, $proto) or
152 return _error
($sock, $!, "$!");
154 if ($arg->{Reuse
} || $arg->{ReuseAddr
}) {
155 $sock->sockopt(SO_REUSEADDR
,1) or
156 return _error
($sock, $!, "$!");
159 if ($arg->{ReusePort
}) {
160 $sock->sockopt(SO_REUSEPORT
,1) or
161 return _error
($sock, $!, "$!");
164 if($lport || ($laddr ne INADDR_ANY
) || exists $arg->{Listen
}) {
165 $sock->bind($lport || 0, $laddr) or
166 return _error
($sock, $!, "$!");
169 if(exists $arg->{Listen
}) {
170 $sock->listen($arg->{Listen
} || 5) or
171 return _error
($sock, $!, "$!");
175 # don't try to connect unless we're given a PeerAddr
176 last unless exists($arg->{PeerAddr
});
178 $raddr = shift @raddr;
180 return _error
($sock, $EINVAL, 'Cannot determine remote port')
181 unless($rport || $type == SOCK_DGRAM
|| $type == SOCK_RAW
);
184 unless($type == SOCK_STREAM
|| defined $raddr);
186 return _error
($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr
},"'")
187 unless defined $raddr;
189 # my $timeout = ${*$sock}{'io_socket_timeout'};
190 # my $before = time() if $timeout;
192 if ($sock->connect(pack_sockaddr_in
($rport, $raddr))) {
193 # ${*$sock}{'io_socket_timeout'} = $timeout;
197 return _error
($sock, $!, "Timeout")
201 # my $new_timeout = $timeout - (time() - $before);
202 # return _error($sock,
203 # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
204 # "Timeout") if $new_timeout <= 0;
205 # ${*$sock}{'io_socket_timeout'} = $new_timeout;
214 @_ == 2 || @_ == 3 or
215 croak
'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
217 return $sock->SUPER::connect(@_ == 1 ?
shift : pack_sockaddr_in
(@_));
221 @_ == 2 || @_ == 3 or
222 croak
'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
224 return $sock->SUPER::bind(@_ == 1 ?
shift : pack_sockaddr_in
(@_))
228 @_ == 1 or croak
'usage: $sock->sockaddr()';
230 my $name = $sock->sockname;
231 $name ?
(sockaddr_in
($name))[1] : undef;
235 @_ == 1 or croak
'usage: $sock->sockport()';
237 my $name = $sock->sockname;
238 $name ?
(sockaddr_in
($name))[0] : undef;
242 @_ == 1 or croak
'usage: $sock->sockhost()';
244 my $addr = $sock->sockaddr;
245 $addr ? inet_ntoa
($addr) : undef;
249 @_ == 1 or croak
'usage: $sock->peeraddr()';
251 my $name = $sock->peername;
252 $name ?
(sockaddr_in
($name))[1] : undef;
256 @_ == 1 or croak
'usage: $sock->peerport()';
258 my $name = $sock->peername;
259 $name ?
(sockaddr_in
($name))[0] : undef;
263 @_ == 1 or croak
'usage: $sock->peerhost()';
265 my $addr = $sock->peeraddr;
266 $addr ? inet_ntoa
($addr) : undef;
275 IO::Socket::INET - Object interface for AF_INET domain sockets
279 use IO::Socket::INET;
283 C<IO::Socket::INET> provides an object interface to creating and using sockets
284 in the AF_INET domain. It is built upon the L<IO::Socket> interface and
285 inherits all the methods defined by L<IO::Socket>.
293 Creates an C<IO::Socket::INET> object, which is a reference to a
294 newly created symbol (see the C<Symbol> package). C<new>
295 optionally takes arguments, these arguments are in key-value pairs.
297 In addition to the key-value pairs accepted by L<IO::Socket>,
298 C<IO::Socket::INET> provides.
301 PeerAddr Remote host address <hostname>[:<port>]
302 PeerHost Synonym for PeerAddr
303 PeerPort Remote port or service <service>[(<no>)] | <no>
304 LocalAddr Local host bind address hostname[:port]
305 LocalHost Synonym for LocalAddr
306 LocalPort Local host bind port <service>[(<no>)] | <no>
307 Proto Protocol name (or number) "tcp" | "udp" | ...
308 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
309 Listen Queue size for listen
310 ReuseAddr Set SO_REUSEADDR before binding
311 Reuse Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr)
312 ReusePort Set SO_REUSEPORT before binding
313 Timeout Timeout value for various operations
314 MultiHomed Try all adresses for multi-homed hosts
317 If C<Listen> is defined then a listen socket is created, else if the
318 socket type, which is derived from the protocol, is SOCK_STREAM then
321 Although it is not illegal, the use of C<MultiHomed> on a socket
322 which is in non-blocking mode is of little use. This is because the
323 first connect will never fail with a timeout as the connaect call
326 The C<PeerAddr> can be a hostname or the IP-address on the
327 "xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic
328 service name. The service name might be followed by a number in
329 parenthesis which is used if the service is not known by the system.
330 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
331 by preceding it with a ":".
333 If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
334 then the constructor will try to derive C<Proto> from the service
335 name. As a last resort C<Proto> "tcp" is assumed. The C<Type>
336 parameter will be deduced from C<Proto> if not specified.
338 If the constructor is only passed a single argument, it is assumed to
339 be a C<PeerAddr> specification.
343 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
344 PeerPort => 'http(80)',
347 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
349 $sock = IO::Socket::INET->new(Listen => 5,
350 LocalAddr => 'localhost',
354 $sock = IO::Socket::INET->new('127.0.0.1:25');
357 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
359 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
360 by default. This was not the case with earlier releases.
362 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
372 Return the address part of the sockaddr structure for the socket
376 Return the port number that the socket is using on the local host
380 Return the address part of the sockaddr structure for the socket in a
381 text form xx.xx.xx.xx
385 Return the address part of the sockaddr structure for the socket on
390 Return the port number for the socket on the peer host.
394 Return the address part of the sockaddr structure for the socket on the
395 peer host in a text form xx.xx.xx.xx
401 L<Socket>, L<IO::Socket>
405 Graham Barr. Currently maintained by the Perl Porters. Please report all
406 bugs to <perl5-porters@perl.org>.
410 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
411 This program is free software; you can redistribute it and/or
412 modify it under the same terms as Perl itself.