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)});
68 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
69 $conn->{msg} =~ s/\cM/\cJ/g;
71 if ($conn->{state} eq 'WC') {
72 if (exists $conn->{cmd}) {
73 if (@{$conn->{cmd}}) {
74 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
75 $conn->_docmd($conn->{msg});
78 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
79 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
81 } elsif ($conn->{msg} =~ /\cJ/) {
82 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
83 if ($conn->{msg} =~ /\cJ$/) {
86 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
88 while (defined ($msg = shift @lines)) {
89 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
91 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
92 $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
94 if ($conn->{state} eq 'C') {
95 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
96 } elsif ($conn->{state} eq 'WL' ) {
98 if (is_callsign($msg)) {
99 my $sort = $conn->{csort};
100 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
101 $conn->to_connected($msg, 'A', $sort);
103 $conn->send_now("Sorry $msg is an invalid callsign");
106 } elsif ($conn->{state} eq 'WC') {
107 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
109 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
110 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
120 my ($conn, $call, $dir, $sort) = @_;
121 $conn->{state} = 'C';
124 $conn->{timeout}->del if $conn->{timeout};
125 delete $conn->{timeout};
126 &{$conn->{rproc}}($conn, "$dir$call|$sort");
127 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
131 my $server_conn = shift;
132 my $sock = $server_conn->{sock}->accept();
134 my $conn = $server_conn->new($server_conn->{rproc});
135 $conn->{sock} = $sock;
136 Msg::blocking($sock, 0);
137 $conn->{blocking} = 0;
138 eval {$conn->{peerhost} = $sock->peerhost};
140 dbg($@) if isdbg('connll');
143 eval {$conn->{peerport} = $sock->peerport};
144 $conn->{peerport} = 0 if $@;
145 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
146 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
148 $conn->{eproc} = $eproc;
149 Msg::set_event_handler ($sock, "error" => $eproc);
152 $conn->{rproc} = $rproc;
153 my $callback = sub {$conn->_rcv};
154 Msg::set_event_handler ($sock, "read" => $callback);
156 $conn->{state} = 'WL';
157 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
158 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
159 # $conn->send_raw("\xFF\xFC\x01");
160 $conn->_send_file("$main::data/issue");
161 $conn->send_raw("login: ");
162 $conn->_dotimeout(60);
164 &{$conn->{eproc}}() if $conn->{eproc};
169 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
177 my $conn = ExtMsg->new(\&main::new_channel);
178 $conn->{outgoing} = 1;
181 my $f = new IO::File $fn;
182 push @{$conn->{cmd}}, <$f>;
184 $conn->{state} = 'WC';
185 $conn->_dotimeout($deftimeout);
195 while ($cmd = shift @{$conn->{cmd}}) {
197 next if $cmd =~ /^\s*\#/o;
198 next if $cmd =~ /^\s*$/o;
199 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
200 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
201 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
202 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
203 unless ($conn->_doconnect($1, $2)) {
205 @{$conn->{cmd}} = []; # empty any further commands
209 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
210 $conn->_dochat($cmd, $msg, $1, $2);
213 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
214 $conn->_doclient($1);
217 last if $conn->{state} eq 'E';
223 my ($conn, $sort, $line) = @_;
227 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
228 if ($sort eq 'telnet') {
229 # this is a straight network connect
230 my ($host, $port) = split /\s+/, $line;
231 $port = 23 if !$port;
232 $r = $conn->connect($host, $port);
234 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
236 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
238 } elsif ($sort eq 'agw') {
239 # turn it into an AGW object
240 bless $conn, 'AGWMsg';
241 $r = $conn->connect($line);
242 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
243 local $^F = 10000; # make sure it ain't closed on exec
244 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
254 $conn->{csort} = $sort;
255 $conn->{lineend} = "\cM" if $sort eq 'ax25';
257 if ($conn->{rproc}) {
258 my $callback = sub {$conn->_rcv};
259 Msg::set_event_handler ($a, read => $callback);
261 dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
268 *STDIN = IO::File->new_from_fd($b, 'r') or die;
269 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
270 *STDERR = IO::File->new_from_fd($b, 'w') or die;
272 unless ($main::is_win) {
273 # $SIG{HUP} = 'IGNORE';
274 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
277 exec "$line" or dbg("exec '$line' failed $!");
284 dbg("no socket pair $!");
287 dbg("invalid type of connection ($sort)");
289 $conn->disconnect unless $r;
297 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
298 $conn->{abort} = $string;
305 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
306 $conn->{timeout}->del if $conn->{timeout};
307 $conn->{timeval} = $val;
308 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
315 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
318 $conn->{lineend} = $val;
331 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
332 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
333 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
338 if ($line =~ /\Q$expect/i) {
340 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
341 $conn->send_later("D$conn->{call}|$send");
343 delete $conn->{msg}; # get rid any input if a match
348 $conn->{state} = 'WC';
349 unshift @{$conn->{cmd}}, $cmd;
355 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
359 # handle callsign and connection type firtling
364 my @f = split /\s+/, $line;
365 my $call = uc $f[0] if $f[0];
367 $conn->{csort} = $f[1] if $f[1];
368 $conn->{state} = 'C';
369 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
371 $conn->{timeout}->del if $conn->{timeout};
380 my $f = new IO::File $fn;
385 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
386 $conn->send_raw($l . $conn->{lineend});