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 TELOPT_BINARY);
50 # cease communications
54 # if ($conn && $sendz) {
55 # $conn->send_now("Z$call|bye...");
58 $stdout->flush if $stdout;
60 dbg('connect', "killing $pid");
64 # $SIG{__WARN__} = sub {my $a = shift; cluck($a); };
68 $conn->disconnect if $conn;
72 # terminate program from signal
81 $SIG{CHLD} = \&sig_chld;
83 dbg('connect', "caught $pid");
94 $out_lineend = "\r\n";
100 # handle incoming messages
103 my ($con, $msg, $err) = @_;
104 if (defined $err && $err) {
108 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
113 $snl = "" if $mode == 0;
114 $snl = "\r\n" if $mode == 3;
115 $snl = "\n" if $mode == 2;
116 if ($mode == 2 && $line =~ />$/) {
120 $line =~ s/\n/\r/og if $mode == 1;
121 #my $p = qq($line$snl);
123 if (length $outqueue >= $client_buffer_lth) {
124 print $stdout $outqueue;
125 pop @echo while (@echo > $maxecho);
126 push @echo, $outqueue;
129 $outqueue .= "$savenl$line$snl";
132 print $stdout $savenl, $line, $snl;;
134 $savenl = $newsavenl;
135 } elsif ($sort eq 'M') {
136 $mode = $line; # set new mode from cluster
138 } elsif ($sort eq 'E') {
139 if ($sort eq 'telnet') {
140 $mode = $line; # set echo mode from cluster
141 my $term = POSIX::Termios->new;
142 $term->getattr(fileno($sock));
143 $term->setiflag( 0 );
144 $term->setoflag( 0 );
145 $term->setattr(fileno($sock), &POSIX::TCSANOW );
147 } elsif ($sort eq 'I') {
148 ; # ignore echoed I frames
149 } elsif ($sort eq 'B') {
150 if ($buffered && $outqueue) {
151 print $stdout $outqueue;
152 pop @echo while(@echo > $maxecho);
153 push @echo, $outqueue;
156 $buffered = $line; # set buffered or unbuffered
157 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
161 # ******************************************************
162 # ******************************************************
163 # any other sorts that might happen are silently ignored.
164 # ******************************************************
165 # ******************************************************
181 $r = sysread($fh, $buf, 1024);
184 # $prbuf =~ s/\r/\\r/;
185 # $prbuf =~ s/\n/\\n/;
186 # print "sys: $r ($prbuf)\n";
187 if (!defined $r || $r == 0) {
191 $buf =~ s/\r/\n/g if $mode == 1;
192 $buf =~ s/[\r\x00]//g if $mode == 2 || $mode == 3;
194 $dangle = !($buf =~ /\n$/);
198 @lines = split /\n/, $buf;
200 if ($dangle) { # pull off any dangly bits
205 $first = shift @lines;
206 unshift @lines, ($lastbit . $first) if ($first);
207 foreach $first (@lines) {
208 # print "send_now $call $first\n";
209 next if grep {$_ eq $first } @echo;
210 $conn->send_later("I$call|$first");
213 $savenl = ""; # reset savenl 'cos we will have done a newline on input
215 $conn->send_later("I$call|$buf");
227 my ($sort, $line) = @_;
228 dbg('connect', "CONNECT sort: $sort command: $line");
229 if ($sort eq 'telnet') {
230 # this is a straight network connect
231 my ($host, $port) = split /\s+/, $line;
232 $port = 23 if !$port;
234 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
235 $sock->option_callback(\&optioncb);
236 $sock->output_record_separator('');
237 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
238 $sock->open($host) or die "Can't connect to $host port $port $!";
240 $sock->telnetmode(1);
241 $sock->option_send(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO) if $port == 23;
243 $sock->telnetmode(0);
247 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
248 my @args = split /\s+/, $line;
251 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
252 die "no receive channel $!" unless $rfh;
253 die "no transmit channel $!" unless $wfh;
254 dbg('connect', "got pid $pid");
258 die "invalid type of connection ($sort)";
266 dbg('connect', "abort $string");
273 dbg('connect', "timeout set to $val");
281 $out_lineend =~ s/\\r/\r/g;
282 $out_lineend =~ s/\\n/\n/g;
283 dbg('connect', "lineend set to $val ");
284 $out_lineend = $mynl unless $out_lineend;
289 my ($expect, $send) = @_;
290 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
297 if ($csort eq 'telnet') {
298 $line = $sock->get();
299 cease(11) unless $line; # the socket has gone away?
300 if (length $line == 0) {
301 dbg('connect', "received 0 length line, aborting...");
306 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
309 if (length $line == 0) {
310 dbg('connect', "received 0 length line, aborting...");
316 dbg('connect', map { "received \"$_\"" } split /\n/, $line);
317 if ($abort && $line =~ /$abort/i) {
318 dbg('connect', "aborted on /$abort/");
321 last if $line =~ /$expect/i;
325 if ($csort eq 'telnet') {
326 # local $\ = $out_lineend;
327 $sock->print("$send\n");
328 } elsif ($csort eq 'ax25') {
329 local $\ = $out_lineend;
330 $wfh->print("$send");
332 dbg('connect', "sent \"$send\"");
338 dbg('connect', "timed out after $timeout seconds");
342 # handle callsign and connection type firtling
346 my @f = split /\s+/, $line;
347 $call = uc $f[0] if $f[0];
348 $csort = $f[1] if $f[1];
355 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
356 $call = ""; # the callsign being used
357 $conn = 0; # the connection object for the cluster
358 $lastbit = ""; # the last bit of an incomplete input line
359 $mynl = "\n"; # standard terminator
360 $lasttime = time; # lasttime something happened on the interface
361 $outqueue = ""; # the output queue
362 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
363 $buffered = 1; # buffer output
364 $savenl = ""; # an NL that has been saved from last time
365 $timeout = 60; # default timeout for connects
366 $abort = ""; # the current abort string
367 $cpath = "$root/connect"; # the basic connect directory
368 $maxecho = 5; # length of max echo queue
370 $pid = 0; # the pid of the child program
371 $csort = ""; # the connection type
372 $sock = 0; # connection socket
373 $out_lineend = $mynl; # connection lineending (used for outgoing connects)
386 $call = uc shift @ARGV if @ARGV;
387 $call = uc $myalias if !$call;
388 $connsort = lc shift @ARGV if @ARGV;
389 $connsort = 'local' if !$connsort;
391 $loginreq = $call eq 'LOGIN';
393 # we will do this again later 'cos things may have changed
394 $mode = ($connsort eq 'ax25') ? 1 : 2;
397 if ($call eq $mycall) {
398 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
402 $stdout->autoflush(1);
404 $SIG{'INT'} = \&sig_term;
405 $SIG{'TERM'} = \&sig_term;
406 $SIG{'HUP'} = \&sig_term;
407 $SIG{'CHLD'} = \&sig_chld;
408 $SIG{'ALRM'} = \&timeout;
412 # do we need to do a login and password job?
417 $connsort = 'telnet' if $connsort eq 'local';
420 if (-e "$data/issue") {
421 open(I, "$data/issue") or die;
425 $issue = s/\n/\r/og if $mode == 1;
427 $stdout->print($issue) if $issue;
430 # allow a login from an existing user. I could create a user but
431 # I want to check for valid callsigns and I don't have the
432 # necessary info / regular expression yet
435 $stdout->print('login: ');
438 $s = $stdin->getline();
441 $s =~ s/-\d+$//o; # no ssids!
442 cease(0) unless $s && $s gt ' ';
443 unless (is_callsign($s)) {
444 $stdout->print("Sorry, $s is an invalid callsign");
451 # is this an out going connection?
452 if ($connsort eq "connect") {
453 my $mcall = lc $call;
455 open(IN, "$cpath/$mcall") or cease(2);
465 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
466 doabort($1) if /^\s*a\w*\s+(.*)/io;
467 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
468 dolineend($1) if /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/;
469 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
471 if (/^\s*cl\w+\s+(.*)/io) {
477 dbg('connect', "Connected to $call ($csort), starting normal protocol");
480 # if we get here we are connected
481 if ($csort eq 'ax25' || $csort eq 'prog') {
482 # open(STDIN, "<&R");
483 # open(STDOUT, ">&W");
488 $csort = 'telnet' if $csort eq 'prog';
489 } elsif ($csort eq 'telnet') {
490 # open(STDIN, "<&$sock");
491 # open(STDOUT, ">&$sock");
499 $stdout->autoflush(1);
500 $mode = ($connsort eq 'ax25') ? 1 : $mode;
508 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
509 $call =~ s/-0$//; # strip off -0 as this is equiv to just call on its own
510 my ($scall, $ssid) = split /-/, $call;
511 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
513 $ssid = 15 if $ssid > 15;
514 if ($connsort eq 'ax25') {
519 $call = "$scall-$ssid";
523 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
525 if (-r "$data/offline") {
526 open IN, "$data/offline" or die;
528 s/\n/\r/og if $mode == 1;
533 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
538 $let = $outbound ? 'O' : 'A';
539 $conn->send_now("$let$call|$connsort");
540 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
544 Msg->event_loop(1, 1);
546 if ($t > $lasttime) {
548 print $stdout $outqueue;