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', Timeout => 30);
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
66 # optional m frame to enable monitoring
69 _sendf('X', $main::mycall);
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 a10 a10 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;
231 dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
232 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
234 if ($conn->{state} eq 'WC') {
235 if (exists $conn->{cmd}) {
236 if (@{$conn->{cmd}}) {
237 dbg('connect', $conn->{msg});
238 $conn->_docmd($conn->{msg});
241 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
242 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
245 &{$conn->{rproc}}($conn, "I$conn->{call}|$data");
248 dbg('err', "AGW error Unsolicited Data!");
250 } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M') {
251 dbg('agw', "AGW Monitor \"$data\"");
252 } elsif ($sort eq 'C') {
253 dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to'");
254 my $call = $from eq $main::mycall ? $to : $from;
255 my $conn = Msg->conns($call);
257 if ($conn->{state} eq 'WC') {
258 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
259 $conn->_docmd($data);
260 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
261 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
266 $conn = AGWMsg->new($rproc);
267 $conn->{agwpid} = $pid;
268 $conn->{agwport} = $port;
269 $conn->{lineend} = "\cR";
270 $conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
272 } elsif ($sort eq 'd') {
273 dbg('agw', "AGW '$from'->'$to' Disconnected");
274 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
275 $conn->in_disconnect if $conn;
276 } elsif ($sort eq 'y') {
277 my ($frames) = unpack "V", $data;
278 dbg('agw', "AGW Frames Outstanding on port $port = $frames");
279 my $conn = Msg->conns($from);
280 $conn->{oframes} = $frames if $conn;
281 } elsif ($sort eq 'Y') {
282 my ($frames) = unpack "V", $data;
283 dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
284 my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
285 $conn->{oframes} = $frames if $conn;
286 } elsif ($sort eq 'X') {
287 my ($r) = unpack "C", $data;
288 $r = $r ? "Successful" : "Failed";
289 dbg('err', "AGW Register $from $r");
291 } elsif ($sort eq 'R') {
292 my ($major, $minor) = unpack "v x2 v x2", $data;
293 dbg('agw', "AGW Version $major.$minor");
294 } elsif ($sort eq 'G') {
295 my @ports = split /;/, $data;
296 dbg('agw', "AGW $ports[0] Ports available");
297 my $n = shift @ports;
298 pop @ports while @ports > $n;
301 dbg('agw', "AGW Port: $_");
304 dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
311 $conn->SUPER->disconnect;
317 _sendf('d', $main::mycall, $conn->{call}, $conn->{agwport});
318 $conn->SUPER->disconnect;
323 my ($conn, $msg) = @_;
324 if ($msg =~ /^[D]/) {
325 $msg =~ s/^[-\w]+\|//;
326 _sendf('D', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});