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
|
/*
* Copyright (C) 2003 Luca Deri <deri@ntop.org>
* Andreas Pfaller <apfaller@yahoo.com.au>
*
* http://www.ntop.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* ************************************************************************* */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <getopt.h>
#include "p2clib.h"
/* Note: Setting DO_FREES to 0 skips free() calls for performance reasons.
Since the program exits anyway after the run the leak does
not really matter.
*/
#define DO_FREES 0
#define VERSION "2.0"
extern double log2(double);
typedef struct IPNode
{
struct IPNode *b[2];
char cc[8];
int fnum;
} IPNode;
IPNode *Head;
int OptVerbose=0;
int OptReportDuplicateMappings=0;
int OptReportConflictingMappings=0;
int OptEmitConflictingMappings=0;
int OptOnlyTest=0;
/* ************************************************************************* */
int imaxblock(uint32_t ip, int prefix)
{
while (prefix>0) {
if ((ip & PREFIX2MASK(prefix-1)) != ip)
break;
prefix--;
}
return prefix;
}
/* ************************************************************************* */
void recursiveDump(FILE *fp, IPNode *p, uint32_t ip, int prefix)
{
int i;
static char ips[32];
if (p->cc[0]!=0 && prefix!=0)
fprintf(fp, "%s:%s/%d\n", p->cc, xntoa(ip, ips, sizeof(ips)), prefix);
for (i=0; i<2; i++)
if (p->b[i])
recursiveDump(fp, p->b[i], ip | (i<<(31-prefix)), prefix+1);
}
/* ************************************************************************* */
void printInfo(char *msg, char *country, uint32_t ip, int prefix)
{
char ips[32];
fprintf(stderr, "%s: %3s:%s/%d\n", msg, country, xntoa(ip, ips, sizeof(ips)), prefix);
}
/* ************************************************************************* */
IPNode *consolidateTree(IPNode *p, char *country)
{
int i;
char *cc=country;
if (strcmp(p->cc, country)==0)
p->cc[0]=0;
if (p->cc[0]!=0)
cc=p->cc;
for (i=0; i<2; i++)
if (p->b[i])
p->b[i]=consolidateTree(p->b[i], cc);
if (p->b[0]==NULL && p->b[1]==NULL &&
(p->cc[0]==0 || strcmp(country, p->cc)==0)) {
#if DO_FREES
free(p);
#endif
return NULL;
}
if (p->b[0]!=NULL && p->b[1]!=NULL &&
p->b[0]->cc[0]!=0 && p->b[1]->cc[0]!=0 &&
strcmp(p->b[0]->cc, p->b[1]->cc)==0) {
strcpy(p->cc, p->b[0]->cc);
for (i=0; i<2; i++) {
p->b[i]->cc[0]=0;
if (p->b[i]->b[0]==NULL && p->b[i]->b[1]==NULL) {
#if DO_FREES
free(p->b[i]);
#endif
p->b[i]=NULL;
}
}
}
return p;
}
/* ************************************************************************* */
void addNodeInternal(uint32_t ip, int prefix, char *country, int fnum)
{
IPNode *p1=Head;
IPNode *p2;
int i, b;
for (i=0; i<prefix; i++) {
b=(ip>>(31-i)) & 0x1;
if (!p1->b[b]) {
if (!(p2=malloc(sizeof(IPNode))))
exit(EXIT_FAILURE);
memset(p2, 0, sizeof(IPNode));
p1->b[b]=p2;
}
else
p2=p1->b[b];
p1=p2;
}
if (p2->cc[0]==0) {
strcpy(p2->cc, country);
p2->fnum=fnum;
}
else if (OptReportConflictingMappings || OptEmitConflictingMappings) {
/* Note: the strstr test is a little too simple but works ok */
if (strcmp(p2->cc, "LOC")!=0) {
if (strstr(p2->cc, country)==NULL) {
if (OptReportConflictingMappings) {
fprintf(stderr, "%2d-%2d %3s ", p2->fnum, fnum, p2->cc);
printInfo("CONFLICT", country, ip, prefix);
}
if (OptEmitConflictingMappings) {
i=strlen(p2->cc);
if (strlen(p2->cc)+strlen(country)+1 < sizeof(p2->cc)) {
strcat(p2->cc, "+");
strcat(p2->cc, country);
}
else {
fprintf(stderr, "%2d-%2d %s ", p2->fnum, fnum, p2->cc);
printInfo("Too many conflicts", country, ip, prefix);
}
}
}
else if (OptReportDuplicateMappings) {
fprintf(stderr, "%2d-%2d %3s ", p2->fnum, fnum, p2->cc);
printInfo("DUPLICATE", country, ip, prefix);
}
}
}
}
/* ************************************************************************* */
void addNode(uint32_t ip, int range, char *country, int fnum)
{
uint32_t ip1, ip2;
int maxsize, maxdiff;
if (range<1)
return;
if (!ip)
return;
ip1=ip;
ip2=ip1+range-1;
while (ip2>=ip1 && ip1!=0) {
maxsize=imaxblock(ip1, 32);
maxdiff=32 - (int) log2(ip2-ip1+1);
if (maxsize<maxdiff)
maxsize=maxdiff;
addNodeInternal(ip1, maxsize, country, fnum);
ip1 += 1 << (32-maxsize);
}
}
/* ************************************************************************* */
void initIPCountryTable(int argc, char *argv[])
{
int i;
FILE *fin;
if ((Head=malloc(sizeof(IPNode)))==NULL)
exit(EXIT_FAILURE);
strcpy(Head->cc, "***");
Head->b[0]=NULL;
Head->b[1]=NULL;
addNode(QUAD2IP(10,0,0,0), 256*256*256, "LOC", 0);
addNode(QUAD2IP(127,0,0,0), 256*256*256, "LOC", 0);
addNode(QUAD2IP(172,16,0,0), 16*256*256, "LOC", 0);
addNode(QUAD2IP(192,168,0,0), 256*256, "LOC", 0);
for (i=0; i<argc-optind; i++) {
if ((fin=fopen(argv[i+optind], "r"))==NULL)
continue;
if (OptVerbose || OptReportConflictingMappings)
fprintf(stderr, "== Reading File %d: %s\n", i, argv[i+optind]);
while (!feof(fin)) {
char buff[256];
char *strtokState, *token, *cc, *ip, *range;
if (fgets(buff, sizeof(buff), fin)==NULL)
continue;
if ((token=strtok_r(buff, "|", &strtokState))==NULL)
continue;
if ((cc=strtok_r(NULL, "|", &strtokState))==NULL)
continue;
if ((token=strtok_r(NULL, "|", &strtokState))==NULL)
continue;
if (strcmp(token, "ipv4"))
continue;
if ((ip=strtok_r(NULL, "|", &strtokState))==NULL)
continue;
if ((range=strtok_r(NULL, "|", &strtokState))==NULL)
continue;
strtoupper(cc);
if (strcmp(cc, "GB")==0)
cc="UK";
addNode(xaton(ip), atoi(range), cc, i);
}
fclose(fin);
}
}
/* ************************************************************************* */
void usage(void)
{
fprintf(stderr, "prefixtablegen %s\n", VERSION);
fprintf(stderr, "Usage: prefixtablegen [OPTION]... [FILE]...\n");
fprintf(stderr, "Generate raw (p2c.raw.table) and optimized (p2c.opt.table)\n");
fprintf(stderr, "IP prefix to country mapping files.\n\n");
fprintf(stderr, " -v Print progress information\n");
fprintf(stderr, " -c Print information about conflicting entries\n");
fprintf(stderr, " -C Output all conflicting country names to mapping files\n");
fprintf(stderr, " WARNING: These files are not compatible with ntop\n");
fprintf(stderr, " -d Print information about duplicate entries\n");
fprintf(stderr, " -t Test only, dont write output file\n");
exit(EXIT_FAILURE);
}
/* ************************************************************************* */
void parseOptions(int argc, char *argv[])
{
int c;
while ((c=getopt(argc, argv, "?cCdthv"))!=-1) {
switch (c) {
case 'c':
OptReportConflictingMappings=1;
break;
case 'C':
OptEmitConflictingMappings=1;
break;
case 'd':
OptReportDuplicateMappings=1;
break;
case 't':
OptOnlyTest=1;
break;
case 'v':
OptVerbose++;
break;
case '?':
case 'h':
default:
usage();
break;
}
}
}
/* ************************************************************************* */
int main(int argc, char *argv[])
{
FILE *fp;
parseOptions(argc, argv);
initIPCountryTable(argc, argv);
if (!OptOnlyTest) {
if (OptVerbose)
fprintf(stderr, "== Creating file p2c.raw.table ...\n");
if ((fp=fopen("p2c.raw.table", "w"))==NULL)
exit(EXIT_FAILURE);
if (OptVerbose)
fprintf(stderr, "== Writing file p2c.raw.table ...\n");
recursiveDump(fp, Head, 0, 0);
fclose(fp);
if (OptVerbose)
fprintf(stderr, "== Creating file p2c.opt.table ...\n");
if ((fp=fopen("p2c.opt.table", "w"))==NULL)
exit(EXIT_FAILURE);
if (OptVerbose)
fprintf(stderr, "== Optimizing table size ...\n");
consolidateTree(Head, "");
if (OptVerbose)
fprintf(stderr, "== Writing file p2c.opt.table ...\n");
recursiveDump(fp, Head, 0, 0);
fclose(fp);
}
if (OptVerbose)
fprintf(stderr, "== Done\n");
exit(EXIT_SUCCESS);
}
|