download AGWMsg.pm
Language: Perl
Copyright: (c) 2001 - Dirk Koopman G1TLH
LOC: 380
Project Info
DXSpider DX Cluster System(dxspider)
Server: SourceForge
Type: cvs
...pider\dxspider\spider\perl\
   AGWConnect.pm
   AGWMsg.pm
   AnnTalk.pm
   BadWords.pm
   Bands.pm
   BBS.pm
   Buck.pm
   call.pl
   callbot.pl
   Chain.pm
   cluster.pl
   CmdAlias.pm
   connect.pl
   console.pl
   Console.pm
   convert_users.pl
   convkeps.pl
   create_prefix.pl
   create_qsl.pl
   create_sysop.pl
   create_usdb.pl
   DB0SDX.pm
   DXBearing.pm
   dxcc.pl
   DXChannel.pm
   DXCommandmode.pm
   DXConnect.pm
   DXCron.pm
   DXDb.pm
   DXDebug.pm
   DXDupe.pm
   DXHash.pm
   DXLog.pm
   DXLogPrint.pm
   DXM.pm
   DXMsg.pm
   dxoldtonew.pl
   DXProt.pm
   DXProtout.pm
   DXProtVars.pm
   DXSql.pm
   DXUser.pm
   DXUtil.pm
   DXVars.pm.issue
   DXXml.pm
   Editable.pm
   export_opernam.pl
   ExtMsg.pm
   Filter.pm
   ForkingServer.pm
   gen_usdb_data.pl
   Geomag.pm
   hlptohtml.pl
   importkeps.pl
   importwwv.pl
   Internet.pm
   IntMsg.pm
   Investigate.pm
   IsoTime.pm
   Julian.pm
   K4UTE.pm
   Keps.pm
   Listeners.pm
   Local.pm
   lock_nodes.pl
   log2csv.pl
   LRU.pm
   Minimuf.pm
   MiscLog.pm
   Mrtg.pm
   Msg.pm
   PC.pm
   Prefix.pm
   process_ursa.pl
   Prot.pm
   proto.html
   QRZ.pm
   QSL.pm
   RingBuf.pm
   Route.pm
   RouteDB.pm
   Script.pm
   Spot.pm
   spot2csv.pl
   Sun.pm
   talias.pl
   Thingy.pm
   Timer.pm
   UDPMsg.pm
   update_sysop.pl
   USDB.pm
   VE7CC.pm
   Verify.pm
   WCY.pm
   winclient.pl
   y2k.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#
# This class is the internal subclass that deals with AGW Engine connections
#
# The complication here is that there only one 'real' (and from the node's point
# of view, invisible) IP connection. This connection then has multiplexed 
# connections passed down it, a la BPQ native host ports (but not as nicely).
#
# It is a shame that the author has chosen an inherently dangerous binary format
# which is non-framed and has the potential for getting out of sync and not
# being able to recover. Relying on length fields is recipe for disaster (esp.
# for him!). DoS attacks are a wonderful thing....
#
# Also making the user handle the distinction between a level 2 and 4 connection
# and especially Digis, in the way that he has, is a bit of a cop out! If I can
# be arsed to do anything other than straight ax25 connects then it will only
# because I have the 'power of perl' available that avoids me getting 
# terminally bored sorting out other people's sloppyness.
#
# $Id: AGWMsg.pm,v 1.27 2005/01/24 09:08:54 minima Exp $
#
# Copyright (c) 2001 - Dirk Koopman G1TLH
#

package AGWMsg;

use strict;
use IO::Socket;
use Msg;
use AGWConnect;
use DXDebug;

use vars qw(@ISA $sock @outqueue $send_offset $inmsg $rproc $noports $lastytime 
			$lasthtime $ypolltime $hpolltime %circuit $total_in $total_out);

@ISA = qw(Msg ExtMsg);
$sock = undef;
@outqueue = ();
$send_offset = 0;
$inmsg = '';
$rproc = undef;
$noports = 0;
$lastytime = $lasthtime = time;
$ypolltime = 10 unless defined $ypolltime;
$hpolltime = 300 unless defined $hpolltime;
%circuit = ();
$total_in = $total_out = 0;

