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");
57 $stdout->flush if $stdout;
59 dbg('connect', "killing $pid");
63 # $SIG{__WARN__} = sub {my $a = shift; cluck($a); };
68 # terminate program from signal
77 $SIG{CHLD} = \&sig_chld;
79 dbg('connect', "caught $pid");
93 # handle incoming messages
96 my ($con, $msg, $err) = @_;
97 if (defined $err && $err) {
101 my ($sort, $call, $line) = $msg =~ /^(\w)([A-Z0-9\-]+)\|(.*)$/;
106 $snl = "" if $mode == 0;
107 if ($mode == 2 && $line =~ />$/) {
111 $line =~ s/\n/\r/og if $mode == 1;
112 #my $p = qq($line$snl);
114 if (length $outqueue >= $client_buffer_lth) {
115 print $stdout $outqueue;
118 $outqueue .= "$savenl$line$snl";
121 print $stdout $savenl, $line, $snl;;
123 $savenl = $newsavenl;
124 } elsif ($sort eq 'M') {
125 $mode = $line; # set new mode from cluster
127 } elsif ($sort eq 'E') {
128 if ($sort eq 'telnet') {
129 $mode = $line; # set echo mode from cluster
130 my $term = POSIX::Termios->new;
131 $term->getattr(fileno($sock));
132 $term->setiflag( 0 );
133 $term->setoflag( 0 );
134 $term->setattr(fileno($sock), &POSIX::TCSANOW );
136 } elsif ($sort eq 'I') {
137 ; # ignore echoed I frames
138 } elsif ($sort eq 'B') {
139 if ($buffered && $outqueue) {
140 print $stdout $outqueue;
143 $buffered = $line; # set buffered or unbuffered
144 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
160 $r = sysread($fh, $buf, 1024);
163 # $prbuf =~ s/\r/\\r/;
164 # $prbuf =~ s/\n/\\n/;
165 # print "sys: $r ($prbuf)\n";
166 if (!defined $r || $r == 0) {
170 $buf =~ s/\r/\n/og if $mode == 1;
171 $buf =~ s/\r\n/\n/og if $mode == 2;
172 $dangle = !($buf =~ /\n$/);
176 @lines = split /\n/, $buf;
178 if ($dangle) { # pull off any dangly bits
183 $first = shift @lines;
184 unshift @lines, ($lastbit . $first) if ($first);
185 foreach $first (@lines) {
186 # print "send_now $call $first\n";
187 $conn->send_later("I$call|$first");
190 $savenl = ""; # reset savenl 'cos we will have done a newline on input
192 $conn->send_later("I$call|$buf");
204 my ($sort, $line) = @_;
205 dbg('connect', "CONNECT sort: $sort command: $line");
206 if ($sort eq 'telnet') {
207 # this is a straight network connect
208 my ($host, $port) = split /\s+/, $line;
209 $port = 23 if !$port;
213 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
214 $sock->option_callback(\&optioncb);
215 $sock->output_record_separator('');
216 # $sock->option_log('option_log');
217 # $sock->dump_log('dump');
218 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
219 $sock->open($host) or die "Can't connect to $host port $port $!";
221 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
222 # or die "Can't connect to $host port $port $!";
224 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
225 my @args = split /\s+/, $line;
228 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
229 die "no receive channel $!" unless $rfh;
230 die "no transmit channel $!" unless $wfh;
231 dbg('connect', "got pid $pid");
234 die "invalid type of connection ($sort)";
242 dbg('connect', "abort $string");
249 dbg('connect', "timeout set to $val");
255 my ($expect, $send) = @_;
256 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
263 if ($csort eq 'telnet') {
264 $line = $sock->get();
265 cease(11) unless $line; # the socket has gone away?
266 $line =~ s/\r\n/\n/og;
268 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
273 if (length $line == 0) {
274 dbg('connect', "received 0 length line, aborting...");
277 dbg('connect', "received \"$line\"");
278 if ($abort && $line =~ /$abort/i) {
279 dbg('connect', "aborted on /$abort/");
282 last if $line =~ /$expect/i;
286 if ($csort eq 'telnet') {
287 $sock->print("$send\n");
288 } elsif ($csort eq 'ax25') {
290 $wfh->print("$send");
292 dbg('connect', "sent \"$send\"");
298 dbg('connect', "timed out after $timeout seconds");
307 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
308 $call = ""; # the callsign being used
309 $conn = 0; # the connection object for the cluster
310 $lastbit = ""; # the last bit of an incomplete input line
311 $mynl = "\n"; # standard terminator
312 $lasttime = time; # lasttime something happened on the interface
313 $outqueue = ""; # the output queue
314 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
315 $buffered = 1; # buffer output
316 $savenl = ""; # an NL that has been saved from last time
317 $timeout = 60; # default timeout for connects
318 $abort = ""; # the current abort string
319 $cpath = "$root/connect"; # the basic connect directory
321 $pid = 0; # the pid of the child program
322 $csort = ""; # the connection type
323 $sock = 0; # connection socket
336 $call = uc shift @ARGV if @ARGV;
337 $call = uc $myalias if !$call;
338 $connsort = lc shift @ARGV if @ARGV;
339 $connsort = 'local' if !$connsort;
341 $loginreq = $call eq 'LOGIN';
343 # we will do this again later 'cos things may have changed
344 $mode = ($connsort eq 'ax25') ? 1 : 2;
347 if ($call eq $mycall) {
348 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
352 $stdout->autoflush(1);
354 $SIG{'INT'} = \&sig_term;
355 $SIG{'TERM'} = \&sig_term;
356 $SIG{'HUP'} = 'IGNORE';
357 $SIG{'CHLD'} = \&sig_chld;
358 $SIG{'ALRM'} = \&timeout;
362 # do we need to do a login and password job?
367 if (-e "$data/issue") {
368 open(I, "$data/issue") or die;
372 $issue = s/\n/\r/og if $mode == 1;
374 $stdout->print($issue) if $issue;
380 DXUser->init($userfn);
382 # allow a login from an existing user. I could create a user but
383 # I want to check for valid callsigns and I don't have the
384 # necessary info / regular expression yet
385 for ($state = 0; $state < 2; ) {
389 $stdout->print('login: ');
392 $s = $stdin->getline();
395 $s =~ s/-\d+$//o; # no ssids!
396 cease(0) unless $s gt ' ';
398 $user = DXUser->get($call);
400 } elsif ($state == 1) {
401 $stdout->print('password: ');
404 $s = $stdin->getline();
407 if (!$user || ($user->passwd && $user->passwd ne $s)) {
408 $stdout->print("sorry...$nl");
415 # handle callsign and connection type firtling
419 my @f = split /\s+/, $line;
420 $call = uc $f[0] if $f[0];
421 $csort = $f[1] if $f[1];
424 # is this an out going connection?
425 if ($connsort eq "connect") {
426 my $mcall = lc $call;
428 open(IN, "$cpath/$mcall") or cease(2);
438 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
439 doabort($1) if /^\s*a\w*\s+(.*)/io;
440 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
441 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
442 if (/\s*cl\w+\s+(.*)/io) {
448 dbg('connect', "Connected to $call ($csort), starting normal protocol");
451 # if we get here we are connected
452 if ($csort eq 'ax25' || $csort eq 'prog') {
453 # open(STDIN, "<&R");
454 # open(STDOUT, ">&W");
459 $csort = 'telnet' if $csort eq 'prog';
460 } elsif ($csort eq 'telnet') {
461 # open(STDIN, "<&$sock");
462 # open(STDOUT, ">&$sock");
470 $stdout->autoflush(1);
476 $mode = ($connsort eq 'ax25') ? 1 : 2;
479 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
481 if (-r "$data/offline") {
482 open IN, "$data/offline" or die;
484 s/\n/\r/og if $mode == 1;
489 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
494 $let = $outbound ? 'O' : 'A';
495 $conn->send_now("$let$call|$connsort");
496 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
500 Msg->event_loop(1, 1);
502 if ($t > $lasttime) {
504 print $stdout $outqueue;