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);
35 @ISA = qw(Msg ExtMsg);
42 $lastytime = $lasthtime = time;
43 $ypolltime = 10 unless defined $ypolltime;
44 $hpolltime = 300 unless defined $hpolltime;
49 return unless $enable;
53 dbg('err', "AGW initialising and connecting to $addr/$port ...");
54 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout=>15);
56 dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
59 Msg::blocking($sock, 0);
60 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
62 # send a P frame for the login if required
64 my $data = pack "a255 a255", $login, $passwd;
65 _sendf('P', undef, undef, undef, undef, $data);
69 # R frame for the release number
70 # G frame to ask for ports
71 # X frame to say who we are
72 # optional m frame to enable monitoring
75 _sendf('X', $main::mycall);
76 _sendf('m') if $monitor;
86 dbg('err', "AGW ending...");
87 for (values %circuit) {
88 &{$_->{eproc}}() if $_->{eproc};
92 _sendf('m') if $monitor;
93 _sendf('x', $main::mycall);
95 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
102 my $sort = shift || confess "need a valid AGW command letter";
103 my $from = shift || '';
104 my $to = shift || '';
105 my $port = shift || 0;
106 my $pid = shift || 0;
107 my $data = shift || '';
111 if ($sort eq 'y' || $sort eq 'H') {
112 dbg('agwpoll', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
113 } elsif ($sort eq 'D') {
117 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"");
120 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
122 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
123 Msg::set_event_handler($sock, write=>\&_send);
130 # If $flush is set, set the socket to blocking, and send all
131 # messages in the queue - return only if there's an error
132 # If $flush is 0 (deferred mode) make the socket non-blocking, and
133 # return to the event loop only after every message, or if it
134 # is likely to block in the middle of a message.
136 my $offset = $send_offset;
139 my $msg = $outqueue[0];
140 my $mlth = length($msg);
141 my $bytes_to_write = $mlth - $offset;
142 my $bytes_written = 0;
143 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
144 while ($bytes_to_write > 0) {
145 $bytes_written = syswrite ($sock, $msg,
146 $bytes_to_write, $offset);
147 if (!defined($bytes_written)) {
148 if (Msg::_err_will_block($!)) {
149 # Should happen only in deferred mode. Record how
150 # much we have already sent.
151 $send_offset = $offset;
152 # Event handler should already be set, so we will
153 # be called back eventually, and will resume sending
157 return 0; # fail. Message remains in queue ..
160 $offset += $bytes_written;
161 $bytes_to_write -= $bytes_written;
163 $send_offset = $offset = 0;
165 last; # Go back to select and wait
166 # for it to fire again.
169 # Call me back if queue has not been drained.
171 Msg::set_event_handler ($sock, write => \&_send);
173 Msg::set_event_handler ($sock, write => undef);
178 sub _rcv { # Complement to _send
180 my ($msg, $offset, $bytes_read);
182 $bytes_read = sysread ($sock, $msg, 1024, 0);
183 if (defined ($bytes_read)) {
184 if ($bytes_read > 0) {
186 # $msg =~ s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
187 # dbg('connll', $msg);
190 if (Msg::_err_will_block($!)) {
198 if (defined $bytes_read && $bytes_read == 0) {
201 _decode() if length $inmsg >= 36;
207 dbg('agw', "error on AGW connection $addr/$port $!");
208 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
211 &{$_->{eproc}}() if $_->{eproc};
220 # we have at least 36 bytes of data (ugh!)
221 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
224 # do a sanity check on the length
226 dbg('err', "AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
231 if (length $inmsg > 36) {
232 $inmsg = substr($inmsg, 36);
236 } elsif (length $inmsg > $len + 36) {
237 $data = substr($inmsg, 36, $len);
238 $inmsg = substr($inmsg, $len + 36);
239 } elsif (length $inmsg == $len + 36) {
240 $data = substr($inmsg, 36);
243 # we don't have enough data or something
244 # or we have screwed up
248 $data = '' unless defined $data;
250 my $d = unpack "Z*", $data;
252 dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
253 my $conn = _find($from eq $main::mycall ? $to : $from);
255 if ($conn->{state} eq 'WC') {
256 if (exists $conn->{cmd}) {
257 if (@{$conn->{cmd}}) {
262 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
263 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
266 my @lines = split /\cM/, $data;
269 &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
272 &{$conn->{rproc}}($conn, "I$conn->{call}|");
276 dbg('err', "AGW error Unsolicited Data!");
278 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
279 my $d = unpack "Z*", $data;
281 my @lines = split /\cM/, $d;
284 s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
285 dbg('agw', "AGW Monitor port: $port \"$_\"");
287 } elsif ($sort eq 'C') {
288 my $d = unpack "Z*", $data;
290 dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"");
291 my $call = $from eq $main::mycall ? $to : $from;
292 my $conn = _find($call);
294 if ($conn->{state} eq 'WC') {
295 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
297 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
298 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
303 $conn = AGWMsg->new($rproc);
304 $conn->{agwpid} = $pid;
305 $conn->{agwport} = $port;
306 $conn->{lineend} = "\cM";
307 $conn->{incoming} = 1;
308 $conn->{agwcall} = $call;
309 $circuit{$call} = $conn;
310 if ($call =~ /^(\w+)-(\d\d?)$/) {
314 if ($s <= 8 && $s > 0) {
320 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
322 } elsif ($sort eq 'd') {
323 dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
324 my $conn = _find($from eq $main::mycall ? $to : $from);
326 &{$conn->{eproc}}() if $conn->{eproc};
327 $conn->in_disconnect;
329 } elsif ($sort eq 'y') {
330 my ($frames) = unpack "V", $data;
331 dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames");
332 my $conn = _find($from);
333 $conn->{oframes} = $frames if $conn;
334 } elsif ($sort eq 'Y') {
335 my ($frames) = unpack "V", $data;
336 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
337 my $conn = _find($from eq $main::mycall ? $to : $from);
338 $conn->{oframes} = $frames if $conn;
339 } elsif ($sort eq 'H') {
340 unless ($from =~ /^\s+$/) {
341 my $d = unpack "Z*", $data;
343 dbg('agw', "AGW Heard port: $port \"$d\"");
345 } elsif ($sort eq 'X') {
346 my ($r) = unpack "C", $data;
347 $r = $r ? "Successful" : "Failed";
348 dbg('err', "AGW Register $from $r");
350 } elsif ($sort eq 'R') {
351 my ($major, $minor) = unpack "v x2 v x2", $data;
352 dbg('agw', "AGW Version $major.$minor");
353 } elsif ($sort eq 'G') {
354 my @ports = split /;/, $data;
355 $noports = shift @ports || '0';
356 dbg('agw', "AGW $noports Ports available");
357 pop @ports while @ports > $noports;
360 dbg('agw', "AGW Port: $_");
362 for (my $i = 0; $i < $noports; $i++) {
363 _sendf('y', undef, undef, $i);
364 _sendf('g', undef, undef, $i);
367 my $d = unpack "Z*", $data;
368 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
375 return $circuit{$call};
380 my ($conn, $line) = @_;
382 my ($port, $call) = split /\s+/, $line;
383 $conn->{agwpid} = ord "\xF0";
384 $conn->{agwport} = $port - 1;
385 $conn->{lineend} = "\cM";
386 $conn->{incoming} = 0;
387 $conn->{csort} = 'ax25';
388 $conn->{agwcall} = uc $call;
389 $circuit{$conn->{agwcall}} = $conn;
391 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
392 $conn->{state} = 'WC';
400 delete $circuit{$conn->{agwcall}};
401 $conn->SUPER::disconnect;
407 delete $circuit{$conn->{agwcall}};
408 if ($conn->{incoming}) {
409 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
411 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
413 $conn->SUPER::disconnect;
418 my ($conn, $msg) = @_;
420 $msg =~ s/^[-\w]+\|//;
421 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
422 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
423 my $len = length($msg) + 1;
424 dbg('agw', "AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"");
431 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
432 for (my $i = 0; $i < $noports; $i++) {
433 _sendf('y', undef, undef, $i );
435 $lastytime = $main::systime;
437 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
438 for (my $i = 0; $i < $noports; $i++) {
439 _sendf('H', undef, undef, $i );
441 $lasthtime = $main::systime;