forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
udpfrontend.c
executable file
·206 lines (167 loc) · 4.53 KB
/
udpfrontend.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
#include "udpfrontend.h"
#include "socketpuller.h"
#include "addresslist.h"
#include "utils.h"
#include "mmgr.h"
#include "logs.h"
/* UDP is main; TCP is fallback. */
BOOL Ipv6_Enabled = FALSE;
static SocketPuller Frontend;
static void
#ifdef WIN32
WINAPI
#endif
UdpFrontend_Work(void *Unused)
{
char *ReceiveBuffer;
IHeader *Header;
#define LEFT_LENGTH (SOCKET_CONTEXT_LENGTH - sizeof(IHeader))
char *Entity;
ReceiveBuffer = SafeMalloc(SOCKET_CONTEXT_LENGTH);
if( ReceiveBuffer == NULL )
{
ERRORMSG("No enough memory, 26.\n");
return;
}
Header = (IHeader *)ReceiveBuffer;
Entity = ReceiveBuffer + sizeof(IHeader);
/* Loop */
while( TRUE )
{
/* Address */
char AddressBuffer[sizeof(Address_Type)];
struct sockaddr *IncomingAddress = (struct sockaddr *)AddressBuffer;
SOCKET sock;
const sa_family_t *f;
int RecvState;
socklen_t AddrLen;
char Agent[sizeof(Header->Agent)];
sock = Frontend.Select(&Frontend,
NULL,
(void **)&f,
TRUE,
FALSE,
NULL
);
if( sock == INVALID_SOCKET )
{
ERRORMSG("Fatal error 57.\n");
break;
}
AddrLen = sizeof(Address_Type);
RecvState = recvfrom(sock,
Entity,
LEFT_LENGTH,
0,
IncomingAddress,
&AddrLen
);
if( *f == AF_INET )
{
IPv4AddressToAsc(&(((struct sockaddr_in *)IncomingAddress)->sin_addr),
Agent
);
} else {
IPv6AddressToAsc(&(((struct sockaddr_in6 *)IncomingAddress)->sin6_addr),
Agent
);
}
if( RecvState < 0 )
{
INFO("An error occured while receiving from UDP client %s, not a big deal.\n",
Agent
);
continue;
}
IHeader_Fill(Header,
FALSE,
Entity,
RecvState,
IncomingAddress,
sock,
*f,
Agent
);
MMgr_Send(ReceiveBuffer, SOCKET_CONTEXT_LENGTH);
}
SafeFree(ReceiveBuffer);
}
void UdpFrontend_StartWork(void)
{
ThreadHandle t;
CREATE_THREAD(UdpFrontend_Work, NULL, t);
DETACH_THREAD(t);
}
static void UdpFrontend_Cleanup(void)
{
Frontend.Free(&Frontend);
}
int UdpFrontend_Init(ConfigFileInfo *ConfigInfo, BOOL StartWork)
{
StringList *UDPLocal;
StringListIterator i;
const char *One;
int Count = 0;
UDPLocal = ConfigGetStringList(ConfigInfo, "UDPLocal");
if( UDPLocal == NULL )
{
ERRORMSG("No UDP interface specified.\n");
return -11;
}
if( StringListIterator_Init(&i, UDPLocal) != 0 )
{
return -20;
}
if( SocketPuller_Init(&Frontend, sizeof(sa_family_t)) != 0 )
{
return -19;
}
while( (One = i.Next(&i)) != NULL )
{
Address_Type a;
sa_family_t f;
SOCKET sock;
f = AddressList_ConvertFromString(&a, One, 53);
if( f == AF_UNSPEC )
{
ERRORMSG("Invalid UDPLocal option: %s .\n", One);
continue;
}
sock = socket(f, SOCK_DGRAM, IPPROTO_UDP);
if( sock == INVALID_SOCKET )
{
continue;
}
if( bind(sock,
(const struct sockaddr *)&(a.Addr),
GetAddressLength(f)
)
!= 0 )
{
char p[128];
snprintf(p, sizeof(p), "Opening UDP interface %s failed", One);
p[sizeof(p) - 1] = '\0';
ShowSocketError(p, GET_LAST_ERROR());
CLOSE_SOCKET(sock);
continue;
}
if( f == AF_INET6 )
{
Ipv6_Enabled = TRUE;
}
Frontend.Add(&Frontend, sock, &f, sizeof(sa_family_t));
INFO("UDP interface %s opened.\n", One);
++Count;
}
atexit(UdpFrontend_Cleanup);
if( Count == 0 )
{
ERRORMSG("No UDP interface opened.\n");
return -163;
}
if( StartWork )
{
UdpFrontend_StartWork();
}
return 0;
}