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