use vars qw($VERSION $BRANCH);
$VERSION = sprintf( "%d.%03d", q$Revision: 1.27 $ =~ /(\d+)\.(\d+)/ );
$BRANCH = sprintf( "%d.%03d", q$Revision: 1.27 $ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
$main::build += $VERSION;
$main::branch += $BRANCH;

sub init
{
	return unless $enable;
	$rproc = shift;
	
	finish();
	dbg("AGW initialising and connecting to $addr/$port ...");
	$sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout=>15);
	unless ($sock) {
		dbg("Cannot connect to AGW Engine at $addr/$port $!");
		return;
	}
	Msg::blocking($sock, 0);
	Msg::set_event_handler($sock, read=>\&_rcv, error=>\&_error);
	
	# send a P frame for the login if required
	if ($login) {
		my $data = pack "a255 a255", $login, $passwd;
		_sendf('P', undef, undef, undef, undef, $data);
	}

	# send:
	# R frame for the release number
	# G frame to ask for ports
	# X frame to say who we are
	# optional m frame to enable monitoring
	_sendf('R');
	_sendf('G');
	_sendf('X', $main::mycall);
	_sendf('m') if $monitor;
}

my $finishing = 0;

sub finish
{
	return if $finishing;
	if ($sock) {
		$finishing = 1;
		dbg("AGW ending...");
		for (values %circuit) {
			&{$_->{eproc}}() if $_->{eproc};
			$_->disconnect;
		}
		# say we are going
		_sendf('m') if $monitor;
		_sendf('x', $main::mycall);
		Msg->sleep(2);
		Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
		$sock->close;
	}
}

sub login
{
	goto &main::login;        # save some writing, this was the default
}

sub active
{
	return $sock;
}

sub _sendf
{
	my $sort = shift || confess "need a valid AGW command letter";
	my $from = shift || '';
	my $to   = shift || '';
	my $port = shift || 0;
	my $pid  = shift || 0;
	my $data = shift || '';
	my $len  = 0;
	
	$len = length $data; 
	if ($sort eq 'y' || $sort eq 'H') {
		dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agwpoll');
	} elsif ($sort eq 'D') {
		if (isdbg('agw')) {
			my $d = $data;
			$d =~ s/\cM$//;
			dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"") if isdbg('agw');
		}
	} else {
		dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agw');
	}
	push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
	Msg::set_event_handler($sock, write=>\&_send);
}

sub _send 
{
    return unless $sock;

    # If $flush is set, set the socket to blocking, and send all
    # messages in the queue - return only if there's an error
    # If $flush is 0 (deferred mode) make the socket non-blocking, and
    # return to the event loop only after every message, or if it
    # is likely to block in the middle of a message.

    my $offset = $send_offset;

    while (@outqueue) {
        my $msg            = $outqueue[0];
		my $mlth           = length($msg);
        my $bytes_to_write = $mlth - $offset;
        my $bytes_written  = 0;
		confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
        while ($bytes_to_write > 0) {
            $bytes_written = syswrite ($sock, $msg,
                                       $bytes_to_write, $offset);
            if (!defined($bytes_written)) {
                if (Msg::_err_will_block($!)) {
                    # Should happen only in deferred mode. Record how
                    # much we have already sent.
                    $send_offset = $offset;
                    # Event handler should already be set, so we will
                    # be called back eventually, and will resume sending
                    return 1;
                } else {    # Uh, oh
					_error();
                    return 0; # fail. Message remains in queue ..
                }
            }
			if (isdbg('raw')) {
				dbgdump('raw', "AGW send $bytes_written: ", $msg);
			}
            $total_out      += $bytes_written;
            $offset         += $bytes_written;
            $bytes_to_write -= $bytes_written;
        }
        $send_offset = $offset = 0;
        shift @outqueue;
        last;  # Go back to select and wait
		       # for it to fire again.
    }

    # Call me back if queue has not been drained.
    if (@outqueue) {
        Msg::set_event_handler ($sock, write => \&_send);
    } else {
        Msg::set_event_handler ($sock, write => undef);
    }
    1;  # Success
}

