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";
44 use Net::Telnet qw(TELOPT_ECHO);
50 # cease communications
54 if ($conn && $sendz) {
55 $conn->send_now("Z$call|bye...\n");
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");
311 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
312 $call = ""; # the callsign being used
313 $conn = 0; # the connection object for the cluster
314 $lastbit = ""; # the last bit of an incomplete input line
315 $mynl = "\n"; # standard terminator
316 $lasttime = time; # lasttime something happened on the interface
317 $outqueue = ""; # the output queue
318 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
319 $buffered = 1; # buffer output
320 $savenl = ""; # an NL that has been saved from last time
321 $timeout = 60; # default timeout for connects
322 $abort = ""; # the current abort string
323 $cpath = "$root/connect"; # the basic connect directory
325 $pid = 0; # the pid of the child program
326 $csort = ""; # the connection type
327 $sock = 0; # connection socket
340 $call = uc shift @ARGV if @ARGV;
341 $call = uc $myalias if !$call;
342 $connsort = lc shift @ARGV if @ARGV;
343 $connsort = 'local' if !$connsort;
345 $loginreq = $call eq 'LOGIN';
347 # we will do this again later 'cos things may have changed
348 $mode = ($connsort eq 'ax25') ? 1 : 2;
351 if ($call eq $mycall) {
352 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
356 $stdout->autoflush(1);
358 $SIG{'INT'} = \&sig_term;
359 $SIG{'TERM'} = \&sig_term;
360 $SIG{'HUP'} = 'IGNORE';
361 $SIG{'CHLD'} = \&sig_chld;
362 $SIG{'ALRM'} = \&timeout;
366 # do we need to do a login and password job?
371 if (-e "$data/issue") {
372 open(I, "$data/issue") or die;
376 $issue = s/\n/\r/og if $mode == 1;
378 $stdout->print($issue) if $issue;
384 DXUser->init($userfn);
386 # allow a login from an existing user. I could create a user but
387 # I want to check for valid callsigns and I don't have the
388 # necessary info / regular expression yet
389 for ($state = 0; $state < 2; ) {
393 $stdout->print('login: ');
396 $s = $stdin->getline();
399 $s =~ s/-\d+$//o; # no ssids!
400 cease(0) unless $s gt ' ';
402 $user = DXUser->get($call);
404 } elsif ($state == 1) {
405 $stdout->print('password: ');
408 $s = $stdin->getline();
411 if (!$user || ($user->passwd && $user->passwd ne $s)) {
412 $stdout->print("sorry...$nl");
419 # handle callsign and connection type firtling
423 my @f = split /\s+/, $line;
424 $call = uc $f[0] if $f[0];
425 $csort = $f[1] if $f[1];
428 # is this an out going connection?
429 if ($connsort eq "connect") {
430 my $mcall = lc $call;
432 open(IN, "$cpath/$mcall") or cease(2);
442 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
443 doabort($1) if /^\s*a\w*\s+(.*)/io;
444 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
445 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
446 if (/\s*cl\w+\s+(.*)/io) {
452 dbg('connect', "Connected to $call ($csort), starting normal protocol");
455 # if we get here we are connected
456 if ($csort eq 'ax25' || $csort eq 'prog') {
457 # open(STDIN, "<&R");
458 # open(STDOUT, ">&W");
463 $csort = 'telnet' if $csort eq 'prog';
464 } elsif ($csort eq 'telnet') {
465 # open(STDIN, "<&$sock");
466 # open(STDOUT, ">&$sock");
474 $stdout->autoflush(1);
480 $mode = ($connsort eq 'ax25') ? 1 : 2;
483 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
485 if (-r "$data/offline") {
486 open IN, "$data/offline" or die;
488 s/\n/\r/og if $mode == 1;
493 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
498 $let = $outbound ? 'O' : 'A';
499 $conn->send_now("$let$call|$connsort");
500 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
504 Msg->event_loop(1, 1);
506 if ($t > $lasttime) {
508 print $stdout $outqueue;