-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxemkgeodb.c
655 lines (549 loc) · 13.6 KB
/
xemkgeodb.c
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
/*
* Copyright (c) 2023-2025, Vladimir Misyurov
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include "geoip.h"
#include "ip-btrie.h"
#define GEOIP_SIGN_IPAPI "ip_version,start_ip,end_ip,continent,country_code,"\
"country,state,city,zip,timezone,latitude,longitude,accuracy"
static int verbose = 0;
static int
geodb_add4(struct btrie_node_geo *db, size_t *db_size,
uint32_t addr, int mask, struct geoip_info *g)
{
uint8_t *addr_ptr = (uint8_t *)&addr;
IP_BTRIE_ADD_MMAP(db, *db_size, btrie_node_geo);
db[node].g = *g;
return 1;
}
static int
geodb_add6(struct btrie_node_geo *db, size_t *db_size,
xe_ip addr, int mask, struct geoip_info *g)
{
uint8_t *addr_ptr = (uint8_t *)&addr;
IP_BTRIE_ADD_MMAP(db, *db_size, btrie_node_geo);
db[node].g = *g;
return 1;
}
/* as */
static int
asdb_add4(struct btrie_node_as *db, size_t *db_size,
uint32_t addr, int mask, struct as_info *a)
{
uint8_t *addr_ptr = (uint8_t *)&addr;
IP_BTRIE_ADD_MMAP(db, *db_size, btrie_node_as);
db[node].a = *a;
return 1;
}
static int
asdb_add6(struct btrie_node_as *db, size_t *db_size,
xe_ip addr, int mask, struct as_info *a)
{
uint8_t *addr_ptr = (uint8_t *)&addr;
IP_BTRIE_ADD_MMAP(db, *db_size, btrie_node_as);
db[node].a = *a;
return 1;
}
static void
add_range4(struct btrie_node_geo *geodb, size_t *geodb_size,
struct btrie_node_as *asdb, size_t *asdb_size,
uint32_t ip1, uint32_t ip2, struct geoip_info *g,
struct as_info *a)
{
uint32_t subnet_first, subnet_last, end;
subnet_first = be32toh(ip1);
end = be32toh(ip2);
for (;;) {
int mask_bits;
if (subnet_first > end) {
break;
} else if (subnet_first == end) {
if (g) {
geodb_add4(geodb, geodb_size,
htobe32(subnet_first), 32, g);
} else {
asdb_add4(asdb, asdb_size,
htobe32(subnet_first), 32, a);
}
break;
}
if (subnet_first != 0) {
mask_bits = __builtin_ctz(subnet_first);
subnet_last = subnet_first + (1 << mask_bits) - 1;
} else {
mask_bits = 32;
subnet_last = subnet_first - 1;
}
if (subnet_last == end) {
if (g) {
geodb_add4(geodb, geodb_size,
htobe32(subnet_first),
32 - mask_bits, g);
} else {
asdb_add4(asdb, asdb_size,
htobe32(subnet_first),
32 - mask_bits, a);
}
break;
} else if (subnet_last > end) {
uint32_t diff = end - subnet_first + 1;
int p = 32 - __builtin_clz(diff) - 1;
uint32_t ndiff = 1 << p;
if (g) {
geodb_add4(geodb, geodb_size,
htobe32(subnet_first), 32 - p, g);
} else {
asdb_add4(asdb, asdb_size,
htobe32(subnet_first), 32 - p, a);
}
subnet_first += ndiff;
} else {
if (g) {
geodb_add4(geodb, geodb_size,
htobe32(subnet_first),
32 - mask_bits, g);
} else {
asdb_add4(asdb, asdb_size,
htobe32(subnet_first),
32 - mask_bits, a);
}
subnet_first = subnet_last + 1;
}
}
}
inline int
clz_128(xe_ip x)
{
int count = 0;
while (!(x & ((xe_ip)1 << (128 - 1)))) {
x = x << 1;
count++;
}
return count;
}
inline int
ctz_128(xe_ip x)
{
int count = 0;
while ((x & 1) == 0) {
x = x >> 1;
count++;
}
return count;
}
static void
add_range6(struct btrie_node_geo *geodb, size_t *geodb_size,
struct btrie_node_as *asdb, size_t *asdb_size,
xe_ip *ip1, xe_ip *ip2, struct geoip_info *g, struct as_info *a)
{
xe_ip subnet_first, subnet_last, end;
/* FIXME: check endianess? */
subnet_first = bswap128(*ip1);
end = bswap128(*ip2);
for (;;) {
int mask_bits;
if (subnet_first > end) {
break;
} else if (subnet_first == end) {
if (g) {
geodb_add6(geodb, geodb_size,
bswap128(subnet_first),
128, g);
} else {
asdb_add6(asdb, asdb_size,
bswap128(subnet_first),
128, a);
}
break;
}
if (subnet_first != 0) {
mask_bits = ctz_128(subnet_first);
subnet_last = subnet_first + ((xe_ip)1 << mask_bits)
- 1;
} else {
mask_bits = 128;
subnet_last = subnet_first - 1;
}
if (subnet_last == end) {
if (g) {
geodb_add6(geodb, geodb_size,
bswap128(subnet_first),
128 - mask_bits, g);
} else {
asdb_add6(asdb, asdb_size,
bswap128(subnet_first),
128 - mask_bits, a);
}
break;
} else if (subnet_last > end) {
xe_ip diff = end - subnet_first + 1;
int p = 128 - clz_128(diff) - 1;
xe_ip ndiff = (xe_ip)1 << p;
if (g) {
geodb_add6(geodb, geodb_size,
bswap128(subnet_first),
128 - p, g);
} else {
asdb_add6(asdb, asdb_size,
bswap128(subnet_first),
128 - p, a);
}
subnet_first += ndiff;
} else {
if (g) {
geodb_add6(geodb, geodb_size,
bswap128(subnet_first),
128 - mask_bits, g);
} else {
asdb_add6(asdb, asdb_size,
bswap128(subnet_first),
128 - mask_bits, a);
}
subnet_first = subnet_last + 1;
}
}
}
/* geo */
static int
process_line_ipapi(struct btrie_node_geo *geodb4, size_t *geodb_size4,
struct btrie_node_geo *geodb6, size_t *geodb_size6,
char *line, char *err)
{
char *lptr = line;
struct geoip_info g;
char ip_ver[5];
char addr1[100], addr2[100];
char tz[100];
struct in_addr ip, ip2;
size_t i;
memset(&g, 0, sizeof(g));
csv_next(&lptr, ip_ver);
csv_next(&lptr, addr1);
csv_next(&lptr, addr2);
csv_next(&lptr, g.CONTINENT);
csv_next(&lptr, g.COUNTRY_CODE);
csv_next(&lptr, g.COUNTRY);
csv_next(&lptr, g.STATE);
csv_next(&lptr, g.CITY);
csv_next(&lptr, g.ZIP);
csv_next(&lptr, tz);
csv_next(&lptr, g.LAT);
csv_next(&lptr, g.LONG);
for (i=0; i<strlen(g.CONTINENT); i++) {
g.CONTINENT[i] = tolower(g.CONTINENT[i]);
}
for (i=0; i<strlen(g.COUNTRY_CODE); i++) {
g.COUNTRY_CODE[i] = tolower(g.COUNTRY_CODE[i]);
}
if (strcmp(ip_ver, "6") == 0) {
/* IPv6 */
xe_ip ipv6_1, ipv6_2;
if (inet_pton(AF_INET6, addr1, &ipv6_1) == 0) {
sprintf(err, "can't parse IPv6 address 1'%s'",
addr1);
return 0;
}
if (inet_pton(AF_INET6, addr2, &ipv6_2) == 0) {
sprintf(err, "can't parse IPv6 address 2'%s'",
addr2);
return 0;
}
add_range6(geodb6, geodb_size6, NULL, NULL,
&ipv6_1, &ipv6_2, &g, NULL);
} else {
/* IPv4 */
if (inet_aton(addr1, &ip) == 0) {
sprintf(err, "can't parse IPv4 address 1'%s'",
addr1);
return 0;
}
if (inet_aton(addr2, &ip2) == 0) {
sprintf(err, "can't parse IPv4 address 2'%s'",
addr2);
return 0;
}
add_range4(geodb4, geodb_size4, NULL, NULL,
ip.s_addr, ip2.s_addr, &g, NULL);
}
return 1;
}
static int
geoip_add_file(struct btrie_node_geo *geodb4, size_t *geodb_size4,
struct btrie_node_geo *geodb6, size_t *geodb_size6,
const char *path)
{
FILE *f;
char line[4096];
char *line_ptr;
size_t line_num = 2;
f = fopen(path, "r");
if (!f) {
LOG("geoip: can't open file '%s': %s", path, strerror(errno));
return 0;
}
/* skip first line */
fgets(line, sizeof(line), f);
if (feof(f)) {
LOG("geoip: file '%s' too short", path);
return 0;
}
if (strcmp(string_trim(line), GEOIP_SIGN_IPAPI) != 0) {
LOG("geoip: file '%s': unknown format", path);
goto fail_format;
}
LOG("geoip: loading file '%s'", path);
for (;;) {
char err[256];
fgets(line, sizeof(line), f);
if (feof(f)) {
break;
}
line_ptr = string_trim(line);
if (!process_line_ipapi(geodb4, geodb_size4,
geodb6, geodb_size6, line_ptr, err)) {
LOG("geoip: file '%s', line #%lu: %s", path,
line_num, err);
}
line_num++;
if (verbose) {
if ((line_num % 100000) == 0) {
LOG("geoip: file '%s', %lu lines loaded",
path, line_num);
}
}
}
LOG("geoip: file '%s' added, %lu lines", path, line_num);
fail_format:
fclose(f);
return 1;
}
static int
process_line_as(struct btrie_node_as *asdb4, size_t *asdb_size4,
struct btrie_node_as *asdb6, size_t *asdb_size6,
char *line, char *err)
{
char *lptr = line;
struct as_info a;
char addr1[100], addr2[100];
char asn[10];
struct in_addr ip, ip2;
xe_ip ipv6_1, ipv6_2;
memset(&a, 0, sizeof(a));
csv_next(&lptr, addr1);
csv_next(&lptr, addr2);
csv_next(&lptr, asn);
csv_next(&lptr, a.asd);
a.asn = htobe32(atoi(asn));
if (inet_pton(AF_INET6, addr1, &ipv6_1) == 0) {
/* can't parse as IPv6, so it's probably IPv4 */
if (inet_aton(addr1, &ip) == 0) {
sprintf(err, "can't parse address 1'%s'", addr1);
return 0;
}
if (inet_aton(addr2, &ip2) == 0) {
sprintf(err, "can't parse address 2'%s'", addr2);
return 0;
}
add_range4(NULL, NULL, asdb4, asdb_size4,
ip.s_addr, ip2.s_addr, NULL, &a);
} else {
/* IPv6 */
if (inet_pton(AF_INET6, addr2, &ipv6_2) == 0) {
sprintf(err, "can't parse IPv6 address 2'%s'", addr2);
return 0;
}
add_range6(NULL, NULL, asdb6, asdb_size6,
&ipv6_1, &ipv6_2, NULL, &a);
}
return 1;
}
static int
as_add_file(struct btrie_node_as *asdb4, size_t *asdb_size4,
struct btrie_node_as *asdb6, size_t *asdb_size6,
const char *path)
{
FILE *f;
char line[4096];
size_t line_num = 1;
char *line_ptr;
f = fopen(path, "r");
if (!f) {
LOG("as: can't open file '%s': %s", path, strerror(errno));
return 0;
}
LOG("as: loading file '%s'", path);
for (;;) {
char err[256];
fgets(line, sizeof(line), f);
if (feof(f)) {
break;
}
line_ptr = string_trim(line);
if (!process_line_as(asdb4, asdb_size4,
asdb6, asdb_size6,
line_ptr, err)) {
LOG("as: file '%s', line #%lu: %s",
path, line_num, err);
}
line_num++;
if (verbose) {
if ((line_num % 100000) == 0) {
LOG("as: file '%s', %lu lines loaded",
path, line_num);
}
}
}
LOG("as: file '%s' added, %lu lines", path, line_num);
fclose(f);
return 1;
}
static void
print_usage(const char *progname)
{
fprintf(stderr,
"Usage: %s [-o out_dir] [-s max_file_size] [-v] -t type "
"dbfile1.csv [dbfile2.csv ...]\n",
progname);
fprintf(stderr, "\t-o /path/to/dir: where output files will be placed\n");
fprintf(stderr, "\t-s max_file_size: max size (in megabytes)"
" of a single result file\n");
fprintf(stderr, "\t-v show message on each 100000 lines loaded\n");
fprintf(stderr, "\t-t geo: data files of GeoIP\n");
fprintf(stderr, "\t-t as: data files of AS info\n");
fprintf(stderr, "\tdbfile1.csv, dbfile2.csv, etc.: data files\n");
fprintf(stderr, "\n %s -h\n", progname);
fprintf(stderr, "\t-h: print this message\n");
}
static void *
make_cache(const char *cache, off_t max_size)
{
void *addr = NULL;
int fd;
fd = open(cache, O_RDWR | O_CREAT, 0644);
if (fd == -1) {
LOG("Can't open cache file '%s': %s", cache, strerror(errno));
goto fail_open;
return NULL;
}
if (ftruncate(fd, max_size) != 0) {
LOG("ftruncate() failed on file '%s': %s",
cache, strerror(errno));
goto fail_trunc;
}
addr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == (void *) -1) {
LOG("mmap() failed on file '%s': %s", cache, strerror(errno));
addr = NULL;
goto fail_mmap;
}
fail_mmap:
fail_trunc:
close(fd);
fail_open:
return addr;
}
int
main(int argc, char *argv[])
{
int opt, i;
char out_dir[PATH_MAX] = "./"; /* current dir by default */
char type[10] = "";
int max_size_m = 4*1024; /* 4G by default */
size_t max_size;
void *db4, *db6;
size_t db4_size, db6_size;
char path4[PATH_MAX + 32], path6[PATH_MAX + 32];
openlog(NULL, LOG_PERROR, LOG_USER);
while ((opt = getopt(argc, argv, "ho:s:t:v")) != -1) {
switch (opt) {
case 'o':
strcpy(out_dir, optarg);
break;
case 's':
max_size_m = atoi(optarg);
if (max_size_m <= 0) {
LOG("Incorrect value for"
" max file size '%s'", optarg);
print_usage(argv[0]);
return EXIT_FAILURE;
}
break;
case 't':
strcpy(type, optarg);
break;
case 'v':
verbose = 1;
break;
case 'h':
default:
print_usage(argv[0]);
return EXIT_FAILURE;
}
}
max_size = (size_t)max_size_m * 1024 * 1024;
if (type[0] == '\0') {
LOG("Type ('geo' or 'as') not specified");
print_usage(argv[0]);
return EXIT_FAILURE;
}
if ((strcmp(type, "geo") != 0) && (strcmp(type, "as") != 0)) {
LOG("Unknown type '%s', should be 'geo' or 'as'", type);
print_usage(argv[0]);
return EXIT_FAILURE;
}
if (optind == argc) {
LOG("No input files");
print_usage(argv[0]);
return EXIT_FAILURE;
}
sprintf(path4, "%s/%s4.db", out_dir, type);
db4 = make_cache(path4, max_size);
if (!db4) {
return EXIT_FAILURE;
}
sprintf(path6, "%s/%s6.db", out_dir, type);
db6 = make_cache(path6, max_size);
if (!db6) {
return EXIT_FAILURE;
}
db4_size = db6_size = 0;
for (i=optind; i<argc; i++) {
char *filename;
filename = argv[i];
if (strcmp(type, "geo") == 0) {
geoip_add_file(db4, &db4_size, db6, &db6_size,
filename);
} else {
as_add_file(db4, &db4_size, db6, &db6_size, filename);
}
}
munmap(db4, max_size);
munmap(db6, max_size);
if (strcmp(type, "geo") == 0) {
truncate(path4, db4_size * sizeof(struct btrie_node_geo));
truncate(path6, db6_size * sizeof(struct btrie_node_geo));
} else {
truncate(path4, db4_size * sizeof(struct btrie_node_as));
truncate(path6, db6_size * sizeof(struct btrie_node_as));
}
return EXIT_SUCCESS;
}