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);
35 @ISA = qw(Msg ExtMsg);
42 $lastytime = $lasthtime = time;
43 $ypolltime = 10 unless defined $ypolltime;
44 $hpolltime = 120 unless defined $hpolltime;
48 return unless $enable;
52 dbg('err', "AGW initialising and connecting to $addr/$port ...");
53 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout => 30);
55 dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
58 Msg::blocking($sock, 0);
59 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
61 # send a P frame for the login if required
63 my $data = pack "a255 a255", $login, $passwd;
64 _sendf('P', undef, undef, undef, undef, $data);
68 # R frame for the release number
69 # G frame to ask for ports
70 # X frame to say who we are
71 # optional m frame to enable monitoring
74 _sendf('X', $main::mycall);
75 _sendf('m') if $monitor;
81 dbg('err', "AGW ending...");
82 for (values %Msg::conns) {
83 next unless $_->isa('AGWMsg');
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);
203 for (values %Msg::conns) {
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 if ($call =~ /^(\w+)-(\d\d?)$/) {
306 if ($s <= 8 && $s > 0) {
312 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
314 } elsif ($sort eq 'd') {
315 dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
316 my $conn = _find($from eq $main::mycall ? $to : $from);
317 $conn->in_disconnect if $conn;
318 } elsif ($sort eq 'y') {
319 my ($frames) = unpack "V", $data;
320 dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames");
321 my $conn = Msg->conns($from);
322 $conn->{oframes} = $frames if $conn;
323 } elsif ($sort eq 'Y') {
324 my ($frames) = unpack "V", $data;
325 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
326 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
327 $conn->{oframes} = $frames if $conn;
328 } elsif ($sort eq 'H') {
329 unless ($from =~ /^\s+$/) {
330 my $d = unpack "Z*", $data;
332 dbg('agw', "AGW Heard port: $port \"$d\"");
334 } elsif ($sort eq 'X') {
335 my ($r) = unpack "C", $data;
336 $r = $r ? "Successful" : "Failed";
337 dbg('err', "AGW Register $from $r");
339 } elsif ($sort eq 'R') {
340 my ($major, $minor) = unpack "v x2 v x2", $data;
341 dbg('agw', "AGW Version $major.$minor");
342 } elsif ($sort eq 'G') {
343 my @ports = split /;/, $data;
344 $noports = shift @ports || '0';
345 dbg('agw', "AGW $noports Ports available");
346 pop @ports while @ports > $noports;
349 dbg('agw', "AGW Port: $_");
351 for (my $i = 0; $i < $noports; $i++) {
352 _sendf('y', undef, undef, $i );
355 my $d = unpack "Z*", $data;
356 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
363 for (values %Msg::conns) {
364 if ($_->isa('AGWMsg') && $call eq $_->{agwcall}) {
373 my ($conn, $line) = @_;
375 my ($port, $call) = split /\s+/, $line;
376 $conn->{agwpid} = ord "\xF0";
377 $conn->{agwport} = $port - 1;
378 $conn->{lineend} = "\cM";
379 $conn->{incoming} = 0;
380 $conn->{csort} = 'ax25';
381 $conn->{agwcall} = uc $call;
383 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
384 $conn->{state} = 'WC';
392 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
393 $conn->SUPER::disconnect;
399 if ($conn->{incoming}) {
400 _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
402 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
404 $conn->SUPER::disconnect;
409 my ($conn, $msg) = @_;
411 $msg =~ s/^[-\w]+\|//;
412 # _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
413 _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
420 if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
421 for (my $i = 0; $i < $noports; $i++) {
422 _sendf('y', undef, undef, $i );
424 $lastytime = $main::systime;
426 if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
427 for (my $i = 0; $i < $noports; $i++) {
428 _sendf('H', undef, undef, $i );
430 $lasthtime = $main::systime;