re-arrange ExtMsg a bit to cope with $sock->peerhost and port failures
[spider.git] / perl / ExtMsg.pm
1 #
2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
4 #
5 # This is where the cluster handles direct connections coming both in
6 # and out
7 #
8 # $Id$
9 #
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 #
12
13 package ExtMsg;
14
15 use strict;
16 use Msg;
17 use DXVars;
18 use DXUtil;
19 use DXDebug;
20 use IO::File;
21 use IO::Socket;
22 use IPC::Open3;
23
24 use vars qw(@ISA $deftimeout);
25
26 @ISA = qw(Msg);
27 $deftimeout = 60;
28
29 sub enqueue
30 {
31         my ($conn, $msg) = @_;
32         unless ($msg =~ /^[ABZ]/) {
33                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
34                         $conn->{echo} = $1;
35                         if ($1) {
36 #                               $conn->send_raw("\xFF\xFC\x01");
37                         } else {
38 #                               $conn->send_raw("\xFF\xFB\x01");
39                         }
40                 } else {
41                         $msg =~ s/^[-\w]+\|//;
42                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
43                 }
44         }
45 }
46
47 sub send_raw
48 {
49         my ($conn, $msg) = @_;
50     my $sock = $conn->{sock};
51     return unless defined($sock);
52         push (@{$conn->{outqueue}}, $msg);
53         dbg('connect', "connect $conn->{cnum}: $msg") unless $conn->{state} eq 'C';
54     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
55 }
56
57 sub dequeue
58 {
59         my $conn = shift;
60         my $msg;
61
62         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
63                 $conn->{msg} =~ s/\cM/\cJ/g;
64         }
65         if ($conn->{state} eq 'WC') {
66                 if (exists $conn->{cmd}) {
67                         if (@{$conn->{cmd}}) {
68                                 dbg('connect', "connect $conn->{cnum}: $conn->{msg}");
69                                 $conn->_docmd($conn->{msg});
70                         } 
71                 }
72                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
73                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
74                 }
75         } elsif ($conn->{msg} =~ /\cJ/) {
76                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
77                 if ($conn->{msg} =~ /\cJ$/) {
78                         delete $conn->{msg};
79                 } else {
80                         $conn->{msg} = pop @lines;
81                 }
82                 while (defined ($msg = shift @lines)) {
83                         dbg('connect', "connect $conn->{cnum}: $msg") unless $conn->{state} eq 'C';
84                 
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
87                         
88                         if ($conn->{state} eq 'C') {
89                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
90                         } elsif ($conn->{state} eq 'WL' ) {
91                                 $msg = uc $msg;
92                                 if (is_callsign($msg)) {
93                                         my $sort = $conn->{csort};
94                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
95                                         $conn->to_connected($msg, 'A', $sort);
96                                 } else {
97                                         $conn->send_now("Sorry $msg is an invalid callsign");
98                                         $conn->disconnect;
99                                 }
100                         } elsif ($conn->{state} eq 'WC') {
101                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
102                                         $conn->_docmd($msg);
103                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
104                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
105                                         }
106                                 }
107                         }
108                 }
109         }
110 }
111
112 sub to_connected
113 {
114         my ($conn, $call, $dir, $sort) = @_;
115         $conn->{state} = 'C';
116         $conn->conns($call);
117         delete $conn->{cmd};
118         $conn->{timeout}->del if $conn->{timeout};
119         delete $conn->{timeout};
120         &{$conn->{rproc}}($conn, "$dir$call|$sort");
121         $conn->_send_file("$main::data/connected");
122 }
123
124 sub new_client {
125         my $server_conn = shift;
126     my $sock = $server_conn->{sock}->accept();
127         if ($sock) {
128                 my $conn = $server_conn->new($server_conn->{rproc});
129                 $conn->{sock} = $sock;
130                 Msg::blocking($sock, 0);
131                 $conn->{blocking} = 0;
132                 eval {$conn->{peerhost} = $sock->peerhost};
133                 if ($@) {
134                         dbg('conn', $@);
135                         $conn->disconnect;
136                 } else {
137                         eval {$conn->{peerport} = $sock->peerport};
138                         $conn->{peerport} = 0 if $@;
139                         my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
140                         dbg('connll', "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}");
141                         if ($eproc) {
142                                 $conn->{eproc} = $eproc;
143                                 Msg::set_event_handler ($sock, "error" => $eproc);
144                         }
145                         if ($rproc) {
146                                 $conn->{rproc} = $rproc;
147                                 my $callback = sub {$conn->_rcv};
148                                 Msg::set_event_handler ($sock, "read" => $callback);
149                                 # send login prompt
150                                 $conn->{state} = 'WL';
151                                 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
152                                 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
153                                 #               $conn->send_raw("\xFF\xFC\x01");
154                                 $conn->_send_file("$main::data/issue");
155                                 $conn->send_raw("login: ");
156                                 $conn->_dotimeout(60);
157                         } else { 
158                                 &{$conn->{eproc}}() if $conn->{eproc};
159                                 $conn->disconnect();
160                         }
161                 }
162         } else {
163                 dbg('err', "ExtMsg: error on accept ($!)");
164         }
165 }
166
167 sub start_connect
168 {
169         my $call = shift;
170         my $fn = shift;
171         my $conn = ExtMsg->new(\&main::new_channel); 
172         $conn->conns($call);
173         
174         my $f = new IO::File $fn;
175         push @{$conn->{cmd}}, <$f>;
176         $f->close;
177         $conn->{state} = 'WC';
178         $conn->_dotimeout($deftimeout);
179         $conn->_docmd;
180 }
181
182 sub _docmd
183 {
184         my $conn = shift;
185         my $msg = shift;
186         my $cmd;
187
188         while ($cmd = shift @{$conn->{cmd}}) {
189                 chomp $cmd;
190                 next if $cmd =~ /^\s*\#/o;
191                 next if $cmd =~ /^\s*$/o;
192                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
193                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
194                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
195                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
196                         unless ($conn->_doconnect($1, $2)) {
197                                 $conn->disconnect;
198                                 @{$conn->{cmd}} = [];    # empty any further commands
199                                 last;
200                         }  
201                 }
202                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
203                         $conn->_dochat($cmd, $msg);
204                         last;
205                 }
206                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
207                         $conn->_doclient($1);
208                         last;
209                 }
210                 last if $conn->{state} eq 'E';
211         }
212 }
213
214 sub _doconnect
215 {
216         my ($conn, $sort, $line) = @_;
217         my $r;
218
219         $sort = lc $sort;
220         dbg('connect', "CONNECT $conn->{cnum} sort: $sort command: $line");
221         if ($sort eq 'telnet') {
222                 # this is a straight network connect
223                 my ($host, $port) = split /\s+/, $line;
224                 $port = 23 if !$port;
225                 $r = $conn->connect($host, $port);
226                 if ($r) {
227                         dbg('connect', "Connected $conn->{cnum} to $host $port");
228                 } else {
229                         dbg('connect', "***Connect $conn->{cnum} Failed to $host $port $!");
230                 }
231         } elsif ($sort eq 'agw') {
232                 # turn it into an AGW object
233                 bless $conn, 'AGWMsg';
234                 $r = $conn->connect($line);
235         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
236                 local $^F = 10000;              # make sure it ain't closed on exec
237                 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
238                 if ($a && $b) {
239                         $r = 1;
240                         $a->autoflush(1);
241                         $b->autoflush(1);
242                         my $pid = fork;
243                         if (defined $pid) {
244                                 if ($pid) {
245                                         close $b;
246                                         $conn->{sock} = $a;
247                                         $conn->{csort} = $sort;
248                                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
249                                         $conn->{pid} = $pid;
250                                         if ($conn->{rproc}) {
251                                                 my $callback = sub {$conn->_rcv};
252                                                 Msg::set_event_handler ($a, read => $callback);
253                                         }
254                                         dbg('connect', "connect $conn->{cnum}: started pid: $conn->{pid} as $line");
255                                 } else {
256                                         $^W = 0;
257                                         dbgclose();
258                                         STDIN->close;
259                                         STDOUT->close;
260                                         STDOUT->close;
261                                         *STDIN = IO::File->new_from_fd($b, 'r') or die;
262                                         *STDOUT = IO::File->new_from_fd($b, 'w') or die;
263                                         *STDERR = IO::File->new_from_fd($b, 'w') or die;
264                                         close $a;
265                                         unless ($main::is_win) {
266 #                                               $SIG{HUP} = 'IGNORE';
267                                                 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
268                                                 alarm(0);
269                                         }
270                                         exec "$line" or dbg('err', "exec '$line' failed $!");
271                                 } 
272                         } else {
273                                 dbg('err', "cannot fork");      
274                                 $r = undef;
275                         }
276                 } else {
277                         dbg('err', "no socket pair $!");
278                 }
279         } else {
280                 dbg('err', "invalid type of connection ($sort)");
281         }
282         $conn->disconnect unless $r;
283         return $r;
284 }
285
286 sub _doabort
287 {
288         my $conn = shift;
289         my $string = shift;
290         dbg('connect', "connect $conn->{cnum}: abort $string");
291         $conn->{abort} = $string;
292 }
293
294 sub _dotimeout
295 {
296         my $conn = shift;
297         my $val = shift;
298         dbg('connect', "connect $conn->{cnum}: timeout set to $val");
299         $conn->{timeout}->del if $conn->{timeout};
300         $conn->{timeval} = $val;
301         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
302 }
303
304 sub _dolineend
305 {
306         my $conn = shift;
307         my $val = shift;
308         dbg('connect', "connect $conn->{cnum}: lineend set to $val ");
309         $val =~ s/\\r/\r/g;
310         $val =~ s/\\n/\n/g;
311         $conn->{lineend} = $val;
312 }
313
314 sub _dochat
315 {
316         my $conn = shift;
317         my $cmd = shift;
318         my $line = shift;
319                 
320         if ($line) {
321                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
322                 if ($expect) {
323                         dbg('connect', "connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"");
324                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
325                                 dbg('connect', "connect $conn->{cnum}: aborted on /$conn->{abort}/");
326                                 $conn->disconnect;
327                                 delete $conn->{cmd};
328                                 return;
329                         }
330                         if ($line =~ /\Q$expect/i) {
331                                 if (length $send) {
332                                         dbg('connect', "connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"");
333                                         $conn->send_later("D$conn->{call}|$send");
334                                 }
335                                 delete $conn->{msg}; # get rid any input if a match
336                                 return;
337                         }
338                 }
339         }
340         $conn->{state} = 'WC';
341         unshift @{$conn->{cmd}}, $cmd;
342 }
343
344 sub _timedout
345 {
346         my $conn = shift;
347         dbg('connect', "connect $conn->{cnum}: timed out after $conn->{timeval} seconds");
348         $conn->disconnect;
349 }
350
351 # handle callsign and connection type firtling
352 sub _doclient
353 {
354         my $conn = shift;
355         my $line = shift;
356         my @f = split /\s+/, $line;
357         my $call = uc $f[0] if $f[0];
358         $conn->conns($call);
359         $conn->{csort} = $f[1] if $f[1];
360         $conn->{state} = 'C';
361         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
362         delete $conn->{cmd};
363         $conn->{timeout}->del if $conn->{timeout};
364 }
365
366 sub _send_file
367 {
368         my $conn = shift;
369         my $fn = shift;
370         
371         if (-e $fn) {
372                 my $f = new IO::File $fn;
373                 if ($f) {
374                         while (<$f>) {
375                                 chomp;
376                                 my $l = $_;
377                                 dbg('connll', "connect $conn->{cnum}: $l");
378                                 $conn->send_raw($l . $conn->{lineend});
379                         }
380                         $f->close;
381                 }
382         }
383 }