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', $msg) unless $conn->{state} eq 'C';
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->{msg});
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', $msg) unless $conn->{state} eq 'C';
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 $conn->to_connected($msg, 'A', $conn->{csort});
95 $conn->send_now("Sorry $msg is an invalid callsign");
98 } elsif ($conn->{state} eq 'WC') {
99 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
101 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
102 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
112 my ($conn, $call, $dir, $sort) = @_;
113 $conn->{state} = 'C';
116 $conn->{timeout}->del if $conn->{timeout};
117 delete $conn->{timeout};
118 $conn->_send_file("$main::data/connected");
119 &{$conn->{rproc}}($conn, "$dir$call|$sort");
123 my $server_conn = shift;
124 my $sock = $server_conn->{sock}->accept();
126 my $conn = $server_conn->new($server_conn->{rproc});
127 $conn->{sock} = $sock;
129 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
131 $conn->{eproc} = $eproc;
132 Msg::set_event_handler ($sock, "error" => $eproc);
135 $conn->{rproc} = $rproc;
136 my $callback = sub {$conn->_rcv};
137 Msg::set_event_handler ($sock, "read" => $callback);
139 $conn->{state} = 'WL';
140 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
141 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
142 # $conn->send_raw("\xFF\xFC\x01");
143 $conn->_send_file("$main::data/issue");
144 $conn->send_raw("login: ");
145 $conn->_dotimeout(60);
147 &{$conn->{eproc}}() if $conn->{eproc};
151 dbg('err', "ExtMsg: error on accept ($!)");
159 my $conn = ExtMsg->new(\&main::new_channel);
162 my $f = new IO::File $fn;
163 push @{$conn->{cmd}}, <$f>;
165 $conn->{state} = 'WC';
166 $conn->_dotimeout($deftimeout);
176 while ($cmd = shift @{$conn->{cmd}}) {
178 next if $cmd =~ /^\s*\#/o;
179 next if $cmd =~ /^\s*$/o;
180 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
181 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
182 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
183 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
184 unless ($conn->_doconnect($1, $2)) {
186 @{$conn->{cmd}} = []; # empty any further commands
190 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
191 $conn->_dochat($cmd, $msg);
194 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
195 $conn->_doclient($1);
198 last if $conn->{state} eq 'E';
204 my ($conn, $sort, $line) = @_;
208 dbg('connect', "CONNECT sort: $sort command: $line");
209 if ($sort eq 'telnet') {
210 # this is a straight network connect
211 my ($host, $port) = split /\s+/, $line;
212 $port = 23 if !$port;
213 $r = $conn->connect($host, $port);
215 dbg('connect', "Connected to $host $port");
217 dbg('connect', "***Connect Failed to $host $port $!");
219 } elsif ($sort eq 'agw') {
220 # turn it into an AGW object
221 bless $conn, 'AGWMsg';
222 $r = $conn->connect($line);
223 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
224 local $^F = 10000; # make sure it ain't closed on exec
225 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
235 $conn->{csort} = $sort;
236 $conn->{lineend} = "\cM" if $sort eq 'ax25';
238 if ($conn->{rproc}) {
239 my $callback = sub {$conn->_rcv};
240 Msg::set_event_handler ($a, read => $callback);
242 dbg('connect', "started pid: $conn->{pid} as $line");
249 *STDIN = IO::File->new_from_fd($b, 'r') or die;
250 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
251 *STDERR = IO::File->new_from_fd($b, 'w') or die;
253 unless ($^O =~ /^MS/) {
254 # $SIG{HUP} = 'IGNORE';
255 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
258 exec "$line" or dbg('err', "exec '$line' failed $!");
261 dbg('err', "cannot fork");
265 dbg('err', "no socket pair $!");
268 dbg('err', "invalid type of connection ($sort)");
270 $conn->disconnect unless $r;
278 dbg('connect', "abort $string");
279 $conn->{abort} = $string;
286 dbg('connect', "timeout set to $val");
287 $conn->{timeout}->del if $conn->{timeout};
288 $conn->{timeval} = $val;
289 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
296 dbg('connect', "lineend set to $val ");
299 $conn->{lineend} = $val;
309 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
311 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
312 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
313 dbg('connect', "aborted on /$conn->{abort}/");
318 if ($line =~ /\Q$expect/i) {
319 dbg('connect', "got: \"$expect\" sending: \"$send\"");
320 $conn->send_later("D$conn->{call}|$send");
321 delete $conn->{msg}; # get rid any input if a match
326 $conn->{state} = 'WC';
327 unshift @{$conn->{cmd}}, $cmd;
333 dbg('connect', "timed out after $conn->{timeval} seconds");
334 $conn->{timeout}->del;
335 delete $conn->{timeout};
339 # handle callsign and connection type firtling
344 my @f = split /\s+/, $line;
345 my $call = uc $f[0] if $f[0];
347 $conn->{csort} = $f[1] if $f[1];
348 $conn->{state} = 'C';
349 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
351 $conn->{timeout}->del if $conn->{timeout};
360 my $f = new IO::File $fn;
364 $conn->send_raw($_ . $conn->{lineend});