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);
35 @ISA = qw(Msg ExtMsg);
42 $lastytime = $lasthtime = time;
43 $ypolltime = 10 unless defined $ypolltime;
44 $hpolltime = 300 unless defined $hpolltime;
46 $total_in = $total_out = 0;
48 use vars qw($VERSION $BRANCH);
49 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
50 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
51 $main::build += $VERSION;
52 $main::branch += $BRANCH;
56 return unless $enable;
60 dbg("AGW initialising and connecting to $addr/$port ...");
61 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout=>15);
63 dbg("Cannot connect to AGW Engine at $addr/$port $!");
66 Msg::blocking($sock, 0);
67 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
69 # send a P frame for the login if required
71 my $data = pack "a255 a255", $login, $passwd;
72 _sendf('P', undef, undef, undef, undef, $data);
76 # R frame for the release number
77 # G frame to ask for ports
78 # X frame to say who we are
79 # optional m frame to enable monitoring
82 _sendf('X', $main::mycall);
83 _sendf('m') if $monitor;
94 for (values %circuit) {
95 &{$_->{eproc}}() if $_->{eproc};
99 _sendf('m') if $monitor;
100 _sendf('x', $main::mycall);
102 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
114 my $sort = shift || confess "need a valid AGW command letter";
115 my $from = shift || '';
116 my $to = shift || '';
117 my $port = shift || 0;
118 my $pid = shift || 0;
119 my $data = shift || '';
123 if ($sort eq 'y' || $sort eq 'H') {
124 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agwpoll');
125 } elsif ($sort eq 'D') {
129 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"") if isdbg('agw');
132 dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agw');
134 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
135 Msg::set_event_handler($sock, write=>\&_send);
142 # If $flush is set, set the socket to blocking, and send all
143 # messages in the queue - return only if there's an error
144 # If $flush is 0 (deferred mode) make the socket non-blocking, and
145 # return to the event loop only after every message, or if it
146 # is likely to block in the middle of a message.
148 my $offset = $send_offset;
151 my $msg = $outqueue[0];
152 my $mlth = length($msg);
153 my $bytes_to_write = $mlth - $offset;
154 my $bytes_written = 0;
155 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
156 while ($bytes_to_write > 0) {
157 $bytes_written = syswrite ($sock, $msg,
158 $bytes_to_write, $offset);
159 if (!defined($bytes_written)) {
160 if (Msg::_err_will_block($!)) {
161 # Should happen only in deferred mode. Record how
162 # much we have already sent.
163 $send_offset = $offset;
164 # Event handler should already be set, so we will
165 # be called back eventually, and will resume sending
169 return 0; # fail. Message remains in queue ..
173 dbgdump('raw', "AGW send $bytes_written: ", $msg);
175 $total_out += $bytes_written;
176 $offset += $bytes_written;
177 $bytes_to_write -= $bytes_written;
179 $send_offset = $offset = 0;
181 last; # Go back to select and wait
182 # for it to fire again.
185 # Call me back if queue has not been drained.
187 Msg::set_event_handler ($sock, write => \&_send);
189 Msg::set_event_handler ($sock, write => undef);
194 sub _rcv { # Complement to _send
196 my ($msg, $offset, $bytes_read);
198 $bytes_read = sysread ($sock, $msg, 1024, 0);
199 if (defined ($bytes_read)) {
200 if ($bytes_read > 0) {
201 $total_in += $bytes_read;
204 dbgdump('raw', "AGW read $bytes_read: ", $msg);
208 if (Msg::_err_will_block($!)) {
216 if (defined $bytes_read && $bytes_read == 0) {
219 _decode() if length $inmsg >= 36;
225 dbg("error on AGW connection $addr/$port $!");
226 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
229 &{$_->{eproc}}() if $_->{eproc};
238 # we have at least 36 bytes of data (ugh!)
239 while (length $inmsg >= 36) {
240 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
243 # do a sanity check on the length
245 dbg("AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
250 if (length $inmsg > 36) {
251 $inmsg = substr($inmsg, 36);
255 } elsif (length $inmsg > $len + 36) {
256 $data = substr($inmsg, 36, $len);
257 $inmsg = substr($inmsg, $len + 36);
258 } elsif (length $inmsg == $len + 36) {
259 $data = substr($inmsg, 36);
263 # we don't have enough data or something
264 # or we have screwed up
269 $data = '' unless defined $data;
271 my $d = unpack "Z*", $data;
274 dbg("AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
275 my $conn = _find($from eq $main::mycall ? $to : $from);
277 if ($conn->{state} eq 'WC') {
278 if (exists $conn->{cmd}) {
279 if (@{$conn->{cmd}}) {
280 dbg($d) if isdbg('connect');
284 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
285 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
288 my @lines = split /\cM\cJ?/, $d;
289 push @lines, $d unless @lines;
291 &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
295 dbg("AGW error Unsolicited Data!");
297 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
298 my $d = unpack "Z*", $data;
301 my @lines = split /\cM\cJ?/, $d;
304 # s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
305 dbg("AGW Monitor port: $port \"$_\"") if isdbg('agw');
307 } elsif ($sort eq 'C') {
308 my $d = unpack "Z*", $data;
310 dbg("AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"") if isdbg('agw');
311 my $call = $from eq $main::mycall ? $to : $from;
312 my $conn = _find($call);
314 if ($conn->{state} eq 'WC') {
315 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
317 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
318 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
323 $conn = AGWMsg->new($rproc);
324 $conn->{agwpid} = $pid;
325 $conn->{agwport} = $port;
326 $conn->{lineend} = "\cM";
327 $conn->{incoming} = 1;
328 $conn->{agwcall} = $call;
329 $circuit{$call} = $conn;
330 if (my ($c, $s) = $call =~ /^(\w+)-(\d\d?)$/) {
331 $s = 15 - $s if $s > 8;
332 $call = $s > 0 ? "${c}-${s}" : $c;
334 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
336 } elsif ($sort eq 'd') {
337 my $d = unpack "Z*", $data;
339 dbg("AGW '$from'->'$to' port: $port Disconnected ($d)") if isdbg('agw');
340 my $conn = _find($from eq $main::mycall ? $to : $from);
342 &{$conn->{eproc}}() if $conn->{eproc};
343 $conn->in_disconnect;
345 } elsif ($sort eq 'y') {
346 my ($frames) = unpack "V", $data;
347 dbg("AGW Frames Outstanding on port $port = $frames") if isdbg('agwpollans');
348 my $conn = _find($from);
349 $conn->{oframes} = $frames if $conn;
350 } elsif ($sort eq 'Y') {
351 my ($frames) = unpack "V", $data;
352 dbg("AGW Frames Outstanding on circuit '$from'->'$to' = $frames") if isdbg('agw');
353 my $conn = _find($from eq $main::mycall ? $to : $from);
354 $conn->{oframes} = $frames if $conn;
355 } elsif ($sort eq 'H') {
356 unless ($from =~ /^\s+$/) {
357 my $d = unpack "Z*", $data;
359 dbg("AGW Heard port: $port \"$d\"") if isdbg('agw');
361 } elsif ($sort eq 'X') {
362 my ($r) = unpack "C", $data;
363 $r = $r ? "Successful" : "Failed";
364 dbg("AGW Register $from $r");
366 } elsif ($sort eq 'R') {
367 my ($major, $minor) = unpack "v x2 v x2", $data;
368 dbg("AGW Version $major.$minor") if isdbg('agw');
369 } elsif ($sort eq 'G') {
370 my @ports = split /;/, $data;
371 $noports = shift @ports || '0';
372 dbg("AGW $noports Ports available") if isdbg('agw');
373 pop @ports while @ports > $noports;
376 dbg("AGW Port: $_") if isdbg('agw');
378 for (my $i = 0; $i < $noports; $i++) {
379 _sendf('y', undef, undef, $i);
380 _sendf('g', undef, undef, $i);
383 my $d = unpack "Z*", $data;
384 dbg("AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
392 return $circuit{$call};
397 my ($conn, $line) = @_;
399 my ($port, $call) = split /\s+/, $line;
400 $conn->{agwpid} = ord "\xF0";
401 $conn->{agwport} = $port - 1;
402 $conn->{lineend} = "\cM";
403 $conn->{incoming} = 0;
404 $conn->{csort} = 'ax25';
405 $conn->{agwcall} = uc $call;
406 $circuit{$conn->{agwcall}} = $conn;
408 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
409 $conn->{state} = 'WC';
417 delete $circuit{$conn->{agwcall}};
418 $conn->SUPER::disconnect;
424 delete $circuit{$conn->{agwcall}};
425 if ($conn->{incoming}) {
426 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
428 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
430 $conn->SUPER::disconnect;
435 my ($conn, $msg) = @_;
437 $msg =~ s/^[-\w]+\|//;
438 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
439 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
440 my $len = length($msg) + 1;
441 dbg("AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"") if isdbg('agw');
448 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
449 for (my $i = 0; $i < $noports; $i++) {
450 _sendf('y', undef, undef, $i );
452 $lastytime = $main::systime;
454 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
455 for (my $i = 0; $i < $noports; $i++) {
456 _sendf('H', undef, undef, $i );
458 $lasthtime = $main::systime;