-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2c.c
165 lines (129 loc) · 3.33 KB
/
ip2c.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
#include <Winsock2.h>
#endif
#include "ip2c.h"
CountryRangeTreeNode *ip2c_new_node(IPDBItem *data, unsigned int key)
{
CountryRangeTreeNode *node = malloc(sizeof(CountryRangeTreeNode));
if (node == NULL)
return NULL;
node->min = data[key].start;
node->max = data[key].end;
node->key = key;
node->left = NULL;
node->right = NULL;
return node;
}
unsigned int ip2c_interval(IPDBItem* data)
{
return data->end - data->start;
}
void ip2c_free_tree(CountryRangeTreeNode *node)
{
if(node == NULL)
return;
ip2c_free_tree(node->right);
ip2c_free_tree(node->left);
free(node);
}
CountryRangeTreeNode *ip2c_build_tree(IPDBItem *data, int floor, int ceil)
{
int mid;
CountryRangeTreeNode *node;
if(floor > ceil)
return NULL;
mid = (floor + ceil) / 2;
node = ip2c_new_node(data, mid);
node->right = ip2c_build_tree(data, mid + 1, ceil);
node->left = ip2c_build_tree(data, floor, mid - 1);
if(node->right && (node->right->min < node->min))
node->min = node->right->min;
if(node->left && (node->left->min < node->min))
node->min = node->left->min;
if(node->right && (node->right->max > node->max))
node->max = node->right->max;
if(node->left && (node->left->max > node->max))
node->max = node->left->max;
return node;
}
IPDBItem *ip2c_search(IPDBItem *data, CountryRangeTreeNode *node, ip2c_ip ip)
{
IPDBItem *item, *found_right, *found_left, *found = NULL;
if(node == NULL)
return NULL;
item = &data[node->key];
if((ip < node->min) || (ip > node->max))
return NULL;
if((ip >= item->start) && (ip <= item->end))
found = item;
if(found_right = ip2c_search(data, node->right, ip)) {
if(found){
if(ip2c_interval(found_right) < ip2c_interval(found)){
found = found_right;
}
} else {
found = found_right;
}
}
if(found_left = ip2c_search(data, node->left, ip)) {
if(found){
if(ip2c_interval(found_left) < ip2c_interval(found)){
found = found_left;
}
} else {
found = found_left;
}
}
return found;
}
IPDB *ip2c_db_load_file(const char *file_name)
{
IPDB *db = NULL;
FILE *f;
if ((f = fopen(file_name, "rb")) == NULL)
return NULL;
if ((db = malloc(sizeof(IPDB))) == NULL)
return NULL;
fread(&db->ident, strlen(IP2C_DB_IDENT) + 1, 1, f);
if (strcmp(IP2C_DB_IDENT, db->ident) != 0) {
fclose(f);
return NULL;
}
fread(&db->vers_hi, sizeof(db->vers_hi), 1, f);
fread(&db->vers_lo, sizeof(db->vers_lo), 1, f);
fread(&db->rec_count, sizeof(db->rec_count), 1, f);
fread(&db->ip_count, sizeof(db->ip_count), 1, f);
if (db->data = malloc(sizeof(IPDBItem) * db->rec_count))
fread(db->data, sizeof(IPDBItem), db->rec_count, f);
fclose(f);
if (db->data == NULL)
return NULL;
if (db->root = ip2c_build_tree(db->data, 0, db->rec_count - 1))
return db;
else
return NULL;
}
void ip2c_db_free(IPDB *db)
{
if(db){
ip2c_free_tree(db->root);
free(db->data);
}
}
unsigned int ip2c_getcountry(const IPDB *db, const unsigned int *ip_array, const unsigned int ip_array_size, ip2c_iso *iso_codes)
{
unsigned int r, found_c = 0;
IPDBItem *item;
for (r = 0; r < ip_array_size; r++)
if ((item = ip2c_search(db->data, db->root, ip_array[r])) && ++found_c)
strncpy(iso_codes[r], item->iso, sizeof(ip2c_iso));
return found_c;
}
ip2c_ip ip2c_ip2long(const char *ip)
{
return ntohl(inet_addr(ip));
}