forked from gitpan/LaBrea-Tarpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetIO.pm
385 lines (303 loc) · 8.4 KB
/
NetIO.pm
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/perl
package LaBrea::NetIO;
use strict;
#use diagnostics;
use vars qw($VERSION @ISA @EXPORT_OK );
$VERSION = do { my @r = (q$Revision: 0.01 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
use Socket;
use AutoLoader 'AUTOLOAD';
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = (@Socket::EXPORT, @Socket::EXPORT_OK, qw(
TARPIT_PORT
open_listen_sock
open_tcp
alarm_wrap
daemon_handler
read_daemon
fetch
reap_kids
set_so_linger
));
# autoload declarations
sub open_listen_sock;
sub open_tcp;
sub alarm_wrap;
sub daemon_handler;
sub fetch;
sub read_daemon;
sub reap_kids;
sub set_so_linger;
sub TARPIT_PORT { 8686; };
#
sub _fetch;
sub _want_daemon;
sub DESTROY {};
1;
__END__
=head1 NAME
LaBrea::Tarpit::NetIO
=head1 SYNOPSIS
use LaBrea::Tarpit::NetIO qw (
TARPIT_PORT
open_listen_sock
open_tcp
alarm_wrap
daemon_handler
read_daemon
fetch
reap_kids
set_so_linger
[plus any Socket.pm variable]
);
$error=open_listen_sock(HANDLE,address,port);
$error=open_tcp(*S,$host,$port);
*rv = alarm_wrap($timeout,$subref,@args);
$subref=daemon_handler(*HANDLE,$target);
read_daemon($subref,\@response);
$err=fetch($target,\@response,$command);
$alive = reap_kids(\%kids);
$rv = set_so_linger(*HANDLE,$seconds);
=head1 DESCRIPTION
B<NetIO> contains TCP client and server modules used by Tarpit modules.
B<NetIO> has available for EXPORT, any variable from the standard Socket.pm
module.
=over 4
=item $error=open_listen_sock(HANDLE,address,port);
Opens a server listening socket on HANDLE
input: HANDLE,
address, name or ip
defaults to all
interfaces if false
port defaults to 8686
returns: false on success
or error message
=cut
sub open_listen_sock {
my ($S,$host,$port) = @_;
# default connection is to ANY interface
my $iaddr = INADDR_ANY;
return 'interface address not found'
if $host && ! ($iaddr = inet_aton($host));
my $proto = getprotobyname('tcp');
$port = TARPIT_PORT unless $port && $port !~ /[\D]/;
return 'failed to create socket'
unless socket($S,PF_INET,SOCK_STREAM,$proto);
unless (setsockopt($S,SOL_SOCKET,SO_REUSEADDR,1)) {
close $S;
return 'failed to set socket options';
}
unless (bind($S,sockaddr_in($port,$iaddr))) {
close $S;
return 'failed to bind socket';
}
unless (listen($S,SOMAXCONN)) {
close $S;
return "failed to set listen queue";
}
$_ = select $S;
$| = 1;
select $_;
return undef;
}
=item $error=open_tcp(*S,$host,$port);
Open a tcp connection on port to host.
input: *S,hostname, port
returns: false on success
error message on failure
=cut
sub open_tcp {
my ($S,$host,$port) = @_;
my $iaddr;
return 'port is not numeric'
if !$port || $port =~ /\D/;
return 'hostname not found'
unless ($iaddr = inet_aton($host));
my $proto = getprotobyname('tcp');
return 'unable to open socket'
unless socket($S, PF_INET, SOCK_STREAM, $proto );
my $paddr = sockaddr_in($port, $iaddr);
unless (connect($S, $paddr)) {
close $S;
return 'could not connect to host';
}
$host = select $S; # temp save old selection
$| = 1;
select $host; # restore selection
return undef;
}
=item $rv = alarm_wrap($timeout,$subref,@args);
Provides an alarm wrapper for subroutines that may time out or B<die>.
input: timeout,
$subref,
arguments for $subref
returns: $subref return value(s)
on error
undef or () on error
$@ is set with error value
which will contain the string
'alarm_wrap timeout' if
the fault was timeout only
timeout is ignored if false
=cut
sub alarm_wrap {
my ($timeout,$subref,@args) = @_;
local $SIG{ALRM} = sub { die 'alarm_wrap timeout' };
my @rv;
alarm $timeout if $timeout;
eval { @rv = &$subref(@args) };
alarm 0;
@rv = () if $@;
return (wantarray) ? @rv : "@rv";
}
=item $subref=daemon_handler(*HANDLE,$target);
Opens a handle *HANDLE pointing to the Tarpit daemon, pipe or file
and returns a CODEREF to a subroutine that will read full
lines of data from the HANDLE. Do not try to read the handle directly.
input: *HANDLE
file name/path
or
hash ->{d_host} [optional]
->{d_port} [optional]
If B<target> is a HASH and d_host and/or d_port are not specified,
they default to localhost:8686
returns: subref or undef on open fail
usage: $present = daemon_handler(*H,$t);
while ( $data = &$present ) {
do something with $data;
}
close H;
=cut
sub daemon_handler {
my ($S,$target) = @_;
if ( &_want_daemon(\$target) ) {
my $d_port = $target->{d_port} || TARPIT_PORT;
my $d_host = $target->{d_host} || 'localhost';
return undef if open_tcp($S,$d_host,$d_port);
return sub { readline($S) };
} else {
return undef unless open($S,$target);
return sub { return scalar <$S> };
}
return undef;
}
# input: pointer to target
# returns: true if daemon
# false if file
# target is modified in place
# to point to file if HASH->{file}
#
sub _want_daemon {
my ($tgp) = @_;
return undef unless $tgp;
return undef unless ref $$tgp eq 'HASH';
if ( exists ${$tgp}->{file} ) {
$$tgp = ${$tgp}->{file}; # replace with file name
return undef;
}
1;
}
=item read_daemon($subref,\@response);
B<read_daemon> retrieves the response text from a
file or daemon and places the lines in array.
input: $subref to execute
pointer to @response
returns: number of lines
fills @response
Note: use 'alarm_wrap' with this routine
=cut
sub read_daemon {
my ($subref,$ary) = @_;
while ($_ = &$subref) {
push @$ary, $_; # recover report from daemon
}
$ary = @$ary; # return number of lines
}
=item $error=fetch($target,\@response,$command);
B<fetch> a response from B<target> using B<args>. Essentially a combination
of B<daemon_handler> and B<read_daemon> wrapped with B<alarm_wrap>.
Retrieves data from the host or file specified by B<target>. The B<args>
argument is ignored if B<target> is a file.
input: target, # hash->{host} [optional]
# hash->{port} [optional]
# hash->{d_timeout} [optional]
\@response, # result lines
command, # what to tell host
returns: error if fail
false on success
=cut
sub fetch {
my ($target,$ary,$command) = @_;
local *DAEMON;
my $subref = daemon_handler(*DAEMON,$target);
return "failed to open target" unless $subref;
my $timeout = (&_want_daemon(\$target) && $target->{d_timeout})
? $target->{d_timeout}
: 180;
alarm_wrap($timeout,\&_fetch,*DAEMON,$target,$subref,$ary,$command);
close DAEMON;
return $@;
}
sub _fetch {
my ($DAEMON,$target,$subref,$ary,$command) = @_;
print $DAEMON $command,"\n"
if $command && &_want_daemon(\$target);
read_daemon($subref,$ary);
}
=item $alive = reap_kids(\%kids);
Non-blocking reaper for PID's in (keys %kids). Deletes zombie children from
%kids and returns the number of kids remaining.
input: \%kids # hash of child PID's
returns: number of kids remaining
=cut
sub reap_kids {
my ($kp) = @_;
return 0 unless (@_ = keys %$kp);
require POSIX;
$_ = &POSIX::WNOHANG;
foreach my $pid (@_) {
delete $kp->{$pid} if waitpid($pid,$_);
}
return scalar keys %$kp;
}
=item $rv = set_so_linger(*HANDLE,$seconds);
Set SO_LINGER on top level socket
input: *HANDLE, seconds
returns: true = success, false = fail
=back
=cut
sub set_so_linger {
my ($FH,$sec) = @_;
setsockopt($FH,SOL_SOCKET,SO_LINGER,pack("ll",1,$sec));
}
=head1 EXPORT_OK
TARPIT_PORT
open_listen_sock
open_tcp
alarm_wrap
daemon_handler
read_daemon
fetch
reap_kids
set_so_linger
[plus any Socket.pm variable]
=head1 COPYRIGHT
Copyright 2002, Michael Robinton & BizSystems
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=head1 AUTHOR
Michael Robinton, [email protected]
=head1 SEE ALSO
perl(1), Socket(3), LaBrea::Tarpit(3), LaBrea::Tarpit::Get(3), LaBrea::Tarpit::Report(3),
LaBrea::Tarpit::Util(3), LaBrea::Tarpit::DShield(3), LaBrea::Tarpit::Codes(3)
=cut
1;