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 = 120 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 => 30);
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;
82 dbg('err', "AGW ending...");
83 for (values %circuit) {
87 _sendf('m') if $monitor;
88 _sendf('x', $main::mycall);
90 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
97 my $sort = shift || confess "need a valid AGW command letter";
98 my $from = shift || '';
100 my $port = shift || 0;
101 my $pid = shift || 0;
102 my $data = shift || '';
106 if ($sort eq 'y' || $sort eq 'H') {
107 dbg('agwpoll', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
108 } elsif ($sort eq 'D') {
112 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"");
115 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
117 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
118 Msg::set_event_handler($sock, write=>\&_send);
125 # If $flush is set, set the socket to blocking, and send all
126 # messages in the queue - return only if there's an error
127 # If $flush is 0 (deferred mode) make the socket non-blocking, and
128 # return to the event loop only after every message, or if it
129 # is likely to block in the middle of a message.
131 my $offset = $send_offset;
134 my $msg = $outqueue[0];
135 my $mlth = length($msg);
136 my $bytes_to_write = $mlth - $offset;
137 my $bytes_written = 0;
138 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
139 while ($bytes_to_write > 0) {
140 $bytes_written = syswrite ($sock, $msg,
141 $bytes_to_write, $offset);
142 if (!defined($bytes_written)) {
143 if (Msg::_err_will_block($!)) {
144 # Should happen only in deferred mode. Record how
145 # much we have already sent.
146 $send_offset = $offset;
147 # Event handler should already be set, so we will
148 # be called back eventually, and will resume sending
152 return 0; # fail. Message remains in queue ..
155 $offset += $bytes_written;
156 $bytes_to_write -= $bytes_written;
158 $send_offset = $offset = 0;
160 last; # Go back to select and wait
161 # for it to fire again.
164 # Call me back if queue has not been drained.
166 Msg::set_event_handler ($sock, write => \&_send);
168 Msg::set_event_handler ($sock, write => undef);
173 sub _rcv { # Complement to _send
175 my ($msg, $offset, $bytes_read);
177 $bytes_read = sysread ($sock, $msg, 1024, 0);
178 if (defined ($bytes_read)) {
179 if ($bytes_read > 0) {
183 if (Msg::_err_will_block($!)) {
191 if (defined $bytes_read && $bytes_read == 0) {
194 _decode() if length $inmsg > 36;
200 dbg('agw', "error on AGW connection $addr/$port $!");
201 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
204 next unless $_->isa('AGWMsg');
213 # we have at least 36 bytes of data (ugh!)
214 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
217 # do a sanity check on the length
219 dbg('err', "AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
224 if (length $inmsg > 36) {
225 $inmsg = substr($inmsg, 36);
229 } elsif (length $inmsg > $len + 36) {
230 $data = substr($inmsg, 36, $len);
231 $inmsg = substr($inmsg, $len + 36);
232 } elsif (length $inmsg == $len + 36) {
233 $data = substr($inmsg, 36);
236 # we don't have enough data or something
237 # or we have screwed up
241 $data = '' unless defined $data;
243 my $d = unpack "Z*", $data;
245 dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
246 my $conn = _find($from eq $main::mycall ? $to : $from);
248 if ($conn->{state} eq 'WC') {
249 if (exists $conn->{cmd}) {
250 if (@{$conn->{cmd}}) {
255 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
256 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
259 my @lines = split /\cM/, $data;
262 &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
265 &{$conn->{rproc}}($conn, "I$conn->{call}|");
269 dbg('err', "AGW error Unsolicited Data!");
271 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
272 my $d = unpack "Z*", $data;
274 my @lines = split /\cM/, $d;
277 s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
278 dbg('agw', "AGW Monitor port: $port \"$_\"");
280 } elsif ($sort eq 'C') {
281 my $d = unpack "Z*", $data;
283 dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"");
284 my $call = $from eq $main::mycall ? $to : $from;
285 my $conn = _find($call);
287 if ($conn->{state} eq 'WC') {
288 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
290 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
291 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
296 $conn = AGWMsg->new($rproc);
297 $conn->{agwpid} = $pid;
298 $conn->{agwport} = $port;
299 $conn->{lineend} = "\cM";
300 $conn->{incoming} = 1;
301 $conn->{agwcall} = $call;
302 $circuit{$call} = $conn;
303 if ($call =~ /^(\w+)-(\d\d?)$/) {
307 if ($s <= 8 && $s > 0) {
313 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
315 } elsif ($sort eq 'd') {
316 dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
317 my $conn = _find($from eq $main::mycall ? $to : $from);
318 $conn->in_disconnect if $conn;
319 } elsif ($sort eq 'y') {
320 my ($frames) = unpack "V", $data;
321 dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames");
322 my $conn = _find($from);
323 $conn->{oframes} = $frames if $conn;
324 } elsif ($sort eq 'Y') {
325 my ($frames) = unpack "V", $data;
326 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
327 my $conn = _find($from eq $main::mycall ? $to : $from);
328 $conn->{oframes} = $frames if $conn;
329 } elsif ($sort eq 'H') {
330 unless ($from =~ /^\s+$/) {
331 my $d = unpack "Z*", $data;
333 dbg('agw', "AGW Heard port: $port \"$d\"");
335 } elsif ($sort eq 'X') {
336 my ($r) = unpack "C", $data;
337 $r = $r ? "Successful" : "Failed";
338 dbg('err', "AGW Register $from $r");
340 } elsif ($sort eq 'R') {
341 my ($major, $minor) = unpack "v x2 v x2", $data;
342 dbg('agw', "AGW Version $major.$minor");
343 } elsif ($sort eq 'G') {
344 my @ports = split /;/, $data;
345 $noports = shift @ports || '0';
346 dbg('agw', "AGW $noports Ports available");
347 pop @ports while @ports > $noports;
350 dbg('agw', "AGW Port: $_");
352 for (my $i = 0; $i < $noports; $i++) {
353 _sendf('y', undef, undef, $i );
356 my $d = unpack "Z*", $data;
357 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
364 return $circuit{$call};
369 my ($conn, $line) = @_;
371 my ($port, $call) = split /\s+/, $line;
372 $conn->{agwpid} = ord "\xF0";
373 $conn->{agwport} = $port - 1;
374 $conn->{lineend} = "\cM";
375 $conn->{incoming} = 0;
376 $conn->{csort} = 'ax25';
377 $conn->{agwcall} = uc $call;
378 $circuit{$conn->{agwcall}} = $conn;
380 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
381 $conn->{state} = 'WC';
389 delete $circuit{$conn->{agwcall}};
390 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
391 $conn->SUPER::disconnect;
397 delete $circuit{$conn->{agwcall}};
398 if ($conn->{incoming}) {
399 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
401 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
403 $conn->SUPER::disconnect;
408 my ($conn, $msg) = @_;
410 $msg =~ s/^[-\w]+\|//;
411 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
412 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
419 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
420 for (my $i = 0; $i < $noports; $i++) {
421 _sendf('y', undef, undef, $i );
423 $lastytime = $main::systime;
425 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
426 for (my $i = 0; $i < $noports; $i++) {
427 _sendf('H', undef, undef, $i );
429 $lasthtime = $main::systime;