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
|
// $Id$
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "bbs.h"
typedef struct {
uint32_t network, netmask;
char desc[32];
} db_entry;
static db_entry *db = NULL;
static int db_len = 0, db_size = 0;
#define DB_INCREMENT 200
int ip_desc_db_reload(const char * cfgfile)
{
FILE *fp;
char buf[256];
char *ip, *mask;
db_entry *new_db = NULL;
int new_db_len = 0, new_db_size = 0;
int result = 0;
if ( (fp = fopen(cfgfile, "r")) == NULL)
return -1;
while (fgets(buf, sizeof(buf), fp)) {
//skip empty lines
if (!buf[0] || buf[0] == '\n')
continue;
ip = strtok(buf, " \t\n");
if (ip == NULL || *ip == '#' || *ip == '@')
continue;
// expand database if nessecary
if (new_db_len == new_db_size) {
void * ptr;
if ( (ptr = realloc(new_db, sizeof(db_entry) *
(new_db_size + DB_INCREMENT))) == NULL ) {
result = 1;
break;
}
new_db = ptr;
new_db_size += DB_INCREMENT;
}
// netmask
if ( (mask = strchr(ip, '/')) != NULL ) {
int shift = 32 - atoi(mask + 1);
new_db[new_db_len].netmask = htonl(0xFFFFFFFFu << shift);
*mask = '\0';
}
else
new_db[new_db_len].netmask = 0xFFFFFFFFu;
// network
new_db[new_db_len].network = htonl(ipstr2int(ip)) &
new_db[new_db_len].netmask;
// description
if ( (ip = strtok(NULL, " \t\n")) == NULL ) {
strcpy(new_db[new_db_len].desc, "雲深不知處");
}
else {
strlcpy(new_db[new_db_len].desc, ip,
sizeof(new_db[new_db_len].desc));
}
new_db_len++;
}
fclose(fp);
if (new_db != NULL) {
free(db);
db = new_db;
db_len = new_db_len;
db_size = new_db_size;
}
return result;
}
const char * ip_desc_db_lookup(const char * ip)
{
int i;
for (i = 0; i < db_len; i++) {
if (db[i].network == (htonl(ipstr2int(ip)) & db[i].netmask)) {
return db[i].desc;
}
}
return ip;
}
|