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 = 60;
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=>3);
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;
111 goto &main::login; # save some writing, this was the default
121 my $sort = shift || confess "need a valid AGW command letter";
122 my $from = shift || '';
123 my $to = shift || '';
124 my $port = shift || 0;
125 my $pid = shift || 0;
126 my $data = shift || '';
130 if ($sort eq 'y' || $sort eq 'H') {
131 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agwpoll');
132 } elsif ($sort eq 'D') {
136 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"") if isdbg('agw');
139 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agw');
141 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
142 Msg::set_event_handler($sock, write=>\&_send);
149 # If $flush is set, set the socket to blocking, and send all
150 # messages in the queue - return only if there's an error
151 # If $flush is 0 (deferred mode) make the socket non-blocking, and
152 # return to the event loop only after every message, or if it
153 # is likely to block in the middle of a message.
155 my $offset = $send_offset;
158 my $msg = $outqueue[0];
159 my $mlth = length($msg);
160 my $bytes_to_write = $mlth - $offset;
161 my $bytes_written = 0;
162 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
163 while ($bytes_to_write > 0) {
164 $bytes_written = syswrite ($sock, $msg,
165 $bytes_to_write, $offset);
166 if (!defined($bytes_written)) {
167 if (Msg::_err_will_block($!)) {
168 # Should happen only in deferred mode. Record how
169 # much we have already sent.
170 $send_offset = $offset;
171 # Event handler should already be set, so we will
172 # be called back eventually, and will resume sending
176 return 0; # fail. Message remains in queue ..
180 dbgdump('raw', "AGW send $bytes_written: ", $msg);
182 $total_out += $bytes_written;
183 $offset += $bytes_written;
184 $bytes_to_write -= $bytes_written;
186 $send_offset = $offset = 0;
188 last; # Go back to select and wait
189 # for it to fire again.
192 # Call me back if queue has not been drained.
194 Msg::set_event_handler ($sock, write => \&_send);
196 Msg::set_event_handler ($sock, write => undef);
201 sub _rcv { # Complement to _send
203 my ($msg, $offset, $bytes_read);
205 $bytes_read = sysread ($sock, $msg, 1024, 0);
206 if (defined ($bytes_read)) {
207 if ($bytes_read > 0) {
208 $total_in += $bytes_read;
211 dbgdump('raw', "AGW read $bytes_read: ", $msg);
215 if (Msg::_err_will_block($!)) {
224 if (defined $bytes_read && $bytes_read == 0) {
227 _decode() if length $inmsg >= 36;
233 return if $finishing;
235 dbg("AGW connection error on $addr/$port $!");
236 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
238 &{$_->{eproc}}() if $_->{eproc};
242 $lastconnect = $main::systime;
250 # we have at least 36 bytes of data (ugh!)
251 while (length $inmsg >= 36) {
252 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
255 # do a sanity check on the length
257 dbg("AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
262 if (length $inmsg > 36) {
263 $inmsg = substr($inmsg, 36);
267 } elsif (length $inmsg > $len + 36) {
268 $data = substr($inmsg, 36, $len);
269 $inmsg = substr($inmsg, $len + 36);
270 } elsif (length $inmsg == $len + 36) {
271 $data = substr($inmsg, 36);
275 # we don't have enough data or something
276 # or we have screwed up
281 $data = '' unless defined $data;
283 my $d = unpack "Z*", $data;
286 dbg("AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
287 my $conn = _find($from eq $main::mycall ? $to : $from);
289 if ($conn->{state} eq 'WC') {
290 if (exists $conn->{cmd}) {
291 if (@{$conn->{cmd}}) {
292 dbg($d) if isdbg('connect');
296 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
297 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
300 my @lines = split /\cM\cJ?/, $d;
301 push @lines, $d unless @lines;
303 &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
307 dbg("AGW error Unsolicited Data!");
309 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
310 my $d = unpack "Z*", $data;
313 my @lines = split /\cM\cJ?/, $d;
316 # s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
317 dbg("AGW Monitor port: $port \"$_\"") if isdbg('agw');
319 } elsif ($sort eq 'C') {
320 my $d = unpack "Z*", $data;
322 dbg("AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"") if isdbg('agw');
323 my $call = $from eq $main::mycall ? $to : $from;
324 my $conn = _find($call);
326 if ($conn->{state} eq 'WC') {
327 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
329 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
330 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
335 $conn = AGWMsg->new($rproc);
336 $conn->{agwpid} = $pid;
337 $conn->{agwport} = $port;
338 $conn->{lineend} = "\cM";
339 $conn->{incoming} = 1;
340 $conn->{agwcall} = $call;
341 $circuit{$call} = $conn;
342 if (my ($c, $s) = $call =~ /^(\w+)-(\d\d?)$/) {
343 $s = 15 - $s if $s > 8;
344 $call = $s > 0 ? "${c}-${s}" : $c;
346 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
348 } elsif ($sort eq 'd') {
349 my $d = unpack "Z*", $data;
351 dbg("AGW '$from'->'$to' port: $port Disconnected ($d)") if isdbg('agw');
352 my $conn = _find($from eq $main::mycall ? $to : $from);
354 &{$conn->{eproc}}() if $conn->{eproc};
355 $conn->in_disconnect;
357 } elsif ($sort eq 'y') {
358 my ($frames) = unpack "V", $data;
359 dbg("AGW Frames Outstanding on port $port = $frames") if isdbg('agwpollans');
360 my $conn = _find($from);
361 $conn->{oframes} = $frames if $conn;
362 } elsif ($sort eq 'Y') {
363 my ($frames) = unpack "V", $data;
364 dbg("AGW Frames Outstanding on circuit '$from'->'$to' = $frames") if isdbg('agw');
365 my $conn = _find($from eq $main::mycall ? $to : $from);
366 $conn->{oframes} = $frames if $conn;
367 } elsif ($sort eq 'H') {
368 unless ($from =~ /^\s+$/) {
369 my $d = unpack "Z*", $data;
371 dbg("AGW Heard port: $port \"$d\"") if isdbg('agw');
373 } elsif ($sort eq 'X') {
374 my ($r) = unpack "C", $data;
375 $r = $r ? "Successful" : "Failed";
376 dbg("AGW Register $from $r");
378 } elsif ($sort eq 'R') {
379 my ($major, $minor) = unpack "v x2 v x2", $data;
380 dbg("AGW Version $major.$minor") if isdbg('agw');
381 } elsif ($sort eq 'G') {
382 my @ports = split /;/, $data;
383 $noports = shift @ports || '0';
384 dbg("AGW $noports Ports available") if isdbg('agw');
385 pop @ports while @ports > $noports;
388 dbg("AGW Port: $_") if isdbg('agw');
390 for (my $i = 0; $i < $noports; $i++) {
391 _sendf('y', undef, undef, $i);
392 _sendf('g', undef, undef, $i);
395 my $d = unpack "Z*", $data;
396 dbg("AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
404 return $circuit{$call};
409 my ($conn, $line) = @_;
411 my ($port, $call) = split /\s+/, $line;
412 $conn->{agwpid} = ord "\xF0";
413 $conn->{agwport} = $port - 1;
414 $conn->{lineend} = "\cM";
415 $conn->{incoming} = 0;
416 $conn->{csort} = 'ax25';
417 $conn->{agwcall} = uc $call;
418 $circuit{$conn->{agwcall}} = $conn;
420 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
421 $conn->{state} = 'WC';
429 delete $circuit{$conn->{agwcall}};
430 $conn->SUPER::disconnect;
436 delete $circuit{$conn->{agwcall}};
437 if ($conn->{incoming}) {
438 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
440 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
442 $conn->SUPER::disconnect;
447 my ($conn, $msg) = @_;
449 $msg =~ s/^[-\w]+\|//;
450 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
451 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
452 my $len = length($msg) + 1;
453 dbg("AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"") if isdbg('agw');
459 # try to reconnect to AGW if we could not previously or there was an error
460 if ($enable && !$sock && $main::systime >= $lastconnect + $connectinterval) {
465 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
466 for (my $i = 0; $i < $noports; $i++) {
467 _sendf('y', undef, undef, $i );
469 $lastytime = $main::systime;
471 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
472 for (my $i = 0; $i < $noports; $i++) {
473 _sendf('H', undef, undef, $i );
475 $lasthtime = $main::systime;