forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
socketpuller.c
executable file
·196 lines (163 loc) · 3.81 KB
/
socketpuller.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
#include "socketpuller.h"
PUBFUNC int SocketPuller_Add(SocketPuller *p,
SOCKET s,
const void *Data,
int DataLength
)
{
if( s == INVALID_SOCKET )
{
return -11;
}
if( p->p.Add(&(p->p), s, Data, DataLength) != 0 )
{
return -16;
}
if( s > p->Max )
{
p->Max = s;
}
FD_SET(s, &(p->s));
return 0;
}
PUBFUNC int SocketPuller_Del(SocketPuller *p, SOCKET s)
{
if( p->p.Del(&(p->p), s) != 0 )
{
return -33;
}
FD_CLR(s, &(p->s));
return 0;
}
PUBFUNC SOCKET SocketPuller_Select(SocketPuller *p,
struct timeval *tv,
void **Data,
BOOL Reading,
BOOL Writing,
int *err
)
{
fd_set ReadySet;
SOCKET s;
int Err = 0;
ReadySet = p->s;
while( TRUE )
{
switch( select(p->Max + 1,
Reading ? &ReadySet : NULL,
Writing ? &ReadySet : NULL,
NULL,
tv)
)
{
case SOCKET_ERROR:
Err = GET_LAST_ERROR();
SLEEP(1); /* dead loop? */
if( FatalErrorDecideding(Err) == 0 )
{
continue;
}
/* No break; */
case 0:
/* timeout */
s = INVALID_SOCKET;
break;
default:
s = p->p.FetchOnSet(&(p->p), &ReadySet, Data);
break;
}
break;
}
if( err != NULL )
{
*err = Err;
}
return s;
}
PUBFUNC void SocketPuller_CloseAll(SocketPuller *p, SOCKET ExceptFor)
{
p->p.CloseAll(&(p->p), ExceptFor);
}
PUBFUNC void SocketPuller_Free(SocketPuller *p)
{
p->p.Free(&(p->p), TRUE);
}
PUBFUNC void SocketPuller_FreeWithoutClose(SocketPuller *p)
{
p->p.Free(&(p->p), FALSE);
}
int SocketPuller_Init(SocketPuller *p, int DataLength)
{
p->Max = -1;
p->Add = SocketPuller_Add;
p->Del = SocketPuller_Del;
p->Select = SocketPuller_Select;
p->CloseAll = SocketPuller_CloseAll;
p->Free = SocketPuller_Free;
p->FreeWithoutClose = SocketPuller_FreeWithoutClose;
FD_ZERO(&(p->s));
return SocketPool_Init(&(p->p), DataLength);
}
SocketPuller **SocketPullers_Init(int Count, int DataLength)
{
SocketPuller *Buffer;
SocketPuller **Pullers;
int i;
if( Count <= 0 )
{
return NULL;
}
Pullers = SafeMalloc(sizeof(SocketPuller *) * (Count + 1));
if( Pullers == NULL )
{
return NULL;
}
Buffer = SafeMalloc(sizeof(SocketPuller) * Count);
if( Buffer == NULL )
{
SafeFree(Pullers);
return NULL;
}
for( i = 0; i < Count; ++i )
{
Pullers[i] = Buffer + i;
if( SocketPuller_Init(Pullers[i], DataLength) != 0 )
{
Pullers[i] = NULL;
goto EXIT_1;
}
}
Pullers[i] = NULL;
return Pullers;
EXIT_1:
SocketPullers_FreeWithoutClose(Pullers);
SafeFree(Buffer);
return NULL;
}
void SocketPullers_CloseAll(SocketPuller **Pullers)
{
for( ; *Pullers != NULL; ++Pullers )
{
(*Pullers)->CloseAll(*Pullers, INVALID_SOCKET);
}
}
void SocketPullers_FreeWithoutClose(SocketPuller **Pullers)
{
SocketPuller **p;
p = Pullers;
for( ; *Pullers != NULL; ++Pullers )
{
(*Pullers)->FreeWithoutClose(*Pullers);
}
SafeFree(p);
}
void SocketPullers_Free(SocketPuller **Pullers)
{
SocketPuller **p;
p = Pullers;
for( ; *Pullers != NULL; ++Pullers )
{
(*Pullers)->Free(*Pullers);
}
SafeFree(p);
}