X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=mbd%2Fmbd.pl;h=4b73ac9b4e1e484a1b20b28cc4d288664e657b6b;hb=9725b2cf4b9f59d38b6aa2e45c2da91766c90c91;hp=4843b0bb60cfea9a62388340e854749a8a3b3d4a;hpb=e30bd8928e466b1b7b33440352af079779bd0611;p=nms diff --git a/mbd/mbd.pl b/mbd/mbd.pl index 4843b0b..4b73ac9 100644 --- a/mbd/mbd.pl +++ b/mbd/mbd.pl @@ -9,6 +9,12 @@ require './access_list.pl'; require './nets.pl'; require './survey.pl'; require './mbd.pm'; +use lib '../include'; +use nms; +use strict; +use warnings; + +my ($dbh, $q); sub fhbits { my $bits = 0; @@ -49,7 +55,7 @@ sub cache_cidrrange { open LOG, ">>", "mbd.log"; -my @ports = ( mbd::find_all_ports() , $Config::survey_port_low, $Config::survey_port_high ); +my @ports = ( mbd::find_all_ports() , $Config::survey_port_low .. $Config::survey_port_high ); # Open a socket for each port my @socks = (); @@ -77,9 +83,17 @@ while (1) { for my $sport (keys %active_surveys) { my $age = Time::HiRes::tv_interval($active_surveys{$sport}{start}, $now); if ($age > $Config::survey_time && $active_surveys{$sport}{active}) { - print "Survey for port " . $active_surveys{$sport}{dport} . ": " . - $active_surveys{$sport}{num} . " active servers.\n"; + print "Survey for '" . $Config::access_list[$active_surveys{$sport}{entry}]->{name} . "'/" . + $active_surveys{$sport}{dport} . ": " . $active_surveys{$sport}{num} . " active servers.\n"; $active_surveys{$sport}{active} = 0; + + # (re)connect to the database if needed + if (!defined($dbh) || !$dbh->ping) { + $dbh = nms::db_connect(); + $q = $dbh->prepare("INSERT INTO mbd_log (ts,game,port,description,active_servers) VALUES (CURRENT_TIMESTAMP,?,?,?,?)") + or die "Couldn't prepare query"; + } + $q->execute($active_surveys{$sport}{entry}, $active_surveys{$sport}{dport}, $Config::access_list[$active_surveys{$sport}{entry}]->{name}, $active_surveys{$sport}{num}); } if ($age > $Config::survey_time * 3.0) { delete $active_surveys{$sport}; @@ -94,7 +108,7 @@ while (1) { my ($sport, $saddr) = sockaddr_in($addr); my ($dport, $daddr) = sockaddr_in(getsockname($sock)); my $size = length($data); - + # Check if this is a survey reply if ($dport >= $Config::survey_port_low && $dport <= $Config::survey_port_high) { if (!exists($active_surveys{$dport})) { @@ -110,21 +124,24 @@ while (1) { next; } - + # Rate limiting if (exists($last_sent{$saddr}{$dport})) { my $elapsed = Time::HiRes::tv_interval($last_sent{$saddr}{$dport}, $now); if ($elapsed < 1.0) { print LOG "$dport $size 2\n"; print inet_ntoa($saddr), ", $dport, $size bytes => rate-limited ($elapsed secs since last)\n"; + next; } - next; } - + # We don't get the packet's destination address, but I guess this should do... # Check against the ACL. my $pass = 0; + my $entry = -1; for my $rule (@Config::access_list) { + ++$entry; + next unless (mbd::match_ranges($dport, $rule->{'ports'})); next unless (mbd::match_ranges($size, $rule->{'sizes'})); @@ -148,8 +165,8 @@ while (1) { # The packet is OK! Do we already have a recent enough survey # for this port, or should we use this packet? my $survey = 1; - if (exists($last_survey{$dport})) { - my $age = Time::HiRes::tv_interval($last_survey{$dport}, $now); + if (exists($last_survey{$entry . "/" . $dport})) { + my $age = Time::HiRes::tv_interval($last_survey{$entry . "/" . $dport}, $now); if ($age < $Config::survey_freq) { $survey = 0; } @@ -157,25 +174,28 @@ while (1) { # New survey; find an unused port my $survey_sport; - for my $port ($Config::survey_port_low..$Config::survey_port_high) { - if (!exists($active_surveys{$port})) { - $survey_sport = $port; - - $active_surveys{$port} = { - start => $now, - active => 1, - dport => $dport, - num => 0 - }; - $last_survey{$dport} = $now; - - last; + if ($survey) { + for my $port ($Config::survey_port_low..$Config::survey_port_high) { + if (!exists($active_surveys{$port})) { + $survey_sport = $port; + + $active_surveys{$port} = { + start => $now, + active => 1, + dport => $dport, + entry => $entry, + num => 0 + }; + $last_survey{$entry . "/" . $dport} = $now; + + last; + } } - } - if (!defined($survey_sport)) { - print "WARNING: no free survey source ports, not surveying.\n"; - $survey = 0; + if (!defined($survey_sport)) { + print "WARNING: no free survey source ports, not surveying.\n"; + $survey = 0; + } } my $num_nets = 0;