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);
34 @ISA = qw(Msg ExtMsg);
43 return unless $enable;
47 dbg('err', "AGW initialising and connecting to $addr/$port ...");
48 $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp');
50 dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
53 Msg::blocking($sock, 0);
54 Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
56 # send a P frame for the login if required
58 my $data = pack "a255 a255", $login, $passwd;
59 _sendf('P', undef, undef, undef, undef, $data);
63 # R frame for the release number
64 # G frame to ask for ports
65 # X frame to say who we are
68 _sendf('X', $main::mycall);
77 dbg('err', "AGW ending...");
78 for (values %Msg::conns) {
79 next unless $_->isa('AGWMsg');
83 _sendf('x', $main::mycall);
85 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
92 my $sort = shift || confess "need a valid AGW command letter";
93 my $from = shift || '';
95 my $port = shift || 0;
97 my $data = shift || '';
101 dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
102 push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
103 Msg::set_event_handler($sock, write=>\&_send);
110 # If $flush is set, set the socket to blocking, and send all
111 # messages in the queue - return only if there's an error
112 # If $flush is 0 (deferred mode) make the socket non-blocking, and
113 # return to the event loop only after every message, or if it
114 # is likely to block in the middle of a message.
116 my $offset = $send_offset;
119 my $msg = $outqueue[0];
120 my $mlth = length($msg);
121 my $bytes_to_write = $mlth - $offset;
122 my $bytes_written = 0;
123 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
124 while ($bytes_to_write > 0) {
125 $bytes_written = syswrite ($sock, $msg,
126 $bytes_to_write, $offset);
127 if (!defined($bytes_written)) {
128 if (Msg::_err_will_block($!)) {
129 # Should happen only in deferred mode. Record how
130 # much we have already sent.
131 $send_offset = $offset;
132 # Event handler should already be set, so we will
133 # be called back eventually, and will resume sending
137 return 0; # fail. Message remains in queue ..
140 $offset += $bytes_written;
141 $bytes_to_write -= $bytes_written;
143 $send_offset = $offset = 0;
145 last; # Go back to select and wait
146 # for it to fire again.
149 # Call me back if queue has not been drained.
151 Msg::set_event_handler ($sock, write => \&_send);
153 Msg::set_event_handler ($sock, write => undef);
158 sub _rcv { # Complement to _send
160 my ($msg, $offset, $bytes_read);
162 $bytes_read = sysread ($sock, $msg, 1024, 0);
163 if (defined ($bytes_read)) {
164 if ($bytes_read > 0) {
168 if (Msg::_err_will_block($!)) {
176 if (defined $bytes_read && $bytes_read == 0) {
179 _decode() if length $inmsg > 36;
185 dbg('agw', "error on AGW connection $addr/$port $!");
186 Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
188 for (values %Msg::conns) {
189 next unless $_->isa('AGWMsg');
198 # we have at least 36 bytes of data (ugh!)
199 my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 a10 a10 V x4', $inmsg);
202 # do a sanity check on the length
204 dbg('err', "AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
209 if (length $inmsg > 36) {
210 $inmsg = substr($inmsg, 36);
214 } elsif (length $inmsg > $len + 36) {
215 $data = substr($inmsg, 36, $len);
216 $inmsg = substr($inmsg, $len + 36);
217 } elsif (length $inmsg == $len + 36) {
218 $data = substr($inmsg, 36);
221 # we don't have enough data or something
222 # or we have screwed up
226 $data = '' unless defined $data;
229 dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
230 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
232 if ($conn->{state} eq 'WC') {
233 if (exists $conn->{cmd}) {
234 if (@{$conn->{cmd}}) {
235 dbg('connect', $conn->{msg});
236 $conn->_docmd($conn->{msg});
239 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
240 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
243 &{$conn->{rproc}}($conn, "I$conn->{call}|$data");
246 dbg('err', "AGW error Unsolicited Data!");
248 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M') {
249 dbg('agw', "AGW Monitor \"$data\"");
250 } elsif ($sort eq 'C') {
251 dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to'");
252 my $call = $from eq $main::mycall ? $to : $from;
253 my $conn = Msg->conns($call);
255 if ($conn->{state} eq 'WC') {
256 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
257 $conn->_docmd($data);
258 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
259 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
264 $conn = AGWMsg->new($rproc);
265 $conn->{agwpid} = $pid;
266 $conn->{agwport} = $port;
267 $conn->{lineend} = "\cR";
268 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
270 } elsif ($sort eq 'd') {
271 dbg('agw', "AGW '$from'->'$to' Disconnected");
272 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
273 $conn->in_disconnect if $conn;
274 } elsif ($sort eq 'y') {
275 my ($frames) = unpack "V", $data;
276 dbg('agw', "AGW Frames Outstanding on port $port = $frames");
277 my $conn = Msg->conns($from);
278 $conn->{oframes} = $frames if $conn;
279 } elsif ($sort eq 'Y') {
280 my ($frames) = unpack "V", $data;
281 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
282 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
283 $conn->{oframes} = $frames if $conn;
284 } elsif ($sort eq 'X') {
285 my ($r) = unpack "C", $data;
286 $r = $r ? "Successful" : "Failed";
287 dbg('err', "AGW Register $from $r");
289 } elsif ($sort eq 'R') {
290 my ($major, $minor) = unpack "v x2 v x2", $data;
291 dbg('agw', "AGW Version $major.$minor");
292 } elsif ($sort eq 'G') {
293 my @ports = split /;/, $data;
294 dbg('agw', "AGW $ports[0] Ports available");
295 my $n = shift @ports;
296 pop @ports while @ports > $n;
299 dbg('agw', "AGW Port: $_");
302 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
309 $conn->SUPER->disconnect;
315 _sendf('d', $main::mycall, $conn->{call}, $conn->{agwport});
316 $conn->SUPER->disconnect;
321 my ($conn, $msg) = @_;
322 if ($msg =~ /^[D]/) {
323 $msg =~ s/^[-\w]+\|//;
324 _sendf('D', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});