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";
46 use Net::Telnet qw(TELOPT_ECHO TELOPT_BINARY);
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 unless ($^O =~ /^MS/i) {
83 $SIG{CHLD} = \&sig_chld;
85 dbg('connect', "caught $waitedpid");
97 $out_lineend = "\r\n";
100 $out_lineend = $mynl;
103 # handle incoming messages
106 my ($con, $msg, $err) = @_;
107 if (defined $err && $err) {
111 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
116 $snl = "" if $mode == 0;
117 $snl = "\r\n" if $mode == 3;
118 $snl = "\n" if $mode == 2;
119 if ($mode == 2 && $line =~ />$/) {
123 $line =~ s/\n/\r/og if $mode == 1;
124 #my $p = qq($line$snl);
126 if (length $outqueue >= $client_buffer_lth) {
127 print $stdout $outqueue;
128 pop @echo while (@echo > $maxecho);
129 push @echo, $outqueue;
132 $outqueue .= "$savenl$line$snl";
135 print $stdout $savenl, $line, $snl;;
137 $savenl = $newsavenl;
138 } elsif ($sort eq 'M') {
139 $mode = $line; # set new mode from cluster
141 } elsif ($sort eq 'E') {
142 if ($sort eq 'telnet') {
143 $mode = $line; # set echo mode from cluster
144 my $term = POSIX::Termios->new;
145 $term->getattr(fileno($sock));
146 $term->setiflag( 0 );
147 $term->setoflag( 0 );
148 $term->setattr(fileno($sock), &POSIX::TCSANOW );
150 } elsif ($sort eq 'I') {
151 ; # ignore echoed I frames
152 } elsif ($sort eq 'B') {
153 if ($buffered && $outqueue) {
154 print $stdout $outqueue;
155 pop @echo while(@echo > $maxecho);
156 push @echo, $outqueue;
159 $buffered = $line; # set buffered or unbuffered
160 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
164 # ******************************************************
165 # ******************************************************
166 # any other sorts that might happen are silently ignored.
167 # ******************************************************
168 # ******************************************************
184 $r = sysread($fh, $buf, 1024);
187 # $prbuf =~ s/\r/\\r/;
188 # $prbuf =~ s/\n/\\n/;
189 # print "sys: $r ($prbuf)\n";
190 if (!defined $r || $r == 0) {
194 $buf =~ s/\r/\n/g if $mode == 1;
195 $buf =~ s/[\r\x00]//g if $mode == 2 || $mode == 3;
197 $dangle = !($buf =~ /\n$/);
201 @lines = split /\n/, $buf;
203 if ($dangle) { # pull off any dangly bits
208 $first = shift @lines;
209 unshift @lines, ($lastbit . $first) if ($first);
210 foreach $first (@lines) {
211 # print "send_now $call $first\n";
212 next if grep {$_ eq $first } @echo;
213 $conn->send_later("I$call|$first");
216 $savenl = ""; # reset savenl 'cos we will have done a newline on input
218 $conn->send_later("I$call|$buf");
230 my ($sort, $line) = @_;
231 dbg('connect', "CONNECT sort: $sort command: $line");
232 if ($sort eq 'telnet') {
233 # this is a straight network connect
234 my ($host, $port) = split /\s+/, $line;
235 $port = 23 if !$port;
237 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
238 $sock->option_callback(\&optioncb);
239 $sock->output_record_separator('');
240 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
241 $sock->open($host) or die "Can't connect to $host port $port $!";
243 $sock->telnetmode(1);
244 $sock->option_send(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO) if $port == 23;
246 $sock->telnetmode(0);
250 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
251 my @args = split /\s+/, $line;
254 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
255 die "no receive channel $!" unless $rfh;
256 die "no transmit channel $!" unless $wfh;
257 dbg('connect', "got pid $pid");
261 die "invalid type of connection ($sort)";
269 dbg('connect', "abort $string");
276 dbg('connect', "timeout set to $val");
284 $out_lineend =~ s/\\r/\r/g;
285 $out_lineend =~ s/\\n/\n/g;
286 dbg('connect', "lineend set to $val ");
287 $out_lineend = $mynl unless $out_lineend;
292 my ($expect, $send) = @_;
293 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
300 if ($csort eq 'telnet') {
301 $line = $sock->get();
302 cease(11) unless $line; # the socket has gone away?
303 if (length $line == 0) {
304 dbg('connect', "received 0 length line, aborting...");
309 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
312 if (length $line == 0) {
313 dbg('connect', "received 0 length line, aborting...");
319 dbg('connect', map { "received \"$_\"" } split /\n/, $line);
320 if ($abort && $line =~ /$abort/i) {
321 dbg('connect', "aborted on /$abort/");
324 last if $line =~ /$expect/i;
328 if ($csort eq 'telnet') {
329 # local $\ = $out_lineend;
330 $sock->print("$send\n");
331 } elsif ($csort eq 'ax25') {
332 local $\ = $out_lineend;
333 $wfh->print("$send");
335 dbg('connect', "sent \"$send\"");
341 dbg('connect', "timed out after $timeout seconds");
345 # handle callsign and connection type firtling
349 my @f = split /\s+/, $line;
350 $call = uc $f[0] if $f[0];
351 $csort = $f[1] if $f[1];
358 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
359 $call = ""; # the callsign being used
360 $conn = 0; # the connection object for the cluster
361 $lastbit = ""; # the last bit of an incomplete input line
362 $mynl = "\n"; # standard terminator
363 $lasttime = time; # lasttime something happened on the interface
364 $outqueue = ""; # the output queue
365 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
366 $buffered = 1; # buffer output
367 $savenl = ""; # an NL that has been saved from last time
368 $timeout = 60; # default timeout for connects
369 $abort = ""; # the current abort string
370 $cpath = "$root/connect"; # the basic connect directory
371 $maxecho = 5; # length of max echo queue
373 $pid = 0; # the pid of the child program
374 $csort = ""; # the connection type
375 $sock = 0; # connection socket
376 $out_lineend = $mynl; # connection lineending (used for outgoing connects)
389 $call = uc shift @ARGV if @ARGV;
390 $call = uc $myalias if !$call;
391 $connsort = lc shift @ARGV if @ARGV;
392 $connsort = 'local' if !$connsort;
394 $loginreq = $call eq 'LOGIN';
396 # we will do this again later 'cos things may have changed
397 $mode = ($connsort eq 'ax25') ? 1 : 2;
400 if ($call eq $mycall) {
401 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
405 $stdout->autoflush(1);
407 unless ($^O =~ /^MS/i) {
408 $SIG{'INT'} = \&sig_term;
409 $SIG{'TERM'} = \&sig_term;
410 $SIG{'HUP'} = \&sig_term;
411 $SIG{'CHLD'} = \&sig_chld;
416 # do we need to do a login and password job?
421 $connsort = 'telnet' if $connsort eq 'local';
424 if (-e "$data/issue") {
425 open(I, "$data/issue") or die;
429 $issue = s/\n/\r/og if $mode == 1;
431 $stdout->print($issue) if $issue;
434 # allow a login from an existing user. I could create a user but
435 # I want to check for valid callsigns and I don't have the
436 # necessary info / regular expression yet
439 $stdout->print('login: ');
442 $s = $stdin->getline();
445 $s =~ s/-\d+$//o; # no ssids!
446 cease(0) unless $s && $s gt ' ';
447 unless (is_callsign($s)) {
448 $stdout->print("Sorry, $s is an invalid callsign");
455 # is this an out going connection?
456 if ($connsort eq "connect") {
457 my $mcall = lc $call;
459 open(IN, "$cpath/$mcall") or cease(2);
469 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
470 doabort($1) if /^\s*a\w*\s+(.*)/io;
471 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
472 dolineend($1) if /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/;
473 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
475 if (/^\s*cl\w+\s+(.*)/io) {
481 dbg('connect', "Connected to $call ($csort), starting normal protocol");
484 # if we get here we are connected
485 if ($csort eq 'ax25' || $csort eq 'prog') {
486 # open(STDIN, "<&R");
487 # open(STDOUT, ">&W");
492 $csort = 'telnet' if $csort eq 'prog';
493 } elsif ($csort eq 'telnet') {
494 # open(STDIN, "<&$sock");
495 # open(STDOUT, ">&$sock");
503 $stdout->autoflush(1);
504 $mode = ($connsort eq 'ax25') ? 1 : $mode;
512 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
513 $call =~ s/-0$//; # strip off -0 as this is equiv to just call on its own
514 my ($scall, $ssid) = split /-/, $call;
515 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
517 $ssid = 15 if $ssid > 15;
518 if ($connsort eq 'ax25') {
523 $call = "$scall-$ssid";
527 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
529 if (-r "$data/offline") {
530 open IN, "$data/offline" or die;
532 s/\n/\r/og if $mode == 1;
537 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
542 $let = $outbound ? 'O' : 'A';
543 $conn->send_now("$let$call|$connsort");
544 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
548 Msg->event_loop(1, 0.1);
550 if ($t > $lasttime) {
552 print $stdout $outqueue;