forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipchunk.c
executable file
·195 lines (160 loc) · 3.66 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
#include <string.h>
#include "utils.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; /* 4 < 10 < 16 */
if( Bst_Init(&(ic->Chunk), sizeof(IpElement), (CompareFunc)Compare) != 0 )
{
return -1;
}
if( StableBuffer_Init(&(ic->Datas)) != 0 )
{
return -1;
}
if( ic->Chunk.Add(&(ic->Chunk), &Root) == NULL )
{
return -37;
}
return 0;
}
int IpChunk_Add(IpChunk *ic,
uint32_t Ip,
int Type,
const void *Data,
uint32_t DataLength
)
{
StableBuffer *sb = &(ic->Datas);
IpElement New;
New.IpLength = 4;
New.Ip.Ipv4 = Ip;
New.Type = Type;
New.Data = NULL;
if( Data != NULL )
{
New.Data = sb->Add(sb, Data, DataLength, TRUE);
}
return ic->Chunk.Add(&(ic->Chunk), &New) == NULL;
}
int IpChunk_AddFromString(IpChunk *ic,
const char *Ip,
int Type,
const char *Data,
uint32_t DataLength
)
{
uint32_t IpNum = 0;
IPv4AddressToNum(Ip, &IpNum);
return IpChunk_Add(ic, IpNum, Type, Data, DataLength);
}
int IpChunk_Add6(IpChunk *ic, const char *Ipv6, int Type, const char *Data, uint32_t DataLength)
{
StableBuffer *sb = &(ic->Datas);
IpElement New;
New.IpLength = 16;
memcpy(New.Ip.Ipv6, Ipv6, 16);
New.Type = Type;
New.Data = NULL;
if( Data != NULL )
{
New.Data = sb->Add(sb, Data, DataLength, TRUE);
}
return ic->Chunk.Add(&(ic->Chunk), &New) == NULL;
}
int IpChunk_Add6FromString(IpChunk *ic,
const char *Ip,
int Type,
const char *Data,
uint32_t DataLength
)
{
char IpNum[16];
IPv6AddressToNum(Ip, IpNum);
return IpChunk_Add6(ic, IpNum, Type, Data, DataLength);
}
int IpChunk_AddAnyFromString(IpChunk *ic,
const char *Ip,
int Type,
const char *Data,
uint32_t DataLength
)
{
if( strchr(Ip, ':') != NULL )
{
return IpChunk_Add6FromString(ic, Ip, Type, Data, DataLength);
} else {
return IpChunk_AddFromString(ic, Ip, Type, Data, DataLength);
}
}
BOOL IpChunk_Find(IpChunk *ic, uint32_t Ip, int *Type, const char **Data)
{
IpElement Key;
const IpElement *Result;
if( ic == NULL )
{
return FALSE;
}
Key.IpLength = 4;
Key.Ip.Ipv4 = Ip;
Key.Type = 0;
Key.Data = NULL;
Result = ic->Chunk.Search(&(ic->Chunk), &Key, NULL);
if( Result == NULL )
{
return FALSE;
} else {
if( Type != NULL )
{
*Type = Result->Type;
}
if( Data != NULL )
{
*Data = Result->Data;
}
return TRUE;
}
}
BOOL IpChunk_Find6(IpChunk *ic, const char *Ipv6, int *Type, const char **Data)
{
IpElement Key;
const IpElement *Result;
if( ic == NULL )
{
return FALSE;
}
Key.IpLength = 16;
memcpy(Key.Ip.Ipv6, Ipv6, 16);
Key.Type = 0;
Key.Data = NULL;
Result = ic->Chunk.Search(&(ic->Chunk), &Key, NULL);
if( Result == NULL )
{
return FALSE;
} else {
if( Type != NULL )
{
*Type = Result->Type;
}
if( Data != NULL )
{
*Data = Result->Data;
}
return TRUE;
}
}