Changeset 32 for trunk/dnsbl/DNSBL.pm
- Timestamp:
- 03/25/11 17:16:38 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/dnsbl/DNSBL.pm
r29 r32 138 138 sub initexport { 139 139 $parsth = $dbh->prepare("SELECT count(i.ip),b.block,b.level,b.listme AS oobblock,o.listme AS ooborg ". 140 "FROM iplist i INNER JOIN blocks b ON i. ip <<b.block INNER JOIN orgs o ON b.orgid = o.orgid ".140 "FROM iplist i INNER JOIN blocks b ON i.parent = b.block INNER JOIN orgs o ON b.orgid = o.orgid ". 141 141 "WHERE b.block >>= ? ". 142 142 "GROUP BY b.block,b.level,b.listme,o.listme ORDER BY b.block"); 143 $sthmoron = $dbh->prepare("SELECT ip,s4list FROM iplist WHERE ip << ? ORDER BY ip");143 $sthmoron = $dbh->prepare("SELECT ip,s4list FROM iplist WHERE parent = ?"); 144 144 } 145 145 … … 166 166 if ($rep =~ /^[\d.]+$/) { 167 167 # weesa gonna ASS-U-ME IP addresses are sanely formatted. 168 $sth = $dbh->prepare("SELECT count FROM iplist WHERE ip=?"); 169 $sth->execute($rep) or die "eep? ".$dbh->errstr."\n"; 170 $rows = $sth->rows; 171 if ($rows == 0) { 172 $sth = $dbh->prepare("INSERT INTO iplist (ip) VALUES (?)"); 173 } elsif ($rows == 1) { 174 $sth = $dbh->prepare("UPDATE iplist SET count=count+1 WHERE ip=?"); 175 } else { 176 die "db corrupt: found $rows matches on $rep\n"; 168 eval { 169 $sth = $dbh->prepare("SELECT count FROM iplist WHERE ip=?"); 170 $sth->execute($rep) or die "eep? ".$dbh->errstr."\n"; 171 $rows = $sth->rows; 172 if ($rows == 0) { 173 $sth = $dbh->prepare("INSERT INTO iplist (ip,parent) VALUES ". 174 "(?,(SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1))"); 175 $sth->execute($rep,$rep) or die "couldn't add entry for $rep: ".$dbh->errstr."\n"; 176 } elsif ($rows == 1) { 177 $sth = $dbh->prepare("UPDATE iplist SET count=count+1 WHERE ip=?"); 178 $sth->execute($rep) or die "couldn't update listing for $rep: ".$dbh->errstr."\n"; 179 } else { 180 die "db corrupt: found $rows matches on $rep\n"; 181 } 182 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ?"); 183 $sth->execute($rep); 184 my $updsth = $dbh->prepare("UPDATE blocks SET ipcount=(SELECT count(*) FROM iplist WHERE ip << ?) WHERE block=?"); 185 while (my ($block) = $sth->fetchrow_array) { 186 $updsth->execute($block,$block); 187 } 188 $dbh->commit; 189 }; 190 if ($@) { 191 my $msg = $@; 192 return "failed adding $rep: $msg"; 177 193 } 178 $sth->execute($rep) or die "couldn't update listing for $rep: ".$dbh->errstr."\n";179 194 } else { 180 195 return; 181 196 } 182 $dbh->commit;183 197 return $rows; 184 198 } # end report() … … 228 242 return "$blockin not a single CIDR range" if !$block; 229 243 230 local $dbh->{AutoCommit} = 1; # force autocommit 231 232 my $sth = $dbh->prepare("INSERT INTO blocks (block,orgid,level) VALUES (?,?,?)"); 233 $sth->execute("$block",$orgid,$level); 234 return $sth->errstr if $sth->err; 244 # local $dbh->{AutoCommit} = 1; # force autocommit 245 246 my $sth; 247 eval { 248 my $parent = '0/0'; 249 if ($level > 0) { 250 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1"); 251 $sth->execute("$block"); 252 ($parent) = $sth->fetchrow_array; 253 } 254 $sth = $dbh->prepare("INSERT INTO blocks (block,orgid,level,parent,ipcount) VALUES (?,?,?,?,". 255 "(SELECT count(*) FROM iplist WHERE ip << ?))"); 256 $sth->execute("$block",$orgid,$level,$parent,"$block"); 257 $sth = $dbh->prepare("UPDATE iplist SET parent=? WHERE parent=? AND ip << ?"); 258 $sth->execute("$block",$parent,"$block"); 259 $dbh->commit; 260 }; 261 if ($@) { 262 my $msg = $@; 263 eval { dbh->rollback; }; 264 return "failed to add $block: $msg"; 265 } 235 266 # nb: no need to return anything, since the CIDR block is the key 236 267 } … … 287 318 return if !$block; 288 319 289 $sth = $dbh->prepare("SELECT count(*) FROM iplist WHERE ip <<?");320 $sth = $dbh->prepare("SELECT ipcount FROM blocks WHERE block = ?"); 290 321 $sth->execute($entity); 291 322 my ($bcount) = $sth->fetchrow_array; … … 343 374 } 344 375 345 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE block <<?");376 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE parent = ?"); 346 377 $sth->execute($container); 347 378 my ($nblocks) = $sth->fetchrow_array(); … … 356 387 my $bcount; 357 388 if ($container ne '0.0.0.0/0') { 358 $sth = $dbh->prepare("SELECT count(*) FROM iplist WHERE ip << ?"); 359 $sth->execute($container); 360 ($bcount) = $sth->fetchrow_array(); 361 362 $sth = $dbh->prepare("SELECT b.listme,o.listme ". 389 $sth = $dbh->prepare("SELECT b.ipcount,b.listme,o.listme ". 363 390 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ". 364 391 "WHERE b.block = ?"); 365 392 $sth->execute($container); 366 ($ listme,$listorg) = $sth->fetchrow_array();393 ($bcount,$listme,$listorg) = $sth->fetchrow_array(); 367 394 368 395 $bitmask |= $bitfields{$level-1} if $bcount >= $autolist{$masklen}; … … 374 401 if ($nblocks > 0) { 375 402 my $sthsubblocks = $dbh->prepare("SELECT block FROM blocks ". 376 "WHERE level =? and block << ? ORDER BY block, masklen(block) DESC");403 "WHERE level = ? AND parent = ?"); 377 404 $sthsubblocks->execute($level, $container); 378 405 while (my ($cidr) = $sthsubblocks->fetchrow_array()) { … … 490 517 491 518 my $cidr = new NetAddr::IP "$block"; 492 my $sth = $dbh->prepare(" select count(*) from iplist where ip <<?");519 my $sth = $dbh->prepare("SELECT ipcount FROM blocks WHERE block = ?"); 493 520 $sth->execute("$cidr"); 494 521 my ($count) = $sth->fetchrow_array;
Note:
See TracChangeset
for help on using the changeset viewer.