3 # This is the DX cluster 'daemon'. It sits in the middle of its little
4 # web of client routines sucking and blowing data where it may.
6 # Hence the name of 'spider' (although it may become 'dxspider')
8 # Copyright (c) 1998 Dirk Koopman G1TLH
15 # make sure that modules are searched in the order local then perl
19 # root of directory tree for this system
21 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
23 unshift @INC, "$root/perl"; # this IS the right way round!
24 unshift @INC, "$root/local";
26 # do some validation of the input
27 die "The directory $root doesn't exist, please RTFM" unless -d $root;
28 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
29 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
31 mkdir "$root/local_cmd", 0777 unless -d "$root/local_cmd";
34 # try to create and lock a lockfile (this isn't atomic but
36 $lockfn = "$root/local/cluster.lck"; # lock file name
38 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
41 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
44 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
48 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
109 use POSIX ":sys_wait_h";
117 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
118 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
119 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
120 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
121 $can_encode $maxconnect_user $maxconnect_node
124 @inqueue = (); # the main input queue, an array of hashes
125 $systime = 0; # the time now (in seconds)
126 $starttime = 0; # the starting time of the cluster
127 @outstanding_connects = (); # list of outstanding connects
128 @listeners = (); # list of listeners
129 $reqreg = 0; # 1 = registration required, 2 = deregister people
130 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
131 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
132 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
133 $maxconnect_node = 8; # Ditto but for nodes. In either case if a new incoming connection
134 # takes the no of references in the routing table above these numbers
135 # then the connection is refused. This only affects INCOMING connections.
137 # send a message to call on conn and disconnect
140 my ($conn, $call, $mess) = @_;
142 $conn->disable_read(1);
143 dbg("-> D $call $mess\n") if isdbg('chan');
144 $conn->send_now("D$call|$mess");
152 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
153 $dxchan->disconnect(1);
156 # handle incoming messages
159 my ($conn, $msg) = @_;
160 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
161 return unless defined $sort;
163 unless (is_callsign($call)) {
164 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
168 # set up the basic channel info
169 # is there one already connected to me - locally?
170 my $user = DXUser::get_current($call);
171 my $dxchan = DXChannel::get($call);
173 if ($user && $user->is_node) {
174 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
178 my $ip = $conn->{peerhost} || 'unknown';
179 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
180 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
183 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
188 # (fairly) politely disconnect people that are connected to too many other places at once
189 my $r = Route::get($call);
192 my $v = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
193 if ($v && @n >= $v) {
194 my $nodes = join ',', @n;
195 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes, disconnected");
196 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
202 my $basecall = $call;
203 $basecall =~ s/-\d+$//;
204 my $baseuser = DXUser::get_current($basecall);
205 my $lock = $user->lockout if $user;
206 if ($baseuser && $baseuser->lockout || $lock) {
207 if (!$user || !defined $lock || $lock) {
208 my $host = $conn->{peerhost} || "unknown";
209 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
216 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
218 $user = DXUser->new($call);
222 if ($user->is_node) {
223 $dxchan = DXProt->new($call, $conn, $user);
224 } elsif ($user->is_user) {
225 $dxchan = DXCommandmode->new($call, $conn, $user);
226 # } elsif ($user->is_bbs) { # there is no support so
227 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
229 die "Invalid sort of user on $call = $sort";
232 # check that the conn has a callsign
233 $conn->conns($call) if $conn->isa('IntMsg');
236 $conn->set_error(sub {error_handler($dxchan)});
237 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
244 return \&new_channel;
247 # cease running this program, close down all the connections nicely
253 $SIG{'TERM'} = 'IGNORE';
254 $SIG{'INT'} = 'IGNORE';
259 if (defined &Local::finish) {
261 Local::finish(); # end local processing
263 dbg("Local::finish error $@") if $@;
267 foreach $dxchan (DXChannel::get_all_nodes) {
268 $dxchan->disconnect(2) unless $dxchan == $main::me;
270 Msg->event_loop(100, 0.01);
273 foreach $dxchan (DXChannel::get_all_users) {
281 # disconnect UDP customers
284 # end everything else
285 Msg->event_loop(100, 0.01);
289 # close all databases
292 # close all listeners
293 foreach my $l (@listeners) {
297 LogDbg('cluster', "DXSpider V$version, build $subversion.$build ended");
301 $dbh->finish if $dbh;
304 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
308 # the reaper of children
312 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
313 dbg("cpid: $cpid") if isdbg('reap');
314 # Msg->pid_gone($cpid);
315 $zombies-- if $zombies > 0;
317 dbg("cpid: $cpid") if isdbg('reap');
320 # this is where the input queue is dealt with and things are dispatched off to other parts of
325 my $t = $systime - $starttime;
326 my $days = int $t / 86400;
328 my $hours = int $t / 3600;
330 my $mins = int $t / 60;
331 return sprintf "%d %02d:%02d", $days, $hours, $mins;
336 AGWMsg::init(\&new_channel);
339 #############################################################
341 # The start of the main line of code
343 #############################################################
345 $starttime = $systime = time;
346 $systime_days = int ($systime / 86400);
347 $systime_daystart = $systime_days * 86400;
348 $lang = 'en' unless $lang;
350 unless ($DB::VERSION) {
351 $SIG{INT} = $SIG{TERM} = \&cease;
354 # open the debug file, set various FHs to be unbuffered
355 dbginit(\&DXCommandmode::broadcast_debug);
359 STDOUT->autoflush(1);
361 # try to load the database
362 if (DXSql::init($dsn)) {
363 $dbh = DXSql->new($dsn);
364 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
370 my $w = $SIG{__DIE__};
371 $SIG{__DIE__} = 'IGNORE';
372 eval { require Encode; };
380 # try to load XML::Simple
384 my ($year) = (gmtime)[5];
386 LogDbg('cluster', "DXSpider V$version, build $subversion.$build started");
387 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
390 dbg("loading prefixes ...");
392 my $r = Prefix::init();
396 dbg("loading band data ...");
399 # initialise User file system
400 dbg("loading user file system ...");
401 DXUser->init($userfn, 1);
403 # look for the sysop and the alias user and complain if they aren't there
405 my $ref = DXUser::get($mycall);
406 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
407 $ref = DXUser::get($myalias);
408 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
411 # start listening for incoming messages/connects
412 dbg("starting listeners ...");
413 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
414 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
415 push @listeners, $conn;
416 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
417 foreach my $l (@main::listen) {
419 my $pkg = $l->[2] || 'ExtMsg';
420 my $login = $l->[3] || 'login';
422 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
423 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
424 push @listeners, $conn;
425 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
428 dbg("AGW Listener") if $AGWMsg::enable;
431 dbg("BPQ Listener") if $BPQMsg::enable;
432 BPQMsg::init(\&new_channel);
434 dbg("UDP Listener") if $UDPMsg::enable;
435 UDPMsg::init(\&new_channel);
438 dbg("load badwords: " . (BadWords::load or "Ok"));
441 unless ($DB::VERSION) {
442 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
446 $SIG{HUP} = 'IGNORE';
447 $SIG{CHLD} = sub { $zombies++ };
449 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
450 $SIG{IO} = sub { dbg("SIGIO received"); };
451 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
452 $SIG{KILL} = 'DEFAULT'; # as if it matters....
454 # catch the rest with a hopeful message
457 # dbg("Catching SIG $_") if isdbg('chan');
458 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
464 dbg("Starting Dupe system");
467 # read in system messages
468 dbg("Read in Messages");
471 # read in command aliases
472 dbg("Read in Aliases");
475 # initialise the Geomagnetic data engine
481 # initial the Spot stuff
482 dbg("Starting DX Spot system");
485 # initialise the protocol engine
486 dbg("Start Protocol Engines ...");
489 # put in a DXCluster node for us here so we can add users and take them away
490 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
491 $routeroot->do_pc9x(1);
492 $routeroot->via_pc92(1);
494 # make sure that there is a routing OUTPUT node default file
495 #unless (Filter::read_in('route', 'node_default', 0)) {
496 # my $dxcc = $main::me->dxcc;
497 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
500 # read in any existing message headers and clean out old crap
501 dbg("reading existing message headers ...");
505 # read in any cron jobs
506 dbg("reading cron jobs ...");
509 # read in database desriptors
510 dbg("reading database descriptors ...");
513 # starting local stuff
514 dbg("doing local initialisation ...");
516 if (defined &Local::init) {
520 dbg("Local::init error $@") if $@;
524 # this, such as it is, is the main loop!
525 dbg("orft we jolly well go ...");
526 my $script = new Script "startup";
527 $script->run($main::me) if $script;
529 #open(DB::OUT, "|tee /tmp/aa");
534 Msg->event_loop(10, 0.010);
537 DXChannel::process();
541 # do timed stuff, ongoing processing happens one a second
542 if ($timenow != $systime) {
545 my $days = int ($systime / 86400);
546 if ($systime_days != $days) {
547 $systime_days = $days;
548 $systime_daystart = $days * 86400;
550 IsoTime::update($systime);
551 DXCron::process(); # do cron jobs
552 DXCommandmode::process(); # process ongoing command mode stuff
554 DXProt::process(); # process ongoing ak1a pcxx stuff
555 DXConnect::process();
563 if (defined &Local::process) {
565 Local::process(); # do any localised processing
567 dbg("Local::process error $@") if $@;
571 last if --$decease <= 0;