-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstore.h
291 lines (240 loc) · 11 KB
/
store.h
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
// store.h
// ipdbtools
//
// Created by Dr. Rolf Jansen on 2016-07-10.
// Copyright © 2016-2018 Dr. Rolf Jansen. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma mark ••• AVL Tree of IPv4-Ranges •••
typedef union
{
uint8_t byte[4];
uint32_t number;
} IP4Desc;
typedef struct
{
uint32_t lo, hi;
uint32_t cc;
char nso[36];
} IP4Set;
typedef struct IP4Node
{
uint32_t lo, hi; // IPv4 number range
uint32_t cc; // country code
char nso[36]; // unique identifier of the network segment owner
int32_t B; // house holding
struct IP4Node *L, *R;
} IP4Node;
IP4Node *findIP4Node(uint32_t ip, IP4Node *node);
IP4Node *findNet4Node(uint32_t lo, uint32_t hi, uint32_t cc, char *nso, IP4Node *node);
int addIP4Node(uint32_t lo, uint32_t hi, uint32_t cc, char *nso, IP4Node **node);
int removeIP4Node(uint32_t ip, IP4Node **node);
void serializeIP4Tree(FILE *out, IP4Node *node);
void releaseIP4Tree(IP4Node *node);
static inline int bisectionIP4Search(uint32_t ip4, IP4Set sortedIP4Sets[], int count)
{
uint32_t u;
int o, p, q;
for (p = 0, q = count-1; p <= q;)
{
o = (p + q) >> 1;
if ((u = sortedIP4Sets[o].lo) <= ip4 && ip4 <= sortedIP4Sets[o].hi)
return o;
else if (ip4 < u)
q = o-1;
else // (ip4 > sortedIP4Sets[o].hi)
p = o+1;
}
return -1;
}
#pragma mark ••• AVL Tree of IPv6-Ranges •••
typedef union
{
uint64_t quad[2];
uint16_t word[8];
uint128t number;
} IP6Desc;
typedef struct
{
uint128t lo, hi;
uint32_t cc;
char nso[36];
} IP6Set;
typedef struct IP6Node
{
uint128t lo, hi; // IPv4 number range
uint32_t cc; // country code
char nso[36]; // unique identifier of the network segment owner
int32_t B; // house holding
struct IP6Node *L, *R;
} IP6Node;
IP6Node *findIP6Node(uint128t ip, IP6Node *node);
IP6Node *findNet6Node(uint128t lo, uint128t hi, uint32_t cc, char *nso, IP6Node *node);
int addIP6Node(uint128t lo, uint128t hi, uint32_t cc, char *nso, IP6Node **node);
int removeIP6Node(uint128t ip, IP6Node **node);
void serializeIP6Tree(FILE *out, IP6Node *node);
void releaseIP6Tree(IP6Node *node);
static inline int bisectionIP6Search(uint128t ip6, IP6Set sortedIP6Sets[], int count)
{
uint128t u;
int o, p, q;
for (p = 0, q = count-1; p <= q;)
{
o = (p + q) >> 1;
if (le_u128(u = sortedIP6Sets[o].lo, ip6) && le_u128(ip6, sortedIP6Sets[o].hi))
return o;
else if (lt_u128(ip6, u))
q = o-1;
else // (gt_u128(ip6, sortedIP6Sets[o].hi))
p = o+1;
}
return -1;
}
#pragma mark ••• AVL Tree of Country Codes •••
typedef struct CCNode
{
uint32_t cc; // country code
uint32_t val; // value
int32_t B; // house holding
struct CCNode *L, *R;
} CCNode;
CCNode *findCCNode(uint32_t cc, CCNode *node);
int addCCNode(uint32_t cc, uint32_t val, CCNode **node);
int removeCCNode(uint32_t cc, CCNode **node);
void releaseCCTree(CCNode *node);
#pragma mark ••• Pseudo Hash Table of Country Codes •••
#define ccTableSize 676
static inline uint32_t cce(uint16_t cc)
{
uint8_t *ca = (uint8_t *)&cc;
return (ca[b2_0]-'A')*26 + (ca[b2_1]-'A'); // AA to ZZ ranges from 0 to 675
}
CCNode **createCCTable(void);
void releaseCCTable(CCNode *table[]);
CCNode *findCC(CCNode *table[], uint32_t cc);
void storeCC(CCNode *table[], uint32_t cc, uint32_t val);
void removeCC(CCNode *table[], uint32_t cc);
#pragma mark ••• AVL Tree of unique Net Segements Owner ID's •••
typedef struct NSONode
{
char *nso; // net segmenet owner ID is the key
uint32_t val; // value
int B; // house holding
struct NSONode *L, *R;
} NSONode;
// CAUTION: The following recursive functions must not be called with nso == NULL.
// For performace reasons no extra error cheking is done.
NSONode *findNSONode(const char *nso, NSONode *node);
int addNSONode(const char *nso, int nsl, uint32_t val, NSONode **node);
int removeNSONode(const char *nso, int nsl, NSONode **node);
void releaseNSOTree(NSONode *node);
#pragma mark ••• Hash Table of unique Net Segements Owner ID's •••
NSONode **createNSOTable(uint n);
void releaseNSOTable(NSONode *table[]);
// Storing and retrieving nso's
NSONode *findNSO(NSONode *table[], const char *nso);
void storeNSO(NSONode *table[], const char *nso, int nsl, uint32_t val);
void removeNSO(NSONode *table[], const char *nso, int nsl);
#pragma mark ••• IP number/string utility functions •••
#include <sys/socket.h>
#include <arpa/inet.h>
static inline uint32_t ipv4_str2bin(char *str)
{
uint32_t bin;
return (inet_pton(AF_INET, str, &bin) > 0)
? SwapInt32(bin)
: 0;
}
typedef char IP4Str[16];
static inline char *ipv4_bin2str(uint32_t bin, char *str)
{
IP4Desc ipdsc = {.number = bin};
sprintf(str, "%d.%d.%d.%d", ipdsc.byte[b4_3], ipdsc.byte[b4_2], ipdsc.byte[b4_1], ipdsc.byte[b4_0]);
return str;
}
static inline uint128t ipv6_str2bin(char *str)
{
uint64_t bin[2];
return (inet_pton(AF_INET6, str, &bin) > 0)
? (IP6Desc){SwapInt64(bin[b2_1]), SwapInt64(bin[b2_0])}.number
: u64_to_u128t(0);
}
typedef char IP6Str[40];
static inline char *ipv6_bin2str(uint128t bin, char *str)
{
IP6Desc ipdsc = {.number = bin};
sprintf(str, "%x:%x:%x:%x:%x:%x:%x:%x", ipdsc.word[b8_7], ipdsc.word[b8_6], ipdsc.word[b8_5], ipdsc.word[b8_4],
ipdsc.word[b8_3], ipdsc.word[b8_2], ipdsc.word[b8_1], ipdsc.word[b8_0]);
return str;
}
static inline int32_t intlb4_1p(double v)
{
return (int32_t)log2(v+1);
}
static const IP6Desc v6[129] =
{
{0x0, 0x0}, {0x1, 0x0}, {0x3, 0x0}, {0x7, 0x0}, {0xF, 0x0}, {0x1F, 0x0}, {0x3F, 0x0}, {0x7F, 0x0}, {0xFF, 0x0},
{0x1FF, 0x0}, {0x3FF, 0x0}, {0x7FF, 0x0}, {0xFFF, 0x0}, {0x1FFF, 0x0}, {0x3FFF, 0x0}, {0x7FFF, 0x0}, {0xFFFF, 0x0},
{0x1FFFF, 0x0}, {0x3FFFF, 0x0}, {0x7FFFF, 0x0}, {0xFFFFF, 0x0}, {0x1FFFFF, 0x0}, {0x3FFFFF, 0x0}, {0x7FFFFF, 0x0},
{0xFFFFFF, 0x0}, {0x1FFFFFF, 0x0}, {0x3FFFFFF, 0x0}, {0x7FFFFFF, 0x0}, {0xFFFFFFF, 0x0}, {0x1FFFFFFF, 0x0}, {0x3FFFFFFF, 0x0},
{0x7FFFFFFF, 0x0}, {0xFFFFFFFF, 0x0}, {0x1FFFFFFFF, 0x0}, {0x3FFFFFFFF, 0x0}, {0x7FFFFFFFF, 0x0}, {0xFFFFFFFFF, 0x0},
{0x1FFFFFFFFF, 0x0}, {0x3FFFFFFFFF, 0x0}, {0x7FFFFFFFFF, 0x0}, {0xFFFFFFFFFF, 0x0}, {0x1FFFFFFFFFF, 0x0}, {0x3FFFFFFFFFF, 0x0},
{0x7FFFFFFFFFF, 0x0}, {0xFFFFFFFFFFF, 0x0}, {0x1FFFFFFFFFFF, 0x0}, {0x3FFFFFFFFFFF, 0x0}, {0x7FFFFFFFFFFF, 0x0},
{0xFFFFFFFFFFFF, 0x0}, {0x1FFFFFFFFFFFF, 0x0}, {0x3FFFFFFFFFFFF, 0x0}, {0x7FFFFFFFFFFFF, 0x0}, {0xFFFFFFFFFFFFF, 0x0},
{0x1FFFFFFFFFFFFF, 0x0}, {0x3FFFFFFFFFFFFF, 0x0}, {0x7FFFFFFFFFFFFF, 0x0}, {0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xFFFFFFFFFFFFFFFF, 0x1FFFFFFFFFFFFFF}, {0xFFFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFF},
{0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFF}, {0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF}, {0xFFFFFFFFFFFFFFFF, 0x1FFFFFFFFFFFFFFF}, {0xFFFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF},
{0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF}, {0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}
};
static inline uint128t inteb6_m1(int32_t e)
{
return (0 <= e && e <= 127) ? v6[e].number : u64_to_u128t(1);
}
static inline int32_t intlb6_1p(uint128t v)
{
uint128t u;
int o, p, q;
for (p = 0, q = 127; p <= q;)
{
o = (p + q) >> 1;
if (le_u128(u = v6[o].number, v) && lt_u128(v, v6[o+1].number))
return o;
else if (lt_u128(v, u))
q = o-1;
else // (ge_u128(v, v6[o+1]))
p = o+1;
}
return 0;
}