forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goodiplist.c
executable file
·270 lines (218 loc) · 5.4 KB
/
goodiplist.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
#include <string.h>
#include <limits.h>
#include "readconfig.h"
#include "goodiplist.h"
#include "utils.h"
#include "timedtask.h"
#include "socketpuller.h"
#include "logs.h"
typedef struct _ListInfo{
int Interval;
Array List;
} ListInfo;
static StringChunk *GoodIpList = NULL;
/* The fastest returned */
static struct sockaddr_in *CheckAList(struct sockaddr_in *Ips, int Count)
{
SocketPuller p;
int i;
struct timeval Time = {5, 0};
struct sockaddr_in **Fastest = NULL;
struct sockaddr_in *ret = NULL;
if( SocketPuller_Init(&p) != 0 )
{
return NULL;
}
for( i = 0; i != Count; ++i )
{
SOCKET skt;
struct sockaddr *a;
skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if( skt == INVALID_SOCKET )
{
continue;
}
SetSocketNonBlock(skt, TRUE);
a = (struct sockaddr *)&(Ips[i]);
if( connect(skt, a, sizeof(struct sockaddr_in)) != 0 &&
FatalErrorDecideding(GET_LAST_ERROR()) != 0
)
{
CLOSE_SOCKET(skt);
continue;
}
p.Add(&p, skt, &a, sizeof(struct sockaddr *));
}
if( p.Select(&p, &Time, (void **)&Fastest, FALSE, TRUE) == INVALID_SOCKET )
{
ret = NULL;
}
if( Fastest == NULL )
{
ret = NULL;
} else {
ret = *Fastest;
}
p.Free(&p);
return ret;
}
static int ThreadJod(const char *Domain, ListInfo *inf)
{
struct sockaddr_in *Fastest, *First;
if( inf == NULL )
{
return -159;
}
Fastest = CheckAList((struct sockaddr_in *)Array_GetRawArray(&(inf->List)),
Array_GetUsed(&(inf->List))
);
if( Fastest != NULL )
{
struct sockaddr_in t;
INFO("The fastest ip for `%s' is %s\n",
Domain,
inet_ntoa(Fastest->sin_addr)
);
First = Array_GetBySubscript(&(inf->List), 0);
if( First == NULL )
{
return -178;
}
memcpy(&t, Fastest, sizeof(struct sockaddr_in));
memcpy(Fastest, First, sizeof(struct sockaddr_in));
memcpy(First, &t, sizeof(struct sockaddr_in));
} else {
INFO("Checking list `%s' timeout.\n", Domain);
}
return 0;
}
/* GoodIPList list1 1000 */
static int InitListsAndTimes(ConfigFileInfo *ConfigInfo)
{
StringList *l = ConfigGetStringList(ConfigInfo, "GoodIPList");
StringListIterator sli;
const char *Itr = NULL;
if( l == NULL )
{
return -1;
}
if( StringListIterator_Init(&sli, l) != 0 )
{
return -2;
}
GoodIpList = SafeMalloc(sizeof(StringChunk));
if( GoodIpList != NULL && StringChunk_Init(GoodIpList, NULL) != 0 )
{
return -3;
}
while( (Itr = sli.Next(&sli)) != NULL )
{
ListInfo m = {0, Array_Init_Static(sizeof(struct sockaddr_in))};
char n[128];
sscanf(Itr, "%127s%d", n, &(m.Interval));
if( m.Interval <= 0 )
{
ERRORMSG("GoodIpList is invalid : %s\n", Itr);
continue;
}
StringChunk_Add(GoodIpList, n, (const char *)&m, sizeof(ListInfo));
}
return 0;
}
/* GoodIPListAddIP list1 ip:port */
static int AddToLists(ConfigFileInfo *ConfigInfo)
{
StringList *l = ConfigGetStringList(ConfigInfo, "GoodIPListAddIP");
StringListIterator sli;
const char *Itr = NULL;
if( l == NULL )
{
return -1;
}
if( StringListIterator_Init(&sli, l) != 0 )
{
return -2;
}
while( (Itr = sli.Next(&sli)) != NULL )
{
ListInfo *m = NULL;
char n[128], ip_str[LENGTH_OF_IPV4_ADDRESS_ASCII];
int Port;
struct sockaddr_in ip;
sscanf(Itr, "%127s%*[^0123456789]%15[^:]:%d", n, ip_str, &Port);
ip.sin_port = htons(Port);
ip.sin_family = AF_INET; /* IPv4 only */
IPv4AddressToNum(ip_str, &(ip.sin_addr));
if( StringChunk_Match_NoWildCard(GoodIpList,
n,
NULL,
(void **)&m)
== FALSE
)
{
ERRORMSG("GoodIpList is not found : %s\n", Itr);
continue;
}
Array_PushBack(&(m->List), &ip, NULL);
}
return 0;
}
static int AddTask(void)
{
ListInfo *m;
const char *Domain;
int32_t Start = 0;
Domain = StringChunk_Enum_NoWildCard(GoodIpList, &Start, (void **)&m);
while( Domain != NULL )
{
if( m != NULL )
{
TimedTask_Add(TRUE,
FALSE,
m->Interval,
(TaskFunc)ThreadJod,
(void *)Domain,
(void *)m,
TRUE
);
}
Domain = StringChunk_Enum_NoWildCard(GoodIpList, &Start, (void **)&m);
}
return 0;
}
int GoodIpList_Init(ConfigFileInfo *ConfigInfo)
{
if( InitListsAndTimes(ConfigInfo) != 0 )
{
return -1;
}
if( AddToLists(ConfigInfo) != 0 )
{
return -2;
}
if( AddTask() != 0 )
{
return -270;
}
return 0;
}
const char *GoodIpList_Get(const char *List)
{
ListInfo *m;
if( StringChunk_Match_NoWildCard(GoodIpList,
List,
NULL,
(void **)&m
)
== TRUE )
{
if( Array_GetUsed(&(m->List)) <= 0 )
{
return NULL;
} else {
return (const char *)&(((const struct sockaddr_in *)Array_GetBySubscript(&(m->List), 0))->sin_addr);
}
} else {
return NULL;
}
}