| 1 | # $Id: Socket.pm,v 1.24 1998/01/26 11:12:01 aas Exp $ | ||
| 2 | |||
| 3 | package LWP::Socket; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | LWP::Socket - TCP/IP socket interface | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | $socket = new LWP::Socket; | ||
| 12 | $socket->connect('localhost', 7); # echo | ||
| 13 | $quote = 'I dunno, I dream in Perl sometimes...'; | ||
| 14 | $socket->write("$quote\n"); | ||
| 15 | $socket->read_until("\n", \$buffer); | ||
| 16 | $socket->read(\$buffer); | ||
| 17 | $socket = undef; # close | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | B<Beware:> New code should not use this module. The IO::Socket::INET | ||
| 22 | module provide the standard Perl interface to OO Internet sockets. | ||
| 23 | Even LWP is now rewritten to use IO::Socket::INET throughout. | ||
| 24 | |||
| 25 | This class implements TCP/IP sockets. It groups socket generation, | ||
| 26 | TCP address manipulation and buffered reading. Errors are handled by | ||
| 27 | dying (throws exceptions). | ||
| 28 | |||
| 29 | Running this module standalone executes a self test which requires | ||
| 30 | localhost to serve chargen and echo protocols. | ||
| 31 | |||
| 32 | The following methods are available: | ||
| 33 | |||
| 34 | =over 4 | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | |||
| 39 | $VERSION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/); | ||
| 40 | sub Version { $VERSION; } | ||
| 41 | |||
| 42 | use Socket qw(pack_sockaddr_in unpack_sockaddr_in | ||
| 43 | PF_INET SOCK_STREAM INADDR_ANY | ||
| 44 | inet_ntoa inet_aton); | ||
| 45 | Socket->require_version(1.5); | ||
| 46 | |||
| 47 | use Carp (); | ||
| 48 | use Symbol qw(gensym); | ||
| 49 | |||
| 50 | use LWP::Debug (); | ||
| 51 | use LWP::IO (); | ||
| 52 | |||
| 53 | my $tcp_proto = (getprotobyname('tcp'))[2]; | ||
| 54 | |||
| 55 | |||
| 56 | =item $sock = new LWP::Socket() | ||
| 57 | |||
| 58 | Constructs a new socket object. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new | ||
| 63 | { | ||
| 64 | my($class, $socket, $host, $port) = @_; | ||
| 65 | |||
| 66 | unless ($socket) { | ||
| 67 | $socket = gensym(); | ||
| 68 | LWP::Debug::debug("Socket $socket"); | ||
| 69 | |||
| 70 | socket($socket, PF_INET, SOCK_STREAM, $tcp_proto) or | ||
| 71 | Carp::croak("socket: $!"); | ||
| 72 | } | ||
| 73 | |||
| 74 | my $self = bless { | ||
| 75 | 'socket' => $socket, | ||
| 76 | 'host' => $host, | ||
| 77 | 'port' => $port, | ||
| 78 | 'buffer' => '', | ||
| 79 | 'size' => 4096, | ||
| 80 | }, $class; | ||
| 81 | |||
| 82 | $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub DESTROY | ||
| 86 | { | ||
| 87 | my $socket = shift->{'socket'}; | ||
| 88 | close($socket); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub host { shift->{'host'}; } | ||
| 92 | sub port { shift->{'port'}; } | ||
| 93 | |||
| 94 | |||
| 95 | =item $sock->connect($host, $port) | ||
| 96 | |||
| 97 | Connect the socket to given host and port. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub connect | ||
| 102 | { | ||
| 103 | my($self, $host, $port) = @_; | ||
| 104 | Carp::croak("no host") unless defined $host && length $host; | ||
| 105 | Carp::croak("no port") unless defined $port && $port > 0; | ||
| 106 | |||
| 107 | LWP::Debug::trace("($host, $port)"); | ||
| 108 | |||
| 109 | $self->{'host'} = $host; | ||
| 110 | $self->{'port'} = $port; | ||
| 111 | |||
| 112 | my @addr = $self->_getaddress($host, $port); | ||
| 113 | Carp::croak("Can't resolv address for $host") | ||
| 114 | unless @addr; | ||
| 115 | |||
| 116 | LWP::Debug::debug("Connecting to host '$host' on port '$port'..."); | ||
| 117 | for (@addr) { | ||
| 118 | connect($self->{'socket'}, $_) and return; | ||
| 119 | } | ||
| 120 | Carp::croak("Could not connect to $host:$port"); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | =item $sock->shutdown() | ||
| 125 | |||
| 126 | Shuts down the connection. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub shutdown | ||
| 131 | { | ||
| 132 | my($self, $how) = @_; | ||
| 133 | $how = 2 unless defined $how; | ||
| 134 | shutdown($self->{'socket'}, $how); | ||
| 135 | delete $self->{'host'}; | ||
| 136 | delete $self->{'port'}; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | =item $sock->bind($host, $port) | ||
| 141 | |||
| 142 | Binds a name to the socket. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub bind | ||
| 147 | { | ||
| 148 | my($self, $host, $port) = @_; | ||
| 149 | my $name = $self->_getaddress($host, $port); | ||
| 150 | bind($self->{'socket'}, $name); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | =item $sock->listen($queuesize) | ||
| 155 | |||
| 156 | Set up listen queue for socket. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub listen | ||
| 161 | { | ||
| 162 | listen(shift->{'socket'}, @_); | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | =item $sock->accept($timeout) | ||
| 167 | |||
| 168 | Accepts a new connection. Returns a new LWP::Socket object if successful. | ||
| 169 | Timeout not implemented yet. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub accept | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my $timeout = shift; | ||
| 177 | my $ns = gensym(); | ||
| 178 | my $addr = accept($ns, $self->{'socket'}); | ||
| 179 | if ($addr) { | ||
| 180 | my($port, $addr) = unpack_sockaddr_in($addr); | ||
| 181 | return new LWP::Socket $ns, inet_ntoa($addr), $port; | ||
| 182 | } else { | ||
| 183 | Carp::croak("Can't accept: $!"); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | =item $sock->getsockname() | ||
| 189 | |||
| 190 | Returns a 2 element array ($host, $port) | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub getsockname | ||
| 195 | { | ||
| 196 | my($port, $addr) = unpack_sockaddr_in(getsockname(shift->{'socket'})); | ||
| 197 | (inet_ntoa($addr), $port); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | =item $sock->read_until($delim, $data_ref, $size, $timeout) | ||
| 202 | |||
| 203 | Reads data from the socket, up to a delimiter specified by a regular | ||
| 204 | expression. If $delim is undefined all data is read. If $size is | ||
| 205 | defined, data will be read internally in chunks of $size bytes. This | ||
| 206 | does not mean that we will return the data when size bytes are read. | ||
| 207 | |||
| 208 | Note that $delim is discarded from the data returned. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub read_until | ||
| 213 | { | ||
| 214 | my ($self, $delim, $data_ref, $size, $timeout) = @_; | ||
| 215 | |||
| 216 | { | ||
| 217 | my $d = $delim; | ||
| 218 | $d =~ s/\r/\\r/g; | ||
| 219 | $d =~ s/\n/\\n/g; | ||
| 220 | LWP::Debug::trace("('$d',...)"); | ||
| 221 | } | ||
| 222 | |||
| 223 | my $socket = $self->{'socket'}; | ||
| 224 | $delim = '' unless defined $delim; | ||
| 225 | $size ||= $self->{'size'}; | ||
| 226 | |||
| 227 | my $buf = \$self->{'buffer'}; | ||
| 228 | |||
| 229 | if (length $delim) { | ||
| 230 | while ($$buf !~ /$delim/) { | ||
| 231 | LWP::IO::read($socket, $$buf, $size, length($$buf), $timeout) | ||
| 232 | or die "Unexpected EOF"; | ||
| 233 | } | ||
| 234 | ($$data_ref, $self->{'buffer'}) = split(/$delim/, $$buf, 2); | ||
| 235 | } else { | ||
| 236 | $data_ref = $buf; | ||
| 237 | $self->{'buffer'} = ''; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | =item $sock->read($bufref, [$size, $timeout]) | ||
| 245 | |||
| 246 | Reads data of the socket. Not more than $size bytes. Might return | ||
| 247 | less if the data is available. Dies on timeout. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub read | ||
| 252 | { | ||
| 253 | my($self, $data_ref, $size, $timeout) = @_; | ||
| 254 | $size ||= $self->{'size'}; | ||
| 255 | |||
| 256 | LWP::Debug::trace('(...)'); | ||
| 257 | if (length $self->{'buffer'}) { | ||
| 258 | # return data from buffer until it is empty | ||
| 259 | #print "Returning data from buffer...$self->{'buffer'}\n"; | ||
| 260 | $$data_ref = substr($self->{'buffer'}, 0, $size); | ||
| 261 | substr($self->{'buffer'}, 0, $size) = ''; | ||
| 262 | return length $$data_ref; | ||
| 263 | } | ||
| 264 | LWP::IO::read($self->{'socket'}, $$data_ref, $size, undef, $timeout); | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | =item $sock->pushback($data) | ||
| 269 | |||
| 270 | Put data back into the socket. Data will returned next time you | ||
| 271 | read(). Can be used if you find out that you have read too much. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub pushback | ||
| 276 | { | ||
| 277 | LWP::Debug::trace('(' . length($_[1]) . ' bytes)'); | ||
| 278 | my $self = shift; | ||
| 279 | substr($self->{'buffer'}, 0, 0) = shift; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | =item $sock->write($data, [$timeout]) | ||
| 284 | |||
| 285 | Write data to socket. The $data argument might be a scalar or code. | ||
| 286 | |||
| 287 | If data is a reference to a subroutine, then we will call this routine | ||
| 288 | to obtain the data to be written. The routine will be called until it | ||
| 289 | returns undef or empty data. Data might be returned from the callback | ||
| 290 | as a scalar or as a reference to a scalar. | ||
| 291 | |||
| 292 | Write returns the number of bytes written to the socket. | ||
| 293 | |||
| 294 | =cut | ||
| 295 | |||
| 296 | sub write | ||
| 297 | { | ||
| 298 | my $self = shift; | ||
| 299 | my $timeout = $_[1]; # we don't want to copy data in $_[0] | ||
| 300 | LWP::Debug::trace('()'); | ||
| 301 | my $bytes_written = 0; | ||
| 302 | if (!ref $_[0]) { | ||
| 303 | $bytes_written = LWP::IO::write($self->{'socket'}, $_[0], $timeout); | ||
| 304 | } elsif (ref($_[0]) eq 'CODE') { | ||
| 305 | # write data until $callback returns empty data ''; | ||
| 306 | my $callback = shift; | ||
| 307 | while (1) { | ||
| 308 | my $data = &$callback; | ||
| 309 | last unless defined $data; | ||
| 310 | my $dataRef = ref($data) ? $data : \$data; | ||
| 311 | my $len = length $$dataRef; | ||
| 312 | last unless $len; | ||
| 313 | my $n = $self->write($$dataRef, $timeout); | ||
| 314 | $bytes_written += $n; | ||
| 315 | last if $n != $len; | ||
| 316 | } | ||
| 317 | } else { | ||
| 318 | Carp::croak('Illegal LWP::Socket->write() argument'); | ||
| 319 | } | ||
| 320 | $bytes_written; | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | =item $sock->_getaddress($h, $p) | ||
| 326 | |||
| 327 | Given a host and a port, it will return the address (sockaddr_in) | ||
| 328 | suitable as the C<name> argument for connect() or bind(). Might return | ||
| 329 | several addresses in array context if the hostname is bound to several | ||
| 330 | IP addresses. | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | |||
| 335 | sub _getaddress | ||
| 336 | { | ||
| 337 | my($self, $host, $port) = @_; | ||
| 338 | |||
| 339 | my(@addr); | ||
| 340 | if (!defined $host) { | ||
| 341 | # INADDR_ANY | ||
| 342 | $addr[0] = pack_sockaddr_in($port, INADDR_ANY); | ||
| 343 | } | ||
| 344 | elsif ($host =~ /^(\d+\.\d+\.\d+\.\d+)$/) { | ||
| 345 | # numeric IP address | ||
| 346 | $addr[0] = pack_sockaddr_in($port, inet_aton($1)); | ||
| 347 | } else { | ||
| 348 | # hostname | ||
| 349 | LWP::Debug::debug("resolving host '$host'..."); | ||
| 350 | (undef,undef,undef,undef,@addr) = gethostbyname($host); | ||
| 351 | for (@addr) { | ||
| 352 | LWP::Debug::debug(" ..." . inet_ntoa($_)); | ||
| 353 | $_ = pack_sockaddr_in($port, $_); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | wantarray ? @addr : $addr[0]; | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | ##################################################################### | ||
| 361 | |||
| 362 | package main; | ||
| 363 | |||
| 364 | eval join('',<DATA>) || die $@ unless caller(); | ||
| 365 | |||
| 366 | =back | ||
| 367 | |||
| 368 | =head1 SELF TEST | ||
| 369 | |||
| 370 | This self test is only executed when this file is run standalone. It | ||
| 371 | tests its functions against some standard TCP services implemented by | ||
| 372 | inetd. If you do not have them around the tests will fail. | ||
| 373 | |||
| 374 | =cut | ||
| 375 | |||
| 376 | 1; | ||
| 377 | |||
| 378 | __END__ |