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";
108 if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
110 $conn->{state} = 'WP';
111 $conn->{decho} = $conn->{echo};
113 $conn->send_raw('password: ');
115 $conn->to_connected($msg, 'A', $sort);
118 $conn->send_now("Sorry $msg is an invalid callsign");
121 } elsif ($conn->{state} eq 'WP' ) {
122 my $uref = DXUser->get_current($conn->{call});
123 $msg =~ s/[\r\n]+$//;
124 if ($uref && $msg eq $uref->passwd) {
125 my $sort = $conn->{csort};
126 $conn->{echo} = $conn->{decho};
127 delete $conn->{decho};
128 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
129 $conn->{usedpasswd} = 1;
130 $conn->to_connected($conn->{call}, 'A', $sort);
132 $conn->send_now("Sorry");
135 } elsif ($conn->{state} eq 'WC') {
136 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
138 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
139 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
149 my ($conn, $call, $dir, $sort) = @_;
150 $conn->{state} = 'C';
153 $conn->{timeout}->del if $conn->{timeout};
154 delete $conn->{timeout};
155 &{$conn->{rproc}}($conn, "$dir$call|$sort");
156 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
160 my $server_conn = shift;
161 my $sock = $server_conn->{sock}->accept();
163 my $conn = $server_conn->new($server_conn->{rproc});
164 $conn->{sock} = $sock;
165 Msg::blocking($sock, 0);
166 $conn->{blocking} = 0;
167 eval {$conn->{peerhost} = $sock->peerhost};
169 dbg($@) if isdbg('connll');
172 eval {$conn->{peerport} = $sock->peerport};
173 $conn->{peerport} = 0 if $@;
174 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
175 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
177 $conn->{eproc} = $eproc;
178 Msg::set_event_handler ($sock, "error" => $eproc);
181 $conn->{rproc} = $rproc;
182 my $callback = sub {$conn->_rcv};
183 Msg::set_event_handler ($sock, "read" => $callback);
185 $conn->{state} = 'WL';
186 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
187 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
188 # $conn->send_raw("\xFF\xFC\x01");
189 $conn->_send_file("$main::data/issue");
190 $conn->send_raw("login: ");
191 $conn->_dotimeout(60);
194 &{$conn->{eproc}}() if $conn->{eproc};
199 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
207 my $conn = ExtMsg->new(\&main::new_channel);
208 $conn->{outgoing} = 1;
211 my $f = new IO::File $fn;
212 push @{$conn->{cmd}}, <$f>;
214 $conn->{state} = 'WC';
215 $conn->_dotimeout($deftimeout);
225 while ($cmd = shift @{$conn->{cmd}}) {
227 next if $cmd =~ /^\s*\#/o;
228 next if $cmd =~ /^\s*$/o;
229 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
230 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
231 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
232 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
233 unless ($conn->_doconnect($1, $2)) {
235 @{$conn->{cmd}} = []; # empty any further commands
239 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
240 $conn->_dochat($cmd, $msg, $1, $2);
243 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
244 $conn->_doclient($1);
247 last if $conn->{state} eq 'E';
253 my ($conn, $sort, $line) = @_;
257 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
258 if ($sort eq 'telnet') {
259 # this is a straight network connect
260 my ($host, $port) = split /\s+/, $line;
261 $port = 23 if !$port;
262 $r = $conn->connect($host, $port);
264 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
266 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
268 } elsif ($sort eq 'agw') {
269 # turn it into an AGW object
270 bless $conn, 'AGWMsg';
271 $r = $conn->connect($line);
272 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
273 local $^F = 10000; # make sure it ain't closed on exec
274 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
284 $conn->{csort} = $sort;
285 $conn->{lineend} = "\cM" if $sort eq 'ax25';
287 if ($conn->{rproc}) {
288 my $callback = sub {$conn->_rcv};
289 Msg::set_event_handler ($a, read => $callback);
291 dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
298 *STDIN = IO::File->new_from_fd($b, 'r') or die;
299 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
300 *STDERR = IO::File->new_from_fd($b, 'w') or die;
302 unless ($main::is_win) {
303 # $SIG{HUP} = 'IGNORE';
304 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
307 exec "$line" or dbg("exec '$line' failed $!");
314 dbg("no socket pair $!");
317 dbg("invalid type of connection ($sort)");
319 $conn->disconnect unless $r;
327 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
328 $conn->{abort} = $string;
335 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
336 $conn->{timeout}->del if $conn->{timeout};
337 $conn->{timeval} = $val;
338 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
345 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
348 $conn->{lineend} = $val;
361 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
362 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
363 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
368 if ($line =~ /\Q$expect/i) {
370 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
371 $conn->send_later("D$conn->{call}|$send");
373 delete $conn->{msg}; # get rid any input if a match
378 $conn->{state} = 'WC';
379 unshift @{$conn->{cmd}}, $cmd;
385 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
389 # handle callsign and connection type firtling
394 my @f = split /\s+/, $line;
395 my $call = uc $f[0] if $f[0];
397 $conn->{csort} = $f[1] if $f[1];
398 $conn->{state} = 'C';
399 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
401 $conn->{timeout}->del if $conn->{timeout};
410 my $f = new IO::File $fn;
415 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
416 $conn->send_raw($l . $conn->{lineend});