1#!/usr/bin/perl 2# $OpenBSD: echo.pl,v 1.1.1.1 2013/01/03 17:36:39 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 Child; 24use Client; 25use Server; 26require 'funcs.pl'; 27 28sub usage { 29 die "usage: echo.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 41exit 0 if $args{noecho} || $args{client}{alarm} || $args{server}{alarm}; 42 43my $r = Server->new( 44 forward => $ARGV[0], 45 func => \&relay, 46 logfile => "relay.log", 47 listendomain => AF_INET, 48 listenaddr => "127.0.0.1", 49 %{$args{relay}}, 50); 51my $s = Child->new( 52 logfile => "server.log", 53 oobinline => 1, 54 %{$args{server}}, 55 func => sub { 56 ($args{server}{func} || \&read_stream)->(@_); 57 eval { shutout(@_) }; 58 }, 59); 60my $c = Client->new( 61 connectdomain => AF_INET, 62 connectaddr => "127.0.0.1", 63 connectport => $r->{listenport}, 64 oobinline => 1, 65 %{$args{client}}, 66 func => sub { 67 $s->run->up; 68 eval { ($args{client}{func} || \&write_stream)->(@_) }; 69 warn $@ if $@; 70 eval { shutout(@_) }; 71 $s->down; 72 }, 73); 74 75$r->run; 76$c->run->up; 77$r->up; 78 79$c->down; 80$r->down; 81$s->{pid} = -1; # XXX hack 82$s->down; 83 84exit if $args{nocheck} || $args{client}{nocheck}; 85 86check_logs($c, $r, $s, %args); 87