sub _rcv {                     # Complement to _send
    return unless $sock;
    my ($msg, $offset, $bytes_read);

	$bytes_read = sysread ($sock, $msg, 1024, 0);
	if (defined ($bytes_read)) {
		if ($bytes_read > 0) {
            $total_in += $bytes_read;
			$inmsg .= $msg;
			if (isdbg('raw')) {
				dbgdump('raw', "AGW read $bytes_read: ", $msg);
			}
		} 
	} else {
		if (Msg::_err_will_block($!)) {
			return; 
		} else {
			$bytes_read = 0;
		}
    }

FINISH:
    if (defined $bytes_read && $bytes_read == 0) {
		finish();
    } else {
		_decode() if length $inmsg >= 36;
	}
}

sub _error
{
	dbg("error on AGW connection $addr/$port $!");
	Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
	$sock = undef;
	for (%circuit) {
		&{$_->{eproc}}() if $_->{eproc};
		$_->disconnect;
	}
}

sub _decode
{
	return unless $sock;

	# we have at least 36 bytes of data (ugh!)
	while (length $inmsg >= 36) {
		my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
		my $data;
	
		# do a sanity check on the length
		if ($len > 2000) {
			dbg("AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')");
			finish();
			return;
		}
		if ($len == 0){
			if (length $inmsg > 36) {
				$inmsg = substr($inmsg, 36);
			} else {
				$inmsg = '';
			}
		} elsif (length $inmsg > $len + 36) {
			$data = substr($inmsg, 36, $len);
			$inmsg = substr($inmsg, $len + 36);
		} elsif (length $inmsg == $len + 36) {
			$data = substr($inmsg, 36);
			$inmsg = '';
		} else {
			#
			# we don't have enough data or something
			# or we have screwed up
			#
			return;
		}
		
		$data = '' unless defined $data;
		if ($sort eq 'D') {
			my $d = unpack "Z*", $data;
			$d =~ s/\cM\cJ?$//;
			$d =~ s/^\cJ//;
			dbg("AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
			my $conn = _find($from eq $main::mycall ? $to : $from);
			if ($conn) {
				if ($conn->{state} eq 'WC') {
					if (exists $conn->{cmd}) {
						if (@{$conn->{cmd}}) {
							dbg($d) if isdbg('connect');
							$conn->_docmd($d);
						}
					}
					if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
						$conn->to_connected($conn->{call}, 'O', $conn->{csort});
					}
				} else {
					my @lines = split /\cM\cJ?/, $d;
					push @lines, $d unless @lines;
					for (@lines) {
						&{$conn->{rproc}}($conn, "I$conn->{call}|$_");
					}
				}
			} else {
				dbg("AGW error Unsolicited Data!");
			}
		} elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
			my $d = unpack "Z*", $data;
			$d =~ s/^\cJ//;
			$d =~ s/\cM\cJ?$//;
			my @lines = split /\cM\cJ?/, $d;
			
			for (@lines) {
#				s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
				dbg("AGW Monitor port: $port \"$_\"") if isdbg('agw');
			}
		} elsif ($sort eq 'C') {
			my $d = unpack "Z*", $data;
			$d =~ s/\cM\cJ?$//;
			dbg("AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"") if isdbg('agw');
			my $call = $from eq $main::mycall ? $to : $from;
			my $conn = _find($call);
			if ($conn) {
				if ($conn->{state} eq 'WC') {
					if (exists $conn->{cmd} && @{$conn->{cmd}}) {
						$conn->_docmd($d);
						if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
							$conn->to_connected($conn->{call}, 'O', $conn->{csort});
						}
					}
				}
			} else {
				$conn = AGWMsg->new($rproc);
				$conn->{agwpid} = $pid;
				$conn->{agwport} = $port;
				$conn->{lineend} = "\cM";
				$conn->{incoming} = 1;
				$conn->{agwcall} = $call;
				$circuit{$call} = $conn;
				if (my ($c, $s) = $call =~ /^(\w+)-(\d\d?)$/) {
					$s = 15 - $s if $s > 8;
					$call = $s > 0 ? "${c}-${s}" : $c;
				}
				$conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
			}
		} elsif ($sort eq 'd') {
			my $d = unpack "Z*", $data;
			$d =~ s/\cM\cJ?$//;
			dbg("AGW '$from'->'$to' port: $port Disconnected ($d)") if isdbg('agw');
			my $conn = _find($from eq $main::mycall ? $to : $from);
			if ($conn) {
				&{$conn->{eproc}}() if $conn->{eproc};
				$conn->in_disconnect;
			}
		} elsif ($sort eq 'y') {
			my ($frames) = unpack "V", $data;
			dbg("AGW Frames Outstanding on port $port = $frames") if isdbg('agwpollans');
			my $conn = _find($from);
			$conn->{oframes} = $frames if $conn;
		} elsif ($sort eq 'Y') {
			my ($frames) = unpack "V", $data;
			dbg("AGW Frames Outstanding on circuit '$from'->'$to' = $frames") if isdbg('agw');
			my $conn = _find($from eq $main::mycall ? $to : $from);
			$conn->{oframes} = $frames if $conn;
		} elsif ($sort eq 'H') {
			unless ($from =~ /^\s+$/) {
				my $d = unpack "Z*", $data;
				$d =~ s/\cM\cJ?$//;
				dbg("AGW Heard port: $port \"$d\"") if isdbg('agw');
			}
		} elsif ($sort eq 'X') {
			my ($r) = unpack "C", $data;
			$r = $r ? "Successful" : "Failed";
			dbg("AGW Register $from $r");
			finish() unless $r;
		} elsif ($sort eq 'R') {
			my ($major, $minor) = unpack "v x2 v x2", $data;
			dbg("AGW Version $major.$minor") if isdbg('agw');
		} elsif ($sort eq 'G') {
			my @ports = split /;/, $data;
			$noports = shift @ports || '0';
			dbg("AGW $noports Ports available") if isdbg('agw');
			pop @ports while @ports > $noports;
			for (@ports) {
				next unless $_;
				dbg("AGW Port: $_") if isdbg('agw');
			}
			for (my $i = 0; $i < $noports; $i++) {
				_sendf('y', undef, undef, $i);
				_sendf('g', undef, undef, $i);
			}
		} else {
			my $d = unpack "Z*", $data;
			dbg("AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw');
		}
	}
}

