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 $lasttime);
34 @ISA = qw(Msg ExtMsg);
45 return unless $enable;
49 dbg('err', "AGW initialising and connecting to $addr/$port ...");
50 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout => 30);
52 dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
55 Msg::blocking($sock, 0);
56 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
58 # send a P frame for the login if required
60 my $data = pack "a255 a255", $login, $passwd;
61 _sendf('P', undef, undef, undef, undef, $data);
65 # R frame for the release number
66 # G frame to ask for ports
67 # X frame to say who we are
68 # optional m frame to enable monitoring
71 _sendf('X', $main::mycall);
72 _sendf('m') if $monitor;
78 dbg('err', "AGW ending...");
79 for (values %Msg::conns) {
80 next unless $_->isa('AGWMsg');
84 _sendf('m') if $monitor;
85 _sendf('x', $main::mycall);
87 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
94 my $sort = shift || confess "need a valid AGW command letter";
95 my $from = shift || '';
97 my $port = shift || 0;
99 my $data = shift || '';
103 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
104 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
105 Msg::set_event_handler($sock, write=>\&_send);
112 # If $flush is set, set the socket to blocking, and send all
113 # messages in the queue - return only if there's an error
114 # If $flush is 0 (deferred mode) make the socket non-blocking, and
115 # return to the event loop only after every message, or if it
116 # is likely to block in the middle of a message.
118 my $offset = $send_offset;
121 my $msg = $outqueue[0];
122 my $mlth = length($msg);
123 my $bytes_to_write = $mlth - $offset;
124 my $bytes_written = 0;
125 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
126 while ($bytes_to_write > 0) {
127 $bytes_written = syswrite ($sock, $msg,
128 $bytes_to_write, $offset);
129 if (!defined($bytes_written)) {
130 if (Msg::_err_will_block($!)) {
131 # Should happen only in deferred mode. Record how
132 # much we have already sent.
133 $send_offset = $offset;
134 # Event handler should already be set, so we will
135 # be called back eventually, and will resume sending
139 return 0; # fail. Message remains in queue ..
142 $offset += $bytes_written;
143 $bytes_to_write -= $bytes_written;
145 $send_offset = $offset = 0;
147 last; # Go back to select and wait
148 # for it to fire again.
151 # Call me back if queue has not been drained.
153 Msg::set_event_handler ($sock, write => \&_send);
155 Msg::set_event_handler ($sock, write => undef);
160 sub _rcv { # Complement to _send
162 my ($msg, $offset, $bytes_read);
164 $bytes_read = sysread ($sock, $msg, 1024, 0);
165 if (defined ($bytes_read)) {
166 if ($bytes_read > 0) {
170 if (Msg::_err_will_block($!)) {
178 if (defined $bytes_read && $bytes_read == 0) {
181 _decode() if length $inmsg > 36;
187 dbg('agw', "error on AGW connection $addr/$port $!");
188 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
190 for (values %Msg::conns) {
191 next unless $_->isa('AGWMsg');
200 # we have at least 36 bytes of data (ugh!)
201 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
204 # do a sanity check on the length
206 dbg('err', "AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
211 if (length $inmsg > 36) {
212 $inmsg = substr($inmsg, 36);
216 } elsif (length $inmsg > $len + 36) {
217 $data = substr($inmsg, 36, $len);
218 $inmsg = substr($inmsg, $len + 36);
219 } elsif (length $inmsg == $len + 36) {
220 $data = substr($inmsg, 36);
223 # we don't have enough data or something
224 # or we have screwed up
228 $data = '' unless defined $data;
230 my $d = unpack "Z*", $data;
232 dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
233 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
235 if ($conn->{state} eq 'WC') {
236 if (exists $conn->{cmd}) {
237 if (@{$conn->{cmd}}) {
242 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
243 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
246 &{$conn->{rproc}}($conn, "I$conn->{call}|$data");
249 dbg('err', "AGW error Unsolicited Data!");
251 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
252 my $d = unpack "Z*", $data;
254 dbg('agw', "AGW Monitor port: $port \"$d\"");
255 } elsif ($sort eq 'C') {
256 my $d = unpack "Z*", $data;
258 dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"");
259 my $call = $from eq $main::mycall ? $to : $from;
260 my $conn = Msg->conns($call);
262 if ($conn->{state} eq 'WC') {
263 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
265 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
266 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
271 $conn = AGWMsg->new($rproc);
272 $conn->{agwpid} = $pid;
273 $conn->{agwport} = $port;
274 $conn->{lineend} = "\cM";
275 $conn->{incoming} = 1;
276 $conn->{agwcall} = $call;
277 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
279 } elsif ($sort eq 'd') {
280 dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
281 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
282 $conn->in_disconnect if $conn;
283 } elsif ($sort eq 'y') {
284 my ($frames) = unpack "V", $data;
285 dbg('agw', "AGW Frames Outstanding on port $port = $frames");
286 my $conn = Msg->conns($from);
287 $conn->{oframes} = $frames if $conn;
288 } elsif ($sort eq 'Y') {
289 my ($frames) = unpack "V", $data;
290 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
291 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
292 $conn->{oframes} = $frames if $conn;
293 } elsif ($sort eq 'X') {
294 my ($r) = unpack "C", $data;
295 $r = $r ? "Successful" : "Failed";
296 dbg('err', "AGW Register $from $r");
298 } elsif ($sort eq 'R') {
299 my ($major, $minor) = unpack "v x2 v x2", $data;
300 dbg('agw', "AGW Version $major.$minor");
301 } elsif ($sort eq 'G') {
302 my @ports = split /;/, $data;
303 $noports = shift @ports || '0';
304 dbg('agw', "AGW $noports Ports available");
305 pop @ports while @ports > $noports;
308 dbg('agw', "AGW Port: $_");
310 for (my $i = 0; $i < $noports; $i++) {
311 _sendf('y', undef, undef, $i );
314 my $d = unpack "Z*", $data;
315 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
321 my ($conn, $line) = @_;
323 my ($port, $call) = split /\s+/, $line;
324 $conn->{agwpid} = ord "\xF0";
325 $conn->{agwport} = $port - 1;
326 $conn->{lineend} = "\cM";
327 $conn->{incoming} = 0;
328 $conn->{csort} = 'ax25';
329 $conn->{agwcall} = uc $call;
331 _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
337 $conn->SUPER::disconnect;
343 _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
344 $conn->SUPER::disconnect;
349 my ($conn, $msg) = @_;
350 if ($msg =~ /^[D]/) {
351 $msg =~ s/^[-\w]+\|//;
352 _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
353 _sendf('D', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
360 if ($main::systime - $lasttime >= 60) {
361 for (my $i = 0; $i < $noports; $i++) {
362 _sendf('y', undef, undef, $i );
363 # _sendf('H', undef, undef, $i );
365 $lasttime = $main::systime;