forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipchunk.c
executable file
·153 lines (125 loc) · 2.75 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
#include <string.h>
#include "ipchunk.h"
static int Compare(IpElement *_1, IpElement *_2)
{
if( _1 -> IpLength != _2 -> IpLength )
{
return _1 -> IpLength - _2 -> IpLength;
} else {
if( _1 -> IpLength == 4 )
{
return _1 -> Ip.Ipv4 - _2 -> Ip.Ipv4;
} else {
return memcmp(_1 -> Ip.Ipv6, _2 -> Ip.Ipv6, _1 -> IpLength);
}
}
}
int IpChunk_Init(IpChunk *ic)
{
IpElement Root;
Root.IpLength = 10;
if( Bst_Init(&(ic -> Chunk), NULL, sizeof(IpElement), (int (*)(const void *, const void *))Compare) != 0 )
{
return -1;
}
if( ExtendableBuffer_Init(&(ic -> Datas), 0, -1) != 0 )
{
Array_Free(ic -> Chunk.Nodes);
return -1;
}
return Bst_Add(&(ic -> Chunk), &Root);
}
int IpChunk_Add(IpChunk *ic, uint32_t Ip, int Type, const char *Data, uint32_t DataLength)
{
IpElement New;
New.IpLength = 4;
New.Ip.Ipv4 = Ip;
New.Type = Type;
New.DataOffset = -1;
if( Data != NULL )
{
New.DataOffset = ExtendableBuffer_Add(&(ic -> Datas), Data, DataLength);
}
return Bst_Add(&(ic -> Chunk), &New);
}
int IpChunk_Add6(IpChunk *ic, const char *Ipv6, int Type, const char *Data, uint32_t DataLength)
{
IpElement New;
New.IpLength = 16;
memcpy(New.Ip.Ipv6, Ipv6, 16);
New.Type = Type;
New.DataOffset = -1;
if( Data != NULL )
{
New.DataOffset = ExtendableBuffer_Add(&(ic -> Datas), Data, DataLength);
}
return Bst_Add(&(ic -> Chunk), &New);
}
BOOL IpChunk_Find(IpChunk *ic, uint32_t Ip, int *Type, const char **Data)
{
IpElement Key;
int32_t Result;
Key.IpLength = 4;
Key.Ip.Ipv4 = Ip;
Key.Type = 0;
Key.DataOffset = -1;
Result = Bst_Search(&(ic -> Chunk), &Key, NULL);
if( Result < 0 )
{
return FALSE;
} else {
IpElement *IpResult;
if( Type != NULL || Data != NULL )
{
IpResult = Bst_GetDataByNumber(&(ic -> Chunk), Result);
if( Type != NULL )
{
*Type = IpResult -> Type;
}
if( Data != NULL )
{
if( IpResult -> DataOffset < 0 )
{
*Data = NULL;
} else {
*Data = ExtendableBuffer_GetPositionByOffset(&(ic -> Datas), IpResult -> DataOffset);
}
}
}
return TRUE;
}
}
BOOL IpChunk_Find6(IpChunk *ic, const char *Ipv6, int *Type, const char **Data)
{
IpElement Key;
int32_t Result;
Key.IpLength = 16;
memcpy(Key.Ip.Ipv6, Ipv6, 16);
Key.Type = 0;
Key.DataOffset = -1;
Result = Bst_Search(&(ic -> Chunk), &Key, NULL);
if( Result < 0 )
{
return FALSE;
} else {
IpElement *IpResult;
if( Type != NULL || Data != NULL )
{
IpResult = Bst_GetDataByNumber(&(ic -> Chunk), Result);
if( Type != NULL )
{
*Type = IpResult -> Type;
}
if( Data != NULL )
{
if( IpResult -> DataOffset < 0 )
{
*Data = NULL;
} else {
*Data = ExtendableBuffer_GetPositionByOffset(&(ic -> Datas), IpResult -> DataOffset);
}
}
}
return TRUE;
}
}