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");
97 # handle incoming messages
100 my ($con, $msg, $err) = @_;
101 if (defined $err && $err) {
105 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
110 $snl = "" if $mode == 0;
111 $snl = "\r\n" if $mode == 3;
112 $snl = "\n" if $mode == 2;
113 if ($mode == 2 && $line =~ />$/) {
117 $line =~ s/\n/\r/og if $mode == 1;
118 #my $p = qq($line$snl);
120 if (length $outqueue >= $client_buffer_lth) {
121 print $stdout $outqueue;
122 pop @echo while (@echo > $maxecho);
123 push @echo, $outqueue;
126 $outqueue .= "$savenl$line$snl";
129 print $stdout $savenl, $line, $snl;;
131 $savenl = $newsavenl;
132 } elsif ($sort eq 'M') {
133 $mode = $line; # set new mode from cluster
135 } elsif ($sort eq 'E') {
136 if ($sort eq 'telnet') {
137 $mode = $line; # set echo mode from cluster
138 my $term = POSIX::Termios->new;
139 $term->getattr(fileno($sock));
140 $term->setiflag( 0 );
141 $term->setoflag( 0 );
142 $term->setattr(fileno($sock), &POSIX::TCSANOW );
144 } elsif ($sort eq 'I') {
145 ; # ignore echoed I frames
146 } elsif ($sort eq 'B') {
147 if ($buffered && $outqueue) {
148 print $stdout $outqueue;
149 pop @echo while(@echo > $maxecho);
150 push @echo, $outqueue;
153 $buffered = $line; # set buffered or unbuffered
154 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
158 # ******************************************************
159 # ******************************************************
160 # any other sorts that might happen are silently ignored.
161 # ******************************************************
162 # ******************************************************
178 $r = sysread($fh, $buf, 1024);
181 # $prbuf =~ s/\r/\\r/;
182 # $prbuf =~ s/\n/\\n/;
183 # print "sys: $r ($prbuf)\n";
184 if (!defined $r || $r == 0) {
188 $buf =~ s/\r/\n/g if $mode == 1;
189 $buf =~ s/[\r\x00]//g if $mode == 2 || $mode == 3;
191 $dangle = !($buf =~ /\n$/);
195 @lines = split /\n/, $buf;
197 if ($dangle) { # pull off any dangly bits
202 $first = shift @lines;
203 unshift @lines, ($lastbit . $first) if ($first);
204 foreach $first (@lines) {
205 # print "send_now $call $first\n";
206 next if grep {$_ eq $first } @echo;
207 $conn->send_later("I$call|$first");
210 $savenl = ""; # reset savenl 'cos we will have done a newline on input
212 $conn->send_later("I$call|$buf");
224 my ($sort, $line) = @_;
225 dbg('connect', "CONNECT sort: $sort command: $line");
226 if ($sort eq 'telnet') {
227 # this is a straight network connect
228 my ($host, $port) = split /\s+/, $line;
229 $port = 23 if !$port;
231 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
232 $sock->option_callback(\&optioncb);
233 $sock->output_record_separator('');
234 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
235 $sock->open($host) or die "Can't connect to $host port $port $!";
238 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
239 my @args = split /\s+/, $line;
242 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
243 die "no receive channel $!" unless $rfh;
244 die "no transmit channel $!" unless $wfh;
245 dbg('connect', "got pid $pid");
249 die "invalid type of connection ($sort)";
257 dbg('connect', "abort $string");
264 dbg('connect', "timeout set to $val");
270 my ($expect, $send) = @_;
271 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
278 if ($csort eq 'telnet') {
279 $line = $sock->get();
280 cease(11) unless $line; # the socket has gone away?
281 if (length $line == 0) {
282 dbg('connect', "received 0 length line, aborting...");
287 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
290 if (length $line == 0) {
291 dbg('connect', "received 0 length line, aborting...");
297 dbg('connect', map { "received \"$_\"" } split /\n/, $line);
298 if ($abort && $line =~ /$abort/i) {
299 dbg('connect', "aborted on /$abort/");
302 last if $line =~ /$expect/i;
306 if ($csort eq 'telnet') {
307 $sock->print("$send\n");
308 } elsif ($csort eq 'ax25') {
310 $wfh->print("$send");
312 dbg('connect', "sent \"$send\"");
318 dbg('connect', "timed out after $timeout seconds");
322 # handle callsign and connection type firtling
326 my @f = split /\s+/, $line;
327 $call = uc $f[0] if $f[0];
328 $csort = $f[1] if $f[1];
335 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
336 $call = ""; # the callsign being used
337 $conn = 0; # the connection object for the cluster
338 $lastbit = ""; # the last bit of an incomplete input line
339 $mynl = "\n"; # standard terminator
340 $lasttime = time; # lasttime something happened on the interface
341 $outqueue = ""; # the output queue
342 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
343 $buffered = 1; # buffer output
344 $savenl = ""; # an NL that has been saved from last time
345 $timeout = 60; # default timeout for connects
346 $abort = ""; # the current abort string
347 $cpath = "$root/connect"; # the basic connect directory
348 $maxecho = 5; # length of max echo queue
350 $pid = 0; # the pid of the child program
351 $csort = ""; # the connection type
352 $sock = 0; # connection socket
365 $call = uc shift @ARGV if @ARGV;
366 $call = uc $myalias if !$call;
367 $connsort = lc shift @ARGV if @ARGV;
368 $connsort = 'local' if !$connsort;
370 $loginreq = $call eq 'LOGIN';
372 # we will do this again later 'cos things may have changed
373 $mode = ($connsort eq 'ax25') ? 1 : 2;
376 if ($call eq $mycall) {
377 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
381 $stdout->autoflush(1);
383 $SIG{'INT'} = \&sig_term;
384 $SIG{'TERM'} = \&sig_term;
385 $SIG{'HUP'} = \&sig_term;
386 $SIG{'CHLD'} = \&sig_chld;
387 $SIG{'ALRM'} = \&timeout;
391 # do we need to do a login and password job?
396 $connsort = 'telnet' if $connsort eq 'local';
399 if (-e "$data/issue") {
400 open(I, "$data/issue") or die;
404 $issue = s/\n/\r/og if $mode == 1;
406 $stdout->print($issue) if $issue;
409 # allow a login from an existing user. I could create a user but
410 # I want to check for valid callsigns and I don't have the
411 # necessary info / regular expression yet
414 $stdout->print('login: ');
417 $s = $stdin->getline();
420 $s =~ s/-\d+$//o; # no ssids!
421 cease(0) unless $s && $s gt ' ';
422 unless (is_callsign($s)) {
423 $stdout->print("Sorry, $s is an invalid callsign");
430 # is this an out going connection?
431 if ($connsort eq "connect") {
432 my $mcall = lc $call;
434 open(IN, "$cpath/$mcall") or cease(2);
444 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
445 doabort($1) if /^\s*a\w*\s+(.*)/io;
446 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
447 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
448 if (/^\s*cl\w+\s+(.*)/io) {
454 dbg('connect', "Connected to $call ($csort), starting normal protocol");
457 # if we get here we are connected
458 if ($csort eq 'ax25' || $csort eq 'prog') {
459 # open(STDIN, "<&R");
460 # open(STDOUT, ">&W");
465 $csort = 'telnet' if $csort eq 'prog';
466 } elsif ($csort eq 'telnet') {
467 # open(STDIN, "<&$sock");
468 # open(STDOUT, ">&$sock");
476 $stdout->autoflush(1);
477 $mode = ($connsort eq 'ax25') ? 1 : $mode;
485 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
486 $call =~ s/-0$//; # strip off -0 as this is equiv to just call on its own
487 my ($scall, $ssid) = split /-/, $call;
488 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
490 $ssid = 15 if $ssid > 15;
491 if ($connsort eq 'ax25') {
496 $call = "$scall-$ssid";
500 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
502 if (-r "$data/offline") {
503 open IN, "$data/offline" or die;
505 s/\n/\r/og if $mode == 1;
510 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
515 $let = $outbound ? 'O' : 'A';
516 $conn->send_now("$let$call|$connsort");
517 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
521 Msg->event_loop(1, 1);
523 if ($t > $lasttime) {
525 print $stdout $outqueue;