3 # A thing that implements dxcluster 'protocol'
5 # This is a perl module/program that sits on the end of a dxcluster
6 # 'protocol' connection and deals with anything that might come along.
8 # this program is called by ax25d or inetd and gets raw ax25 text on its input
9 # It can also be launched into the ether by the cluster program itself for outgoing
14 # client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
16 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
18 # if there is no connection type then 'local' is assumed
20 # if there is a 'connect' keyword then it will try to launch the following program
21 # and any arguments and connect the stdin & stdout of both the program and the
24 # Copyright (c) 1998 Dirk Koopman G1TLH
31 # search local then perl directories
33 # root of directory tree for this system
35 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
37 unshift @INC, "$root/perl"; # this IS the right way round!
38 unshift @INC, "$root/local";
45 use Net::Telnet qw(TELOPT_ECHO);
51 # cease communications
55 if ($conn && $sendz) {
56 $conn->send_now("Z$call|bye...");
59 $stdout->flush if $stdout;
61 dbg('connect', "killing $pid");
65 # $SIG{__WARN__} = sub {my $a = shift; cluck($a); };
69 $conn->disconnect if $conn;
73 # terminate program from signal
82 $SIG{CHLD} = \&sig_chld;
84 dbg('connect', "caught $pid");
98 # handle incoming messages
101 my ($con, $msg, $err) = @_;
102 if (defined $err && $err) {
106 my ($sort, $call, $line) = $msg =~ /^(\w)([A-Z0-9\-]+)\|(.*)$/;
111 $snl = "" if $mode == 0;
112 if ($mode == 2 && $line =~ />$/) {
116 $line =~ s/\n/\r/og if $mode == 1;
117 #my $p = qq($line$snl);
119 if (length $outqueue >= $client_buffer_lth) {
120 print $stdout $outqueue;
123 $outqueue .= "$savenl$line$snl";
126 print $stdout $savenl, $line, $snl;;
128 $savenl = $newsavenl;
129 } elsif ($sort eq 'M') {
130 $mode = $line; # set new mode from cluster
132 } elsif ($sort eq 'E') {
133 if ($sort eq 'telnet') {
134 $mode = $line; # set echo mode from cluster
135 my $term = POSIX::Termios->new;
136 $term->getattr(fileno($sock));
137 $term->setiflag( 0 );
138 $term->setoflag( 0 );
139 $term->setattr(fileno($sock), &POSIX::TCSANOW );
141 } elsif ($sort eq 'I') {
142 ; # ignore echoed I frames
143 } elsif ($sort eq 'B') {
144 if ($buffered && $outqueue) {
145 print $stdout $outqueue;
148 $buffered = $line; # set buffered or unbuffered
149 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
165 $r = sysread($fh, $buf, 1024);
168 # $prbuf =~ s/\r/\\r/;
169 # $prbuf =~ s/\n/\\n/;
170 # print "sys: $r ($prbuf)\n";
171 if (!defined $r || $r == 0) {
175 $buf =~ s/\r/\n/og if $mode == 1;
176 $buf =~ s/\r\n/\n/og if $mode == 2;
177 $dangle = !($buf =~ /\n$/);
181 @lines = split /\n/, $buf;
183 if ($dangle) { # pull off any dangly bits
188 $first = shift @lines;
189 unshift @lines, ($lastbit . $first) if ($first);
190 foreach $first (@lines) {
191 # print "send_now $call $first\n";
192 $conn->send_later("I$call|$first");
195 $savenl = ""; # reset savenl 'cos we will have done a newline on input
197 $conn->send_later("I$call|$buf");
209 my ($sort, $line) = @_;
210 dbg('connect', "CONNECT sort: $sort command: $line");
211 if ($sort eq 'telnet') {
212 # this is a straight network connect
213 my ($host, $port) = split /\s+/, $line;
214 $port = 23 if !$port;
218 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
219 $sock->option_callback(\&optioncb);
220 $sock->output_record_separator('');
221 # $sock->option_log('option_log');
222 # $sock->dump_log('dump');
223 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
224 $sock->open($host) or die "Can't connect to $host port $port $!";
226 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
227 # or die "Can't connect to $host port $port $!";
229 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
230 my @args = split /\s+/, $line;
233 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
234 die "no receive channel $!" unless $rfh;
235 die "no transmit channel $!" unless $wfh;
236 dbg('connect', "got pid $pid");
239 die "invalid type of connection ($sort)";
247 dbg('connect', "abort $string");
254 dbg('connect', "timeout set to $val");
260 my ($expect, $send) = @_;
261 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
268 if ($csort eq 'telnet') {
269 $line = $sock->get();
270 cease(11) unless $line; # the socket has gone away?
271 $line =~ s/\r\n/\n/og;
273 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
278 if (length $line == 0) {
279 dbg('connect', "received 0 length line, aborting...");
282 dbg('connect', "received \"$line\"");
283 if ($abort && $line =~ /$abort/i) {
284 dbg('connect', "aborted on /$abort/");
287 last if $line =~ /$expect/i;
291 if ($csort eq 'telnet') {
292 $sock->print("$send\n");
293 } elsif ($csort eq 'ax25') {
295 $wfh->print("$send");
297 dbg('connect', "sent \"$send\"");
303 dbg('connect', "timed out after $timeout seconds");
307 # handle callsign and connection type firtling
311 my @f = split /\s+/, $line;
312 $call = uc $f[0] if $f[0];
313 $csort = $f[1] if $f[1];
320 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
321 $call = ""; # the callsign being used
322 $conn = 0; # the connection object for the cluster
323 $lastbit = ""; # the last bit of an incomplete input line
324 $mynl = "\n"; # standard terminator
325 $lasttime = time; # lasttime something happened on the interface
326 $outqueue = ""; # the output queue
327 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
328 $buffered = 1; # buffer output
329 $savenl = ""; # an NL that has been saved from last time
330 $timeout = 60; # default timeout for connects
331 $abort = ""; # the current abort string
332 $cpath = "$root/connect"; # the basic connect directory
334 $pid = 0; # the pid of the child program
335 $csort = ""; # the connection type
336 $sock = 0; # connection socket
349 $call = uc shift @ARGV if @ARGV;
350 $call = uc $myalias if !$call;
351 $connsort = lc shift @ARGV if @ARGV;
352 $connsort = 'local' if !$connsort;
354 $loginreq = $call eq 'LOGIN';
356 # we will do this again later 'cos things may have changed
357 $mode = ($connsort eq 'ax25') ? 1 : 2;
360 if ($call eq $mycall) {
361 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
365 $stdout->autoflush(1);
367 $SIG{'INT'} = \&sig_term;
368 $SIG{'TERM'} = \&sig_term;
369 $SIG{'HUP'} = \&sig_term;
370 $SIG{'CHLD'} = \&sig_chld;
371 $SIG{'ALRM'} = \&timeout;
375 # do we need to do a login and password job?
380 $connsort = 'telnet' if $connsort eq 'local';
383 if (-e "$data/issue") {
384 open(I, "$data/issue") or die;
388 $issue = s/\n/\r/og if $mode == 1;
390 $stdout->print($issue) if $issue;
393 # allow a login from an existing user. I could create a user but
394 # I want to check for valid callsigns and I don't have the
395 # necessary info / regular expression yet
398 $stdout->print('login: ');
401 $s = $stdin->getline();
404 $s =~ s/-\d+$//o; # no ssids!
405 cease(0) unless $s && $s gt ' ';
406 unless (iscallsign($s)) {
407 $stdout->print("Sorry, $s is an invalid callsign");
414 # is this an out going connection?
415 if ($connsort eq "connect") {
416 my $mcall = lc $call;
418 open(IN, "$cpath/$mcall") or cease(2);
428 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
429 doabort($1) if /^\s*a\w*\s+(.*)/io;
430 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
431 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
432 if (/^\s*cl\w+\s+(.*)/io) {
438 dbg('connect', "Connected to $call ($csort), starting normal protocol");
441 # if we get here we are connected
442 if ($csort eq 'ax25' || $csort eq 'prog') {
443 # open(STDIN, "<&R");
444 # open(STDOUT, ">&W");
449 $csort = 'telnet' if $csort eq 'prog';
450 } elsif ($csort eq 'telnet') {
451 # open(STDIN, "<&$sock");
452 # open(STDOUT, ">&$sock");
460 $stdout->autoflush(1);
466 $mode = ($connsort eq 'ax25') ? 1 : 2;
469 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
470 my ($scall, $ssid) = split /-/, $call;
471 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
473 $ssid = 15 if $ssid > 15;
474 if ($connsort eq 'ax25') {
479 $call = "$scall-$ssid";
483 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
485 if (-r "$data/offline") {
486 open IN, "$data/offline" or die;
488 s/\n/\r/og if $mode == 1;
493 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
498 $let = $outbound ? 'O' : 'A';
499 $conn->send_now("$let$call|$connsort");
500 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
504 Msg->event_loop(1, 1);
506 if ($t > $lasttime) {
508 print $stdout $outqueue;