2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
5 # This is where the cluster handles direct connections coming both in
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 use vars qw(@ISA $deftimeout);
37 my ($conn, $msg) = @_;
38 unless ($msg =~ /^[ABZ]/) {
39 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
42 # $conn->send_raw("\xFF\xFC\x01");
44 # $conn->send_raw("\xFF\xFB\x01");
47 $msg =~ s/^[-\w]+\|//;
48 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
55 my ($conn, $msg) = @_;
56 my $sock = $conn->{sock};
57 return unless defined($sock);
58 push (@{$conn->{outqueue}}, $msg);
59 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
60 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
66 $conn->{echo} = shift;
74 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
75 $conn->{msg} =~ s/\cM/\cJ/g;
77 if ($conn->{state} eq 'WC') {
78 if (exists $conn->{cmd}) {
79 if (@{$conn->{cmd}}) {
80 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
81 $conn->_docmd($conn->{msg});
84 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
85 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
87 } elsif ($conn->{msg} =~ /\cJ/) {
88 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
89 if ($conn->{msg} =~ /\cJ$/) {
92 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
94 while (defined ($msg = shift @lines)) {
95 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
97 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
98 $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
100 if ($conn->{state} eq 'C') {
101 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
102 } elsif ($conn->{state} eq 'WL' ) {
104 if (is_callsign($msg) && $msg !~ m|/| ) {
105 my $sort = $conn->{csort};
106 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
107 $conn->to_connected($msg, 'A', $sort);
109 $conn->send_now("Sorry $msg is an invalid callsign");
112 } elsif ($conn->{state} eq 'WC') {
113 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
115 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
116 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
126 my ($conn, $call, $dir, $sort) = @_;
127 $conn->{state} = 'C';
130 $conn->{timeout}->del if $conn->{timeout};
131 delete $conn->{timeout};
132 &{$conn->{rproc}}($conn, "$dir$call|$sort");
133 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
137 my $server_conn = shift;
138 my $sock = $server_conn->{sock}->accept();
140 my $conn = $server_conn->new($server_conn->{rproc});
141 $conn->{sock} = $sock;
142 Msg::blocking($sock, 0);
143 $conn->{blocking} = 0;
144 eval {$conn->{peerhost} = $sock->peerhost};
146 dbg($@) if isdbg('connll');
149 eval {$conn->{peerport} = $sock->peerport};
150 $conn->{peerport} = 0 if $@;
151 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
152 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
154 $conn->{eproc} = $eproc;
155 Msg::set_event_handler ($sock, "error" => $eproc);
158 $conn->{rproc} = $rproc;
159 my $callback = sub {$conn->_rcv};
160 Msg::set_event_handler ($sock, "read" => $callback);
162 $conn->{state} = 'WL';
163 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
164 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
165 # $conn->send_raw("\xFF\xFC\x01");
166 $conn->_send_file("$main::data/issue");
167 $conn->send_raw("login: ");
168 $conn->_dotimeout(60);
171 &{$conn->{eproc}}() if $conn->{eproc};
176 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
184 my $conn = ExtMsg->new(\&main::new_channel);
185 $conn->{outgoing} = 1;
188 my $f = new IO::File $fn;
189 push @{$conn->{cmd}}, <$f>;
191 $conn->{state} = 'WC';
192 $conn->_dotimeout($deftimeout);
202 while ($cmd = shift @{$conn->{cmd}}) {
204 next if $cmd =~ /^\s*\#/o;
205 next if $cmd =~ /^\s*$/o;
206 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
207 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
208 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
209 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
210 unless ($conn->_doconnect($1, $2)) {
212 @{$conn->{cmd}} = []; # empty any further commands
216 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
217 $conn->_dochat($cmd, $msg, $1, $2);
220 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
221 $conn->_doclient($1);
224 last if $conn->{state} eq 'E';
230 my ($conn, $sort, $line) = @_;
234 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
235 if ($sort eq 'telnet') {
236 # this is a straight network connect
237 my ($host, $port) = split /\s+/, $line;
238 $port = 23 if !$port;
239 $r = $conn->connect($host, $port);
241 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
243 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
245 } elsif ($sort eq 'agw') {
246 # turn it into an AGW object
247 bless $conn, 'AGWMsg';
248 $r = $conn->connect($line);
249 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
250 local $^F = 10000; # make sure it ain't closed on exec
251 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
261 $conn->{csort} = $sort;
262 $conn->{lineend} = "\cM" if $sort eq 'ax25';
264 if ($conn->{rproc}) {
265 my $callback = sub {$conn->_rcv};
266 Msg::set_event_handler ($a, read => $callback);
268 dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
275 *STDIN = IO::File->new_from_fd($b, 'r') or die;
276 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
277 *STDERR = IO::File->new_from_fd($b, 'w') or die;
279 unless ($main::is_win) {
280 # $SIG{HUP} = 'IGNORE';
281 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
284 exec "$line" or dbg("exec '$line' failed $!");
291 dbg("no socket pair $!");
294 dbg("invalid type of connection ($sort)");
296 $conn->disconnect unless $r;
304 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
305 $conn->{abort} = $string;
312 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
313 $conn->{timeout}->del if $conn->{timeout};
314 $conn->{timeval} = $val;
315 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
322 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
325 $conn->{lineend} = $val;
338 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
339 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
340 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
345 if ($line =~ /\Q$expect/i) {
347 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
348 $conn->send_later("D$conn->{call}|$send");
350 delete $conn->{msg}; # get rid any input if a match
355 $conn->{state} = 'WC';
356 unshift @{$conn->{cmd}}, $cmd;
362 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
366 # handle callsign and connection type firtling
371 my @f = split /\s+/, $line;
372 my $call = uc $f[0] if $f[0];
374 $conn->{csort} = $f[1] if $f[1];
375 $conn->{state} = 'C';
376 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
378 $conn->{timeout}->del if $conn->{timeout};
387 my $f = new IO::File $fn;
392 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
393 $conn->send_raw($l . $conn->{lineend});