Streamline #5218 even more.
[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)) {
db33da1c 109 if ($timeout && exists &IO::EINPROGRESS && ($! == &IO::EINPROGRESS)) {
c9fcc6c4 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 }
f9c1db8d 118 elsif(!connect($sock,$addr) && not $!{EISCONN}) {
119 # Some systems (e.g. Digital UNIX/Tru64) fail to
120 # re-connect() to an already open socket and set
121 # errno to EISCONN (Socket is already connected)
122 # for such an attempt.
123 $err = $!;
124 $@ = "connect: $!";
cf7fe8a2 125 }
126 }
c9fcc6c4 127 else {
128 $err = $!;
129 $@ = "connect: $!";
130 }
131 }
760ac839 132
c9fcc6c4 133 $sock->blocking(1) if $blocking;
00fdd80d 134
c9fcc6c4 135 $! = $err if $err;
00fdd80d 136
c9fcc6c4 137 $err ? undef : $sock;
8add82fc 138}
139
140sub bind {
cf7fe8a2 141 @_ == 2 or croak 'usage: $sock->bind(NAME)';
142 my $sock = shift;
143 my $addr = shift;
8add82fc 144
cf7fe8a2 145 return bind($sock, $addr) ? $sock
146 : undef;
8add82fc 147}
148
149sub listen {
cf7fe8a2 150 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
151 my($sock,$queue) = @_;
8add82fc 152 $queue = 5
153 unless $queue && $queue > 0;
154
cf7fe8a2 155 return listen($sock, $queue) ? $sock
156 : undef;
8add82fc 157}
158
159sub accept {
cf7fe8a2 160 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
161 my $sock = shift;
162 my $pkg = shift || $sock;
163 my $timeout = ${*$sock}{'io_socket_timeout'};
8add82fc 164 my $new = $pkg->new(Timeout => $timeout);
165 my $peer = undef;
166
c9fcc6c4 167 if($timeout) {
168 require IO::Select;
cf7fe8a2 169
c9fcc6c4 170 my $sel = new IO::Select $sock;
171
172 unless ($sel->can_read($timeout)) {
173 $@ = 'accept: timeout';
174 $! = (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
175 return;
176 }
177 }
178
179 $peer = accept($new,$sock)
180 or return;
cf7fe8a2 181
c9fcc6c4 182 return wantarray ? ($new, $peer)
183 : $new;
8add82fc 184}
185
186sub sockname {
cf7fe8a2 187 @_ == 1 or croak 'usage: $sock->sockname()';
8add82fc 188 getsockname($_[0]);
189}
190
191sub peername {
cf7fe8a2 192 @_ == 1 or croak 'usage: $sock->peername()';
193 my($sock) = @_;
194 getpeername($sock)
195 || ${*$sock}{'io_socket_peername'}
8add82fc 196 || undef;
197}
198
cf7fe8a2 199sub connected {
200 @_ == 1 or croak 'usage: $sock->connected()';
201 my($sock) = @_;
202 getpeername($sock);
203}
204
8add82fc 205sub send {
cf7fe8a2 206 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
207 my $sock = $_[0];
8add82fc 208 my $flags = $_[2] || 0;
cf7fe8a2 209 my $peer = $_[3] || $sock->peername;
8add82fc 210
211 croak 'send: Cannot determine peer address'
212 unless($peer);
213
cf7fe8a2 214 my $r = defined(getpeername($sock))
215 ? send($sock, $_[1], $flags)
216 : send($sock, $_[1], $flags, $peer);
8add82fc 217
218 # remember who we send to, if it was sucessful
cf7fe8a2 219 ${*$sock}{'io_socket_peername'} = $peer
8add82fc 220 if(@_ == 4 && defined $r);
221
222 $r;
223}
224
225sub recv {
cf7fe8a2 226 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
8add82fc 227 my $sock = $_[0];
228 my $len = $_[2];
229 my $flags = $_[3] || 0;
230
231 # remember who we recv'd from
232 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
233}
234
cf7fe8a2 235sub shutdown {
236 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
237 my($sock, $how) = @_;
238 shutdown($sock, $how);
239}
8add82fc 240
241sub setsockopt {
cf7fe8a2 242 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
8add82fc 243 setsockopt($_[0],$_[1],$_[2],$_[3]);
244}
245
246my $intsize = length(pack("i",0));
247
248sub getsockopt {
cf7fe8a2 249 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
8add82fc 250 my $r = getsockopt($_[0],$_[1],$_[2]);
251 # Just a guess
252 $r = unpack("i", $r)
253 if(defined $r && length($r) == $intsize);
254 $r;
255}
256
257sub sockopt {
cf7fe8a2 258 my $sock = shift;
259 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
260 : $sock->setsockopt(SOL_SOCKET,@_);
8add82fc 261}
262
263sub timeout {
cf7fe8a2 264 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
265 my($sock,$val) = @_;
266 my $r = ${*$sock}{'io_socket_timeout'} || undef;
8add82fc 267
cf7fe8a2 268 ${*$sock}{'io_socket_timeout'} = 0 + $val
8add82fc 269 if(@_ == 2);
270
271 $r;
272}
273
27d4819a 274sub sockdomain {
cf7fe8a2 275 @_ == 1 or croak 'usage: $sock->sockdomain()';
276 my $sock = shift;
277 ${*$sock}{'io_socket_domain'};
27d4819a 278}
279
8add82fc 280sub socktype {
cf7fe8a2 281 @_ == 1 or croak 'usage: $sock->socktype()';
282 my $sock = shift;
283 ${*$sock}{'io_socket_type'}
8add82fc 284}
285
27d4819a 286sub protocol {
cf7fe8a2 287 @_ == 1 or croak 'usage: $sock->protocol()';
288 my($sock) = @_;
8fd73a68 289 ${*$sock}{'io_socket_proto'};
27d4819a 290}
291
cf7fe8a2 2921;
8add82fc 293
cf7fe8a2 294__END__
27d4819a 295
cf7fe8a2 296=head1 NAME
e713eafe 297
cf7fe8a2 298IO::Socket - Object interface to socket communications
8add82fc 299
cf7fe8a2 300=head1 SYNOPSIS
7a4c00b4 301
cf7fe8a2 302 use IO::Socket;
7a4c00b4 303
cf7fe8a2 304=head1 DESCRIPTION
7a4c00b4 305
cf7fe8a2 306C<IO::Socket> provides an object interface to creating and using sockets. It
307is built upon the L<IO::Handle> interface and inherits all the methods defined
308by L<IO::Handle>.
8add82fc 309
cf7fe8a2 310C<IO::Socket> only defines methods for those operations which are common to all
311types of socket. Operations which are specified to a socket in a particular
312domain have methods defined in sub classes of C<IO::Socket>
e713eafe 313
cf7fe8a2 314C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
e713eafe 315
cf7fe8a2 316=head1 CONSTRUCTOR
8add82fc 317
27d4819a 318=over 4
319
cf7fe8a2 320=item new ( [ARGS] )
27d4819a 321
cf7fe8a2 322Creates an C<IO::Socket>, which is a reference to a
323newly created symbol (see the C<Symbol> package). C<new>
324optionally takes arguments, these arguments are in key-value pairs.
325C<new> only looks for one key C<Domain> which tells new which domain
326the socket will be in. All other arguments will be passed to the
327configuration method of the package for that domain, See below.
8add82fc 328
cf7fe8a2 329 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
330
331As of VERSION 1.18 all IO::Socket objects have autoflush turned on
332by default. This was not the case with earlier releases.
27d4819a 333
cf7fe8a2 334 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
27d4819a 335
336=back
8add82fc 337
cf7fe8a2 338=head1 METHODS
8add82fc 339
cf7fe8a2 340See L<perlfunc> for complete descriptions of each of the following
341supported C<IO::Socket> methods, which are just front ends for the
342corresponding built-in functions:
8add82fc 343
cf7fe8a2 344 socket
345 socketpair
346 bind
347 listen
348 accept
349 send
350 recv
351 peername (getpeername)
352 sockname (getsockname)
353 shutdown
8add82fc 354
cf7fe8a2 355Some methods take slightly different arguments to those defined in L<perlfunc>
356in attempt to make the interface more flexible. These are
8add82fc 357
cf7fe8a2 358=over 4
8add82fc 359
cf7fe8a2 360=item accept([PKG])
8add82fc 361
cf7fe8a2 362perform the system call C<accept> on the socket and return a new object. The
363new object will be created in the same class as the listen socket, unless
364C<PKG> is specified. This object can be used to communicate with the client
365that was trying to connect. In a scalar context the new socket is returned,
366or undef upon failure. In an array context a two-element array is returned
c4be5b27 367containing the new socket and the peer address; the list will
cf7fe8a2 368be empty upon failure.
8add82fc 369
c4be5b27 370=item socketpair(DOMAIN, TYPE, PROTOCOL)
371
372Call C<socketpair> and return a list of two sockets created, or an
373empty list on failure.
374
375=back
376
377Additional methods that are provided are:
378
379=over 4
8add82fc 380
cf7fe8a2 381=item timeout([VAL])
8add82fc 382
cf7fe8a2 383Set or get the timeout value associated with this socket. If called without
384any arguments then the current setting is returned. If called with an argument
385the current setting is changed and the previous value returned.
8add82fc 386
cf7fe8a2 387=item sockopt(OPT [, VAL])
27d4819a 388
cf7fe8a2 389Unified method to both set and get options in the SOL_SOCKET level. If called
390with one argument then getsockopt is called, otherwise setsockopt is called.
8add82fc 391
cf7fe8a2 392=item sockdomain
8add82fc 393
cf7fe8a2 394Returns the numerical number for the socket domain type. For example, for
395a AF_INET socket the value of &AF_INET will be returned.
8add82fc 396
cf7fe8a2 397=item socktype
8add82fc 398
cf7fe8a2 399Returns the numerical number for the socket type. For example, for
400a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
27d4819a 401
cf7fe8a2 402=item protocol
8add82fc 403
cf7fe8a2 404Returns the numerical number for the protocol being used on the socket, if
405known. If the protocol is unknown, as with an AF_UNIX socket, zero
406is returned.
8add82fc 407
cf7fe8a2 408=item connected
8add82fc 409
cf7fe8a2 410If the socket is in a connected state the the peer address is returned.
411If the socket is not in a connected state then undef will be returned.
27d4819a 412
413=back
8add82fc 414
7a4c00b4 415=head1 SEE ALSO
8add82fc 416
cf7fe8a2 417L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
8add82fc 418
7a4c00b4 419=head1 AUTHOR
8add82fc 420
854822f1 421Graham Barr. Currently maintained by the Perl Porters. Please report all
422bugs to <perl5-porters@perl.org>.
760ac839 423
8add82fc 424=head1 COPYRIGHT
425
cf7fe8a2 426Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
427This program is free software; you can redistribute it and/or
428modify it under the same terms as Perl itself.
8add82fc 429
430=cut