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
28 use vars qw(@ISA $deftimeout);
35 goto &main::login; # save some writing, this was the default
40 my ($conn, $msg) = @_;
41 unless ($msg =~ /^[ABZ]/) {
42 if ($msg =~ m{^E[-\w\/]+\|([01])} && $conn->{csort} eq 'telnet') {
45 # $conn->send_raw("\xFF\xFC\x01");
47 # $conn->send_raw("\xFF\xFB\x01");
50 $msg =~ s{^[-\w\/]+\|}{};
51 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
58 my ($conn, $msg) = @_;
59 dbg((ref $conn) . " connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
60 $conn->SUPER::send_raw($msg);
66 $conn->{echo} = shift;
71 my $conn = shift; # $rcv_now complement of $flush
73 my $sock = $conn->{sock};
74 return unless defined($sock);
75 return if $conn->{disconnecting};
77 if ($conn->{state} eq 'WL' && $conn->{sort} =~ /^I/ && $msg =~ /^PROXY/) {
78 my $echo = $conn->{echo};
80 $conn->SUPER::_rcv($msg);
81 $conn->{echo} = $echo;
83 $conn->SUPER::_rcv($msg);
92 if ($conn->ax25 && exists $conn->{msg}) {
93 $conn->{msg} =~ s/\cM/\cJ/g;
95 if ($conn->{state} eq 'WC') {
96 if (exists $conn->{cmd}) {
97 if (@{$conn->{cmd}}) {
98 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
99 $conn->_docmd($conn->{msg});
102 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
103 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
105 } elsif ($conn->{msg} =~ /\cJ/) {
106 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
107 if ($conn->{msg} =~ /\cJ$/) {
110 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
112 $conn->{linesin} += @lines;
113 $Msg::total_lines_in += @lines;
114 while (defined ($msg = shift @lines)) {
115 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
117 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
118 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
120 if ($conn->{state} eq 'C') {
121 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
122 } elsif ($conn->{state} eq 'WL' ) {
124 if ($conn->{sort} =~ /^I/ && (my ($ip, $from) = $msg =~ /^PROXY TCP[46] ([\da-fA-F:\.]+) ([\da-fA-F:\.]+)/) ) {
125 # SOMEONE appears to have affixed an HA Proxy to my connection
126 $ip =~ s|^::ffff:||; # chop off leading pseudo IPV6 stuff on dual stack listeners
127 $from =~ s|^::ffff:||;
128 if ($from eq $conn->{peerhost}) {
129 dbg("ExtMsg: connect - PROXY IP change from '$conn->{peerhost}' -> '$ip'");
130 $conn->{peerhost} = $ip;
132 dbg("ExtMsg: connect - PROXY someone ($from) is trying to spoof '$ip'");
133 $conn->send_now("Sorry $msg is an invalid callsign");
136 } elsif (is_callsign($msg)) {
137 if ($main::allowslashcall || $msg !~ m|/|) {
138 my $sort = $conn->{csort};
139 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
141 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
143 $conn->{state} = 'WP';
144 $conn->{decho} = $conn->{echo};
146 $conn->send_raw('password: ');
148 $conn->to_connected($msg, 'A', $sort);
151 $conn->send_now("Sorry $msg is an invalid callsign");
155 $conn->send_now("Sorry $msg is an invalid callsign");
158 } elsif ($conn->{state} eq 'WP' ) {
159 my $uref = DXUser::get_current($conn->{call});
160 $msg =~ s/[\r\n]+$//;
161 if ($uref && $msg eq $uref->passwd) {
162 my $sort = $conn->{csort};
163 $conn->{echo} = $conn->{decho};
164 delete $conn->{decho};
165 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
166 $conn->{usedpasswd} = 1;
167 $conn->to_connected($conn->{call}, 'A', $sort);
169 $conn->send_now("Sorry");
172 } elsif ($conn->{state} eq 'WC') {
173 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
175 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
176 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
186 my ($conn, $call, $dir, $sort) = @_;
187 $conn->{state} = 'C';
190 $conn->{timeout}->del if $conn->{timeout};
191 delete $conn->{timeout};
192 $conn->{csort} = $sort;
193 &{$conn->{rproc}}($conn, "$dir$call|$sort");
194 $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
199 my $server_conn = shift;
201 my $conn = $server_conn->SUPER::new_client($client);
203 $conn->{state} = 'WL';
204 $conn->_send_file(localdata("issue"));
205 $conn->send_raw("login: ");
206 $conn->_dotimeout(60);
214 my $conn = ExtMsg->new(\&main::new_channel);
215 $conn->{outgoing} = 1;
218 my $f = new IO::File $fn;
219 push @{$conn->{cmd}}, <$f>;
221 $conn->{state} = 'WC';
222 $conn->_dotimeout($deftimeout);
232 while ($cmd = shift @{$conn->{cmd}}) {
234 next if $cmd =~ /^\s*\#/o;
235 next if $cmd =~ /^\s*$/o;
236 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
237 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
238 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
239 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
240 unless ($conn->_doconnect($1, $2)) {
242 @{$conn->{cmd}} = []; # empty any further commands
246 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
247 $conn->_dochat($cmd, $msg, $1, $2);
250 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
251 $conn->_doclient($1);
254 last if $conn->{state} eq 'E';
260 my ($conn, $sort, $line) = @_;
264 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
265 if ($sort eq 'telnet') {
266 # this is a straight network connect
267 my ($host, $port) = split /\s+/, $line;
268 $port = 23 if !$port;
269 $r = $conn->connect($host, $port);
271 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
273 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
275 } elsif ($sort eq 'agw') {
276 # turn it into an AGW object
277 bless $conn, 'AGWMsg';
278 $r = $conn->connect($line);
279 } elsif ($sort eq 'bpq') {
280 # turn it into an BPQ object
281 bless $conn, 'BPQMsg';
282 $r = $conn->connect($line);
283 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
284 $r = $conn->start_program($line, $sort);
286 dbg("invalid type of connection ($sort)");
288 $conn->disconnect unless $r;
296 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
297 $conn->{abort} = $string;
304 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
305 $conn->{timeout}->del if $conn->{timeout};
306 $conn->{timeval} = $val;
307 $conn->{timeout} = DXTimer->new($val, sub{ &_timedout($conn) });
314 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
317 $conn->{lineend} = $val;
330 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
331 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
332 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
337 if ($line =~ /\Q$expect/i) {
339 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
340 $conn->send_later("D$conn->{call}|$send");
342 delete $conn->{msg}; # get rid any input if a match
347 $conn->{state} = 'WC';
348 unshift @{$conn->{cmd}}, $cmd;
354 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
358 # handle callsign and connection type firtling
363 my @f = split /\s+/, $line;
364 my $call = uc $f[0] if $f[0];
366 $conn->{csort} = $f[1] if $f[1];
367 $conn->{state} = 'C';
368 eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
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});