aboutsummaryrefslogtreecommitdiff
path: root/whoseip/Whoseip/DB.pm
diff options
context:
space:
mode:
Diffstat (limited to 'whoseip/Whoseip/DB.pm')
-rw-r--r--whoseip/Whoseip/DB.pm35
1 files changed, 21 insertions, 14 deletions
diff --git a/whoseip/Whoseip/DB.pm b/whoseip/Whoseip/DB.pm
index f6b08f0..55e86d2 100644
--- a/whoseip/Whoseip/DB.pm
+++ b/whoseip/Whoseip/DB.pm
@@ -264,6 +264,7 @@ sub ipdb_open {
my $ug = new Data::UUID;
print STDERR "file $filename, UUID ".$ug->to_string($ipdbfile{uuid})."\n";
}
+ $ipdbfile{ttl} = $_{ttl};
push @ipdb_open_files, \%ipdbfile;
return \%ipdbfile;
}
@@ -359,15 +360,15 @@ sub ipdb_locker {
sub ipdb_save_page($$) {
my ($dbf, $page) = @_;
- if (sysseek($dbf->{fd}, $page->{off}, SEEK_SET) != $page->{off}) {
- croak "$dbf->{filename}: can't seek: $!";
- }
my $ret;
if ($page->{type} == IPDB_PAGE_INDEX) {
print STDERR "saving index page $page->{off}: ".
join(',', @{$page->{tab}})."\n"
if $dbf->{debug} > 1;
+ if (sysseek($dbf->{fd}, $page->{off}, SEEK_SET) != $page->{off}) {
+ croak "$dbf->{filename}: can't seek: $!";
+ }
$ret = syswrite($dbf->{fd}, pack('LL[257].',
$page->{type},
@{$page->{tab}},
@@ -386,15 +387,7 @@ sub ipdb_save_page($$) {
my $i = 0;
my $a;
foreach my $ent (@{$page->{tab}}) {
- my $fdata = eval { freeze($ent->[4]) };
- if ($@) {
- print STDERR "failed to freeze data for " .
- inet_ntoa(pack('N', $ent->[0])) . "/" .
- inet_ntoa(pack('N', $ent->[1])) . ":".
- $ent->[3] ."\n";
- exit;
- }
- my $x = pack('LLLa2L/a', @{$ent}[0 .. 3],$fdata);
+ my $x = pack('LLLa2L/a', @{$ent}[0 .. 3], freeze($ent->[4]));
my $l = length($x);
if ($size + $l > $dbf->{pagesize}) {
print STDERR "SPLIT at $i: $size + $l, rest ".
@@ -412,6 +405,9 @@ sub ipdb_save_page($$) {
$a .= $x;
++$i;
}
+ if (sysseek($dbf->{fd}, $page->{off}, SEEK_SET) != $page->{off}) {
+ croak "$dbf->{filename}: can't seek: $!";
+ }
$ret = syswrite($dbf->{fd},
pack('LLLa'.length($a).'@'.$dbf->{pagesize},
$page->{type},
@@ -492,7 +488,8 @@ sub ipdb_cache_get($$) {
my ($dbf,$off) = @_;
my $page;
if (defined($dbf->{pagecache}{$off})) {
- print STDERR "$off found in cache\n" if $dbf->{debug};
+ print STDERR "$off found in cache: ".
+ pagetypestr($dbf->{pagecache}{$off}{type})."\n" if $dbf->{debug};
$page = $dbf->{pagecache}{$off};
if (defined($page->{lru_newer})) {
print STDERR "promoting $page->{off}\n" if $dbf->{debug} > 2;
@@ -613,7 +610,7 @@ sub ipdb_get_page($$) {
push @{$ret{tab}}, [ @a[$i*5 .. $i*5 + 3], $href ];
}
} else {
- croak "$dbf->{filename}: invalid page type at offset $off";
+ croak "$dbf->{filename}: invalid page type $ret{type} at offset $off";
}
return \%ret;
@@ -724,6 +721,7 @@ sub ipdb_lookup_unlocked($$) {
$page = ipdb_cache_get($dbf, $page->{tab}[LEAF_IDX]);
}
+ my $i = 0;
foreach my $r (@{$page->{tab}}) {
print STDERR "ipdb_lookup: compare ($ipn & $r->[1]) == $r->[0]\n"
if $dbf->{debug};
@@ -731,6 +729,14 @@ sub ipdb_lookup_unlocked($$) {
# FIXME: check timestamp
print STDERR "ipdb_lookup: MATCH $r->[3]\n"
if $dbf->{debug};
+ if (defined($dbf->{ttl}) and
+ (time - $r->[2]) > $dbf->{ttl}) {
+ print STDERR "ipdb_lookup: record expired, removing\n"
+ if $dbf->{debug};
+ splice @{$page->{tab}}, $i, 1;
+ $page->{dirty} = 1;
+ return undef;
+ }
my %res = ( country => $r->[3],
network => inet_ntoa(pack('N', $r->[0])),
netmask => inet_ntoa(pack('N', $r->[1])) );
@@ -738,6 +744,7 @@ sub ipdb_lookup_unlocked($$) {
if (defined($r->[4]) and ref($r->[4]) eq 'HASH');
return %res;
}
+ ++$i;
}
return undef if (!$page->{next});
$page = ipdb_cache_get($dbf, $page->{next});

Return to:

Send suggestions and report system problems to the System administrator.