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
9 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 # Modified Jan 2006 by John Wiseman G8BPQ to support connections to BPQ32 node,
12 # and fix pattern matching on 'chat' abort handling
26 use vars qw(@ISA $deftimeout);
33 goto &main::login; # save some writing, this was the default
38 my ($conn, $msg) = @_;
39 unless ($msg =~ /^[ABZ]/) {
40 if ($msg =~ m{^E[-\w\/]+\|([01])} && $conn->{csort} eq 'telnet') {
43 # $conn->send_raw("\xFF\xFC\x01");
45 # $conn->send_raw("\xFF\xFB\x01");
48 $msg =~ s{^[-\w\/]+\|}{};
49 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
56 my ($conn, $msg) = @_;
57 dbg((ref $conn) . " connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
58 $conn->SUPER::send_raw($msg);
64 $conn->{echo} = shift;
69 my $conn = shift; # $rcv_now complement of $flush
71 my $sock = $conn->{sock};
72 return unless defined($sock);
73 return if $conn->{disconnecting};
75 if ($conn->{state} eq 'WL' && $conn->{sort} =~ /^I/ && $msg =~ /^PROXY/) {
76 my $echo = $conn->{echo};
78 $conn->SUPER::_rcv($msg);
79 $conn->{echo} = $echo;
81 $conn->SUPER::_rcv($msg);
90 if ($conn->ax25 && exists $conn->{msg}) {
91 $conn->{msg} =~ s/\cM/\cJ/g;
93 if ($conn->{state} eq 'WC') {
94 if (exists $conn->{cmd}) {
95 if (@{$conn->{cmd}}) {
96 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
97 $conn->_docmd($conn->{msg});
100 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
101 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
103 } elsif ($conn->{msg} =~ /\cJ/) {
104 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
105 if ($conn->{msg} =~ /\cJ$/) {
108 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
110 $conn->{linesin} += @lines;
111 $Msg::total_lines_in += @lines;
112 while (defined ($msg = shift @lines)) {
113 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
115 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
116 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
118 if ($conn->{state} eq 'C') {
119 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
120 } elsif ($conn->{state} eq 'WL' ) {
122 if ($conn->{sort} =~ /^I/ && (my ($ip, $from) = $msg =~ /^PROXY TCP[46] ([\da-fA-F:\.]+) ([\da-fA-F:\.]+)/) ) {
123 # SOMEONE appears to have affixed an HA Proxy to my connection
124 $ip =~ s|^::ffff:||; # chop off leading pseudo IPV6 stuff on dual stack listeners
125 $from =~ s|^::ffff:||;
126 if ($from eq $conn->{peerhost}) {
127 dbg("ExtMsg: connect - PROXY IP change from '$conn->{peerhost}' -> '$ip'");
128 $conn->{peerhost} = $ip;
130 dbg("ExtMsg: connect - PROXY someone ($from) is trying to spoof '$ip'");
131 $conn->send_now("Sorry $msg is an invalid callsign");
134 } elsif (is_callsign($msg)) {
135 if ($main::allowslashcall || $msg !~ m|/|) {
136 my $sort = $conn->{csort};
137 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
139 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
141 $conn->{state} = 'WP';
142 $conn->{decho} = $conn->{echo};
144 $conn->send_raw('password: ');
146 $conn->to_connected($msg, 'A', $sort);
149 $conn->send_now("Sorry $msg is an invalid callsign");
153 $conn->send_now("Sorry $msg is an invalid callsign");
156 } elsif ($conn->{state} eq 'WP' ) {
157 my $uref = DXUser::get_current($conn->{call});
158 $msg =~ s/[\r\n]+$//;
159 if ($uref && $msg eq $uref->passwd) {
160 my $sort = $conn->{csort};
161 $conn->{echo} = $conn->{decho};
162 delete $conn->{decho};
163 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
164 $conn->{usedpasswd} = 1;
165 $conn->to_connected($conn->{call}, 'A', $sort);
167 $conn->send_now("Sorry");
170 } elsif ($conn->{state} eq 'WC') {
171 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
173 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
174 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
184 my ($conn, $call, $dir, $sort) = @_;
185 $conn->{state} = 'C';
188 $conn->{timeout}->del if $conn->{timeout};
189 delete $conn->{timeout};
190 $conn->{csort} = $sort;
191 &{$conn->{rproc}}($conn, "$dir$call|$sort");
192 $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
197 my $server_conn = shift;
199 my $conn = $server_conn->SUPER::new_client($client);
201 $conn->{state} = 'WL';
202 $conn->_send_file(localdata("issue"));
203 $conn->send_raw("login: ");
204 $conn->_dotimeout(60);
212 my $conn = ExtMsg->new(\&main::new_channel);
213 $conn->{outgoing} = 1;
216 my $f = new IO::File $fn;
217 push @{$conn->{cmd}}, <$f>;
219 $conn->{state} = 'WC';
220 $conn->_dotimeout($deftimeout);
230 while ($cmd = shift @{$conn->{cmd}}) {
232 next if $cmd =~ /^\s*\#/o;
233 next if $cmd =~ /^\s*$/o;
234 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
235 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
236 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
237 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
238 unless ($conn->_doconnect($1, $2)) {
240 @{$conn->{cmd}} = []; # empty any further commands
244 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
245 $conn->_dochat($cmd, $msg, $1, $2);
248 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
249 $conn->_doclient($1);
252 last if $conn->{state} eq 'E';
258 my ($conn, $sort, $line) = @_;
262 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
263 if ($sort eq 'telnet') {
264 # this is a straight network connect
265 my ($host, $port) = split /\s+/, $line;
266 $port = 23 if !$port;
267 $r = $conn->connect($host, $port);
269 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
271 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
273 } elsif ($sort eq 'agw') {
274 # turn it into an AGW object
275 bless $conn, 'AGWMsg';
276 $r = $conn->connect($line);
277 } elsif ($sort eq 'bpq') {
278 # turn it into an BPQ object
279 bless $conn, 'BPQMsg';
280 $r = $conn->connect($line);
281 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
282 $r = $conn->start_program($line, $sort);
284 dbg("invalid type of connection ($sort)");
286 $conn->disconnect unless $r;
294 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
295 $conn->{abort} = $string;
302 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
303 $conn->{timeout}->del if $conn->{timeout};
304 $conn->{timeval} = $val;
305 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
312 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
315 $conn->{lineend} = $val;
328 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
329 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
330 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
335 if ($line =~ /\Q$expect/i) {
337 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
338 $conn->send_later("D$conn->{call}|$send");
340 delete $conn->{msg}; # get rid any input if a match
345 $conn->{state} = 'WC';
346 unshift @{$conn->{cmd}}, $cmd;
352 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
356 # handle callsign and connection type firtling
361 my @f = split /\s+/, $line;
362 my $call = uc $f[0] if $f[0];
364 $conn->{csort} = $f[1] if $f[1];
365 $conn->{state} = 'C';
366 eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
367 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
369 $conn->{timeout}->del if $conn->{timeout};
378 my $f = new IO::File $fn;
383 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
384 $conn->send_raw($l . $conn->{lineend});