sub _find
{
	my $call = shift;
	return $circuit{$call};
}

sub connect
{
	my ($conn, $line) = @_;
	
	my ($port, $call) = split /\s+/, $line;
	$conn->{agwpid} = ord "\xF0";
	$conn->{agwport} = $port - 1;
	$conn->{lineend} = "\cM";
	$conn->{incoming} = 0;
	$conn->{csort} = 'ax25';
	$conn->{agwcall} = uc $call;
	$circuit{$conn->{agwcall}} = $conn; 
	
	_sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
	$conn->{state} = 'WC';
	
	return 1;
}

sub in_disconnect
{
	my $conn = shift;
	delete $circuit{$conn->{agwcall}}; 
	$conn->SUPER::disconnect;
}

sub disconnect
{
	my $conn = shift;
	delete $circuit{$conn->{agwcall}}; 
	if ($conn->{incoming}) {
		_sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
	} else {
		_sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
	}
	$conn->SUPER::disconnect;
}

sub enqueue
{
	my ($conn, $msg) = @_;
	if ($msg =~ /^D/) {
		$msg =~ s/^[-\w]+\|//;
#		_sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
		_sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
		my $len = length($msg) + 1; 
		dbg("AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"") if isdbg('agw');
	}
}

sub process
{
	return unless $sock;
	if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
		for (my $i = 0; $i < $noports; $i++) {
			_sendf('y', undef, undef, $i );
		}
		$lastytime = $main::systime;
	}
	if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
		for (my $i = 0; $i < $noports; $i++) {
			_sendf('H', undef, undef, $i );
		}
		$lasthtime = $main::systime;
	}
}

1;

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us