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 = $serv[2] || $defport || $pnum;
67 unless (defined $port) {
68 $@ = "Bad service '$origport'";
72 $proto = (getprotobyname($serv[3]))[2] || undef
76 return ($addr || undef,
87 my $title = ref($sock).": ";
88 $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_);
90 if(defined fileno($sock));
97 my($sock,$addr_str, $multi) = @_;
99 if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
100 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
102 my $h = inet_aton($addr_str);
103 push(@addr, $h) if defined $h;
110 my($lport,$rport,$laddr,$raddr,$proto,$type);
113 $arg->{LocalAddr} = $arg->{LocalHost}
114 if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
116 ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
119 or return _error($sock, $!, $@);
121 $laddr = defined $laddr ? inet_aton($laddr)
124 return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'")
125 unless(defined $laddr);
127 $arg->{PeerAddr} = $arg->{PeerHost}
128 if exists $arg->{PeerHost} && !exists $arg->{PeerAddr};
130 unless(exists $arg->{Listen}) {
131 ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
134 or return _error($sock, $!, $@);
137 $proto ||= (getprotobyname('tcp'))[2];
139 my $pname = (getprotobynumber($proto))[0];
140 $type = $arg->{Type} || $socket_type{$pname};
145 @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed});
146 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
152 $sock->socket(AF_INET, $type, $proto) or
153 return _error($sock, $!, "$!");
155 if ($arg->{Reuse} || $arg->{ReuseAddr}) {
156 $sock->sockopt(SO_REUSEADDR,1) or
157 return _error($sock, $!, "$!");
160 if ($arg->{ReusePort}) {
161 $sock->sockopt(SO_REUSEPORT,1) or
162 return _error($sock, $!, "$!");
165 if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
166 $sock->bind($lport || 0, $laddr) or
167 return _error($sock, $!, "$!");
170 if(exists $arg->{Listen}) {
171 $sock->listen($arg->{Listen} || 5) or
172 return _error($sock, $!, "$!");
176 # don't try to connect unless we're given a PeerAddr
177 last unless exists($arg->{PeerAddr});
179 $raddr = shift @raddr;
181 return _error($sock, $EINVAL, 'Cannot determine remote port')
182 unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW);
185 unless($type == SOCK_STREAM || defined $raddr);
187 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
188 unless defined $raddr;
190 # my $timeout = ${*$sock}{'io_socket_timeout'};
191 # my $before = time() if $timeout;
194 if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
195 # ${*$sock}{'io_socket_timeout'} = $timeout;
199 return _error($sock, $!, $@ || "Timeout")
203 # my $new_timeout = $timeout - (time() - $before);
204 # return _error($sock,
205 # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
206 # "Timeout") if $new_timeout <= 0;
207 # ${*$sock}{'io_socket_timeout'} = $new_timeout;
216 @_ == 2 || @_ == 3 or
217 croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
219 return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_));
223 @_ == 2 || @_ == 3 or
224 croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
226 return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_))
230 @_ == 1 or croak 'usage: $sock->sockaddr()';
232 my $name = $sock->sockname;
233 $name ? (sockaddr_in($name))[1] : undef;
237 @_ == 1 or croak 'usage: $sock->sockport()';
239 my $name = $sock->sockname;
240 $name ? (sockaddr_in($name))[0] : undef;
244 @_ == 1 or croak 'usage: $sock->sockhost()';
246 my $addr = $sock->sockaddr;
247 $addr ? inet_ntoa($addr) : undef;
251 @_ == 1 or croak 'usage: $sock->peeraddr()';
253 my $name = $sock->peername;
254 $name ? (sockaddr_in($name))[1] : undef;
258 @_ == 1 or croak 'usage: $sock->peerport()';
260 my $name = $sock->peername;
261 $name ? (sockaddr_in($name))[0] : undef;
265 @_ == 1 or croak 'usage: $sock->peerhost()';
267 my $addr = $sock->peeraddr;
268 $addr ? inet_ntoa($addr) : undef;
277 IO::Socket::INET - Object interface for AF_INET domain sockets
281 use IO::Socket::INET;
285 C<IO::Socket::INET> provides an object interface to creating and using sockets
286 in the AF_INET domain. It is built upon the L<IO::Socket> interface and
287 inherits all the methods defined by L<IO::Socket>.
295 Creates an C<IO::Socket::INET> object, which is a reference to a
296 newly created symbol (see the C<Symbol> package). C<new>
297 optionally takes arguments, these arguments are in key-value pairs.
299 In addition to the key-value pairs accepted by L<IO::Socket>,
300 C<IO::Socket::INET> provides.
303 PeerAddr Remote host address <hostname>[:<port>]
304 PeerHost Synonym for PeerAddr
305 PeerPort Remote port or service <service>[(<no>)] | <no>
306 LocalAddr Local host bind address hostname[:port]
307 LocalHost Synonym for LocalAddr
308 LocalPort Local host bind port <service>[(<no>)] | <no>
309 Proto Protocol name (or number) "tcp" | "udp" | ...
310 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
311 Listen Queue size for listen
312 ReuseAddr Set SO_REUSEADDR before binding
313 Reuse Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr)
314 ReusePort Set SO_REUSEPORT before binding
315 Timeout Timeout value for various operations
316 MultiHomed Try all adresses for multi-homed hosts
319 If C<Listen> is defined then a listen socket is created, else if the
320 socket type, which is derived from the protocol, is SOCK_STREAM then
323 Although it is not illegal, the use of C<MultiHomed> on a socket
324 which is in non-blocking mode is of little use. This is because the
325 first connect will never fail with a timeout as the connaect call
328 The C<PeerAddr> can be a hostname or the IP-address on the
329 "xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic
330 service name. The service name might be followed by a number in
331 parenthesis which is used if the service is not known by the system.
332 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
333 by preceding it with a ":".
335 If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
336 then the constructor will try to derive C<Proto> from the service
337 name. As a last resort C<Proto> "tcp" is assumed. The C<Type>
338 parameter will be deduced from C<Proto> if not specified.
340 If the constructor is only passed a single argument, it is assumed to
341 be a C<PeerAddr> specification.
345 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
346 PeerPort => 'http(80)',
349 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
351 $sock = IO::Socket::INET->new(Listen => 5,
352 LocalAddr => 'localhost',
356 $sock = IO::Socket::INET->new('127.0.0.1:25');
359 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
361 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
362 by default. This was not the case with earlier releases.
364 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
374 Return the address part of the sockaddr structure for the socket
378 Return the port number that the socket is using on the local host
382 Return the address part of the sockaddr structure for the socket in a
383 text form xx.xx.xx.xx
387 Return the address part of the sockaddr structure for the socket on
392 Return the port number for the socket on the peer host.
396 Return the address part of the sockaddr structure for the socket on the
397 peer host in a text form xx.xx.xx.xx
403 L<Socket>, L<IO::Socket>
407 Graham Barr. Currently maintained by the Perl Porters. Please report all
408 bugs to <perl5-porters@perl.org>.
412 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
413 This program is free software; you can redistribute it and/or
414 modify it under the same terms as Perl itself.