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\(\)/]+)$,,);
44 if(defined $proto && $proto =~ /\D/) {
45 if(@proto = getprotobyname($proto)) {
46 $proto = $proto[2] || undef;
49 $@ = "Bad protocol '$proto'";
55 my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef;
56 my $pnum = ($port =~ m,^(\d+)$,)[0];
58 @serv = getservbyname($port, $proto[0] || "")
61 $port = $serv[2] || $defport || $pnum;
62 unless (defined $port) {
63 $@ = "Bad service '$origport'";
67 $proto = (getprotobyname($serv[3]))[2] || undef
71 return ($addr || undef,
82 my $title = ref($sock).": ";
83 $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_);
85 if(defined fileno($sock));
92 my($sock,$addr_str, $multi) = @_;
94 if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
95 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
97 my $h = inet_aton($addr_str);
98 push(@addr, $h) if defined $h;
105 my($lport,$rport,$laddr,$raddr,$proto,$type);
108 $arg->{LocalAddr} = $arg->{LocalHost}
109 if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
111 ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
114 or return _error($sock, $!, $@);
116 $laddr = defined $laddr ? inet_aton($laddr)
119 return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'")
120 unless(defined $laddr);
122 $arg->{PeerAddr} = $arg->{PeerHost}
123 if exists $arg->{PeerHost} && !exists $arg->{PeerAddr};
125 unless(exists $arg->{Listen}) {
126 ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
129 or return _error($sock, $!, $@);
132 $proto ||= (getprotobyname('tcp'))[2];
134 my $pname = (getprotobynumber($proto))[0];
135 $type = $arg->{Type} || $socket_type{lc $pname};
140 @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed});
141 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
147 $sock->socket(AF_INET, $type, $proto) or
148 return _error($sock, $!, "$!");
150 if (defined $arg->{Blocking}) {
151 defined $sock->blocking($arg->{Blocking})
152 or 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 ($arg->{Broadcast}) {
166 $sock->sockopt(SO_BROADCAST,1) or
167 return _error($sock, $!, "$!");
170 if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
171 $sock->bind($lport || 0, $laddr) or
172 return _error($sock, $!, "$!");
175 if(exists $arg->{Listen}) {
176 $sock->listen($arg->{Listen} || 5) or
177 return _error($sock, $!, "$!");
181 # don't try to connect unless we're given a PeerAddr
182 last unless exists($arg->{PeerAddr});
184 $raddr = shift @raddr;
186 return _error($sock, $EINVAL, 'Cannot determine remote port')
187 unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW);
190 unless($type == SOCK_STREAM || defined $raddr);
192 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
193 unless defined $raddr;
195 # my $timeout = ${*$sock}{'io_socket_timeout'};
196 # my $before = time() if $timeout;
199 if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
200 # ${*$sock}{'io_socket_timeout'} = $timeout;
204 return _error($sock, $!, $@ || "Timeout")
208 # my $new_timeout = $timeout - (time() - $before);
209 # return _error($sock,
210 # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
211 # "Timeout") if $new_timeout <= 0;
212 # ${*$sock}{'io_socket_timeout'} = $new_timeout;
221 @_ == 2 || @_ == 3 or
222 croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
224 return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_));
228 @_ == 2 || @_ == 3 or
229 croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
231 return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_))
235 @_ == 1 or croak 'usage: $sock->sockaddr()';
237 my $name = $sock->sockname;
238 $name ? (sockaddr_in($name))[1] : undef;
242 @_ == 1 or croak 'usage: $sock->sockport()';
244 my $name = $sock->sockname;
245 $name ? (sockaddr_in($name))[0] : undef;
249 @_ == 1 or croak 'usage: $sock->sockhost()';
251 my $addr = $sock->sockaddr;
252 $addr ? inet_ntoa($addr) : undef;
256 @_ == 1 or croak 'usage: $sock->peeraddr()';
258 my $name = $sock->peername;
259 $name ? (sockaddr_in($name))[1] : undef;
263 @_ == 1 or croak 'usage: $sock->peerport()';
265 my $name = $sock->peername;
266 $name ? (sockaddr_in($name))[0] : undef;
270 @_ == 1 or croak 'usage: $sock->peerhost()';
272 my $addr = $sock->peeraddr;
273 $addr ? inet_ntoa($addr) : undef;
282 IO::Socket::INET - Object interface for AF_INET domain sockets
286 use IO::Socket::INET;
290 C<IO::Socket::INET> provides an object interface to creating and using sockets
291 in the AF_INET domain. It is built upon the L<IO::Socket> interface and
292 inherits all the methods defined by L<IO::Socket>.
300 Creates an C<IO::Socket::INET> object, which is a reference to a
301 newly created symbol (see the C<Symbol> package). C<new>
302 optionally takes arguments, these arguments are in key-value pairs.
304 In addition to the key-value pairs accepted by L<IO::Socket>,
305 C<IO::Socket::INET> provides.
308 PeerAddr Remote host address <hostname>[:<port>]
309 PeerHost Synonym for PeerAddr
310 PeerPort Remote port or service <service>[(<no>)] | <no>
311 LocalAddr Local host bind address hostname[:port]
312 LocalHost Synonym for LocalAddr
313 LocalPort Local host bind port <service>[(<no>)] | <no>
314 Proto Protocol name (or number) "tcp" | "udp" | ...
315 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
316 Listen Queue size for listen
317 ReuseAddr Set SO_REUSEADDR before binding
318 Reuse Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr)
319 ReusePort Set SO_REUSEPORT before binding
320 Broadcast Set SO_BROADCAST before binding
321 Timeout Timeout value for various operations
322 MultiHomed Try all addresses for multi-homed hosts
323 Blocking Determine if connection will be blocking mode
325 If C<Listen> is defined then a listen socket is created, else if the
326 socket type, which is derived from the protocol, is SOCK_STREAM then
329 Although it is not illegal, the use of C<MultiHomed> on a socket
330 which is in non-blocking mode is of little use. This is because the
331 first connect will never fail with a timeout as the connect call
334 The C<PeerAddr> can be a hostname or the IP-address on the
335 "xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic
336 service name. The service name might be followed by a number in
337 parenthesis which is used if the service is not known by the system.
338 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
339 by preceding it with a ":".
341 If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
342 then the constructor will try to derive C<Proto> from the service
343 name. As a last resort C<Proto> "tcp" is assumed. The C<Type>
344 parameter will be deduced from C<Proto> if not specified.
346 If the constructor is only passed a single argument, it is assumed to
347 be a C<PeerAddr> specification.
349 If C<Blocking> is set to 0, the connection will be in nonblocking mode.
350 If not specified it defaults to 1 (blocking mode).
354 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
355 PeerPort => 'http(80)',
358 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
360 $sock = IO::Socket::INET->new(Listen => 5,
361 LocalAddr => 'localhost',
365 $sock = IO::Socket::INET->new('127.0.0.1:25');
367 $sock = IO::Socket::INET->new(PeerPort => 9999,
368 PeerAddr => inet_ntoa(INADDR_BROADCAST),
370 LocalAddr => 'localhost',
372 or die "Can't bind : $@\n";
374 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
376 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
377 by default. This was not the case with earlier releases.
379 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
389 Return the address part of the sockaddr structure for the socket
393 Return the port number that the socket is using on the local host
397 Return the address part of the sockaddr structure for the socket in a
398 text form xx.xx.xx.xx
402 Return the address part of the sockaddr structure for the socket on
407 Return the port number for the socket on the peer host.
411 Return the address part of the sockaddr structure for the socket on the
412 peer host in a text form xx.xx.xx.xx
418 L<Socket>, L<IO::Socket>
422 Graham Barr. Currently maintained by the Perl Porters. Please report all
423 bugs to <perl5-porters@perl.org>.
427 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
428 This program is free software; you can redistribute it and/or
429 modify it under the same terms as Perl itself.