2 # This class is the internal subclass that deals with AGW Engine connections
4 # The complication here is that there only one 'real' (and from the node's point
5 # of view, invisible) IP connection. This connection then has multiplexed
6 # connections passed down it, a la BPQ native host ports (but not as nicely).
8 # It is a shame that the author has chosen an inherently dangerous binary format
9 # which is non-framed and has the potential for getting out of sync and not
10 # being able to recover. Relying on length fields is recipe for disaster (esp.
11 # for him!). DoS attacks are a wonderful thing....
13 # Also making the user handle the distinction between a level 2 and 4 connection
14 # and especially Digis, in the way that he has, is a bit of a cop out! If I can
15 # be arsed to do anything other than straight ax25 connects then it will only
16 # because I have the 'power of perl' available that avoids me getting
17 # terminally bored sorting out other people's sloppyness.
21 # Copyright (c) 2001 - Dirk Koopman G1TLH
32 use vars qw(@ISA $sock @outqueue $send_offset $inmsg $rproc $noports $lastytime
33 $lasthtime $ypolltime $hpolltime %circuit $total_in $total_out
34 $lastconnect $connectinterval);
36 @ISA = qw(Msg ExtMsg);
43 $lastytime = $lasthtime = time;
44 $ypolltime = 10 unless defined $ypolltime;
45 $hpolltime = 300 unless defined $hpolltime;
47 $total_in = $total_out = 0;
49 $connectinterval = 30;
53 return unless $enable;
58 dbg("AGW initialising and connecting to $addr/$port ...");
59 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout=>15);
60 $lastconnect = $main::systime;
62 dbg("Cannot connect to AGW Engine at $addr/$port $!");
65 Msg::blocking($sock, 0);
66 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
68 # send a P frame for the login if required
70 my $data = pack "a255 a255", $login, $passwd;
71 _sendf('P', undef, undef, undef, undef, $data);
75 # R frame for the release number
76 # G frame to ask for ports
77 # X frame to say who we are
78 # optional m frame to enable monitoring
81 _sendf('X', $main::mycall);
82 _sendf('m') if $monitor;
93 for (values %circuit) {
94 &{$_->{eproc}}() if $_->{eproc};
98 _sendf('m') if $monitor;
99 _sendf('x', $main::mycall);
101 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
103 $lastconnect = $main::systime;
109 goto &main::login; # save some writing, this was the default
119 my $sort = shift || confess "need a valid AGW command letter";
120 my $from = shift || '';
121 my $to = shift || '';
122 my $port = shift || 0;
123 my $pid = shift || 0;
124 my $data = shift || '';
128 if ($sort eq 'y' || $sort eq 'H') {
129 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agwpoll');
130 } elsif ($sort eq 'D') {
134 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"") if isdbg('agw');
137 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agw');
139 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
140 Msg::set_event_handler($sock, write=>\&_send);
147 # If $flush is set, set the socket to blocking, and send all
148 # messages in the queue - return only if there's an error
149 # If $flush is 0 (deferred mode) make the socket non-blocking, and
150 # return to the event loop only after every message, or if it
151 # is likely to block in the middle of a message.
153 my $offset = $send_offset;
156 my $msg = $outqueue[0];
157 my $mlth = length($msg);
158 my $bytes_to_write = $mlth - $offset;
159 my $bytes_written = 0;
160 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
161 while ($bytes_to_write > 0) {
162 $bytes_written = syswrite ($sock, $msg,
163 $bytes_to_write, $offset);
164 if (!defined($bytes_written)) {
165 if (Msg::_err_will_block($!)) {
166 # Should happen only in deferred mode. Record how
167 # much we have already sent.
168 $send_offset = $offset;
169 # Event handler should already be set, so we will
170 # be called back eventually, and will resume sending
174 return 0; # fail. Message remains in queue ..
178 dbgdump('raw', "AGW send $bytes_written: ", $msg);
180 $total_out += $bytes_written;
181 $offset += $bytes_written;
182 $bytes_to_write -= $bytes_written;
184 $send_offset = $offset = 0;
186 last; # Go back to select and wait
187 # for it to fire again.
190 # Call me back if queue has not been drained.
192 Msg::set_event_handler ($sock, write => \&_send);
194 Msg::set_event_handler ($sock, write => undef);
199 sub _rcv { # Complement to _send
201 my ($msg, $offset, $bytes_read);
203 $bytes_read = sysread ($sock, $msg, 1024, 0);
204 if (defined ($bytes_read)) {
205 if ($bytes_read > 0) {
206 $total_in += $bytes_read;
209 dbgdump('raw', "AGW read $bytes_read: ", $msg);
213 if (Msg::_err_will_block($!)) {
221 if (defined $bytes_read && $bytes_read == 0) {
224 _decode() if length $inmsg >= 36;
230 dbg("error on AGW connection $addr/$port $!");
231 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
234 &{$_->{eproc}}() if $_->{eproc};
237 $lastconnect = $main::systime;
244 # we have at least 36 bytes of data (ugh!)
245 while (length $inmsg >= 36) {
246 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
249 # do a sanity check on the length
251 dbg("AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
256 if (length $inmsg > 36) {
257 $inmsg = substr($inmsg, 36);
261 } elsif (length $inmsg > $len + 36) {
262 $data = substr($inmsg, 36, $len);
263 $inmsg = substr($inmsg, $len + 36);
264 } elsif (length $inmsg == $len + 36) {
265 $data = substr($inmsg, 36);
269 # we don't have enough data or something
270 # or we have screwed up
275 $data = '' unless defined $data;
277 my $d = unpack "Z*", $data;
280 dbg("AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
281 my $conn = _find($from eq $main::mycall ? $to : $from);
283 if ($conn->{state} eq 'WC') {
284 if (exists $conn->{cmd}) {
285 if (@{$conn->{cmd}}) {
286 dbg($d) if isdbg('connect');
290 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
291 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
294 my @lines = split /\cM\cJ?/, $d;
295 push @lines, $d unless @lines;
297 &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
301 dbg("AGW error Unsolicited Data!");
303 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
304 my $d = unpack "Z*", $data;
307 my @lines = split /\cM\cJ?/, $d;
310 # s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
311 dbg("AGW Monitor port: $port \"$_\"") if isdbg('agw');
313 } elsif ($sort eq 'C') {
314 my $d = unpack "Z*", $data;
316 dbg("AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"") if isdbg('agw');
317 my $call = $from eq $main::mycall ? $to : $from;
318 my $conn = _find($call);
320 if ($conn->{state} eq 'WC') {
321 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
323 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
324 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
329 $conn = AGWMsg->new($rproc);
330 $conn->{agwpid} = $pid;
331 $conn->{agwport} = $port;
332 $conn->{lineend} = "\cM";
333 $conn->{incoming} = 1;
334 $conn->{agwcall} = $call;
335 $circuit{$call} = $conn;
336 if (my ($c, $s) = $call =~ /^(\w+)-(\d\d?)$/) {
337 $s = 15 - $s if $s > 8;
338 $call = $s > 0 ? "${c}-${s}" : $c;
340 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
342 } elsif ($sort eq 'd') {
343 my $d = unpack "Z*", $data;
345 dbg("AGW '$from'->'$to' port: $port Disconnected ($d)") if isdbg('agw');
346 my $conn = _find($from eq $main::mycall ? $to : $from);
348 &{$conn->{eproc}}() if $conn->{eproc};
349 $conn->in_disconnect;
351 } elsif ($sort eq 'y') {
352 my ($frames) = unpack "V", $data;
353 dbg("AGW Frames Outstanding on port $port = $frames") if isdbg('agwpollans');
354 my $conn = _find($from);
355 $conn->{oframes} = $frames if $conn;
356 } elsif ($sort eq 'Y') {
357 my ($frames) = unpack "V", $data;
358 dbg("AGW Frames Outstanding on circuit '$from'->'$to' = $frames") if isdbg('agw');
359 my $conn = _find($from eq $main::mycall ? $to : $from);
360 $conn->{oframes} = $frames if $conn;
361 } elsif ($sort eq 'H') {
362 unless ($from =~ /^\s+$/) {
363 my $d = unpack "Z*", $data;
365 dbg("AGW Heard port: $port \"$d\"") if isdbg('agw');
367 } elsif ($sort eq 'X') {
368 my ($r) = unpack "C", $data;
369 $r = $r ? "Successful" : "Failed";
370 dbg("AGW Register $from $r");
372 } elsif ($sort eq 'R') {
373 my ($major, $minor) = unpack "v x2 v x2", $data;
374 dbg("AGW Version $major.$minor") if isdbg('agw');
375 } elsif ($sort eq 'G') {
376 my @ports = split /;/, $data;
377 $noports = shift @ports || '0';
378 dbg("AGW $noports Ports available") if isdbg('agw');
379 pop @ports while @ports > $noports;
382 dbg("AGW Port: $_") if isdbg('agw');
384 for (my $i = 0; $i < $noports; $i++) {
385 _sendf('y', undef, undef, $i);
386 _sendf('g', undef, undef, $i);
389 my $d = unpack "Z*", $data;
390 dbg("AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
398 return $circuit{$call};
403 my ($conn, $line) = @_;
405 my ($port, $call) = split /\s+/, $line;
406 $conn->{agwpid} = ord "\xF0";
407 $conn->{agwport} = $port - 1;
408 $conn->{lineend} = "\cM";
409 $conn->{incoming} = 0;
410 $conn->{csort} = 'ax25';
411 $conn->{agwcall} = uc $call;
412 $circuit{$conn->{agwcall}} = $conn;
414 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
415 $conn->{state} = 'WC';
423 delete $circuit{$conn->{agwcall}};
424 $conn->SUPER::disconnect;
430 delete $circuit{$conn->{agwcall}};
431 if ($conn->{incoming}) {
432 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
434 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
436 $conn->SUPER::disconnect;
441 my ($conn, $msg) = @_;
443 $msg =~ s/^[-\w]+\|//;
444 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
445 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
446 my $len = length($msg) + 1;
447 dbg("AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"") if isdbg('agw');
453 # try to reconnect to AGW if we could not previously or there was an error
454 if ($enable && !$sock && ($lastconnect + $connectinterval) >= $main::systime) {
459 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
460 for (my $i = 0; $i < $noports; $i++) {
461 _sendf('y', undef, undef, $i );
463 $lastytime = $main::systime;
465 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
466 for (my $i = 0; $i < $noports; $i++) {
467 _sendf('H', undef, undef, $i );
469 $lasthtime = $main::systime;