summaryrefslogtreecommitdiff
path: root/lib/Net/Socket.pm
blob: d24e6252338e52fd69234aaa7a23907d61fb721c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package Net::Socket;

=head1 NAME

Net::Socket - TEMPORARY Socket filedescriptor class, so Net::FTP still
works while IO::Socket is having a re-fit <GBARR>

=head1 DESCRIPTION

NO TEXT --- THIS MODULE IS TEMPORARY

=cut

require 5.001;
use Socket 1.3;
use Carp;
require Exporter;

@ISA = qw(Exporter);
@EXPORT_OK = @Socket::EXPORT;

$VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
sub Version { $VERSION }

##
## Really WANT FileHandle::new to return this !!!
##
my $seq = 0;
sub _gensym {
    my $pkg = @_ ? ref($_[0]) || $_[0] : "";
    local *{$pkg . "::GLOB" . ++$seq};
    \delete ${$pkg . "::"}{'GLOB' . $seq};
}

my %socket_type = (
 tcp => SOCK_STREAM,
 udp => SOCK_DGRAM,
 rpc => SOCK_DGRAM,
);

# Peer     => remote host name for a 'connect' socket
# Proto    => specifiy protocol by it self (but override by Service)
# Service  => require service eg 'ftp' or 'ftp/tcp', overrides Proto
# Port     => port num for connect eg 'ftp' or 21, defaults to Service
# Bind     => port to bind to, defaults to INADDR_ANY
# Listen   => queue size for listen
#
# if Listen is defined then a listen socket is created, else if the socket
# type, which is derived from the protocol, is SOCK_STREAM then a connect
# is called

=head2 new( %args )

The new constructor takes its arguments in the form of a hash. Accepted 
arguments are

 Peer     => remote host name for a 'connect' socket
 Proto    => specifiy protocol by it self (but override by Service)
 Service  => require service eg 'ftp' or 'ftp/tcp', overrides Proto
 Port     => port num for connect eg 'ftp' or 21, defaults to Service
 Bind     => port to bind to, defaults to INADDR_ANY
 Listen   => queue size for listen

=cut

sub new {
 my $pkg = shift;
 my %arg = @_;

 my $proto    = $arg{Proto} || "";
 my $bindport = $arg{Bind}  || 0;
 my $servport = $arg{Port}  || 0;

 my $service  = $arg{Service} || $servport || $bindport;

 ($service,$proto) = split(m,/,, $service)
	if $service =~ m,/,;

 my @serv  = $service =~ /\D/ ? getservbyname($service,$proto)
                              : getservbyport($service,$proto);

 $proto = $proto || $serv[3];

 croak "cannot determine protocol"
	unless $proto;

 my @proto = $proto =~ /\D/ ? getprotobyname($proto)
                            : getprotobynumber($proto);

 croak "unknown protocol"
	unless @proto;

 my $type = $arg{Type} || $socket_type{$proto[0]} or
	croak "Unknown socket type";

 my $bindaddr = exists $arg{Addr} ? inet_aton($arg{Addr}) 
				  : INADDR_ANY;

 croak "bad bind address $arg{Addr}"
	unless $bindaddr;

 my $sock = bless _gensym(), ref($pkg) || $pkg;

 socket($sock, AF_INET, $type, $proto[2]) or
	croak "socket: $!";
 
 $bindport = (getservbyname($bindport,$proto))[2]
	if $bindport =~ /\D/;

 bind($sock, sockaddr_in($bindport, $bindaddr)) or
	croak "bind: $!";

 if(defined $arg{Listen})
  {
   my $queue = $arg{Listen} || 1;
 
   listen($sock, $queue) or
	croak "listen: $!";
  }
 else
  {
   $servport = $serv[2] || 0
	unless $servport =~ /^\d+$/ && $servport > 0;

   croak "cannot determine port"
	unless($servport);

   my $destaddr = defined $arg{Peer} ? inet_aton($arg{Peer})
				     : undef;

   my $peername = defined $destaddr ? sockaddr_in($servport,$destaddr)
				    : undef;
   
   
   if($type == SOCK_STREAM || $destaddr)
    {
     croak "bad peer address"
	unless defined $destaddr;
     
     connect($sock, $peername) or
	croak "connect: $!";

     ${*$sock}{Peername} = getpeername($sock);
    }
   else
    {
     ${*$sock}{Peername} = $peername;
    }
  }
 
 ${*$sock}{Sockname} = getsockname($sock);

 $sock;
}

