forked from Markus-Go/ip-countryside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
690 lines (604 loc) · 24.8 KB
/
parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/**
* Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
* or its licensors, as applicable.
* Copyright 2008-2015 Markus Goldstein
*
* You may not use this file except under the terms of the accompanying license.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: ip-countryside
* File: parser.cpp
* Purpose: source file for building the ip2country database
* Responsible: Markus Goldstein
* Primary Repository: https://github.com/Markus-Go/ip-countryside/
* Web Sites: madm.dfki.de, www.goldiges.de/ip-countryside
*
*/
#include "parser.h"
#include <unistd.h>
int main() {
char cBuf[LINE_LENGTH];
vConfigFile = readConfig( "config" );
// clears files from old values
ofstream inDBex ( (findValue("DBout")).c_str() );
inDBex.close();
ofstream inDBex1 ( (findValue("Testout")).c_str() );
inDBex1.close();
stringstream sstream;
string sStr;
unsigned int temp = 0;
set < DBRangeEntries , compare > seEntries;
set < DBEntries , compare_entries > setEntries;
vector <string> sEntry;
vector < DBRangeEntries > vDBEntries;
vector < DBEntries > vEntries;
// All delegation files are parsed and merged to one file
delegParser( "afrinic" , "Testout" );
delegParser( "lacnic" , "Testout" );
delegParser( "arin" , "Testout" );
delegParser( "apnic" , "Testout" );
delegParser( "ripencc" , "Testout" );
ifstream inDel ( (findValue("Testout")).c_str() );
if (!inDel ) {
cout <<"Testout File can NOT be opened ! \n";
return 1;
}
ofstream outDel ( (findValue("DBout")).c_str(), ios::app );
// read the Parsed delegation files to set, the set sorts the entries based on ipFrom then on ipTo
while ( !(inDel.getline(cBuf,LINE_LENGTH).eof()) ) {
if ( inDel.fail() )
{
cout << "Error reading Testout file" << std::endl;
return 1;
}
sStr = cBuf;
if ( !sStr.empty() ) {
DBRangeEntries entry;
sstream.clear();
sStr = cBuf;
sEntry = split ( sStr , '-' );
if (sEntry.size() >= 4) {
sstream << sEntry[0];
sstream >> temp;
entry.ipFrom = temp;
sstream.clear();
sstream << sEntry[1];
sstream >> temp;
entry.ipTo = temp;
entry.country[0] = sEntry[2][0];
entry.country[1] = sEntry[2][1];
entry.registry = string(sEntry[3]);
seEntries.insert(entry);
}
}
}
set <DBRangeEntries , compare>::iterator it;
for ( it = seEntries.begin(); it != seEntries.end(); it++ ) {
vDBEntries.push_back(*it);
}
// clear the set
seEntries.clear();
unsigned int uDBsize = vDBEntries.size();
// check for overlaps between delegation files.
for ( unsigned int i = 0 ; i < uDBsize -1 ; i++ ) {
if ( vDBEntries[i+1].ipFrom > vDBEntries[i].ipTo ) {
continue;
}
else if ( vDBEntries[i+1].ipFrom <= vDBEntries[i].ipTo ) {
// if the cause of the conflict is ripencc then change ripencc
// entry to remove the confilct
if ( (vDBEntries[i].registry == "ripencc" ) ||
( vDBEntries[i+1].registry == "ripencc" )) {
if ( vDBEntries[i].registry == "ripencc" ) {
if ( (vDBEntries[i].ipFrom == vDBEntries[i+1].ipFrom) &&
( ( vDBEntries[i].ipTo == vDBEntries[i+1].ipTo ) ||
( vDBEntries[i].ipTo < vDBEntries[i+1].ipTo ) ) ) {
vDBEntries[i].registry = "XX";
}
else if ( (vDBEntries[i].ipFrom < vDBEntries[i+1].ipFrom) &&
(( vDBEntries[i].ipTo == vDBEntries[i+1].ipTo ) ||
( (vDBEntries[i].ipTo >= vDBEntries[i+1].ipFrom)&&
(vDBEntries[i].ipTo <= vDBEntries[i+1].ipTo) ) ) ) {
vDBEntries[i].ipTo = vDBEntries[i+1].ipFrom - 1;
}
else if ( (vDBEntries[i].ipFrom < vDBEntries[i+1].ipFrom) &&
( vDBEntries[i].ipTo > vDBEntries[i+1].ipTo ) ) {
DBRangeEntries entry;
entry.ipFrom = vDBEntries[i+1].ipTo + 1;
entry.ipTo = vDBEntries[i].ipTo;
entry.country[0] = vDBEntries[i].country[0];
entry.country[1] = vDBEntries[i].country[1];
entry.registry = vDBEntries[i].registry;
vDBEntries.push_back(entry);
vDBEntries[i].ipTo = vDBEntries[i+1].ipFrom - 1;
}
}
else {
if ( (vDBEntries[i].ipFrom <= vDBEntries[i+1].ipFrom) &&
( vDBEntries[i].ipTo >= vDBEntries[i+1].ipTo ) ) {
vDBEntries[i+1].registry = "XX";
}
else if ( (vDBEntries[i].ipFrom <= vDBEntries[i+1].ipFrom) &&
(vDBEntries[i].ipTo >= vDBEntries[i+1].ipFrom) &&
( vDBEntries[i].ipTo < vDBEntries[i+1].ipTo ) ) {
vDBEntries[i+1].ipFrom = vDBEntries[i].ipTo + 1;
}
}
}
else {
// if the conflict is between entries except ripe then the change is made on the
// first entry that cause the conflict
if ( (vDBEntries[i].ipFrom == vDBEntries[i+1].ipFrom) &&
( ( vDBEntries[i].ipTo == vDBEntries[i+1].ipTo ) ||
( vDBEntries[i].ipTo < vDBEntries[i+1].ipTo ) ) ) {
vDBEntries[i].registry = "XX";
}
else if ( (vDBEntries[i].ipFrom < vDBEntries[i+1].ipFrom) &&
(( vDBEntries[i].ipTo == vDBEntries[i+1].ipTo ) ||
( (vDBEntries[i].ipTo >= vDBEntries[i+1].ipFrom) &&
(vDBEntries[i].ipTo <= vDBEntries[i+1].ipTo) ) ) ) {
vDBEntries[i].ipTo = vDBEntries[i+1].ipFrom - 1;
}
else if ( (vDBEntries[i].ipFrom < vDBEntries[i+1].ipFrom) &&
( vDBEntries[i].ipTo > vDBEntries[i+1].ipTo ) ) {
DBRangeEntries entry;
entry.ipFrom = vDBEntries[i+1].ipTo + 1;
entry.ipTo = vDBEntries[i].ipTo;
entry.country[0] = vDBEntries[i].country[0];
entry.country[1] = vDBEntries[i].country[1];
entry.registry = vDBEntries[i].registry;
vDBEntries.push_back(entry);
vDBEntries[i].ipTo = vDBEntries[i+1].ipFrom - 1;
}
}
}
}
ofstream apnic ( (findValue("apnicDel")).c_str() );
ofstream ripe ( (findValue("ripeDel")).c_str() );
for ( unsigned int i = 0; i < vDBEntries.size() ; i++ ) {
if ( vDBEntries[i].registry == "apnic" )
apnic << vDBEntries[i].ipFrom << "-" << vDBEntries[i].ipTo << "-"
<< vDBEntries[i].country << endl;
else if ( vDBEntries[i].registry == "ripencc" )
ripe << vDBEntries[i].ipFrom << "-" << vDBEntries[i].ipTo << "-"
<< vDBEntries[i].country<< endl;
else if ( vDBEntries[i].registry == "lacnic" )
outDel << vDBEntries[i].ipFrom << "-" << vDBEntries[i].ipTo << "-"
<< vDBEntries[i].country<< endl;
else if ( vDBEntries[i].registry == "arin" )
outDel << vDBEntries[i].ipFrom << "-" << vDBEntries[i].ipTo << "-"
<< vDBEntries[i].country<< endl;
else if ( vDBEntries[i].registry == "afrinic" )
outDel << vDBEntries[i].ipFrom << "-" << vDBEntries[i].ipTo << "-"
<< vDBEntries[i].country<< endl;
}
dbParser( "ripe.db.inetnum" , "ripe" , "DBout" );
dbParser( "apnic.db.inetnum" , "apnic" , "DBout" );
temp = 0;
sstream.clear();
sEntry.clear();
ifstream inDB ( (findValue("DBout")).c_str() );
if (!inDB ) {
cout <<"DBout File can NOT be opened ! \n";
return 1;
}
ofstream outDB ( (findValue("IpToCountry")).c_str() );
// read the final version of the delegation files to sort it.
while ( !(inDB.getline(cBuf,LINE_LENGTH).eof()) ) {
if ( inDB.fail() )
{
cout << "Error reading DBout file" << std::endl;
return 1;
}
sStr = cBuf;
if ( !sStr.empty() ) {
DBEntries entry;
sstream.clear();
sStr = cBuf;
sEntry = split ( sStr , '-' );
if (sEntry.size() >= 3) {
sstream << sEntry[0];
sstream >> temp;
entry.ipFrom = temp;
sstream.clear();
sstream << sEntry[1];
sstream >> temp;
entry.ipTo = temp;
entry.country[0] = sEntry[2][0];
entry.country[1] = sEntry[2][1];
setEntries.insert(entry);
}
}
}
set <DBEntries , compare_entries>::iterator iter;
for ( iter = setEntries.begin(); iter != setEntries.end(); iter++ ) {
vEntries.push_back(*iter);
}
// clear the set
setEntries.clear();
uDBsize = vEntries.size();
unsigned int from = vEntries[0].ipFrom;
unsigned int to = vEntries[0].ipTo;
for ( unsigned int i = 1 ; i < uDBsize -1 ; i++ ) {
if (vEntries[i].ipFrom > to+1 || vEntries[i-1].country[0] != vEntries[i].country[0]
|| vEntries[i-1].country[1] != vEntries[i].country[1]) {
outDB << from << " " << to << " "
<< vEntries[i-1].country[0] << vEntries[i-1].country[1]<< endl;
from = vEntries[i].ipFrom;
to = vEntries[i].ipTo;
}
else {
to = vEntries[i].ipTo;
}
}
outDB << from << " " << to << " "
<< vEntries[uDBsize-2].country[0] << vEntries[uDBsize-2].country[1]<< endl;
inDB.close();
outDB.close();
apnic.close();
ripe.close();
// time_t current = time(0);
cout << "Program Terminated!"<<endl;
return 0;
}
// this function is used to process the apnic and ripe dbs,
void dbParser( string dbIn , string delName, string dbOut ) {
char cBuf[LINE_LENGTH];
string sStr;
vector <string> sEntry;
vector <string> sStrArray;
vector <string> sIPFrom , sIPTo;
vector < int > vEntries; // contain indeces to the ranges in Database
vector < DBEntries > vRanges;
vector < DBEntries > vRangeIP;
vector < DBEntries > vNets;
set < DBEntries , comp > seEntries;
vector < DBEntries > vDBEntries;
bool bTrigger = false;
unsigned int ipFrom = 0 , ipTo = 0;
string s = delName + "Del";
ifstream in((findValue(s)).c_str());
if ( !in ) {
cout << s <<" File can NOT be opened ! \n";
exit(1);
}
ifstream inDB ((findValue(dbIn)).c_str());
if (!inDB ) {
cout << dbIn <<" File can NOT be opened ! \n";
exit(1);
}
ofstream outDB ((findValue(dbOut)).c_str() , ios::app);
sEntry.clear();
sStrArray.clear();
vEntries.clear();
vRanges.clear();
vRangeIP.clear();
vNets.clear();
seEntries.clear();
vDBEntries.clear();
// parse the Database to extract ipFrom, ipTo and country information. excerpt of the DB.
// "inetnum: 202.14.104.0 - 202.14.104.255"
// "country: AU"
while ( !(inDB.getline(cBuf,LINE_LENGTH).eof()) ) {
if ( inDB.fail() )
{
cout << "Error reading " << dbIn << " file" << std::endl;
exit(1);
}
sStr = cBuf;
if ( sStr.empty() ) {
bTrigger = true;
}
else if ( startWith ( sStr , "inetnum:") && (ipFrom == 0) && bTrigger ) {
sEntry = split( sStr , ':' );
if (sEntry.size() >= 2) {
if ( contain(sEntry[1],"-") ) {
sIPFrom.clear();
sIPTo.clear();
sStrArray = split(sEntry[1],'-');
if (sStrArray.size() >= 2) {
sIPFrom = split ( sStrArray[0],'.' );
sIPTo = split( sStrArray[1],'.' );
if (sIPFrom.size() == 4 && sIPTo.size() == 4) {
ipFrom = convertToInt(sIPFrom);
ipTo = convertToInt(sIPTo);
bTrigger = false;
}
}
}
}
}
else if ( (ipFrom != 0) && startWith ( sStr , "country:" ) && (bTrigger == false) ) {
sEntry.clear();
sEntry = split( sStr , ':' );
if (sEntry.size() >= 2) {
string str = trim ( sEntry[1]);
DBEntries entry;
if ( (ipFrom != 0 && ipTo != UINT_MAX) ) {
entry.ipFrom = ipFrom;
entry.ipTo = ipTo;
entry.country[0] = toupper (str.c_str()[0]);
entry.country[1] = toupper (str.c_str()[1]);
// add entry to a set which will sort the entries based on [ipTo-ipFrom]
seEntries.insert( entry );
}
}
ipFrom = 0;
}
else if ( startWith ( sStr , "inetnum:") && (ipFrom == 0) && (bTrigger== false) ) {
cout<< "two ipFrom in one block" << endl;
}
}
// add all elements of the set to a vector
set <DBEntries , comp>::iterator it;
for ( it = seEntries.begin(); it != seEntries.end(); it++ ) {
vDBEntries.push_back(*it);
}
// clear the set
seEntries.clear();
// parse the delegation file to extract the ranges of the ips. excerpt of the deld. file
// "3723493376-3724541951-JP"
set < DBEntries , compare_entries > seStructEntries;
while ( !(in.getline(cBuf,LINE_LENGTH).eof()) ) {
if ( in.fail() )
{
cout << "Error reading " << s.c_str() << " file" << std::endl;
exit(1);
}
sEntry.clear();
sStr = cBuf;
if( !sStr.empty() ) {
stringstream sstream;
sEntry = split( sStr , '-' );
if (sEntry.size() >= 3) {
DBEntries sRange;
sstream.clear();
sstream << sEntry[0];
sstream >> sRange.ipFrom;
sstream.clear();
sstream << sEntry[1];
sstream >> sRange.ipTo;
sRange.country[0] = sEntry[2][0];
sRange.country[1] = sEntry[2][1];
seStructEntries.insert( sRange );
}
}
}
set <DBEntries , compare_entries>::iterator ite;
for ( ite = seStructEntries.begin(); ite != seStructEntries.end(); ite++ ) {
vRanges.push_back(*ite);
}
unsigned int uRange = 0;
ipFrom = 0;
ipTo = 0 ;
bool bTrig = true;
// merge successive ranges if they are <= 16777216
for ( unsigned int i = 0; i < vRanges.size()-1 ; i++ ) {
if ( bTrig ) {
ipFrom = vRanges[i].ipFrom;
ipTo = vRanges[i].ipTo;
uRange = ipTo - ipFrom;
bTrig = false;
}
if ( uRange == 16777216 ) {
DBEntries rIP;
rIP.ipFrom = ipFrom;
rIP.ipTo = ipTo;
vRangeIP.push_back(rIP);
bTrig = true;
}
else if ( (vRanges[i].ipTo + 1) == vRanges[i+1].ipFrom ) {
if ( ((vRanges[i+1].ipTo - vRanges[i+1].ipFrom) + uRange) <= 16777216 ) {
uRange += vRanges[i+1].ipTo - vRanges[i+1].ipFrom;
ipTo = vRanges[i+1].ipTo;
}
else {
DBEntries rIP;
rIP.ipFrom = ipFrom;
rIP.ipTo = vRanges[i].ipTo;
vRangeIP.push_back(rIP);
bTrig = true;
}
}
else {
DBEntries rIP;
rIP.ipFrom = ipFrom;
rIP.ipTo = ipTo;
vRangeIP.push_back(rIP);
bTrig = true;
}
}
unsigned int iRange[2];
unsigned int ip = 0;
unsigned int iRangeValue = 0;
unsigned int uDifference = 0;
stringstream sstream;
uDifference = vRangeIP.size();
// this part will iterate over the ranges, then go through the DB looking
// for entries which are in this range, and assign the country
// of the entry to a part of the range.
for ( unsigned int ii = 0; ii < uDifference ; ii++ ) { // for-1
iRange[0] = vRangeIP[ii].ipFrom;
iRange[1] = vRangeIP[ii].ipTo;
ip = 0;
iRangeValue = iRange[1]-iRange[0] +1;
// array of structures to keep the single ips of a range
struct net * nets = new struct net [iRangeValue ] ;
// initialize the range of one entry in delegation file
ip = iRange[0];
for (unsigned int i = 0 ; i < iRangeValue ; i ++) {
nets[i].ip = ip;
nets[i].size = 0;
nets[i].country[0] = 'X';
nets[i].country[1] = 'X';
nets[i].bSetFlag = false;
ip++;
}
ip = iRange[0] ;
unsigned int ipF, ipT;
unsigned int uDBSize = vDBEntries.size(); // size of the DB
// check the database against the entry of the delegation file
for ( unsigned int i = 0 ; i < uDBSize ; i++ ) {
if ( ((vDBEntries[i].ipFrom < iRange[0]) &&
(vDBEntries[i].ipTo < iRange[0])) ||
( (vDBEntries[i].ipFrom > iRange[1]) &&
(vDBEntries[i].ipTo > iRange[1]) ) ) {
continue;
}
else if ( (vDBEntries[i].ipFrom >= iRange[0]) &&
(vDBEntries[i].ipTo <= iRange[1]) ) {
ipF = vDBEntries[i].ipFrom;
ipT = vDBEntries[i].ipTo;
}
else if ( (vDBEntries[i].ipFrom <= iRange[0]) &&
(((vDBEntries[i].ipTo >= iRange[0]) &&
((vDBEntries[i].ipTo <= iRange[1]) )) ) ) {
ipF = iRange[0];
ipT = vDBEntries[i].ipTo;
}
else if ( ((vDBEntries[i].ipFrom >= iRange[0]) &&
(vDBEntries[i].ipFrom <= iRange[1])) &&
(vDBEntries[i].ipTo > iRange[1]) ) {
ipF = vDBEntries[i].ipFrom;
ipT = iRange[1];
}
else if ( iRange[0] >= vDBEntries[i].ipFrom &&
iRange[1] <= vDBEntries[i].ipTo ) {
ipF = iRange[0];
ipT = iRange[1];
}
unsigned int uBeginIndex = ipF - iRange[0] ;
unsigned int uEntryRange = ipT - iRange[0];
// assign country to every single ip
for ( unsigned int k = uBeginIndex ; k <= uEntryRange ; k++ ) {
unsigned int uDiff = vDBEntries[i].ipTo - vDBEntries[i].ipFrom;
if ( ( !nets[k].bSetFlag ) || ( uDiff < nets[k].size )) {
nets[k].country[0] = vDBEntries[i].country[0];
nets[k].country[1] = vDBEntries[i].country[1];
nets[k].size = uDiff;
nets[k].bSetFlag = true;
}
}
}
// merge single successor ips into one range
vRanges.clear();
for ( ite = seStructEntries.begin(); ite != seStructEntries.end(); ite++ ) {
vRanges.push_back(*ite);
}
unsigned int value = 0;
for ( unsigned int i = 0 ; i < iRangeValue ; i++ ) {
value = i;
for ( unsigned int j = i+1 ;
((j < iRangeValue) &&
((nets[j].country[0] == nets[i].country[0]) &&
(nets[j].country[1] == nets[i].country[1]))) ; j++ ) {
value = j;
}
if ( !((nets[value].country[0]== 'X') &&
(nets[value].country[1]== 'X')) ) {
if ( nets[value].ip < nets[i].ip) {
cout << "## Warning here " << nets[i].ip
<< "-"<<nets[value].ip << endl;
cout << "The Range is " << convertIP(ipF)
<< "-"<< convertIP(ipT) << endl;
}
outDB << nets[i].ip << "-" << nets[value].ip << "-"
<< nets[value].country[0]<< nets[value].country[1]<< endl;
}
else {
unsigned int uDelSize = vRanges.size(); // size of the Delegation file
unsigned int ipF = nets[i].ip , ipT = nets[value].ip ;
for ( unsigned int ij = 0 ; ij < uDelSize ; ij++ ) {
if ( (vRanges[ij].ipFrom <= ipF) && (vRanges[ij].ipTo >= ipT) ) {
outDB << nets[i].ip << "-" << nets[value].ip << "-"
<< vRanges[ij].country[0]<< vRanges[ij].country[1]<< endl;
break;
}
else if ( ((vRanges[ij].ipFrom <= ipF) &&
(vRanges[ij].ipTo >= ipF)) &&
(vRanges[ij].ipTo < ipT) ) {
outDB << nets[i].ip << "-" << vRanges[ij].ipTo << "-"
<< vRanges[ij].country[0]<< vRanges[ij].country[1]<< endl;
}
else if ( (vRanges[ij].ipFrom >= ipF ) &&
(vRanges[ij].ipTo <= ipT) ) {
outDB << vRanges[ij].ipFrom << "-" << vRanges[ij].ipTo << "-"
<< vRanges[ij].country[0]<< vRanges[ij].country[1]<< endl;
}
else if ( ((vRanges[ij].ipFrom >= ipF ) &&
(vRanges[ij].ipFrom <= ipT)) &&
(vRanges[ij].ipTo >= ipT) ) {
outDB << vRanges[ij].ipFrom << "-" << nets[value].ip << "-"
<< vRanges[ij].country[0]<< vRanges[ij].country[1]<< endl;
break;
}
}
}
i = value ;
}
vRanges.clear();
delete [] nets;
vNets.clear();
sStr.clear();
sEntry.clear();
} // for-1 end of iteration over db
in.close();
inDB.close();
outDB.close();
}
void delegParser( string delName, string delegOut ) {
char cBuf[LINE_LENGTH];
string sStr;
vector <string> sEntry;
vector <string> sIPFrom , sIPTo;
vector < DBRangeEntries > vRanges;
ifstream in( (findValue(delName)).c_str());
if (!in ) {
cout << delName << " File can NOT be opened ! \n";
exit(1);
}
ofstream outDB ((findValue(delegOut)).c_str(), ios::app);
while ( !(in.getline(cBuf,LINE_LENGTH).eof()) ) {
if ( in.fail() )
{
cout << "Error reading " << delName.c_str() << " file" << std::endl;
exit(1);
}
sEntry.clear();
sIPFrom.clear();
sStr = cBuf;
if( startWith(sStr,delName) ) {
sEntry = split( sStr , '|' );
if ( ( sEntry.size() >= 7 ) &&
( sEntry[1] != "ZZ" ) &&
( sEntry[2] == "ipv4" ) &&
( sEntry[1].length() == 2 )) {
DBRangeEntries sRange;
unsigned int temp;
stringstream sstream;
sstream.clear();
sIPFrom = split ( sEntry[3] , '.' );
sRange.ipFrom = convertToInt( sIPFrom );
sstream << sEntry[4];
sstream >> temp;
sRange.ipTo = (sRange.ipFrom + (temp-1)) ;
sRange.country[0] = sEntry[1][0];
sRange.country[1] = sEntry[1][1];
sRange.registry.assign(sEntry[0]);
vRanges.push_back(sRange);
}
}
}
for ( int i = 0; i < (int)vRanges.size()-1 ; i++ ) {
outDB << vRanges[i].ipFrom << "-" << vRanges[i].ipTo << "-"
<< vRanges[i].country[0] << vRanges[i].country[1] << "-"
<< vRanges[i].registry << endl;
}
in.close();
outDB.close();
}