relay.pl revision 1.2
1#!/usr/bin/perl 2# $OpenBSD: relay.pl,v 1.2 2013/01/05 13:53:42 bluhm Exp $ 3 4# Copyright (c) 2010-2013 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 Relay; 25use Server; 26require 'funcs.pl'; 27 28sub usage { 29 die "usage: relay.pl copy|splice [test-args.pl]\n"; 30} 31 32my $test; 33our %args; 34if (@ARGV and -f $ARGV[-1]) { 35 $test = pop; 36 do $test 37 or die "Do test file $test failed: ", $@ || $!; 38} 39@ARGV == 1 or usage(); 40 41my $s = Server->new( 42 idle => 4, 43 func => \&read_datagram, 44 listendomain => AF_INET, 45 listenaddr => "127.0.0.1", 46 %{$args{server}}, 47 protocol => "udp", 48); 49my $r = Relay->new( 50 forward => $ARGV[0], 51 idle => 3, 52 func => \&relay, 53 listendomain => AF_INET, 54 listenaddr => "127.0.0.1", 55 connectdomain => AF_INET, 56 connectaddr => "127.0.0.1", 57 connectport => $s->{listenport}, 58 %{$args{relay}}, 59 protocol => "udp", 60); 61my $c = Client->new( 62 func => \&write_datagram, 63 connectdomain => AF_INET, 64 connectaddr => "127.0.0.1", 65 connectport => $r->{listenport}, 66 %{$args{client}}, 67 protocol => "udp", 68); 69 70$s->run; 71$r->run; 72$c->run->up; 73$r->up; 74$s->up; 75 76$c->down; 77$r->down; 78$s->down; 79 80check_logs($c, $r, $s, %args); 81