Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / msys / IO / Socket.pm
blobb8da0926692dbf727ddc80ca28b10cc4656a7651
1 # IO::Socket.pm
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;
9 require 5.005_64;
11 use IO::Handle;
12 use Socket 1.3;
13 use Carp;
14 use strict;
15 our(@ISA, $VERSION);
16 use Exporter;
17 use Errno;
19 # legacy
21 require IO::Socket::INET;
22 require IO::Socket::UNIX if ($^O ne 'epoc');
24 @ISA = qw(IO::Handle);
26 $VERSION = "1.26";
28 sub import {
29 my $pkg = shift;
30 my $callpkg = caller;
31 Exporter::export 'Socket', $callpkg, @_;
34 sub new {
35 my($class,%arg) = @_;
36 my $sock = $class->SUPER::new();
38 $sock->autoflush(1);
40 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
42 return scalar(%arg) ? $sock->configure(\%arg)
43 : $sock;
46 my @domain2pkg;
48 sub register_domain {
49 my($p,$d) = @_;
50 $domain2pkg[$d] = $p;
53 sub configure {
54 my($sock,$arg) = @_;
55 my $domain = delete $arg->{Domain};
57 croak 'IO::Socket: Cannot configure a generic socket'
58 unless defined $domain;
60 croak "IO::Socket: Unsupported socket domain"
61 unless defined $domain2pkg[$domain];
63 croak "IO::Socket: Cannot configure socket in domain '$domain'"
64 unless ref($sock) eq "IO::Socket";
66 bless($sock, $domain2pkg[$domain]);
67 $sock->configure($arg);
70 sub socket {
71 @_ == 4 or croak 'usage: $sock->socket(DOMAIN, TYPE, PROTOCOL)';
72 my($sock,$domain,$type,$protocol) = @_;
74 socket($sock,$domain,$type,$protocol) or
75 return undef;
77 ${*$sock}{'io_socket_domain'} = $domain;
78 ${*$sock}{'io_socket_type'} = $type;
79 ${*$sock}{'io_socket_proto'} = $protocol;
81 $sock;
84 sub socketpair {
85 @_ == 4 || croak 'usage: IO::Socket->socketpair(DOMAIN, TYPE, PROTOCOL)';
86 my($class,$domain,$type,$protocol) = @_;
87 my $sock1 = $class->new();
88 my $sock2 = $class->new();
90 socketpair($sock1,$sock2,$domain,$type,$protocol) or
91 return ();
93 ${*$sock1}{'io_socket_type'} = ${*$sock2}{'io_socket_type'} = $type;
94 ${*$sock1}{'io_socket_proto'} = ${*$sock2}{'io_socket_proto'} = $protocol;
96 ($sock1,$sock2);
99 sub connect {
100 @_ == 2 or croak 'usage: $sock->connect(NAME)';
101 my $sock = shift;
102 my $addr = shift;
103 my $timeout = ${*$sock}{'io_socket_timeout'};
104 my $err;
105 my $blocking;
106 $blocking = $sock->blocking(0) if $timeout;
108 if (!connect($sock, $addr)) {
109 if ($timeout && $!{EINPROGRESS}) {
110 require IO::Select;
112 my $sel = new IO::Select $sock;
114 if (!$sel->can_write($timeout)) {
115 $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
116 $@ = "connect: timeout";
118 elsif(!connect($sock,$addr) && not $!{EISCONN}) {
119 # Some systems refuse to re-connect() to
120 # an already open socket and set errno to EISCONN.
121 $err = $!;
122 $@ = "connect: $!";
125 else {
126 $err = $!;
127 $@ = "connect: $!";
131 $sock->blocking(1) if $blocking;
133 $! = $err if $err;
135 $err ? undef : $sock;
138 sub bind {
139 @_ == 2 or croak 'usage: $sock->bind(NAME)';
140 my $sock = shift;
141 my $addr = shift;
143 return bind($sock, $addr) ? $sock
144 : undef;
147 sub listen {
148 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
149 my($sock,$queue) = @_;
150 $queue = 5
151 unless $queue && $queue > 0;
153 return listen($sock, $queue) ? $sock
154 : undef;
157 sub accept {
158 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
159 my $sock = shift;
160 my $pkg = shift || $sock;
161 my $timeout = ${*$sock}{'io_socket_timeout'};
162 my $new = $pkg->new(Timeout => $timeout);
163 my $peer = undef;
165 if($timeout) {
166 require IO::Select;
168 my $sel = new IO::Select $sock;
170 unless ($sel->can_read($timeout)) {
171 $@ = 'accept: timeout';
172 $! = (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
173 return;
177 $peer = accept($new,$sock)
178 or return;
180 return wantarray ? ($new, $peer)
181 : $new;
184 sub sockname {
185 @_ == 1 or croak 'usage: $sock->sockname()';
186 getsockname($_[0]);
189 sub peername {
190 @_ == 1 or croak 'usage: $sock->peername()';
191 my($sock) = @_;
192 getpeername($sock)
193 || ${*$sock}{'io_socket_peername'}
194 || undef;
197 sub connected {
198 @_ == 1 or croak 'usage: $sock->connected()';
199 my($sock) = @_;
200 getpeername($sock);
203 sub send {
204 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
205 my $sock = $_[0];
206 my $flags = $_[2] || 0;
207 my $peer = $_[3] || $sock->peername;
209 croak 'send: Cannot determine peer address'
210 unless($peer);
212 my $r = defined(getpeername($sock))
213 ? send($sock, $_[1], $flags)
214 : send($sock, $_[1], $flags, $peer);
216 # remember who we send to, if it was sucessful
217 ${*$sock}{'io_socket_peername'} = $peer
218 if(@_ == 4 && defined $r);
223 sub recv {
224 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
225 my $sock = $_[0];
226 my $len = $_[2];
227 my $flags = $_[3] || 0;
229 # remember who we recv'd from
230 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
233 sub shutdown {
234 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
235 my($sock, $how) = @_;
236 shutdown($sock, $how);
239 sub setsockopt {
240 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
241 setsockopt($_[0],$_[1],$_[2],$_[3]);
244 my $intsize = length(pack("i",0));
246 sub getsockopt {
247 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
248 my $r = getsockopt($_[0],$_[1],$_[2]);
249 # Just a guess
250 $r = unpack("i", $r)
251 if(defined $r && length($r) == $intsize);
255 sub sockopt {
256 my $sock = shift;
257 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
258 : $sock->setsockopt(SOL_SOCKET,@_);
261 sub timeout {
262 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
263 my($sock,$val) = @_;
264 my $r = ${*$sock}{'io_socket_timeout'} || undef;
266 ${*$sock}{'io_socket_timeout'} = 0 + $val
267 if(@_ == 2);
272 sub sockdomain {
273 @_ == 1 or croak 'usage: $sock->sockdomain()';
274 my $sock = shift;
275 ${*$sock}{'io_socket_domain'};
278 sub socktype {
279 @_ == 1 or croak 'usage: $sock->socktype()';
280 my $sock = shift;
281 ${*$sock}{'io_socket_type'}
284 sub protocol {
285 @_ == 1 or croak 'usage: $sock->protocol()';
286 my($sock) = @_;
287 ${*$sock}{'io_socket_proto'};
292 __END__
294 =head1 NAME
296 IO::Socket - Object interface to socket communications
298 =head1 SYNOPSIS
300 use IO::Socket;
302 =head1 DESCRIPTION
304 C<IO::Socket> provides an object interface to creating and using sockets. It
305 is built upon the L<IO::Handle> interface and inherits all the methods defined
306 by L<IO::Handle>.
308 C<IO::Socket> only defines methods for those operations which are common to all
309 types of socket. Operations which are specified to a socket in a particular
310 domain have methods defined in sub classes of C<IO::Socket>
312 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
314 =head1 CONSTRUCTOR
316 =over 4
318 =item new ( [ARGS] )
320 Creates an C<IO::Socket>, which is a reference to a
321 newly created symbol (see the C<Symbol> package). C<new>
322 optionally takes arguments, these arguments are in key-value pairs.
323 C<new> only looks for one key C<Domain> which tells new which domain
324 the socket will be in. All other arguments will be passed to the
325 configuration method of the package for that domain, See below.
327 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
329 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
330 by default. This was not the case with earlier releases.
332 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
334 =back
336 =head1 METHODS
338 See L<perlfunc> for complete descriptions of each of the following
339 supported C<IO::Socket> methods, which are just front ends for the
340 corresponding built-in functions:
342 socket
343 socketpair
344 bind
345 listen
346 accept
347 send
348 recv
349 peername (getpeername)
350 sockname (getsockname)
351 shutdown
353 Some methods take slightly different arguments to those defined in L<perlfunc>
354 in attempt to make the interface more flexible. These are
356 =over 4
358 =item accept([PKG])
360 perform the system call C<accept> on the socket and return a new object. The
361 new object will be created in the same class as the listen socket, unless
362 C<PKG> is specified. This object can be used to communicate with the client
363 that was trying to connect. In a scalar context the new socket is returned,
364 or undef upon failure. In a list context a two-element array is returned
365 containing the new socket and the peer address; the list will
366 be empty upon failure.
368 =item socketpair(DOMAIN, TYPE, PROTOCOL)
370 Call C<socketpair> and return a list of two sockets created, or an
371 empty list on failure.
373 =back
375 Additional methods that are provided are:
377 =over 4
379 =item timeout([VAL])
381 Set or get the timeout value associated with this socket. If called without
382 any arguments then the current setting is returned. If called with an argument
383 the current setting is changed and the previous value returned.
385 =item sockopt(OPT [, VAL])
387 Unified method to both set and get options in the SOL_SOCKET level. If called
388 with one argument then getsockopt is called, otherwise setsockopt is called.
390 =item sockdomain
392 Returns the numerical number for the socket domain type. For example, for
393 a AF_INET socket the value of &AF_INET will be returned.
395 =item socktype
397 Returns the numerical number for the socket type. For example, for
398 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
400 =item protocol
402 Returns the numerical number for the protocol being used on the socket, if
403 known. If the protocol is unknown, as with an AF_UNIX socket, zero
404 is returned.
406 =item connected
408 If the socket is in a connected state the the peer address is returned.
409 If the socket is not in a connected state then undef will be returned.
411 =back
413 =head1 SEE ALSO
415 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
417 =head1 AUTHOR
419 Graham Barr. Currently maintained by the Perl Porters. Please report all
420 bugs to <perl5-porters@perl.org>.
422 =head1 COPYRIGHT
424 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
425 This program is free software; you can redistribute it and/or
426 modify it under the same terms as Perl itself.
428 =cut