Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
10 KiB

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