Rationalised.
[userv-utils] / ipif / udptunnel
CommitLineData
46ab1c83 1#!/usr/bin/perl
2# Simple tunnel for userv-ipif tunnels.
3#
4# usage:
5# udptunnel
abe75cda 6# [ -l[<local-command/arg>] ... . ]
46ab1c83 7# <public-local-host/addr>,<public-local-port>
8# <public-remote-host/addr>,<public-remote-port>
9# <private-local-addr>,<private-remote-addr>,<mtu>,<proto>
10# <keepalive>,<timeout>
11# <extra-local-nets> <extra-remote-nets>
12# [ <remote-command> [<remote-args> ...] ]
13#
14# <local-public-port> may be number or `print' or `silent'
15#
16# <remote-public-port> may number or `command', in which case
17# <remote-command> must be specified and should run udptunnel at the
18# remote end; it will be invoked as
19# <remote-command> <public-remote-host/addr>,print
20# <public-local-addr>,<public-local-port>
21# <private-remote-addr>,<private-local-addr>,<mtu>,<proto>
217afbe6 22# <keepalive>,<timeout>
23# <extra-remote-nets> <extra-local-nets>
46ab1c83 24#
25# udptunnel will userv ipif locally, as
217afbe6 26# userv root ipif <private-local-addr>,<private-remote-addr>,<mtu>,<proto>
27# <extra-local-nets>
abe75cda 28# or, if -lc was given, userv root ipif is replaced with the argument(s) to
29# successive -lc options.
46ab1c83 30
31use Socket;
32use POSIX;
33use Fcntl;
34
35$progname= $0; $progname =~ s,.*/,,;
36$|=1;
37
38chomp($hostname= `uname -n`);
39$? and die "$progname: cannot get hostname (uname failed with code $?)\n";
40
41sub quit ($) { die "$progname - $hostname: fatal error: $_[0]\n"; }
42sub debug ($) { print "$progname - $hostname: debug: $_[0]\n"; }
43sub fail ($) { quit("unexpected system call failure: $_[0]: $!\n"); }
44sub warning ($) { warn "$progname - $hostname: $_[0]\n"; }
45
46sub eat_addr_port ($) {
47 my ($x) = @_;
48 @ARGV or quit("<host/addr>,<port> missing");
49 $_= shift(@ARGV);
50 $_ =~ m/^([^,]+)\,(\d+|$x)$/ or quit("$_: <host/addr>,<port> bad syntax");
51 return ($1,$2);
52}
53sub conv_host_addr ($) {
54 my ($s,$r) = @_;
55 defined($r= inet_aton($s)) or quit("$s: cannot convert to address");
56 return $r;
57}
217afbe6 58sub conv_port_number ($) {
46ab1c83 59 my ($s,$r) = @_;
60 if ($s =~ m/\d/) {
61 $r= $s+0;
62 $r>0 && $r<65536 or quit("$s: port out of range");
63 } else {
64 $r= 0;
65 }
66 return $r;
67}
68sub show_addr_port ($) {
69 my ($s,@s) = @_;
70 @s= unpack_sockaddr_in($s);
71 return inet_ntoa($s[1]).','.$s[0];
72}
73
abe75cda 74@lcmd= ();
75
76while ($ARGV[0] =~ m/^-/) {
77 $_= shift @ARGV;
78 last if $_ eq '--';
79 if (s/^-l//) {
80 push @lcmd,$_ if length;
81 while (@ARGV && ($_= shift @ARGV) ne '-') { push @lcmd, $_; }
82 } else {
83 quit("unknown option \`$_'");
84 }
85}
86
46ab1c83 87($las,$lps)= eat_addr_port('print|silent');
88$la= conv_host_addr($las);
89$lp= conv_port_number($lps);
90$ls= pack_sockaddr_in $lp,$la;
91
92($ras,$rps)= eat_addr_port('command');
93$rp= conv_port_number($rps);
94$ra= $rps eq 'command' ? '' : conv_host_addr($ras);
95
96$_= shift @ARGV;
97m/^([.0-9]+),([.0-9]+),(\d+),(slip|cslip)$/
98 or quit("lvaddr,rvaddr,mtu,proto missing or bad syntax or proto not [c]slip");
217afbe6 99($lva,$rva,$mtu,$proto) = ($1,$2,$3,$4);
46ab1c83 100
101$_= shift @ARGV;
217afbe6 102m/^(\d+),(\d+)$/ or quit("keepalive,timeout missing or bad syntax");
103($keepalive,$timeout)= ($1,$2);
46ab1c83 104$keepalive && ($timeout > $keepalive*2) or quit("timeout must be < 2*keepalive")
105 if $timeout;
106
107$lepn= shift @ARGV;
108$repn= shift @ARGV;
109
110alarm($timeout);
111
112defined($udp= getprotobyname('udp')) or fail("getprotobyname udp");
113
114socket(L,PF_INET,SOCK_DGRAM,$udp) or fail("socket");
115bind(L,$ls) or quit("bind failed: $!");
116defined($ls= getsockname(L)) or fail("getsockname");
117$lsp= show_addr_port($ls);
118
119if ($rps eq 'command') {
46ab1c83 120 quit("when using ,command for remote, must supply command") unless @ARGV;
217afbe6 121 @rcmd= (@ARGV, "$ras,print", "$lsp", "$rva,$lva,$mtu,$proto",
122 "$keepalive,$timeout", $repn, $lepn);
123 debug("remote command @rcmd");
46ab1c83 124 defined($c= open C,"-|") or fail("fork for remote");
125 if (!$c) {
217afbe6 126 exec @rcmd; die "$progname: error: failed to execute $rcmd[0]: $!\n";
46ab1c83 127 }
128 $_= <C>;
129 if (!length) {
130 close C;
131 quit($? ? "remote command failed (code $?)" : "no details received from remote");
132 }
133 chomp;
134 m/^([.0-9]+)\,(\d+)$/ or quit("invalid details from remote end ($_)");
135 ($ras,$rps) = ($1,$2);
136 $ra= conv_host_addr($ras);
217afbe6 137 $rp= conv_port_number($rps);
46ab1c83 138 defined($c2= fork) or fail("fork for cat");
139 if (!$c2) {
217afbe6 140 open(STDIN,"<&C") or fail("redirect remote pipe to stdin");
46ab1c83 141 close C;
142 exec "cat"; fail("execute cat");
143 }
144} else {
145 quit("when not using ,command for remote, must not supply command") if @ARGV;
146}
147
148$rs= pack_sockaddr_in $rp,$ra;
149$rsp= show_addr_port($rs);
150
217afbe6 151if ($lps eq 'print') { print($lsp,"\n") or quit("write port to stdout: $!"); }
46ab1c83 152
abe75cda 153@lcmd= qw(userv root ipif) unless @lcmd;
154
217afbe6 155debug("using remote $rsp local $lsp");
abe75cda 156push @lcmd, ("$lva,$rva,$mtu,$proto",$lepn);
157debug("local command @lcmd");
46ab1c83 158
159pipe(UR,UW) or fail("up pipe");
160pipe(DR,DW) or fail("down pipe");
161
162defined($c3= fork) or fail("fork for ipif");
163if (!$c3) {
164 close UR; close DW;
217afbe6 165 open(STDIN,"<&DR") or fail("reopen stdin for packets");
166 open(STDOUT,">&UW") or fail("reopen stdout for packets");
abe75cda 167 exec @lcmd;
168 quit("cannot execute $lcmd[0]: $!");
46ab1c83 169}
170close UW;
171close DR;
172
173$upyet= 0;
174$downyet= 0;
175
176$wantreadfds='';
217afbe6 177vec($wantreadfds,fileno(UR),1)= 1;
178vec($wantreadfds,fileno(L),1)= 1;
46ab1c83 179
180sub nonblock ($) {
181 my ($fh,$fl) = @_;
182 ($fl= fcntl($fh,F_GETFL,0)) or fail("nonblock F_GETFL");
183 $fl |= O_NONBLOCK;
184 fcntl($fh, F_SETFL, $fl) or fail("nonblock F_SETFL");
185}
186
187nonblock('UR');
188nonblock('L');
189
190$upbuf= '';
191
217afbe6 192sub now () { my ($v); defined($v= time) or fail("get time"); return $v; }
193if ($keepalive) { $nextsendka= now(); }
194
46ab1c83 195for (;;) {
217afbe6 196 if ($keepalive) {
197 $now= now();
198680ad 198 $thistimeout= $nextsendka-$now;
199 if ($thistimeout < 0) {
217afbe6 200 defined(send L,"\300",0,$rs)
201 or warning("transmit keepalive error: $!");
202 $nextsendka= $now+$keepalive;
198680ad 203 $thistimeout= $keepalive;
217afbe6 204 }
205 } else {
198680ad 206 $thistimeout= undef;
217afbe6 207 }
198680ad 208 select($readfds=$wantreadfds,'','',$thistimeout);
46ab1c83 209 for (;;) {
210 if (!defined($r= sysread(UR,$upbuf,$mtu*2+3,length($upbuf)))) {
211 $! == EAGAIN || warning("tunnel endpoint read error: $!");
212 last;
213 }
214 if (!$r) {
215 quit "tunnel endpoint closed by system";
216 }
217 while (($p= index($upbuf,"\300")) >= 0) {
1d1209a4 218 if ($p && !defined(send L,substr($upbuf,0,$p),0,$rs)) {
217afbe6 219 warning("transmit error: $!");
198680ad 220 } else {
221 if (!$upyet) {
222 $upyet= 1;
223 debug($downyet ? "tunnel open at this end" : "transmitting");
224 }
225 if ($keepalive) { $nextsendka= now()+$keepalive; }
46ab1c83 226 }
198680ad 227 $upbuf= substr($upbuf,$p+1);
46ab1c83 228 }
229 }
230 while (defined($rs_from= recv L,$downbuf,$mtu*2+3,0)) {
231 $rsp_from= show_addr_port($rs_from);
232 if ($rsp_from ne $rsp) {
233 warning("got packet from incorrect peer $rsp_from");
217afbe6 234 next;
46ab1c83 235 }
1d1209a4 236 $downbuf= "\300".$downbuf."\300";
46ab1c83 237 if (!defined($r= syswrite(DW,$downbuf,length $downbuf))) {
238 warning("tunnel endpoint write error: $!");
217afbe6 239 } elsif ($r != length $downbuf) {
46ab1c83 240 warning("tunnel endpoint wrong write length");
241 } else {
217afbe6 242 if (!$downyet) {
243 $downyet= 1;
198680ad 244 debug($upyet ? "tunnel open at this end" : "receiving");
217afbe6 245 }
246 alarm($timeout) if $timeout;
46ab1c83 247 }
248 }
40f75550 249 if ($! == ECONNREFUSED) { quit("tunnel closed at remote end"); }
46ab1c83 250 $! == EAGAIN || warning("receive error: $!");
251}