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);
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 == 2;
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 .....
153 # ******************************************************
154 # ******************************************************
155 # any other sorts that might happen are silently ignored.
156 # ******************************************************
157 # ******************************************************
171 $r = sysread($fh, $buf, 1024);
174 # $prbuf =~ s/\r/\\r/;
175 # $prbuf =~ s/\n/\\n/;
176 # print "sys: $r ($prbuf)\n";
177 if (!defined $r || $r == 0) {
181 $buf =~ s/\r/\n/g if $mode == 1;
182 $buf =~ s/[\r\x00]//g if $mode == 2;
184 $dangle = !($buf =~ /\n$/);
188 @lines = split /\n/, $buf;
190 if ($dangle) { # pull off any dangly bits
195 $first = shift @lines;
196 unshift @lines, ($lastbit . $first) if ($first);
197 foreach $first (@lines) {
198 # print "send_now $call $first\n";
199 $conn->send_later("I$call|$first");
202 $savenl = ""; # reset savenl 'cos we will have done a newline on input
204 $conn->send_later("I$call|$buf");
216 my ($sort, $line) = @_;
217 dbg('connect', "CONNECT sort: $sort command: $line");
218 if ($sort eq 'telnet') {
219 # this is a straight network connect
220 my ($host, $port) = split /\s+/, $line;
221 $port = 23 if !$port;
225 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
226 $sock->option_callback(\&optioncb);
227 $sock->output_record_separator('');
228 # $sock->option_log('option_log');
229 # $sock->dump_log('dump');
230 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
231 $sock->open($host) or die "Can't connect to $host port $port $!";
233 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
234 # or die "Can't connect to $host port $port $!";
236 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
237 my @args = split /\s+/, $line;
240 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
241 die "no receive channel $!" unless $rfh;
242 die "no transmit channel $!" unless $wfh;
243 dbg('connect', "got pid $pid");
246 die "invalid type of connection ($sort)";
254 dbg('connect', "abort $string");
261 dbg('connect', "timeout set to $val");
267 my ($expect, $send) = @_;
268 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
275 if ($csort eq 'telnet') {
276 $line = $sock->get();
277 cease(11) unless $line; # the socket has gone away?
278 if (length $line == 0) {
279 dbg('connect', "received 0 length line, aborting...");
284 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
287 if (length $line == 0) {
288 dbg('connect', "received 0 length line, aborting...");
294 dbg('connect', "received \"$line\"");
295 if ($abort && $line =~ /$abort/i) {
296 dbg('connect', "aborted on /$abort/");
299 last if $line =~ /$expect/i;
303 if ($csort eq 'telnet') {
304 $sock->print("$send\n");
305 } elsif ($csort eq 'ax25') {
307 $wfh->print("$send");
309 dbg('connect', "sent \"$send\"");
315 dbg('connect', "timed out after $timeout seconds");
319 # handle callsign and connection type firtling
323 my @f = split /\s+/, $line;
324 $call = uc $f[0] if $f[0];
325 $csort = $f[1] if $f[1];
332 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
333 $call = ""; # the callsign being used
334 $conn = 0; # the connection object for the cluster
335 $lastbit = ""; # the last bit of an incomplete input line
336 $mynl = "\n"; # standard terminator
337 $lasttime = time; # lasttime something happened on the interface
338 $outqueue = ""; # the output queue
339 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
340 $buffered = 1; # buffer output
341 $savenl = ""; # an NL that has been saved from last time
342 $timeout = 60; # default timeout for connects
343 $abort = ""; # the current abort string
344 $cpath = "$root/connect"; # the basic connect directory
346 $pid = 0; # the pid of the child program
347 $csort = ""; # the connection type
348 $sock = 0; # connection socket
361 $call = uc shift @ARGV if @ARGV;
362 $call = uc $myalias if !$call;
363 $connsort = lc shift @ARGV if @ARGV;
364 $connsort = 'local' if !$connsort;
366 $loginreq = $call eq 'LOGIN';
368 # we will do this again later 'cos things may have changed
369 $mode = ($connsort eq 'ax25') ? 1 : 2;
372 if ($call eq $mycall) {
373 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
377 $stdout->autoflush(1);
379 $SIG{'INT'} = \&sig_term;
380 $SIG{'TERM'} = \&sig_term;
381 $SIG{'HUP'} = \&sig_term;
382 $SIG{'CHLD'} = \&sig_chld;
383 $SIG{'ALRM'} = \&timeout;
387 # do we need to do a login and password job?
392 $connsort = 'telnet' if $connsort eq 'local';
395 if (-e "$data/issue") {
396 open(I, "$data/issue") or die;
400 $issue = s/\n/\r/og if $mode == 1;
402 $stdout->print($issue) if $issue;
405 # allow a login from an existing user. I could create a user but
406 # I want to check for valid callsigns and I don't have the
407 # necessary info / regular expression yet
410 $stdout->print('login: ');
413 $s = $stdin->getline();
416 $s =~ s/-\d+$//o; # no ssids!
417 cease(0) unless $s && $s gt ' ';
418 unless (iscallsign($s)) {
419 $stdout->print("Sorry, $s is an invalid callsign");
426 # is this an out going connection?
427 if ($connsort eq "connect") {
428 my $mcall = lc $call;
430 open(IN, "$cpath/$mcall") or cease(2);
440 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
441 doabort($1) if /^\s*a\w*\s+(.*)/io;
442 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
443 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
444 if (/^\s*cl\w+\s+(.*)/io) {
450 dbg('connect', "Connected to $call ($csort), starting normal protocol");
453 # if we get here we are connected
454 if ($csort eq 'ax25' || $csort eq 'prog') {
455 # open(STDIN, "<&R");
456 # open(STDOUT, ">&W");
461 $csort = 'telnet' if $csort eq 'prog';
462 } elsif ($csort eq 'telnet') {
463 # open(STDIN, "<&$sock");
464 # open(STDOUT, ">&$sock");
472 $stdout->autoflush(1);
478 $mode = ($connsort eq 'ax25') ? 1 : 2;
481 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
482 my ($scall, $ssid) = split /-/, $call;
483 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
485 $ssid = 15 if $ssid > 15;
486 if ($connsort eq 'ax25') {
491 $call = "$scall-$ssid";
495 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
497 if (-r "$data/offline") {
498 open IN, "$data/offline" or die;
500 s/\n/\r/og if $mode == 1;
505 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
510 $let = $outbound ? 'O' : 'A';
511 $conn->send_now("$let$call|$connsort");
512 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
516 Msg->event_loop(1, 1);
518 if ($t > $lasttime) {
520 print $stdout $outqueue;