X-Git-Url: https://git.distorted.org.uk/~mdw/userv-utils/blobdiff_plain/198680ad8931fb4310146d92304ca24c87c6b8dd..fe112c0873d68ce9c23f9218593b0e23eab89e06:/ipif/udptunnel diff --git a/ipif/udptunnel b/ipif/udptunnel index a40dce0..1b5031c 100755 --- a/ipif/udptunnel +++ b/ipif/udptunnel @@ -3,6 +3,7 @@ # # usage: # udptunnel +# [ -l[] ... . ] # , # , # ,,, @@ -24,6 +25,8 @@ # udptunnel will userv ipif locally, as # userv root ipif ,,, # +# or, if -lc was given, userv root ipif is replaced with the argument(s) to +# successive -lc options. use Socket; use POSIX; @@ -68,6 +71,19 @@ sub show_addr_port ($) { return inet_ntoa($s[1]).','.$s[0]; } +@lcmd= (); + +while ($ARGV[0] =~ m/^-/) { + $_= shift @ARGV; + last if $_ eq '--'; + if (s/^-l//) { + push @lcmd,$_ if length; + while (@ARGV && ($_= shift @ARGV) ne '-') { push @lcmd, $_; } + } else { + quit("unknown option \`$_'"); + } +} + ($las,$lps)= eat_addr_port('print|silent'); $la= conv_host_addr($las); $lp= conv_port_number($lps); @@ -134,7 +150,11 @@ $rsp= show_addr_port($rs); if ($lps eq 'print') { print($lsp,"\n") or quit("write port to stdout: $!"); } +@lcmd= qw(userv root ipif) unless @lcmd; + debug("using remote $rsp local $lsp"); +push @lcmd, ("$lva,$rva,$mtu,$proto",$lepn); +debug("local command @lcmd"); pipe(UR,UW) or fail("up pipe"); pipe(DR,DW) or fail("down pipe"); @@ -144,8 +164,8 @@ if (!$c3) { close UR; close DW; open(STDIN,"<&DR") or fail("reopen stdin for packets"); open(STDOUT,">&UW") or fail("reopen stdout for packets"); - exec "userv","root","ipif","$lva,$rva,$mtu,$proto",$lepn; - quit("cannot execute userv ipif: $!"); + exec @lcmd; + quit("cannot execute $lcmd[0]: $!"); } close UW; close DR; @@ -195,7 +215,7 @@ for (;;) { quit "tunnel endpoint closed by system"; } while (($p= index($upbuf,"\300")) >= 0) { - if (!defined(send L,substr($upbuf,0,$p+1),0,$rs)) { + if ($p && !defined(send L,substr($upbuf,0,$p),0,$rs)) { warning("transmit error: $!"); } else { if (!$upyet) { @@ -213,6 +233,7 @@ for (;;) { warning("got packet from incorrect peer $rsp_from"); next; } + $downbuf= "\300".$downbuf."\300"; if (!defined($r= syswrite(DW,$downbuf,length $downbuf))) { warning("tunnel endpoint write error: $!"); } elsif ($r != length $downbuf) { @@ -225,5 +246,6 @@ for (;;) { alarm($timeout) if $timeout; } } + if ($! == ECONNREFUSED) { quit("tunnel closed at remote end"); } $! == EAGAIN || warning("receive error: $!"); }