use same treatment for EINVAL as for ETIMEDOUT
[p5sagit/p5-mst-13.2.git] / ext / IO / lib / IO / Socket.pm
CommitLineData
774d564b 1# IO::Socket.pm
8add82fc 2#
cf7fe8a2 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
774d564b 5# modify it under the same terms as Perl itself.
8add82fc 6
7package IO::Socket;
8
17f410f9 9require 5.005_64;
8add82fc 10
8add82fc 11use IO::Handle;
12use Socket 1.3;
13use Carp;
14use strict;
17f410f9 15our(@ISA, $VERSION);
8add82fc 16use Exporter;
c9fcc6c4 17use Errno;
8add82fc 18
cf7fe8a2 19# legacy
20
21require IO::Socket::INET;
3a2f06e9 22require IO::Socket::UNIX if ($^O ne 'epoc');
cf7fe8a2 23
8add82fc 24@ISA = qw(IO::Handle);
25
c9fcc6c4 26$VERSION = "1.26";
8add82fc 27
28sub import {
29 my $pkg = shift;
30 my $callpkg = caller;
31 Exporter::export 'Socket', $callpkg, @_;
32}
33
34sub new {
35 my($class,%arg) = @_;
cf7fe8a2 36 my $sock = $class->SUPER::new();
37
38 $sock->autoflush(1);
8add82fc 39
cf7fe8a2 40 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
8add82fc 41
cf7fe8a2 42 return scalar(%arg) ? $sock->configure(\%arg)
43 : $sock;
8add82fc 44}
45
cf7fe8a2 46my @domain2pkg;
27d4819a 47
48sub register_domain {
49 my($p,$d) = @_;
774d564b 50 $domain2pkg[$d] = $p;
27d4819a 51}
52
8add82fc 53sub configure {
cf7fe8a2 54 my($sock,$arg) = @_;
27d4819a 55 my $domain = delete $arg->{Domain};
56
57 croak 'IO::Socket: Cannot configure a generic socket'
58 unless defined $domain;
59
774d564b 60 croak "IO::Socket: Unsupported socket domain"
61 unless defined $domain2pkg[$domain];
27d4819a 62
7a4c00b4 63 croak "IO::Socket: Cannot configure socket in domain '$domain'"
cf7fe8a2 64 unless ref($sock) eq "IO::Socket";
27d4819a 65
cf7fe8a2 66 bless($sock, $domain2pkg[$domain]);
67 $sock->configure($arg);
8add82fc 68}
69
70sub socket {
cf7fe8a2 71 @_ == 4 or croak 'usage: $sock->socket(DOMAIN, TYPE, PROTOCOL)';
72 my($sock,$domain,$type,$protocol) = @_;
8add82fc 73
cf7fe8a2 74 socket($sock,$domain,$type,$protocol) or
8add82fc 75 return undef;
76
cf7fe8a2 77 ${*$sock}{'io_socket_domain'} = $domain;
78 ${*$sock}{'io_socket_type'} = $type;
79 ${*$sock}{'io_socket_proto'} = $protocol;
774d564b 80
cf7fe8a2 81 $sock;
8add82fc 82}
83
84sub socketpair {
c4be5b27 85 @_ == 4 || croak 'usage: IO::Socket->socketpair(DOMAIN, TYPE, PROTOCOL)';
8add82fc 86 my($class,$domain,$type,$protocol) = @_;
cf7fe8a2 87 my $sock1 = $class->new();
88 my $sock2 = $class->new();
8add82fc 89
cf7fe8a2 90 socketpair($sock1,$sock2,$domain,$type,$protocol) or
8add82fc 91 return ();
92
cf7fe8a2 93 ${*$sock1}{'io_socket_type'} = ${*$sock2}{'io_socket_type'} = $type;
94 ${*$sock1}{'io_socket_proto'} = ${*$sock2}{'io_socket_proto'} = $protocol;
8add82fc 95
cf7fe8a2 96 ($sock1,$sock2);
8add82fc 97}
98
99sub connect {
cf7fe8a2 100 @_ == 2 or croak 'usage: $sock->connect(NAME)';
101 my $sock = shift;
102 my $addr = shift;
103 my $timeout = ${*$sock}{'io_socket_timeout'};
c9fcc6c4 104 my $err;
00fdd80d 105 my $blocking;
106 $blocking = $sock->blocking(0) if $timeout;
cf7fe8a2 107
c9fcc6c4 108 if (!connect($sock, $addr)) {
109 if ($timeout && exists(&IO::EINPROGRESS) && ($! == &IO::EINPROGRESS)) {
110 require IO::Select;
8add82fc 111
c9fcc6c4 112 my $sel = new IO::Select $sock;
8add82fc 113
c9fcc6c4 114 if (!$sel->can_write($timeout)) {
115 $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
116 $@ = "connect: timeout";
cf7fe8a2 117 }
c9fcc6c4 118 elsif(!connect($sock,$addr)) {
119 $err = $!;
120 $@ = "connect: $!";
cf7fe8a2 121 }
122 }
c9fcc6c4 123 else {
124 $err = $!;
125 $@ = "connect: $!";
126 }
127 }
760ac839 128
c9fcc6c4 129 $sock->blocking(1) if $blocking;
00fdd80d 130
c9fcc6c4 131 $! = $err if $err;
00fdd80d 132
c9fcc6c4 133 $err ? undef : $sock;
8add82fc 134}
135
136sub bind {
cf7fe8a2 137 @_ == 2 or croak 'usage: $sock->bind(NAME)';
138 my $sock = shift;
139 my $addr = shift;
8add82fc 140
cf7fe8a2 141 return bind($sock, $addr) ? $sock
142 : undef;
8add82fc 143}
144
145sub listen {
cf7fe8a2 146 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
147 my($sock,$queue) = @_;
8add82fc 148 $queue = 5
149 unless $queue && $queue > 0;
150
cf7fe8a2 151 return listen($sock, $queue) ? $sock
152 : undef;
8add82fc 153}
154
155sub accept {
cf7fe8a2 156 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
157 my $sock = shift;
158 my $pkg = shift || $sock;
159 my $timeout = ${*$sock}{'io_socket_timeout'};
8add82fc 160 my $new = $pkg->new(Timeout => $timeout);
161 my $peer = undef;
162
c9fcc6c4 163 if($timeout) {
164 require IO::Select;
cf7fe8a2 165
c9fcc6c4 166 my $sel = new IO::Select $sock;
167
168 unless ($sel->can_read($timeout)) {
169 $@ = 'accept: timeout';
170 $! = (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
171 return;
172 }
173 }
174
175 $peer = accept($new,$sock)
176 or return;
cf7fe8a2 177
c9fcc6c4 178 return wantarray ? ($new, $peer)
179 : $new;
8add82fc 180}
181
182sub sockname {
cf7fe8a2 183 @_ == 1 or croak 'usage: $sock->sockname()';
8add82fc 184 getsockname($_[0]);
185}
186
187sub peername {
cf7fe8a2 188 @_ == 1 or croak 'usage: $sock->peername()';
189 my($sock) = @_;
190 getpeername($sock)
191 || ${*$sock}{'io_socket_peername'}
8add82fc 192 || undef;
193}
194
cf7fe8a2 195sub connected {
196 @_ == 1 or croak 'usage: $sock->connected()';
197 my($sock) = @_;
198 getpeername($sock);
199}
200
8add82fc 201sub send {
cf7fe8a2 202 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
203 my $sock = $_[0];
8add82fc 204 my $flags = $_[2] || 0;
cf7fe8a2 205 my $peer = $_[3] || $sock->peername;
8add82fc 206
207 croak 'send: Cannot determine peer address'
208 unless($peer);
209
cf7fe8a2 210 my $r = defined(getpeername($sock))
211 ? send($sock, $_[1], $flags)
212 : send($sock, $_[1], $flags, $peer);
8add82fc 213
214 # remember who we send to, if it was sucessful
cf7fe8a2 215 ${*$sock}{'io_socket_peername'} = $peer
8add82fc 216 if(@_ == 4 && defined $r);
217
218 $r;
219}
220
221sub recv {
cf7fe8a2 222 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
8add82fc 223 my $sock = $_[0];
224 my $len = $_[2];
225 my $flags = $_[3] || 0;
226
227 # remember who we recv'd from
228 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
229}
230
cf7fe8a2 231sub shutdown {
232 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
233 my($sock, $how) = @_;
234 shutdown($sock, $how);
235}
8add82fc 236
237sub setsockopt {
cf7fe8a2 238 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
8add82fc 239 setsockopt($_[0],$_[1],$_[2],$_[3]);
240}
241
242my $intsize = length(pack("i",0));
243
244sub getsockopt {
cf7fe8a2 245 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
8add82fc 246 my $r = getsockopt($_[0],$_[1],$_[2]);
247 # Just a guess
248 $r = unpack("i", $r)
249 if(defined $r && length($r) == $intsize);
250 $r;
251}
252
253sub sockopt {
cf7fe8a2 254 my $sock = shift;
255 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
256 : $sock->setsockopt(SOL_SOCKET,@_);
8add82fc 257}
258
259sub timeout {
cf7fe8a2 260 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
261 my($sock,$val) = @_;
262 my $r = ${*$sock}{'io_socket_timeout'} || undef;
8add82fc 263
cf7fe8a2 264 ${*$sock}{'io_socket_timeout'} = 0 + $val
8add82fc 265 if(@_ == 2);
266
267 $r;
268}
269
27d4819a 270sub sockdomain {
cf7fe8a2 271 @_ == 1 or croak 'usage: $sock->sockdomain()';
272 my $sock = shift;
273 ${*$sock}{'io_socket_domain'};
27d4819a 274}
275
8add82fc 276sub socktype {
cf7fe8a2 277 @_ == 1 or croak 'usage: $sock->socktype()';
278 my $sock = shift;
279 ${*$sock}{'io_socket_type'}
8add82fc 280}
281
27d4819a 282sub protocol {
cf7fe8a2 283 @_ == 1 or croak 'usage: $sock->protocol()';
284 my($sock) = @_;
8fd73a68 285 ${*$sock}{'io_socket_proto'};
27d4819a 286}
287
cf7fe8a2 2881;
8add82fc 289
cf7fe8a2 290__END__
27d4819a 291
cf7fe8a2 292=head1 NAME
e713eafe 293
cf7fe8a2 294IO::Socket - Object interface to socket communications
8add82fc 295
cf7fe8a2 296=head1 SYNOPSIS
7a4c00b4 297
cf7fe8a2 298 use IO::Socket;
7a4c00b4 299
cf7fe8a2 300=head1 DESCRIPTION
7a4c00b4 301
cf7fe8a2 302C<IO::Socket> provides an object interface to creating and using sockets. It
303is built upon the L<IO::Handle> interface and inherits all the methods defined
304by L<IO::Handle>.
8add82fc 305
cf7fe8a2 306C<IO::Socket> only defines methods for those operations which are common to all
307types of socket. Operations which are specified to a socket in a particular
308domain have methods defined in sub classes of C<IO::Socket>
e713eafe 309
cf7fe8a2 310C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
e713eafe 311
cf7fe8a2 312=head1 CONSTRUCTOR
8add82fc 313
27d4819a 314=over 4
315
cf7fe8a2 316=item new ( [ARGS] )
27d4819a 317
cf7fe8a2 318Creates an C<IO::Socket>, which is a reference to a
319newly created symbol (see the C<Symbol> package). C<new>
320optionally takes arguments, these arguments are in key-value pairs.
321C<new> only looks for one key C<Domain> which tells new which domain
322the socket will be in. All other arguments will be passed to the
323configuration method of the package for that domain, See below.
8add82fc 324
cf7fe8a2 325 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
326
327As of VERSION 1.18 all IO::Socket objects have autoflush turned on
328by default. This was not the case with earlier releases.
27d4819a 329
cf7fe8a2 330 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
27d4819a 331
332=back
8add82fc 333
cf7fe8a2 334=head1 METHODS
8add82fc 335
cf7fe8a2 336See L<perlfunc> for complete descriptions of each of the following
337supported C<IO::Socket> methods, which are just front ends for the
338corresponding built-in functions:
8add82fc 339
cf7fe8a2 340 socket
341 socketpair
342 bind
343 listen
344 accept
345 send
346 recv
347 peername (getpeername)
348 sockname (getsockname)
349 shutdown
8add82fc 350
cf7fe8a2 351Some methods take slightly different arguments to those defined in L<perlfunc>
352in attempt to make the interface more flexible. These are
8add82fc 353
cf7fe8a2 354=over 4
8add82fc 355
cf7fe8a2 356=item accept([PKG])
8add82fc 357
cf7fe8a2 358perform the system call C<accept> on the socket and return a new object. The
359new object will be created in the same class as the listen socket, unless
360C<PKG> is specified. This object can be used to communicate with the client
361that was trying to connect. In a scalar context the new socket is returned,
362or undef upon failure. In an array context a two-element array is returned
c4be5b27 363containing the new socket and the peer address; the list will
cf7fe8a2 364be empty upon failure.
8add82fc 365
c4be5b27 366=item socketpair(DOMAIN, TYPE, PROTOCOL)
367
368Call C<socketpair> and return a list of two sockets created, or an
369empty list on failure.
370
371=back
372
373Additional methods that are provided are:
374
375=over 4
8add82fc 376
cf7fe8a2 377=item timeout([VAL])
8add82fc 378
cf7fe8a2 379Set or get the timeout value associated with this socket. If called without
380any arguments then the current setting is returned. If called with an argument
381the current setting is changed and the previous value returned.
8add82fc 382
cf7fe8a2 383=item sockopt(OPT [, VAL])
27d4819a 384
cf7fe8a2 385Unified method to both set and get options in the SOL_SOCKET level. If called
386with one argument then getsockopt is called, otherwise setsockopt is called.
8add82fc 387
cf7fe8a2 388=item sockdomain
8add82fc 389
cf7fe8a2 390Returns the numerical number for the socket domain type. For example, for
391a AF_INET socket the value of &AF_INET will be returned.
8add82fc 392
cf7fe8a2 393=item socktype
8add82fc 394
cf7fe8a2 395Returns the numerical number for the socket type. For example, for
396a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
27d4819a 397
cf7fe8a2 398=item protocol
8add82fc 399
cf7fe8a2 400Returns the numerical number for the protocol being used on the socket, if
401known. If the protocol is unknown, as with an AF_UNIX socket, zero
402is returned.
8add82fc 403
cf7fe8a2 404=item connected
8add82fc 405
cf7fe8a2 406If the socket is in a connected state the the peer address is returned.
407If the socket is not in a connected state then undef will be returned.
27d4819a 408
409=back
8add82fc 410
7a4c00b4 411=head1 SEE ALSO
8add82fc 412
cf7fe8a2 413L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
8add82fc 414
7a4c00b4 415=head1 AUTHOR
8add82fc 416
854822f1 417Graham Barr. Currently maintained by the Perl Porters. Please report all
418bugs to <perl5-porters@perl.org>.
760ac839 419
8add82fc 420=head1 COPYRIGHT
421
cf7fe8a2 422Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
423This program is free software; you can redistribute it and/or
424modify it under the same terms as Perl itself.
8add82fc 425
426=cut