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)([A-Z0-9\-]+)\|(.*)$/;
110 $snl = "" if $mode == 0;
111 if ($mode == 2 && $line =~ />$/) {
115 $line =~ s/\n/\r/og if $mode == 1;
116 #my $p = qq($line$snl);
118 if (length $outqueue >= $client_buffer_lth) {
119 print $stdout $outqueue;
122 $outqueue .= "$savenl$line$snl";
125 print $stdout $savenl, $line, $snl;;
127 $savenl = $newsavenl;
128 } elsif ($sort eq 'M') {
129 $mode = $line; # set new mode from cluster
131 } elsif ($sort eq 'E') {
132 if ($sort eq 'telnet') {
133 $mode = $line; # set echo mode from cluster
134 my $term = POSIX::Termios->new;
135 $term->getattr(fileno($sock));
136 $term->setiflag( 0 );
137 $term->setoflag( 0 );
138 $term->setattr(fileno($sock), &POSIX::TCSANOW );
140 } elsif ($sort eq 'I') {
141 ; # ignore echoed I frames
142 } elsif ($sort eq 'B') {
143 if ($buffered && $outqueue) {
144 print $stdout $outqueue;
147 $buffered = $line; # set buffered or unbuffered
148 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
164 $r = sysread($fh, $buf, 1024);
167 # $prbuf =~ s/\r/\\r/;
168 # $prbuf =~ s/\n/\\n/;
169 # print "sys: $r ($prbuf)\n";
170 if (!defined $r || $r == 0) {
174 $buf =~ s/\r/\n/og if $mode == 1;
175 $buf =~ s/\r\n/\n/og if $mode == 2;
176 $dangle = !($buf =~ /\n$/);
180 @lines = split /\n/, $buf;
182 if ($dangle) { # pull off any dangly bits
187 $first = shift @lines;
188 unshift @lines, ($lastbit . $first) if ($first);
189 foreach $first (@lines) {
190 # print "send_now $call $first\n";
191 $conn->send_later("I$call|$first");
194 $savenl = ""; # reset savenl 'cos we will have done a newline on input
196 $conn->send_later("I$call|$buf");
208 my ($sort, $line) = @_;
209 dbg('connect', "CONNECT sort: $sort command: $line");
210 if ($sort eq 'telnet') {
211 # this is a straight network connect
212 my ($host, $port) = split /\s+/, $line;
213 $port = 23 if !$port;
217 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
218 $sock->option_callback(\&optioncb);
219 $sock->output_record_separator('');
220 # $sock->option_log('option_log');
221 # $sock->dump_log('dump');
222 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
223 $sock->open($host) or die "Can't connect to $host port $port $!";
225 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
226 # or die "Can't connect to $host port $port $!";
228 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
229 my @args = split /\s+/, $line;
232 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
233 die "no receive channel $!" unless $rfh;
234 die "no transmit channel $!" unless $wfh;
235 dbg('connect', "got pid $pid");
238 die "invalid type of connection ($sort)";
246 dbg('connect', "abort $string");
253 dbg('connect', "timeout set to $val");
259 my ($expect, $send) = @_;
260 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
267 if ($csort eq 'telnet') {
268 $line = $sock->get();
269 cease(11) unless $line; # the socket has gone away?
270 $line =~ s/\r\n/\n/og;
272 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
277 if (length $line == 0) {
278 dbg('connect', "received 0 length line, aborting...");
281 dbg('connect', "received \"$line\"");
282 if ($abort && $line =~ /$abort/i) {
283 dbg('connect', "aborted on /$abort/");
286 last if $line =~ /$expect/i;
290 if ($csort eq 'telnet') {
291 $sock->print("$send\n");
292 } elsif ($csort eq 'ax25') {
294 $wfh->print("$send");
296 dbg('connect', "sent \"$send\"");
302 dbg('connect', "timed out after $timeout seconds");
306 # handle callsign and connection type firtling
310 my @f = split /\s+/, $line;
311 $call = uc $f[0] if $f[0];
312 $csort = $f[1] if $f[1];
319 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
320 $call = ""; # the callsign being used
321 $conn = 0; # the connection object for the cluster
322 $lastbit = ""; # the last bit of an incomplete input line
323 $mynl = "\n"; # standard terminator
324 $lasttime = time; # lasttime something happened on the interface
325 $outqueue = ""; # the output queue
326 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
327 $buffered = 1; # buffer output
328 $savenl = ""; # an NL that has been saved from last time
329 $timeout = 60; # default timeout for connects
330 $abort = ""; # the current abort string
331 $cpath = "$root/connect"; # the basic connect directory
333 $pid = 0; # the pid of the child program
334 $csort = ""; # the connection type
335 $sock = 0; # connection socket
348 $call = uc shift @ARGV if @ARGV;
349 $call = uc $myalias if !$call;
350 $connsort = lc shift @ARGV if @ARGV;
351 $connsort = 'local' if !$connsort;
353 $loginreq = $call eq 'LOGIN';
355 # we will do this again later 'cos things may have changed
356 $mode = ($connsort eq 'ax25') ? 1 : 2;
359 if ($call eq $mycall) {
360 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
364 $stdout->autoflush(1);
366 $SIG{'INT'} = \&sig_term;
367 $SIG{'TERM'} = \&sig_term;
368 $SIG{'HUP'} = \&sig_term;
369 $SIG{'CHLD'} = \&sig_chld;
370 $SIG{'ALRM'} = \&timeout;
374 # do we need to do a login and password job?
379 $connsort = 'telnet' if $connsort eq 'local';
382 if (-e "$data/issue") {
383 open(I, "$data/issue") or die;
387 $issue = s/\n/\r/og if $mode == 1;
389 $stdout->print($issue) if $issue;
392 # allow a login from an existing user. I could create a user but
393 # I want to check for valid callsigns and I don't have the
394 # necessary info / regular expression yet
397 $stdout->print('login: ');
400 $s = $stdin->getline();
403 $s =~ s/-\d+$//o; # no ssids!
404 cease(0) unless $s && $s gt ' ';
405 unless (iscallsign($s)) {
406 $stdout->print("Sorry, $s is an invalid callsign");
413 # is this an out going connection?
414 if ($connsort eq "connect") {
415 my $mcall = lc $call;
417 open(IN, "$cpath/$mcall") or cease(2);
427 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
428 doabort($1) if /^\s*a\w*\s+(.*)/io;
429 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
430 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
431 if (/^\s*cl\w+\s+(.*)/io) {
437 dbg('connect', "Connected to $call ($csort), starting normal protocol");
440 # if we get here we are connected
441 if ($csort eq 'ax25' || $csort eq 'prog') {
442 # open(STDIN, "<&R");
443 # open(STDOUT, ">&W");
448 $csort = 'telnet' if $csort eq 'prog';
449 } elsif ($csort eq 'telnet') {
450 # open(STDIN, "<&$sock");
451 # open(STDOUT, ">&$sock");
459 $stdout->autoflush(1);
465 $mode = ($connsort eq 'ax25') ? 1 : 2;
468 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
469 my ($scall, $ssid) = split /-/, $call;
470 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
472 $ssid = 15 if $ssid > 15;
473 if ($connsort eq 'ax25') {
478 $call = "$scall-$ssid";
482 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
484 if (-r "$data/offline") {
485 open IN, "$data/offline" or die;
487 s/\n/\r/og if $mode == 1;
492 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
497 $let = $outbound ? 'O' : 'A';
498 $conn->send_now("$let$call|$connsort");
499 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
503 Msg->event_loop(1, 1);
505 if ($t > $lasttime) {
507 print $stdout $outqueue;