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;
72 if ($conn->ax25 && exists $conn->{msg}) {
73 $conn->{msg} =~ s/\cM/\cJ/g;
75 if ($conn->{state} eq 'WC') {
76 if (exists $conn->{cmd}) {
77 if (@{$conn->{cmd}}) {
78 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
79 $conn->_docmd($conn->{msg});
82 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
83 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
85 } elsif ($conn->{msg} =~ /\cJ/) {
86 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
87 if ($conn->{msg} =~ /\cJ$/) {
90 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
92 while (defined ($msg = shift @lines)) {
93 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
95 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
96 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
98 if ($conn->{state} eq 'C') {
99 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
100 } elsif ($conn->{state} eq 'WL' ) {
102 if ($conn->{sort} =~ /^I/ && (my ($ip, $from) = $msg =~ /^PROXY TCP[46] ([\da-fA-F:\.]+) ([\da-fA-F:\.]+)/) ) {
103 # SOMEONE appears to have affixed an HA Proxy to my connection
104 $ip =~ s|^::ffff:||; # chop off leading pseudo IPV6 stuff on dual stack listeners
105 $from =~ s|^::ffff:||;
106 if ($from eq $conn->{peerhost}) {
107 dbg("ExtMsg: connect - PROXY IP change from '$conn->{peerhost}' -> '$ip'");
108 $conn->{peerhost} = $ip;
110 dbg("ExtMsg: connect - PROXY someone ($from) is trying to spoof '$ip'");
111 $conn->send_now("Sorry $msg is an invalid callsign");
114 } elsif (is_callsign($msg)) {
115 if ($main::allowslashcall || $msg !~ m|/|) {
116 my $sort = $conn->{csort};
117 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
119 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
121 $conn->{state} = 'WP';
122 $conn->{decho} = $conn->{echo};
124 $conn->send_raw('password: ');
126 $conn->to_connected($msg, 'A', $sort);
129 $conn->send_now("Sorry $msg is an invalid callsign");
133 $conn->send_now("Sorry $msg is an invalid callsign");
136 } elsif ($conn->{state} eq 'WP' ) {
137 my $uref = DXUser::get_current($conn->{call});
138 $msg =~ s/[\r\n]+$//;
139 if ($uref && $msg eq $uref->passwd) {
140 my $sort = $conn->{csort};
141 $conn->{echo} = $conn->{decho};
142 delete $conn->{decho};
143 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
144 $conn->{usedpasswd} = 1;
145 $conn->to_connected($conn->{call}, 'A', $sort);
147 $conn->send_now("Sorry");
150 } elsif ($conn->{state} eq 'WC') {
151 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
153 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
154 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
164 my ($conn, $call, $dir, $sort) = @_;
165 $conn->{state} = 'C';
168 $conn->{timeout}->del if $conn->{timeout};
169 delete $conn->{timeout};
170 $conn->{csort} = $sort;
171 &{$conn->{rproc}}($conn, "$dir$call|$sort");
172 $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
177 my $server_conn = shift;
179 my $conn = $server_conn->SUPER::new_client($client);
181 $conn->{state} = 'WL';
182 $conn->_send_file(localdata("issue"));
183 $conn->send_raw("login: ");
184 $conn->_dotimeout(60);
192 my $conn = ExtMsg->new(\&main::new_channel);
193 $conn->{outgoing} = 1;
196 my $f = new IO::File $fn;
197 push @{$conn->{cmd}}, <$f>;
199 $conn->{state} = 'WC';
200 $conn->_dotimeout($deftimeout);
210 while ($cmd = shift @{$conn->{cmd}}) {
212 next if $cmd =~ /^\s*\#/o;
213 next if $cmd =~ /^\s*$/o;
214 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
215 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
216 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
217 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
218 unless ($conn->_doconnect($1, $2)) {
220 @{$conn->{cmd}} = []; # empty any further commands
224 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
225 $conn->_dochat($cmd, $msg, $1, $2);
228 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
229 $conn->_doclient($1);
232 last if $conn->{state} eq 'E';
238 my ($conn, $sort, $line) = @_;
242 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
243 if ($sort eq 'telnet') {
244 # this is a straight network connect
245 my ($host, $port) = split /\s+/, $line;
246 $port = 23 if !$port;
247 $r = $conn->connect($host, $port);
249 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
251 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
253 } elsif ($sort eq 'agw') {
254 # turn it into an AGW object
255 bless $conn, 'AGWMsg';
256 $r = $conn->connect($line);
257 } elsif ($sort eq 'bpq') {
258 # turn it into an BPQ object
259 bless $conn, 'BPQMsg';
260 $r = $conn->connect($line);
261 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
262 $r = $conn->start_program($line, $sort);
264 dbg("invalid type of connection ($sort)");
266 $conn->disconnect unless $r;
274 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
275 $conn->{abort} = $string;
282 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
283 $conn->{timeout}->del if $conn->{timeout};
284 $conn->{timeval} = $val;
285 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
292 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
295 $conn->{lineend} = $val;
308 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
309 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
310 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
315 if ($line =~ /\Q$expect/i) {
317 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
318 $conn->send_later("D$conn->{call}|$send");
320 delete $conn->{msg}; # get rid any input if a match
325 $conn->{state} = 'WC';
326 unshift @{$conn->{cmd}}, $cmd;
332 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
336 # handle callsign and connection type firtling
341 my @f = split /\s+/, $line;
342 my $call = uc $f[0] if $f[0];
344 $conn->{csort} = $f[1] if $f[1];
345 $conn->{state} = 'C';
346 eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
347 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
349 $conn->{timeout}->del if $conn->{timeout};
358 my $f = new IO::File $fn;
363 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
364 $conn->send_raw($l . $conn->{lineend});