forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
addresslist.c
executable file
·229 lines (183 loc) · 5.1 KB
/
addresslist.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "addresslist.h"
#include "common.h"
#include "utils.h"
int AddressList_Init(AddressList *a)
{
if( a == NULL )
{
return 0;
}
if( Array_Init(&(a->AddressList), sizeof(Address_Type), 8, FALSE, NULL) != 0 )
{
return -1;
}
a->Counter = 0;
return 0;
}
int AddressList_Add(AddressList *a, const Address_Type *Addr)
{
if( a == NULL )
{
return -1;
}
if( Array_PushBack(&(a->AddressList), Addr, NULL) < 0 )
{
return -1;
} else {
return 0;
}
}
sa_family_t AddressList_ConvertFromString(Address_Type *Out, const char *Addr_Port, int DefaultPort)
{
sa_family_t Family;
memset(Out, 0, sizeof(Address_Type));
Family = GetAddressFamily(Addr_Port);
Out->family = Family;
switch( Family )
{
case AF_INET6:
{
char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII + 1] = {0};
in_port_t Port;
const char *PortPos;
memset(Addr, 0, sizeof(Addr));
PortPos = strchr(Addr_Port, ']');
if( PortPos == NULL )
{
return AF_UNSPEC;
}
/* LENGTH_OF_IPV6_ADDRESS_ASCII = 45 */
sscanf(Addr_Port + 1, "%45[^]]", Addr);
PortPos = strchr(PortPos, ':');
if( PortPos == NULL )
{
Port = DefaultPort;
} else {
sscanf(PortPos + 1, "%hu", &Port);
}
Out->Addr.Addr6.sin6_family = Family;
Out->Addr.Addr6.sin6_port = htons(Port);
IPv6AddressToNum(Addr, &(Out->Addr.Addr6.sin6_addr));
return AF_INET6;
}
break;
case AF_INET:
{
char Addr[LENGTH_OF_IPV4_ADDRESS_ASCII + 1] = {0};
in_port_t Port;
const char *PortPos;
memset(Addr, 0, sizeof(Addr));
PortPos = strchr(Addr_Port, ':');
/* LENGTH_OF_IPV4_ADDRESS_ASCII = 15 */
sscanf(Addr_Port, "%15[^:]", Addr);
if( PortPos == NULL )
{
Port = DefaultPort;
} else {
sscanf(PortPos + 1, "%hu", &Port);
}
FILL_ADDR4(Out->Addr.Addr4, Family, Addr, Port);
return AF_INET;
}
break;
default:
return AF_UNSPEC;
break;
}
}
int AddressList_Add_From_String(AddressList *a, const char *Addr_Port, int DefaultPort)
{
Address_Type Tmp;
if( AddressList_ConvertFromString(&Tmp, Addr_Port, DefaultPort) == AF_UNSPEC )
{
return -1;
}
return AddressList_Add(a, &Tmp);
}
int AddressList_Advance(AddressList *a)
{
if( a == NULL )
{
return 0;
}
return (a->Counter)++;
}
struct sockaddr *AddressList_GetOneBySubscript(AddressList *a, sa_family_t *family, int Subscript)
{
Address_Type *Result;
if( a == NULL )
{
return 0;
}
Result = (Address_Type *)Array_GetBySubscript(&(a->AddressList), Subscript);
if( Result == NULL )
{
return NULL;
} else {
if( family != NULL )
{
*family = Result->family;
}
return (struct sockaddr *)&(Result->Addr);
}
}
struct sockaddr *AddressList_GetOne(AddressList *a, sa_family_t *family)
{
return AddressList_GetOneBySubscript(a, family, a->Counter % Array_GetUsed(&(a->AddressList)));
}
struct sockaddr **AddressList_GetPtrListOfFamily(AddressList *a, sa_family_t family)
{
int Itr;
int NumberOfAddresses = AddressList_GetNumberOfAddresses(a);
struct sockaddr **AddrList, **AddrList_Ori;
struct sockaddr *OneAddr;
sa_family_t OneFamily = AF_UNSPEC;
AddrList = SafeMalloc(sizeof(struct sockaddr *) * (NumberOfAddresses + 1));
if( AddrList == NULL )
{
return NULL;
}
AddrList_Ori = AddrList;
Itr = 0;
while( Itr != NumberOfAddresses )
{
OneAddr = AddressList_GetOneBySubscript(a, &OneFamily, Itr);
if( OneFamily == family )
{
*AddrList = OneAddr;
++AddrList;
}
++Itr;
}
*AddrList = NULL;
return AddrList_Ori;
}
struct sockaddr **AddressList_GetPtrList(AddressList *a, sa_family_t **families)
{
int Itr;
int NumberOfAddresses = AddressList_GetNumberOfAddresses(a);
struct sockaddr **AddrList;
AddrList = SafeMalloc(sizeof(struct sockaddr *) * (NumberOfAddresses + 1));
if( AddrList == NULL )
{
return NULL;
}
*families = SafeMalloc(sizeof(sa_family_t) * (NumberOfAddresses + 1));
if( *families == NULL )
{
SafeFree(AddrList);
return NULL;
}
Itr = 0;
while( Itr != NumberOfAddresses )
{
AddrList[Itr] = AddressList_GetOneBySubscript(a, &((*families)[Itr]), Itr);
++Itr;
}
AddrList[Itr] = NULL;
(*families)[Itr] = AF_UNSPEC;
return AddrList;
}