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 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 .....
152 # ******************************************************
153 # ******************************************************
154 # any other sorts that might happen are silently ignored.
155 # ******************************************************
156 # ******************************************************
170 $r = sysread($fh, $buf, 1024);
173 # $prbuf =~ s/\r/\\r/;
174 # $prbuf =~ s/\n/\\n/;
175 # print "sys: $r ($prbuf)\n";
176 if (!defined $r || $r == 0) {
180 $buf =~ s/\r/\n/og if $mode == 1;
181 $buf =~ s/\r\n/\n/og if $mode == 2;
182 $dangle = !($buf =~ /\n$/);
186 @lines = split /\n/, $buf;
188 if ($dangle) { # pull off any dangly bits
193 $first = shift @lines;
194 unshift @lines, ($lastbit . $first) if ($first);
195 foreach $first (@lines) {
196 # print "send_now $call $first\n";
197 $conn->send_later("I$call|$first");
200 $savenl = ""; # reset savenl 'cos we will have done a newline on input
202 $conn->send_later("I$call|$buf");
214 my ($sort, $line) = @_;
215 dbg('connect', "CONNECT sort: $sort command: $line");
216 if ($sort eq 'telnet') {
217 # this is a straight network connect
218 my ($host, $port) = split /\s+/, $line;
219 $port = 23 if !$port;
223 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
224 $sock->option_callback(\&optioncb);
225 $sock->output_record_separator('');
226 # $sock->option_log('option_log');
227 # $sock->dump_log('dump');
228 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
229 $sock->open($host) or die "Can't connect to $host port $port $!";
231 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
232 # or die "Can't connect to $host port $port $!";
234 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
235 my @args = split /\s+/, $line;
238 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
239 die "no receive channel $!" unless $rfh;
240 die "no transmit channel $!" unless $wfh;
241 dbg('connect', "got pid $pid");
244 die "invalid type of connection ($sort)";
252 dbg('connect', "abort $string");
259 dbg('connect', "timeout set to $val");
265 my ($expect, $send) = @_;
266 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
273 if ($csort eq 'telnet') {
274 $line = $sock->get();
275 cease(11) unless $line; # the socket has gone away?
276 $line =~ s/\r\n/\n/og;
278 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
283 if (length $line == 0) {
284 dbg('connect', "received 0 length line, aborting...");
287 dbg('connect', "received \"$line\"");
288 if ($abort && $line =~ /$abort/i) {
289 dbg('connect', "aborted on /$abort/");
292 last if $line =~ /$expect/i;
296 if ($csort eq 'telnet') {
297 $sock->print("$send\n");
298 } elsif ($csort eq 'ax25') {
300 $wfh->print("$send");
302 dbg('connect', "sent \"$send\"");
308 dbg('connect', "timed out after $timeout seconds");
312 # handle callsign and connection type firtling
316 my @f = split /\s+/, $line;
317 $call = uc $f[0] if $f[0];
318 $csort = $f[1] if $f[1];
325 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
326 $call = ""; # the callsign being used
327 $conn = 0; # the connection object for the cluster
328 $lastbit = ""; # the last bit of an incomplete input line
329 $mynl = "\n"; # standard terminator
330 $lasttime = time; # lasttime something happened on the interface
331 $outqueue = ""; # the output queue
332 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
333 $buffered = 1; # buffer output
334 $savenl = ""; # an NL that has been saved from last time
335 $timeout = 60; # default timeout for connects
336 $abort = ""; # the current abort string
337 $cpath = "$root/connect"; # the basic connect directory
339 $pid = 0; # the pid of the child program
340 $csort = ""; # the connection type
341 $sock = 0; # connection socket
354 $call = uc shift @ARGV if @ARGV;
355 $call = uc $myalias if !$call;
356 $connsort = lc shift @ARGV if @ARGV;
357 $connsort = 'local' if !$connsort;
359 $loginreq = $call eq 'LOGIN';
361 # we will do this again later 'cos things may have changed
362 $mode = ($connsort eq 'ax25') ? 1 : 2;
365 if ($call eq $mycall) {
366 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
370 $stdout->autoflush(1);
372 $SIG{'INT'} = \&sig_term;
373 $SIG{'TERM'} = \&sig_term;
374 $SIG{'HUP'} = \&sig_term;
375 $SIG{'CHLD'} = \&sig_chld;
376 $SIG{'ALRM'} = \&timeout;
380 # do we need to do a login and password job?
385 $connsort = 'telnet' if $connsort eq 'local';
388 if (-e "$data/issue") {
389 open(I, "$data/issue") or die;
393 $issue = s/\n/\r/og if $mode == 1;
395 $stdout->print($issue) if $issue;
398 # allow a login from an existing user. I could create a user but
399 # I want to check for valid callsigns and I don't have the
400 # necessary info / regular expression yet
403 $stdout->print('login: ');
406 $s = $stdin->getline();
409 $s =~ s/-\d+$//o; # no ssids!
410 cease(0) unless $s && $s gt ' ';
411 unless (iscallsign($s)) {
412 $stdout->print("Sorry, $s is an invalid callsign");
419 # is this an out going connection?
420 if ($connsort eq "connect") {
421 my $mcall = lc $call;
423 open(IN, "$cpath/$mcall") or cease(2);
433 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
434 doabort($1) if /^\s*a\w*\s+(.*)/io;
435 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
436 dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
437 if (/^\s*cl\w+\s+(.*)/io) {
443 dbg('connect', "Connected to $call ($csort), starting normal protocol");
446 # if we get here we are connected
447 if ($csort eq 'ax25' || $csort eq 'prog') {
448 # open(STDIN, "<&R");
449 # open(STDOUT, ">&W");
454 $csort = 'telnet' if $csort eq 'prog';
455 } elsif ($csort eq 'telnet') {
456 # open(STDIN, "<&$sock");
457 # open(STDOUT, ">&$sock");
465 $stdout->autoflush(1);
471 $mode = ($connsort eq 'ax25') ? 1 : 2;
474 # adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
475 my ($scall, $ssid) = split /-/, $call;
476 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
478 $ssid = 15 if $ssid > 15;
479 if ($connsort eq 'ax25') {
484 $call = "$scall-$ssid";
488 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
490 if (-r "$data/offline") {
491 open IN, "$data/offline" or die;
493 s/\n/\r/og if $mode == 1;
498 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
503 $let = $outbound ? 'O' : 'A';
504 $conn->send_now("$let$call|$connsort");
505 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
509 Msg->event_loop(1, 1);
511 if ($t > $lasttime) {
513 print $stdout $outqueue;