Skip to content

Commit 1a384b1

Browse files
committed
Bugfix: Merging country list with successive entries.
1 parent 8d9a7e4 commit 1a384b1

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DBout=ip2country.db
1+
DBout=ip2country.tmp
22
Testout=Testout
33
afrinic=delegated-afrinic-latest
44
lacnic=delegated-lacnic-latest

lookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function ip2int($ip) {
9494
<p>
9595
Here we provide an on-line demo of our <a href="http://code.google.com/p/ip-countryside/" target="_BLANK">ip-countryside</a> project.<br>
9696
The database is generated weekly and was last created on <?php print $last_modified; ?> (CEST).<br>
97-
It can be dowloaded as a <a href="ip2country.zip">zip-file</a> (<?php echo $db_size; ?> kb)
97+
It can be dowloaded as a <a href="ip2country.zip">zip-file</a> (<?php echo $db_size; ?> kb).
9898
For more details please contact <a href="http://www.iupr.org/goldstein" target="_BLANK">Markus Goldstein</a>.
9999
</p>
100100
<div align="center">

parser.cpp

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ int main() {
2828
inDBex.close();
2929
ofstream inDBex1 ( (findValue("Testout")).c_str() );
3030
inDBex1.close();
31-
31+
3232
char cBuf[LINE_LENGTH];
3333
stringstream sstream;
3434
string sStr;
35-
unsigned int temp = 0;
36-
35+
unsigned int temp = 0;
36+
3737
set < DBRangeEntries , compare > seEntries;
3838
set < DBEntries , compare_entries > setEntries;
3939
vector <string> sEntry;
4040
vector < DBRangeEntries > vDBEntries;
4141
vector < DBEntries > vEntries;
42-
42+
4343
// All delegation files are parsed and merged to one file
4444
delegParser( "afrinic" , "Testout" );
4545
delegParser( "lacnic" , "Testout" );
@@ -54,7 +54,7 @@ int main() {
5454
}
5555
ofstream outDel ( (findValue("DBout")).c_str(), ios::app );
5656

57-
// read the Parsed delegation files to set, the set sorts the entries based on ipFrom then on ipTo
57+
// read the Parsed delegation files to set, the set sorts the entries based on ipFrom then on ipTo
5858
while ( !(inDel.getline(cBuf,LINE_LENGTH).eof()) ) {
5959
sStr = cBuf;
6060
if ( !sStr.empty() ) {
@@ -81,12 +81,12 @@ int main() {
8181
for ( it = seEntries.begin(); it != seEntries.end(); it++ ) {
8282
vDBEntries.push_back(*it);
8383
}
84-
// clear the set
84+
// clear the set
8585
seEntries.clear();
8686

8787
unsigned int uDBsize = vDBEntries.size();
8888

89-
// check for overlaps between delegation files.
89+
// check for overlaps between delegation files.
9090
for ( unsigned int i = 0 ; i < uDBsize -1 ; i++ ) {
9191
if ( vDBEntries[i+1].ipFrom > vDBEntries[i].ipTo ) {
9292
continue;
@@ -233,32 +233,40 @@ int main() {
233233
for ( iter = setEntries.begin(); iter != setEntries.end(); iter++ ) {
234234
vEntries.push_back(*iter);
235235
}
236-
// clear the set
236+
// clear the set
237237
setEntries.clear();
238238

239239
uDBsize = vEntries.size();
240240

241-
for ( unsigned int i = 0 ; i < uDBsize -1 ; i++ ) {
242-
outDB << vEntries[i].ipFrom << " " << vEntries[i].ipTo << " "
243-
<< vEntries[i].country[0] << vEntries[i].country[1]<< endl;
241+
long from = vEntries[0].ipFrom;
242+
long to = vEntries[0].ipTo;
243+
for ( unsigned int i = 1 ; i < uDBsize -1 ; i++ ) {
244+
if (vEntries[i].ipFrom > to+1 || vEntries[i-1].country[0] != vEntries[i].country[0]
245+
|| vEntries[i-1].country[1] != vEntries[i].country[1]) {
246+
outDB << from << " " << to << " "
247+
<< vEntries[i-1].country[0] << vEntries[i-1].country[1]<< endl;
248+
from = vEntries[i].ipFrom;
249+
to = vEntries[i].ipTo;
250+
}
251+
else {
252+
to = vEntries[i].ipTo;
253+
}
244254
}
245-
246-
255+
outDB << from << " " << to << " "
256+
<< vEntries[uDBsize-2].country[0] << vEntries[uDBsize-2].country[1]<< endl;
257+
247258
inDB.close();
248259
outDB.close();
249260
apnic.close();
250261
ripe.close();
251262

252-
// time_t current = time(0);
263+
// time_t current = time(0);
253264

254265
cout << "Program Terminated!"<<endl;
255266
return 0;
256267
}
257268

258-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
261-
// this function is used to process the apnic and ripe dbs,
269+
// this function is used to process the apnic and ripe dbs,
262270
void dbParser( string dbIn , string delName, string dbOut ) {
263271
char cBuf[LINE_LENGTH];
264272

@@ -333,7 +341,7 @@ void dbParser( string dbIn , string delName, string dbOut ) {
333341
entry->country[0] = toupper (str.c_str()[0]);
334342
entry->country[1] = toupper (str.c_str()[1]);
335343

336-
// add entry to a set which will sort the entries based on [ipTo-ipFrom]
344+
// add entry to a set which will sort the entries based on [ipTo-ipFrom]
337345
seEntries.insert( *entry );
338346
}
339347
ipFrom = 0;
@@ -343,16 +351,16 @@ void dbParser( string dbIn , string delName, string dbOut ) {
343351
}
344352
}
345353

346-
// add all elements of the set to a vector
354+
// add all elements of the set to a vector
347355
set <DBEntries , comp>::iterator it;
348356
for ( it = seEntries.begin(); it != seEntries.end(); it++ ) {
349357
vDBEntries.push_back(*it);
350358
}
351-
// clear the set
359+
// clear the set
352360
seEntries.clear();
353361

354-
// parse the delegation file to extract the ranges of the ips. excerpt of the deld. file
355-
// "3723493376-3724541951-JP"
362+
// parse the delegation file to extract the ranges of the ips. excerpt of the deld. file
363+
// "3723493376-3724541951-JP"
356364
set < DBEntries , compare_entries > seStructEntries;
357365

358366
while ( !(in.getline(cBuf,LINE_LENGTH).eof()) ) {
@@ -384,7 +392,7 @@ void dbParser( string dbIn , string delName, string dbOut ) {
384392
ipTo = 0 ;
385393
bool bTrig = true;
386394

387-
// merge successive ranges if they are <= 16777216
395+
// merge successive ranges if they are <= 16777216
388396
for ( unsigned int i = 0; i < vRanges.size()-1 ; i++ ) {
389397
if ( bTrig ) {
390398
ipFrom = vRanges[i].ipFrom;
@@ -433,12 +441,11 @@ void dbParser( string dbIn , string delName, string dbOut ) {
433441
unsigned int uDifference = 0;
434442
stringstream sstream;
435443

436-
437444
uDifference = vRangeIP.size();
438445

439-
// this part will iterate over the ranges, then go through the DB looking
440-
// for entries which are in this range, and assign the country
441-
// of the entry to a part of the range.
446+
// this part will iterate over the ranges, then go through the DB looking
447+
// for entries which are in this range, and assign the country
448+
// of the entry to a part of the range.
442449

443450
for ( unsigned int ii = 0; ii < uDifference ; ii++ ) { // for-1
444451
iRange[0] = vRangeIP[ii].ipFrom;
@@ -451,10 +458,10 @@ void dbParser( string dbIn , string delName, string dbOut ) {
451458
iCounter = 0;
452459
iRangeValue = iRange[1]-iRange[0] +1;
453460

454-
// array of structures to keep the single ips of a range
461+
// array of structures to keep the single ips of a range
455462
struct net * nets = new struct net [iRangeValue ] ;
456463

457-
// initialize the range of one entry in delegation file
464+
// initialize the range of one entry in delegation file
458465
ip = iRange[0];
459466
for (unsigned int i = 0 ; i < iRangeValue ; i ++) {
460467
nets[i].ip = ip;
@@ -514,21 +521,10 @@ void dbParser( string dbIn , string delName, string dbOut ) {
514521
nets[k].bSetFlag = true;
515522
}
516523

517-
// else if ( uDiff == nets[k].size ) {
518-
// if ( (nets[k].country[0] != vDBEntries[i].country[0]) ||
519-
// (nets[k].country[1] != vDBEntries[i].country[1] ) ) {
520-
//
521-
// cout << "Violation -"<<convertIP(vDBEntries[i].ipFrom) << " - "
522-
// << convertIP(vDBEntries[i].ipTo) << " - "
523-
// << vDBEntries[i].country << " - "<< nets[k].ip << " - "
524-
// << nets[k].country[0] << nets[k].country[1] << " - "
525-
// << nets[k].size << endl ;
526-
// }
527-
// }
528524
}
529525
}
530526

531-
// merge single successor ips into one range
527+
// merge single successor ips into one range
532528
vRanges.clear();
533529

534530
for ( ite = seStructEntries.begin(); ite != seStructEntries.end(); ite++ ) {

0 commit comments

Comments
 (0)