xref: /openbsd/regress/usr.sbin/relayd/relayd.pl (revision d415bd75)
1#!/usr/bin/perl
2#	$OpenBSD: relayd.pl,v 1.15 2016/08/25 22:56:13 bluhm Exp $
3
4# Copyright (c) 2010-2014 Alexander Bluhm <bluhm@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18use strict;
19use warnings;
20use Socket;
21use Socket6;
22
23use Client;
24use Relayd;
25use Server;
26require 'funcs.pl';
27
28sub usage {
29	die "usage: relay.pl copy|splice [test-args.pl]\n";
30}
31
32my $testfile;
33our %args;
34if (@ARGV and -f $ARGV[-1]) {
35	$testfile = pop;
36	do $testfile
37	    or die "Do test file $testfile failed: ", $@ || $!;
38}
39@ARGV == 1 or usage();
40
41my $redo = $args{lengths} && @{$args{lengths}};
42$redo = 0 if $args{client}{http_vers};  # run only one persistent connection
43my($sport, $rport) = find_ports(num => 2);
44my($s, $r, $c);
45$s = Server->new(
46    forward             => $ARGV[0],
47    func                => \&read_char,
48    listendomain        => AF_INET,
49    listenaddr          => "127.0.0.1",
50    listenport          => $sport,
51    redo                => $redo,
52    %{$args{server}},
53    testfile            => $testfile,
54    client              => \$c,
55) unless $args{server}{noserver};
56$r = Relayd->new(
57    forward             => $ARGV[0],
58    listendomain        => AF_INET,
59    listenaddr          => "127.0.0.1",
60    listenport          => $rport,
61    connectdomain       => AF_INET,
62    connectaddr         => "127.0.0.1",
63    connectport         => $sport,
64    %{$args{relayd}},
65    testfile            => $testfile,
66);
67$c = Client->new(
68    forward             => $ARGV[0],
69    func                => \&write_char,
70    connectdomain       => AF_INET,
71    connectaddr         => "127.0.0.1",
72    connectport         => $rport,
73    %{$args{client}},
74    testfile            => $testfile,
75    server              => \$s,
76) unless $args{client}{noclient};
77
78$s->run unless $args{server}{noserver};
79$r->run;
80$r->up;
81$c->run->up unless $args{client}{noclient};
82$s->up unless $args{server}{noserver};
83
84$c->down unless $args{client}{noclient};
85$s->down unless $args{server}{noserver};
86$r->kill_child;
87$r->down;
88
89check_logs($c, $r, $s, %args);
90