=head2 autoflush( [$val] )

Set the file descriptor to autoflush, depending on C<$val>

=cut

sub autoflush {
 my $sock = shift;
 my $val = @_ ? shift : 0;

 select((select($sock), $| = $val)[$[]);
}

=head2 accept

perform the system call C<accept> on the socket and return a new Net::Socket
object. This object can be used to communicate with the client that was trying
to connect.

=cut

sub accept {
 my $sock = shift;

 my $new = bless _gensym();

 accept($new,$sock) or
	croak "accept: $!";

 ${*$new}{Peername} = getpeername($new) or
	croak "getpeername: $!";

 ${*$new}{Sockname} = getsockname($new) or
	croak "getsockname: $!";

 $new;
}

=head2 close

Close the file descriptor

=cut

sub close {
 my $sock = shift;

 delete ${*$sock}{Sockname};
 delete ${*$sock}{Peername};

 close($sock);
}

=head2 dup

Create a duplicate of the socket object

=cut

sub dup {
 my $sock = shift;
 my $dup = bless _gensym(), ref($sock);

 if(open($dup,">&" . fileno($sock))) { 
  # Copy all the internals
  ${*$dup} = ${*$sock};
  @{*$dup} = @{*$sock};
  %{*$dup} = %{*$sock};
 }
 else {
  undef $dup;
 }

 $dup;
}

# Some info about the local socket

=head2 sockname

Return a packed sockaddr structure for the socket

=head2 sockaddr

Return the address part of the sockaddr structure for the socket

=head2 sockport

Return the port number that the socket is using on the local host

=head2 sockhost

Return the address part of the sockaddr structure for the socket in a
text form xx.xx.xx.xx

=cut

sub sockname { my $sock = shift;  ${*$sock}{Sockname} }
sub sockaddr { (sockaddr_in(shift->sockname))[1]}
sub sockport { (sockaddr_in(shift->sockname))[0]}
sub sockhost { inet_ntoa( shift->sockaddr);}

# Some info about the remote socket, for connect-d sockets

=head2 peername, peeraddr, peerport, peerhost

Same as for the sock* functions, but returns the data about the peer
host instead of the local host.

=cut

sub peername { my $sock = shift;  ${*$sock}{Peername} or croak "no peer" }
sub peeraddr { (sockaddr_in(shift->peername))[1]}
sub peerport { (sockaddr_in(shift->peername))[0]}
sub peerhost { inet_ntoa( shift->peeraddr);}

=head2 send( $buf [, $flags [, $to]] )

For a udp socket, send the contents of C<$buf> to the remote host C<$to> using
flags C<$flags>. 

If C<$to> is not specified then the data is sent to the host which the socket
last communicated with, ie sent to or recieved from.

If C<$flags> is ommited then 0 is used

=cut

sub send {
 my $sock = shift;
 local *buf = \$_[0]; shift;
 my $flags = shift || 0;
 my $to = shift || $sock->peername;

 # remember who we send to
 ${*$sock}{Peername} = $to;

 send($sock, $buf, $flags, $to);
}

=head2 recv( $buf, $len [, $flags] )

Receive C<$len> bytes of data from the socket and place into C<$buf>

If C<$flags> is ommited then 0 is used

=cut

sub recv {
 my $sock = shift;
 local *buf = \$_[0]; shift;
 my $len = shift;
 my $flags = shift || 0;

 # remember who we recv'd from
 ${*$sock}{Peername} = recv($sock, $buf='', $len, $flags);
}

=head1 AUTHOR

Graham Barr <Graham.Barr@tiuk.ti.com>

=head1 REVISION

$Revision: 1.2 $

=head1 COPYRIGHT

Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms
as Perl itself.

=cut

1; # Keep require happy