3 # connect to an external entity
5 # This is the routine that is called by the cluster to manage
6 # an outgoing connection to the point where it is 'connected'.
7 # From there the client program is forked and execed over the top of
8 # this program and that connects back to the cluster as though
9 # it were an incoming connection.
11 # Essentially this porgram does the same as chat in that there
12 # are 'expect', 'send' pairs of strings. The 'expect' string is
13 # a pattern. You can include timeout and abort string statements
18 # connect <type> <destination>|<program>
21 # client <client name> <parameters>
22 # '<regexp>' '<send string>'
24 # Copyright (c) Dirk Koopman G1TLH
29 # search local then perl directories
31 # root of directory tree for this system
33 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35 unshift @INC, "$root/perl"; # this IS the right way round!
36 unshift @INC, "$root/local";
47 $timeout = 30; # default timeout for each stage of the connect
48 $abort = ''; # default connection abort string
49 $path = "$root/connect"; # the basic connect directory
50 $client = "$root/perl/client.pl"; # default client
52 $connected = 0; # we have successfully connected or started an interface program
53 $pid = 0; # the pid of the child program
54 $csort = ""; # the connection type
55 $sock = 0; # connection socket
61 $SIG{ALRM} = \&timeout;
67 exit(1) if !$ARGV[0]; # bang out if no callsign
68 open(IN, "$path/$ARGV[0]") or exit(2);
80 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
81 doclient($1) if /^\s*cl\w*\s+(\w+)\s+(.*)$/io;
82 doabort($1) if /^\s*a\w*\s+(.*)/io;
83 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
84 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
89 my ($sort, $line) = @_;
90 dbg("CONNECT sort: $sort command: $line") if isdbg('connect');
92 # this is a straight network connect
93 my ($host) = $line =~ /host\s+(\w+)/o;
94 my ($port) = $line =~ /port\s+(\d+)/o;
97 $sock = IO::Socket::INET->new(PeerAddr => "$host", PeerPort => "$port", Proto => 'tcp')
98 or die "Can't connect to $host port $port $!";
100 } elsif ($sort eq 'ax25') {
101 my @args = split /\s+/, $line;
102 $pid = open2(\*R, \*W, "$line") or die "can't do $line $!";
103 dbg("got pid $pid") if isdbg('connect');
106 die "can't get here";
114 dbg("abort $string") if isdbg('connect');
121 dbg("timeout set to $val") if isdbg('connect');
122 alarm($timeout = $val);
127 my ($expect, $send) = @_;
128 dbg("CHAT \"$expect\" -> \"$send\"") if isdbg('connect');
134 if ($csort eq 'net') {
137 } elsif ($csort eq 'ax25') {
142 dbg("received \"$line\"") if isdbg('connect');
143 if ($abort && $line =~ /$abort/i) {
144 dbg("aborted on /$abort/") if isdbg('connect');
148 if ($send && (!$expect || $line =~ /$expect/i)) {
149 if ($csort eq 'net') {
150 $sock->print("$send\n");
151 } elsif ($csort eq 'ax25') {
155 dbg("sent \"$send\"") if isdbg('connect');
161 my ($cl, $args) = @_;
162 dbg("client: $cl args: $args") if isdbg('connect');
163 my @args = split /\s+/, $args;
165 # if (!defined ($pid = fork())) {
166 # dbg("can't fork") if isdbg('connect');
176 if ($csort eq 'net') {
177 open STDIN, "<&$sock";
178 open STDOUT, ">&$sock";
180 } elsif ($csort eq 'ax25') {
185 dbg("client can't get here") if isdbg('connect');
193 dbg("timed out after $timeout seconds") if isdbg('connect');
199 dbg("caught INT or TERM signal") if isdbg('connect');
208 dbg("pid $wpid has died") if isdbg('connect');