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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
# $OpenBSD: funcs.pl,v 1.1.1.1 2013/01/03 17:36:39 bluhm Exp $
# Copyright (c) 2010-2013 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
use feature 'switch';
use Errno;
use Digest::MD5;
use IO::Socket qw(sockatmark);
use Socket;
use Time::HiRes qw(time alarm sleep);
use BSD::Socket::Splice qw(setsplice getsplice geterror);
########################################################################
# Client funcs
########################################################################
sub write_stream {
my $self = shift;
my $len = shift // $self->{len} // 251;
my $sleep = $self->{sleep};
my $ctx = Digest::MD5->new();
my $char = '0';
for (my $i = 1; $i < $len; $i++) {
$ctx->add($char);
print $char
or die ref($self), " print failed: $!";
given ($char) {
when(/9/) { $char = 'A' }
when(/Z/) { $char = 'a' }
when(/z/) { $char = "\n" }
when(/\n/) { print STDERR "."; $char = '0' }
default { $char++ }
}
if ($self->{sleep}) {
IO::Handle::flush(\*STDOUT);
sleep $self->{sleep};
}
}
if ($len) {
$char = "\n";
$ctx->add($char);
print $char
or die ref($self), " print failed: $!";
print STDERR ".\n";
}
IO::Handle::flush(\*STDOUT);
print STDERR "LEN: $len\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub write_oob {
my $self = shift;
my $len = shift // $self->{len} // 251;
my $ctx = Digest::MD5->new();
my $msg = "";
my $char = '0';
for (my $i = 1; $i < $len; $i++) {
$msg .= $char;
given ($char) {
when(/9/) {
$ctx->add("[$char]");
defined(send(STDOUT, $msg, MSG_OOB))
or die ref($self), " send OOB failed: $!";
# If tcp urgent data is sent too fast,
# it may get overwritten and lost.
sleep .1;
$msg = "";
$char = 'A';
}
when(/Z/) { $ctx->add($char); $char = 'a' }
when(/z/) { $ctx->add($char); $char = "\n" }
when(/\n/) {
$ctx->add($char);
defined(send(STDOUT, $msg, 0))
or die ref($self), " send failed: $!";
print STDERR ".";
$msg = "";
$char = '0';
}
default { $ctx->add($char); $char++ }
}
}
if ($len) {
$char = "\n";
$msg .= $char;
$ctx->add($char);
send(STDOUT, $msg, 0)
or die ref($self), " send failed: $!";
print STDERR ".\n";
}
IO::Handle::flush(\*STDOUT);
print STDERR "LEN: $len\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub write_datagram {
my $self = shift;
my @lengths = @{$self->{lengths} || [ shift // $self->{len} // 251 ]};
my $sleep = $self->{sleep};
my $len = 0;
my $ctx = Digest::MD5->new();
my $char = '0';
my @md5s;
for (my $num = 0; $num < @lengths; $num++) {
my $l = $lengths[$num];
my $string = "";
for (my $i = 1; $i < $l; $i++) {
$ctx->add($char);
$string .= $char;
given ($char) {
when(/9/) { $char = 'A' }
when(/Z/) { $char = 'a' }
when(/z/) { $char = "\n" }
when(/\n/) { $char = '0' }
default { $char++ }
}
}
if ($l) {
$char = "\n";
$ctx->add($char);
$string .= $char;
}
defined(my $write = syswrite(STDOUT, $string))
or die ref($self), " syswrite number $num failed: $!";
$write == $l
or die ref($self), " syswrite length $l did write $write";
$len += $write;
print STDERR ".";
sleep $self->{sleep} if $self->{sleep};
}
print STDERR "\n";
print STDERR "LEN: $len\n";
print STDERR "LENGTHS: @lengths\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub solingerout {
my $self = shift;
setsockopt(STDOUT, SOL_SOCKET, SO_LINGER, pack('ii', 1, 0))
or die ref($self), " set linger out failed: $!";
}
########################################################################
# Relay funcs
########################################################################
sub relay_copy_stream {
my $self = shift;
my $max = $self->{max};
my $idle = $self->{idle};
my $size = $self->{size} || 8093;
my $len = 0;
while (1) {
my $rin = my $win = my $ein = '';
vec($rin, fileno(STDIN), 1) = 1;
vec($ein, fileno(STDIN), 1) = 1 unless $self->{oobinline};
defined(my $n = select($rin, undef, $ein, $idle))
or die ref($self), " select failed: $!";
if ($idle && $n == 0) {
print STDERR "\n";
print STDERR "Timeout\n";
last;
}
my $buf;
my $atmark = sockatmark(\*STDIN)
or die ref($self), " sockatmark failed: $!";
if ($atmark == 1) {
if ($self->{oobinline}) {
defined(recv(STDIN, $buf, 1, 0))
or die ref($self), " recv OOB failed: $!";
$len += length($buf);
defined(send(STDOUT, $buf, MSG_OOB))
or die ref($self), " send OOB failed: $!";
} else {
defined(recv(STDIN, $buf, 1, MSG_OOB)) ||
$!{EINVAL}
or die ref($self), " recv OOB failed: $!";
print STDERR "OOB: $buf\n" if length($buf);
}
}
if ($self->{nonblocking}) {
vec($rin, fileno(STDIN), 1) = 1;
select($rin, undef, undef, undef)
or die ref($self), " select read failed: $!";
}
my $read = sysread(STDIN, $buf,
$max && $max < $size ? $max : $size);
defined($read)
or die ref($self), " sysread at $len failed: $!";
if ($read == 0) {
print STDERR "\n";
print STDERR "End\n";
last;
}
print STDERR ".";
if ($max && $len + $read > $max) {
$read = $max - $len;
}
my $off = 0;
while ($off < $read) {
if ($self->{nonblocking}) {
vec($win, fileno(STDOUT), 1) = 1;
select(undef, $win, undef, undef)
or die ref($self),
" select write failed: $!";
}
my $write;
# Unfortunately Perl installs signal handlers without
# SA_RESTART. Work around by restarting manually.
do {
$write = syswrite(STDOUT, $buf, $read - $off,
$off);
} while (!defined($write) && $!{EINTR});
defined($write) || $!{ETIMEDOUT}
or die ref($self), " syswrite at $len failed: $!";
defined($write) or next;
$off += $write;
$len += $write;
}
if ($max && $len == $max) {
print STDERR "\n";
print STDERR "Max\n";
last;
}
}
print STDERR "LEN: $len\n";
}
sub relay_copy_datagram {
my $self = shift;
my $max = $self->{max};
my $idle = $self->{idle};
my $size = $self->{size} || 2**16;
my $len = 0;
for (my $num = 0;; $num++) {
my $rin = my $win = '';
if ($idle) {
vec($rin, fileno(STDIN), 1) = 1;
defined(my $n = select($rin, undef, undef, $idle))
or die ref($self), " select idle failed: $!";
if ($n == 0) {
print STDERR "\n";
print STDERR "Timeout\n";
last;
}
} elsif ($self->{nonblocking}) {
vec($rin, fileno(STDIN), 1) = 1;
select($rin, undef, undef, undef)
or die ref($self), " select read failed: $!";
}
defined(my $read = sysread(STDIN, my $buf, $size))
or die ref($self), " sysread number $num failed: $!";
print STDERR ".";
if ($max && $len + $read > $max) {
print STDERR "\n";
print STDERR "Max\n";
last;
}
if ($self->{nonblocking}) {
vec($win, fileno(STDOUT), 1) = 1;
select(undef, $win, undef, undef)
or die ref($self), " select write failed: $!";
}
defined(my $write = syswrite(STDOUT, $buf)) || $!{EMSGSIZE}
or die ref($self), " syswrite number $num failed: $!";
if (defined($write)) {
$read == $write
or die ref($self), " syswrite read $read ".
"did write $write";
$len += $write;
}
if ($max && $len == $max) {
print STDERR "\n";
print STDERR "Max\n";
last;
}
}
print STDERR "LEN: $len\n";
}
sub relay_copy {
my $self = shift;
my $protocol = $self->{protocol} || "tcp";
given ($protocol) {
when (/tcp/) { relay_copy_stream($self, @_) }
when (/udp/) { relay_copy_datagram($self, @_) }
default { die ref($self), " unknown protocol name: $protocol" }
}
}
sub relay_splice {
my $self = shift;
my $max = $self->{max};
my $idle = $self->{idle};
my $len = 0;
my $splicelen;
my $shortsplice = 0;
my $error;
do {
my $splicemax = $max ? $max - $len : 0;
setsplice(\*STDIN, \*STDOUT, $splicemax, $idle)
or die ref($self), " splice stdin to stdout failed: $!";
if ($self->{readblocking}) {
my $read;
# block by reading from the source socket
do {
# busy loop to test soreceive
$read = sysread(STDIN, my $buf, 2**16);
} while ($self->{nonblocking} && !defined($read) &&
$!{EAGAIN});
defined($read)
or die ref($self), " read blocking failed: $!";
$read > 0 and die ref($self),
" read blocking has data: $read";
print STDERR "Read\n";
} else {
my $rin = '';
vec($rin, fileno(STDIN), 1) = 1;
select($rin, undef, undef, undef)
or die ref($self), " select failed: $!";
}
defined($error = geterror(\*STDIN))
or die ref($self), " get error from stdin failed: $!";
($! = $error) && ! $!{ETIMEDOUT} && ! $!{EMSGSIZE}
and die ref($self), " splice failed: $!";
defined($splicelen = getsplice(\*STDIN))
or die ref($self), " get splice len from stdin failed: $!";
print STDERR "SPLICELEN: $splicelen\n";
!$max || $splicelen <= $splicemax
or die ref($self), " splice len $splicelen ".
"greater than max $splicemax";
$len += $splicelen;
} while ($max && $max > $len && !$shortsplice++);
if ($idle && $error == Errno::ETIMEDOUT) {
print STDERR "Timeout\n";
}
if ($max && $max == $len) {
print STDERR "Max\n";
} elsif ($max && $max < $len) {
die ref($self), " max $max less than len $len";
} elsif ($max && $max > $len && $splicelen) {
die ref($self), " max $max greater than len $len";
} elsif (!$error) {
defined(my $read = sysread(STDIN, my $buf, 2**16))
or die ref($self), " sysread stdin failed: $!";
$read > 0
and die ref($self), " sysread stdin has data: $read";
print STDERR "End\n";
}
print STDERR "LEN: $len\n";
}
sub relay {
my $self = shift;
my $forward = $self->{forward};
given ($forward) {
when (/copy/) { relay_copy($self, @_) }
when (/splice/) { relay_splice($self, @_) }
default { die ref($self), " unknown forward name: $forward" }
}
my $soerror;
$soerror = getsockopt(STDIN, SOL_SOCKET, SO_ERROR)
or die ref($self), " get error from stdin failed: $!";
print STDERR "ERROR IN: ", unpack('i', $soerror), "\n";
$soerror = getsockopt(STDOUT, SOL_SOCKET, SO_ERROR)
or die ref($self), " get error from stdout failed: $!";
print STDERR "ERROR OUT: ", unpack('i', $soerror), "\n";
}
sub ioflip {
my $self = shift;
open(my $fh, '<&', \*STDIN)
or die ref($self), " ioflip dup failed: $!";
open(STDIN, '<&', \*STDOUT)
or die ref($self), " ioflip dup STDIN failed: $!";
open(STDOUT, '>&', $fh)
or die ref($self), " ioflip dup STDOUT failed: $!";
close($fh)
or die ref($self), " ioflip close failed: $!";
}
sub errignore {
$SIG{PIPE} = 'IGNORE';
$SIG{__DIE__} = sub {
die @_ if $^S;
warn @_;
my $soerror;
$soerror = getsockopt(STDIN, SOL_SOCKET, SO_ERROR);
print STDERR "ERROR IN: ", unpack('i', $soerror), "\n";
$soerror = getsockopt(STDOUT, SOL_SOCKET, SO_ERROR);
print STDERR "ERROR OUT: ", unpack('i', $soerror), "\n";
IO::Handle::flush(\*STDERR);
POSIX::_exit(0);
};
}
sub shutin {
my $self = shift;
shutdown(\*STDIN, SHUT_RD)
or die ref($self), " shutdown read failed: $!";
}
sub shutout {
my $self = shift;
IO::Handle::flush(\*STDOUT)
or die ref($self), " flush stdout failed: $!";
shutdown(\*STDOUT, SHUT_WR)
or die ref($self), " shutdown write failed: $!";
}
########################################################################
# Server funcs
########################################################################
sub read_stream {
my $self = shift;
my $max = $self->{max};
my $ctx = Digest::MD5->new();
my $len = 0;
while (<STDIN>) {
$len += length($_);
$ctx->add($_);
print STDERR ".";
if ($max && $len >= $max) {
print STDERR "\nMax";
last;
}
}
print STDERR "\n";
print STDERR "LEN: $len\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub read_oob {
my $self = shift;
my $size = $self->{size} || 4091;
my $ctx = Digest::MD5->new();
my $len = 0;
while (1) {
my $rin = my $ein = '';
vec($rin, fileno(STDIN), 1) = 1;
vec($ein, fileno(STDIN), 1) = 1 unless $self->{oobinline};
select($rin, undef, $ein, undef)
or die ref($self), " select failed: $!";
my $buf;
my $atmark = sockatmark(\*STDIN)
or die ref($self), " sockatmark failed: $!";
if ($atmark == 1) {
if ($self->{oobinline}) {
defined(recv(STDIN, $buf, 1, 0))
or die ref($self), " recv OOB failed: $!";
print STDERR "[$buf]";
$ctx->add("[$buf]");
$len += length($buf);
} else {
defined(recv(STDIN, $buf, 1, MSG_OOB)) ||
$!{EINVAL}
or die ref($self), " recv OOB failed: $!";
print STDERR "OOB: $buf\n" if length($buf);
}
}
defined(recv(STDIN, $buf, $size, 0))
or die ref($self), " recv failed: $!";
last unless length($buf);
print STDERR $buf;
$ctx->add($buf);
$len += length($buf);
print STDERR ".";
}
print STDERR "\n";
print STDERR "LEN: $len\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub read_datagram {
my $self = shift;
my $num = $self->{num} // 1;
my $max = $self->{max};
my $size = $self->{size} || 2**16;
my $ctx = Digest::MD5->new();
my $len = 0;
my @lengths;
for (my $i = 0; $i < $num; $i++) {
defined(my $read = sysread(STDIN, my $buf, $size))
or die ref($self), " sysread number $i failed: $!";
$len += $read;
push @lengths, $read;
$ctx->add($buf);
print STDERR ".";
if ($max && $len >= $max) {
print STDERR "\nMax";
last;
}
}
print STDERR "\n";
print STDERR "LEN: $len\n";
print STDERR "LENGTHS: @lengths\n";
print STDERR "MD5: ", $ctx->hexdigest, "\n";
}
sub solingerin {
my $self = shift;
setsockopt(STDIN, SOL_SOCKET, SO_LINGER, pack('ii', 1, 0))
or die ref($self), " set linger in failed: $!";
}
########################################################################
# Script funcs
########################################################################
sub check_logs {
my ($c, $r, $s, %args) = @_;
return if $args{nocheck};
$r->loggrep(qr/^Timeout$/) or die "no relay timeout"
if $r && $args{relay}{timeout};
$r->loggrep(qr/^Max$/) or die "no relay max"
if $r && $args{relay}{max} && $args{len};
check_len($c, $r, $s, %args);
check_lengths($c, $r, $s, %args);
check_md5($c, $r, $s, %args);
check_error($c, $r, $s, %args);
}
sub check_len {
my ($c, $r, $s, %args) = @_;
my $clen = $c->loggrep(qr/^LEN: /) // die "no client len"
unless $args{client}{nocheck};
my $rlen = $r->loggrep(qr/^LEN: /) // die "no relay len"
if $r && ! $args{relay}{nocheck};
my $slen = $s->loggrep(qr/^LEN: /) // die "no server len"
unless $args{server}{nocheck};
!$clen || !$rlen || $clen eq $rlen
or die "client: $clen", "relay: $rlen", "len mismatch";
!$rlen || !$slen || $rlen eq $slen
or die "relay: $rlen", "server: $slen", "len mismatch";
!$clen || !$slen || $clen eq $slen
or die "client: $clen", "server: $slen", "len mismatch";
!defined($args{len}) || !$clen || $clen eq "LEN: $args{len}\n"
or die "client: $clen", "len $args{len} expected";
!defined($args{len}) || !$rlen || $rlen eq "LEN: $args{len}\n"
or die "relay: $rlen", "len $args{len} expected";
!defined($args{len}) || !$slen || $slen eq "LEN: $args{len}\n"
or die "server: $slen", "len $args{len} expected";
}
sub check_lengths {
my ($c, $r, $s, %args) = @_;
my $clengths = $c->loggrep(qr/^LENGTHS: /)
unless $args{client}{nocheck};
my $slengths = $s->loggrep(qr/^LENGTHS: /)
unless $args{server}{nocheck};
!$clengths || !$slengths || $clengths eq $slengths
or die "client: $clengths", "server: $slengths", "lengths mismatch";
!defined($args{lengths}) || !$clengths ||
$clengths eq "LENGTHS: $args{lengths}\n"
or die "client: $clengths", "lengths $args{lengths} expected";
!defined($args{lengths}) || !$slengths ||
$slengths eq "LENGTHS: $args{lengths}\n"
or die "server: $slengths", "lengths $args{lengths} expected";
}
sub check_md5 {
my ($c, $r, $s, %args) = @_;
my $cmd5 = $c->loggrep(qr/^MD5: /) unless $args{client}{nocheck};
my $smd5 = $s->loggrep(qr/^MD5: /) unless $args{server}{nocheck};
!$cmd5 || !$smd5 || ref($args{md5}) eq 'ARRAY' || $cmd5 eq $smd5
or die "client: $cmd5", "server: $smd5", "md5 mismatch";
my $md5 = ref($args{md5}) eq 'ARRAY' ?
join('|', @{$args{md5}}) : $args{md5};
!$md5 || !$cmd5 || $cmd5 =~ /^MD5: ($md5)$/
or die "client: $cmd5", "md5 $md5 expected";
!$md5 || !$smd5 || $smd5 =~ /^MD5: ($md5)$/
or die "server: $smd5", "md5 $md5 expected";
}
sub check_error {
my ($c, $r, $s, %args) = @_;
$args{relay}{errorin} //= 0 unless $args{relay}{nocheck};
$args{relay}{errorout} //= 0 unless $args{relay}{nocheck};
my %name2proc = (client => $c, relay => $r, server => $s);
foreach my $name (qw(client relay server)) {
my $p = $name2proc{$name}
or next;
$args{$name}{errorin} //= $args{$name}{error};
if (defined($args{$name}{errorin})) {
my $ein = $p->loggrep(qr/^ERROR IN: /);
defined($ein) &&
$ein eq "ERROR IN: $args{$name}{errorin}\n"
or die "$name: $ein",
"error in $args{$name}{errorin} expected";
}
if (defined($args{$name}{errorout})) {
my $eout = $p->loggrep(qr/^ERROR OUT: /);
defined($eout) &&
$eout eq "ERROR OUT: $args{$name}{errorout}\n"
or die "$name: $eout",
"error out $args{$name}{errorout} expected";
}
}
}
1;
|