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(@ISA $deftimeout);
31 my ($conn, $msg) = @_;
32 unless ($msg =~ /^[ABZ]/) {
33 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
36 # $conn->send_raw("\xFF\xFC\x01");
38 # $conn->send_raw("\xFF\xFB\x01");
41 $msg =~ s/^[-\w]+\|//;
42 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
49 my ($conn, $msg) = @_;
50 my $sock = $conn->{sock};
51 return unless defined($sock);
52 push (@{$conn->{outqueue}}, $msg);
53 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
54 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
62 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
63 $conn->{msg} =~ s/\cM/\cJ/g;
65 if ($conn->{state} eq 'WC') {
66 if (exists $conn->{cmd}) {
67 if (@{$conn->{cmd}}) {
68 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
69 $conn->_docmd($conn->{msg});
72 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
73 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
75 } elsif ($conn->{msg} =~ /\cJ/) {
76 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
77 if ($conn->{msg} =~ /\cJ$/) {
80 $conn->{msg} = pop @lines;
82 while (defined ($msg = shift @lines)) {
83 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
85 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
86 $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
88 if ($conn->{state} eq 'C') {
89 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
90 } elsif ($conn->{state} eq 'WL' ) {
92 if (is_callsign($msg)) {
93 my $sort = $conn->{csort};
94 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
95 $conn->to_connected($msg, 'A', $sort);
97 $conn->send_now("Sorry $msg is an invalid callsign");
100 } elsif ($conn->{state} eq 'WC') {
101 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
103 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
104 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
114 my ($conn, $call, $dir, $sort) = @_;
115 $conn->{state} = 'C';
118 $conn->{timeout}->del if $conn->{timeout};
119 delete $conn->{timeout};
120 &{$conn->{rproc}}($conn, "$dir$call|$sort");
121 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
125 my $server_conn = shift;
126 my $sock = $server_conn->{sock}->accept();
128 my $conn = $server_conn->new($server_conn->{rproc});
129 $conn->{sock} = $sock;
130 Msg::blocking($sock, 0);
131 $conn->{blocking} = 0;
132 eval {$conn->{peerhost} = $sock->peerhost};
134 dbg($@) if isdbg('connll');
137 eval {$conn->{peerport} = $sock->peerport};
138 $conn->{peerport} = 0 if $@;
139 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
140 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
142 $conn->{eproc} = $eproc;
143 Msg::set_event_handler ($sock, "error" => $eproc);
146 $conn->{rproc} = $rproc;
147 my $callback = sub {$conn->_rcv};
148 Msg::set_event_handler ($sock, "read" => $callback);
150 $conn->{state} = 'WL';
151 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
152 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
153 # $conn->send_raw("\xFF\xFC\x01");
154 $conn->_send_file("$main::data/issue");
155 $conn->send_raw("login: ");
156 $conn->_dotimeout(60);
158 &{$conn->{eproc}}() if $conn->{eproc};
163 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
171 my $conn = ExtMsg->new(\&main::new_channel);
172 $conn->{outgoing} = 1;
175 my $f = new IO::File $fn;
176 push @{$conn->{cmd}}, <$f>;
178 $conn->{state} = 'WC';
179 $conn->_dotimeout($deftimeout);
189 while ($cmd = shift @{$conn->{cmd}}) {
191 next if $cmd =~ /^\s*\#/o;
192 next if $cmd =~ /^\s*$/o;
193 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
194 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
195 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
196 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
197 unless ($conn->_doconnect($1, $2)) {
199 @{$conn->{cmd}} = []; # empty any further commands
203 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
204 $conn->_dochat($cmd, $msg);
207 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
208 $conn->_doclient($1);
211 last if $conn->{state} eq 'E';
217 my ($conn, $sort, $line) = @_;
221 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
222 if ($sort eq 'telnet') {
223 # this is a straight network connect
224 my ($host, $port) = split /\s+/, $line;
225 $port = 23 if !$port;
226 $r = $conn->connect($host, $port);
228 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
230 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
232 } elsif ($sort eq 'agw') {
233 # turn it into an AGW object
234 bless $conn, 'AGWMsg';
235 $r = $conn->connect($line);
236 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
237 local $^F = 10000; # make sure it ain't closed on exec
238 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
248 $conn->{csort} = $sort;
249 $conn->{lineend} = "\cM" if $sort eq 'ax25';
251 if ($conn->{rproc}) {
252 my $callback = sub {$conn->_rcv};
253 Msg::set_event_handler ($a, read => $callback);
255 dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
262 *STDIN = IO::File->new_from_fd($b, 'r') or die;
263 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
264 *STDERR = IO::File->new_from_fd($b, 'w') or die;
266 unless ($main::is_win) {
267 # $SIG{HUP} = 'IGNORE';
268 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
271 exec "$line" or dbg("exec '$line' failed $!");
278 dbg("no socket pair $!");
281 dbg("invalid type of connection ($sort)");
283 $conn->disconnect unless $r;
291 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
292 $conn->{abort} = $string;
299 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
300 $conn->{timeout}->del if $conn->{timeout};
301 $conn->{timeval} = $val;
302 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
309 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
312 $conn->{lineend} = $val;
322 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
324 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
325 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
326 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
331 if ($line =~ /\Q$expect/i) {
333 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
334 $conn->send_later("D$conn->{call}|$send");
336 delete $conn->{msg}; # get rid any input if a match
341 $conn->{state} = 'WC';
342 unshift @{$conn->{cmd}}, $cmd;
348 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
352 # handle callsign and connection type firtling
357 my @f = split /\s+/, $line;
358 my $call = uc $f[0] if $f[0];
360 $conn->{csort} = $f[1] if $f[1];
361 $conn->{state} = 'C';
362 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
364 $conn->{timeout}->del if $conn->{timeout};
373 my $f = new IO::File $fn;
378 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
379 $conn->send_raw($l . $conn->{lineend});