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) $!";
42 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
47 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
51 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
114 use POSIX ":sys_wait_h";
122 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
123 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
124 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
125 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
126 $can_encode $maxconnect_user $maxconnect_node $idle_interval
129 @inqueue = (); # the main input queue, an array of hashes
130 $systime = 0; # the time now (in seconds)
131 $starttime = 0; # the starting time of the cluster
132 @outstanding_connects = (); # list of outstanding connects
133 @listeners = (); # list of listeners
134 $reqreg = 0; # 1 = registration required, 2 = deregister people
135 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
136 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
137 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
138 $maxconnect_node = 0; # Ditto but for nodes. In either case if a new incoming connection
139 # takes the no of references in the routing table above these numbers
140 # then the connection is refused. This only affects INCOMING connections.
141 $idle_interval = 0.100; # the wait between invocations of the main idle loop processing.
143 # send a message to call on conn and disconnect
146 my ($conn, $call, $mess) = @_;
148 dbg("-> D $call $mess\n") if isdbg('chan');
149 $conn->disable_read(1);
150 $conn->send_now("D$call|$mess");
157 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
158 $dxchan->disconnect(1);
161 # handle incoming messages
164 my ($conn, $msg) = @_;
165 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
166 return unless defined $sort;
168 unless (is_callsign($call)) {
169 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
173 # set up the basic channel info
174 # is there one already connected to me - locally?
175 my $user = DXUser::get_current($call);
176 my $dxchan = DXChannel::get($call);
178 if ($user && $user->is_node) {
179 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
183 my $ip = $conn->peerhost || 'unknown';
184 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
185 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
188 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
193 # (fairly) politely disconnect people that are connected to too many other places at once
194 my $r = Route::get($call);
195 if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) {
197 my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
198 my $c = $user->maxconnect;
200 $v = defined $c ? $c : $m;
201 if ($v && @n >= $v) {
202 my $nodes = join ',', @n;
203 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected");
204 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
210 my $basecall = $call;
211 $basecall =~ s/-\d+$//;
212 my $baseuser = DXUser::get_current($basecall);
213 my $lock = $user->lockout if $user;
214 if ($baseuser && $baseuser->lockout || $lock) {
215 if (!$user || !defined $lock || $lock) {
216 my $host = $conn->peerhost || "unknown";
217 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
224 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
226 $user = DXUser->new($call);
230 if ($user->is_node) {
231 $dxchan = DXProt->new($call, $conn, $user);
232 } elsif ($user->is_user) {
233 $dxchan = DXCommandmode->new($call, $conn, $user);
234 # } elsif ($user->is_bbs) { # there is no support so
235 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
237 die "Invalid sort of user on $call = $sort";
240 # check that the conn has a callsign
241 $conn->conns($call) if $conn->isa('IntMsg');
244 $conn->set_error(sub {error_handler($dxchan)});
245 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
252 return \&new_channel;
255 # cease running this program, close down all the connections nicely
261 $SIG{'TERM'} = 'IGNORE';
262 $SIG{'INT'} = 'IGNORE';
267 if (defined &Local::finish) {
269 Local::finish(); # end local processing
271 dbg("Local::finish error $@") if $@;
275 foreach $dxchan (DXChannel::get_all_nodes) {
276 $dxchan->disconnect(2) unless $dxchan == $main::me;
280 foreach $dxchan (DXChannel::get_all_users) {
288 # disconnect UDP customers
291 # end everything else
295 # close all databases
298 # close all listeners
299 foreach my $l (@listeners) {
303 LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) ended");
304 dbg("bye bye everyone - bye bye");
309 $dbh->finish if $dbh;
312 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
316 # the reaper of children
320 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
321 dbg("cpid: $cpid") if isdbg('reap');
322 # Msg->pid_gone($cpid);
323 $zombies-- if $zombies > 0;
325 dbg("cpid: $cpid") if isdbg('reap');
328 # this is where the input queue is dealt with and things are dispatched off to other parts of
333 my $t = $systime - $starttime;
334 my $days = int $t / 86400;
336 my $hours = int $t / 3600;
338 my $mins = int $t / 60;
339 return sprintf "%d %02d:%02d", $days, $hours, $mins;
344 AGWMsg::init(\&new_channel);
351 DXChannel::process();
355 # do timed stuff, ongoing processing happens one a second
356 if ($timenow != $systime) {
359 my $days = int ($systime / 86400);
360 if ($systime_days != $days) {
361 $systime_days = $days;
362 $systime_daystart = $days * 86400;
364 IsoTime::update($systime);
365 DXCron::process(); # do cron jobs
366 DXCommandmode::process(); # process ongoing command mode stuff
368 DXProt::process(); # process ongoing ak1a pcxx stuff
369 DXConnect::process();
379 if (defined &Local::process) {
381 Local::process(); # do any localised processing
383 dbg("Local::process error $@") if $@;
388 #############################################################
390 # The start of the main line of code
392 #############################################################
394 $starttime = $systime = time;
395 $systime_days = int ($systime / 86400);
396 $systime_daystart = $systime_days * 86400;
397 $lang = 'en' unless $lang;
399 unless ($DB::VERSION) {
400 $SIG{INT} = $SIG{TERM} = \&cease;
403 # open the debug file, set various FHs to be unbuffered
404 dbginit(\&DXCommandmode::broadcast_debug);
408 STDOUT->autoflush(1);
410 # try to load the database
411 if (DXSql::init($dsn)) {
412 $dbh = DXSql->new($dsn);
413 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
419 my $w = $SIG{__DIE__};
420 $SIG{__DIE__} = 'IGNORE';
421 eval { require Encode; };
429 # try to load XML::Simple
433 my ($year) = (gmtime)[5];
435 LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) started");
436 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
439 dbg("loading prefixes ...");
441 my $r = Prefix::init();
445 dbg("loading band data ...");
448 # initialise User file system
449 dbg("loading user file system ...");
450 DXUser->init($userfn, 1);
452 # look for the sysop and the alias user and complain if they aren't there
454 die "\$myalias \& \$mycall are the same ($mycall)!, they must be different (hint: make \$mycall = '${mycall}-2';). Oh and don't forget to rerun create_sysop.pl!" if $mycall eq $myalias;
455 my $ref = DXUser::get($mycall);
456 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
457 $ref = DXUser::get($myalias);
458 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
461 # start listening for incoming messages/connects
462 dbg("starting listeners ...");
463 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
464 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
465 push @listeners, $conn;
466 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
467 foreach my $l (@main::listen) {
469 my $pkg = $l->[2] || 'ExtMsg';
470 my $login = $l->[3] || 'login';
472 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
473 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
474 push @listeners, $conn;
475 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
478 dbg("AGW Listener") if $AGWMsg::enable;
481 dbg("BPQ Listener") if $BPQMsg::enable;
482 BPQMsg::init(\&new_channel);
484 dbg("UDP Listener") if $UDPMsg::enable;
485 UDPMsg::init(\&new_channel);
488 dbg("load badwords: " . (BadWords::load or "Ok"));
491 $decease = AnyEvent->condvar;
494 my ($sigint, $sigterm);
495 unless ($DB::VERSION) {
496 $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{$decease->send});
497 $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{$decease->send});
498 # $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{AnyEvent->unloop});
499 # $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{AnyEvent->unloop});
503 $SIG{HUP} = 'IGNORE';
504 $SIG{CHLD} = sub { $zombies++ };
506 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
507 $SIG{IO} = sub { dbg("SIGIO received"); };
508 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
509 $SIG{KILL} = 'DEFAULT'; # as if it matters....
511 # catch the rest with a hopeful message
514 # dbg("Catching SIG $_") if isdbg('chan');
515 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
521 dbg("Starting Dupe system");
524 # read in system messages
525 dbg("Read in Messages");
528 # read in command aliases
529 dbg("Read in Aliases");
532 # initialise the Geomagnetic data engine
538 # initial the Spot stuff
539 dbg("Starting DX Spot system");
542 # initialise the protocol engine
543 dbg("Start Protocol Engines ...");
546 # put in a DXCluster node for us here so we can add users and take them away
547 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
548 $routeroot->do_pc9x(1);
549 $routeroot->via_pc92(1);
551 # make sure that there is a routing OUTPUT node default file
552 #unless (Filter::read_in('route', 'node_default', 0)) {
553 # my $dxcc = $main::me->dxcc;
554 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
557 # read in any existing message headers and clean out old crap
558 dbg("reading existing message headers ...");
562 # read in any cron jobs
563 dbg("reading cron jobs ...");
566 # read in database desriptors
567 dbg("reading database descriptors ...");
570 # starting local stuff
571 dbg("doing local initialisation ...");
573 if (defined &Local::init) {
577 dbg("Local::init error $@") if $@;
581 # this, such as it is, is the main loop!
582 dbg("orft we jolly well go ...");
583 my $script = new Script "startup";
584 $script->run($main::me) if $script;
586 #open(DB::OUT, "|tee /tmp/aa");
588 my $per_sec = AnyEvent->timer(after => 0, interval => $idle_interval, cb => sub{idle_loop()});
593 idle_loop() for (1..25);