forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ipchunk.c
executable file
·363 lines (309 loc) · 7.27 KB
/
ipchunk.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
#include <string.h>
#include "utils.h"
#include "ipchunk.h"
static const char *Z0 = NULL;
static const char *Z4 = "\x01";
static const char *Z6noz = "\x02";
/*
IPv6: https://datatracker.ietf.org/doc/html/rfc8200
Zone: https://datatracker.ietf.org/doc/html/rfc6874
CIDR: https://datatracker.ietf.org/doc/html/rfc4632
https://datatracker.ietf.org/doc/html/rfc4291#section-2.3
*/
int IpAddr_BitLength(const IpAddr *ipAddr)
{
if( ipAddr->Zone == Z0 )
{
return 0;
} else if( ipAddr->Zone == Z4 )
{
return 32;
}
return 128;
}
int IpAddr_Is6(const IpAddr *ipAddr)
{
return ipAddr->Zone != Z0 && ipAddr->Zone != Z4;
}
int IpAddr_HasZone(const IpAddr *ipAddr)
{
return IpAddr_Is6(ipAddr) && ipAddr->Zone != Z6noz;
}
int IpAddr_IsValid(const IpAddr *ipAddr)
{
return ipAddr->Zone != Z0;
}
static void IpAddr_SetPrefix4(unsigned char Addr[16])
{
memset(Addr, 0, 10);
*(uint16_t *)(Addr + 10) = 0xffff;
}
void IpAddr_From4(const unsigned char Addr[4], IpAddr *ipAddr)
{
ipAddr->Zone = Z4;
IpAddr_SetPrefix4(ipAddr->Addr);
*(uint32_t *)(ipAddr->Addr + 12) = *(uint32_t *)Addr;
}
void IpAddr_From6(const unsigned char Addr[16], IpAddr *ipAddr)
{
ipAddr->Zone = Z6noz;
memcpy(ipAddr->Addr, Addr, 16);
}
int IpAddr_Parse(const char *s, IpAddr *ipAddr)
{
const char *p = s;
for(; *p; ++p)
{
switch( *p )
{
case '.':
ipAddr->Zone = Z4;
IpAddr_SetPrefix4(ipAddr->Addr);
return IPv4AddressToNum(s, ipAddr->Addr + 12) != 4;
case ':':
p = strchr(p, '%');
if( p == NULL )
{
ipAddr->Zone = Z6noz;
} else {
ipAddr->Zone = p + 1;
}
return IPv6AddressToNum(s, ipAddr->Addr) != 16;
case '%':
break;
}
}
ipAddr->Zone = Z0;
memset(ipAddr->Addr, 0, 16);
return -1;
}
BOOL IpSet_IsSingleIp(const IpSet *ipSet)
{
return ipSet->PrefixBits != 0 && ipSet->PrefixBits == IpAddr_BitLength(&(ipSet->Ip));
}
int IpSet_Parse(const char *s, const char *p, IpSet *ipSet)
{
int n, L;
IpAddr *ipAddr = &(ipSet->Ip);
if( IpAddr_Parse(s, ipAddr) != 0 )
{
return -1;
}
L = IpAddr_BitLength(ipAddr);
if( p != NULL && *p )
{
n = atoi(p);
if( n < 0 )
{
n = -1;
} else if( n > L ) {
n = L;
}
L = n;
if( IpAddr_Is6(ipAddr) )
{
ipSet->Ip.Zone = Z6noz;
L += 10;
}
if( L < 16 )
{
int i = 0;
for( ; L > 0; L -= 8, i++ )
{
ipAddr->Addr[i] &= ~(0xff >> (L >= 8 ? 8 : L));
}
}
} else {
n = L;
}
ipSet->PrefixBits = n;
return 0;
}
static int Contain(IpElement *New, IpElement *Elm)
{
IpSet *ipSetNew = &(New->IpSet);
IpSet *ipSetElm = &(Elm->IpSet);
IpAddr *ipAddrNew = &(ipSetNew->Ip);
IpAddr *ipAddrElm = &(ipSetElm->Ip);
int BitsNew;
int BitsElm;
if( IpAddr_IsValid(ipAddrElm) == FALSE )
{
return -1;
}
BitsNew = IpAddr_BitLength(ipAddrNew);
BitsElm = IpAddr_BitLength(ipAddrElm);
/* 1st: type */
if( BitsNew != BitsElm )
{
return BitsNew - BitsElm;
} else {
unsigned char *bn = ipAddrNew->Addr;
unsigned char *be = ipAddrElm->Addr;
int prefixBitsNew = ipSetNew->PrefixBits;
int prefixBitsElm = ipSetElm->PrefixBits;
int ret = 0;
/* 2nd: prefix value */
if( BitsElm == 32 )
{
bn += 12;
be += 12;
}
for(; prefixBitsElm >= 32; prefixBitsElm -= 32)
{
ret = memcmp(bn, be, 4);
if( ret != 0 )
{
return ret;
}
bn += 4;
be += 4;
}
if( prefixBitsElm > 0 ) {
uint32_t u32New, u32Elm, mask;
mask = htonl(~(~0U >> prefixBitsElm));
u32New = *(uint32_t *)bn & mask;
u32Elm = *(uint32_t *)be & mask;
ret = memcmp(&u32New, &u32Elm, 4);
}
/* 3rd: prefix bits */
if( ret == 0 && prefixBitsNew < prefixBitsElm)
{
ret = 1;
}
/* 4th: zone */
if( ret == 0 )
{
if( IpAddr_HasZone(ipAddrElm) )
{
if( IpAddr_HasZone(ipAddrNew) )
{
return strcmp(ipAddrNew->Zone, ipAddrElm->Zone);
} else {
return 1;
}
}
}
return ret;
}
}
void IpChunk_Free(IpChunk *ic)
{
ic->AddrChunk.Free(&(ic->AddrChunk));
ic->CidrChunk.Free(&(ic->CidrChunk));
ic->Datas.Free(&(ic->Datas));
ic->Extra.Free(&(ic->Extra));
}
int IpChunk_Init(IpChunk *ic)
{
if( Bst_Init(&(ic->AddrChunk), sizeof(IpElement), (CompareFunc)Contain) != 0 )
{
return -1;
}
if( Bst_Init(&(ic->CidrChunk), sizeof(IpElement), (CompareFunc)Contain) != 0 )
{
goto EXIT_1;
}
if( StableBuffer_Init(&(ic->Datas)) != 0 )
{
goto EXIT_2;
}
if( StableBuffer_Init(&(ic->Extra)) != 0 )
{
goto EXIT_3;
}
return 0;
EXIT_3:
ic->Datas.Free(&(ic->Datas));
EXIT_2:
ic->CidrChunk.Free(&(ic->CidrChunk));
EXIT_1:
ic->AddrChunk.Free(&(ic->AddrChunk));
return -1;
}
int IpChunk_Add(IpChunk *ic,
const char *Ip,
int Type,
const void *Data,
uint32_t DataLength
)
{
char *p;
IpElement New;
const IpElement *elm;
char *ip = ic->Extra.Add(&(ic->Extra), Ip, strlen(Ip) + 1, FALSE);
if( ip == NULL )
{
return -1;
}
p = ip;
for(; *p; ++p)
{
if( *p == '/' )
{
*p = 0;
p++;
break;
}
}
if( IpSet_Parse(ip, p, &(New.IpSet)) != 0 )
{
return -1;
}
New.Type = Type;
New.Data = NULL;
if( Data != NULL )
{
New.Data = ic->Datas.Add(&(ic->Datas), Data, DataLength, TRUE);
}
if( IpSet_IsSingleIp(&(New.IpSet)) )
{
elm = ic->AddrChunk.Add(&(ic->AddrChunk), &New);
} else {
elm = ic->CidrChunk.Add(&(ic->CidrChunk), &New);
}
return elm == NULL;
}
BOOL IpChunk_Find(IpChunk *ic, unsigned char *Ip, int IpBytes, int *Type, const char **Data)
{
IpElement Key;
const IpElement *Result = NULL;
if( ic == NULL )
{
return FALSE;
}
switch( IpBytes )
{
case 4:
IpAddr_From4(Ip, &(Key.IpSet.Ip));
Key.IpSet.PrefixBits = 32;
break;
case 16:
IpAddr_From6(Ip, &(Key.IpSet.Ip));
Key.IpSet.PrefixBits = 128;
break;
default:
return FALSE;
}
Key.Type = 0;
Key.Data = NULL;
Result = ic->AddrChunk.Search(&(ic->AddrChunk), &Key, NULL);
if( Result == NULL )
{
Result = ic->CidrChunk.Search(&(ic->CidrChunk), &Key, NULL);
}
if( Result == NULL )
{
return FALSE;
} else {
if( Type != NULL )
{
*Type = Result->Type;
}
if( Data != NULL )
{
*Data = Result->Data;
}
return TRUE;
}
}