-
Notifications
You must be signed in to change notification settings - Fork 3
/
fuckdns.cpp
231 lines (198 loc) · 4.94 KB
/
fuckdns.cpp
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
#include <stdio.h>
#include <time.h>
#include <Winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
//DNS服务器端口
#define DNS_PORT 53
//缓冲区大小
#define BUFF_SIZE 2048
#define DNS_SERV_NUM 4
//DNS服务器ip
const char dns_ip[DNS_SERV_NUM][16] = {
"208.67.222.222",
"208.67.220.220",
"8.8.8.8",
"8.8.4.4",
//"178.79.131.110",
//"106.187.39.23"
};
//DNS服务器选择
static unsigned int sel = 0;
//本地UDP转发服务socket
static SOCKET serv;
//远程DNS地址信息
//struct sockaddr_in dns_addr;
//本地DNS包发送者地址信息
static struct sockaddr_in from_addr;
//本地UDP转发服务程序地址信息
static struct sockaddr_in serv_addr;
int fromlen = sizeof(from_addr);
//线程参数结构体
typedef struct DATA{
char data[BUFF_SIZE];
struct sockaddr_in from_addr;
int len;
}*pData;
//线程函数,处理DNS数据包
DWORD WINAPI ThreadFunc( LPVOID lp );
int main(int argc, char **argv)
{
WSADATA ws;
int cnt;
char *buff = NULL;
char *buf = NULL;
printf("=========================================================\n");
printf("Starting ...\n");
if (0 != WSAStartup(MAKEWORD(2, 0), &ws))
{
printf("Init windows socket error\n");
return -1;
}
serv = socket(AF_INET, SOCK_DGRAM, 0);
if (INVALID_SOCKET == serv)
{
printf("socket() error\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(DNS_PORT);
serv_addr.sin_addr.S_un.S_addr = INADDR_ANY;
if (-1 == bind(serv, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
{
printf("bind error\n");
return -1;
}
printf("Server OK !\n");
printf("DNS Servers IPs:\n");
for (int i = 0; i < DNS_SERV_NUM; i++)
{
printf("\t%s\n", dns_ip[i]);
}
printf("=========================================================\n");
while (1)
{
pData msg = NULL;
msg = (pData)(new char[sizeof(struct DATA)]);
if (NULL == msg)
{
printf("new char[] error !\n");
continue;
}
buff = msg->data;
buf = buff + 2;
cnt = recvfrom(serv, buf, BUFF_SIZE - 2, 0, (struct sockaddr *)&from_addr, &fromlen);
if (cnt == SOCKET_ERROR)
{
//printf("udp recv error [code = %d]\n", WSAGetLastError());
free(msg);
continue;
}
*(UINT16 *)buff = htons(cnt);
memcpy(&(msg->from_addr), &(from_addr), sizeof(from_addr));
msg->len = cnt;
if (NULL == CreateThread(NULL,0,ThreadFunc,(LPVOID)msg, 0,NULL))
{
if (NULL == CreateThread(NULL,0,ThreadFunc,(LPVOID)msg, 0,NULL))
{
if (NULL == CreateThread(NULL,0,ThreadFunc,(LPVOID)msg, 0,NULL))
{
free(msg);
printf("CreateThread error !\n");
}
}
}
}
closesocket(serv);
WSACleanup();
return 0;
}
DWORD WINAPI ThreadFunc( LPVOID lp )
{
pData msg = (pData)lp;
char *buff = msg->data;
char *buf = buff + 2;
SOCKET client;
struct sockaddr_in dns_addr;
int cnt = 0, i = 0;
dns_addr.sin_port = htons(53);
dns_addr.sin_family = AF_INET;
dns_addr.sin_addr.S_un.S_addr = inet_addr(dns_ip[sel % DNS_SERV_NUM]);
//printf("%s\n", dns_ip[sel % DNS_SERV_NUM]);
sel++;
client = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == client)
{
printf("socket() error [dns = %s code = %d]\n", dns_ip[sel % DNS_SERV_NUM], WSAGetLastError());
free(msg);
return -1;
}
if (SOCKET_ERROR == connect(client, (struct sockaddr *)&dns_addr, sizeof(dns_addr)))
{
Sleep(10);
if (SOCKET_ERROR == connect(client, (struct sockaddr *)&dns_addr, sizeof(dns_addr)))
{
Sleep(10);
if (SOCKET_ERROR == connect(client, (struct sockaddr *)&dns_addr, sizeof(dns_addr)))
{
printf("connect error [dns = %s code = %d]\n", dns_ip[sel % DNS_SERV_NUM], WSAGetLastError());
free(msg);
closesocket(client);
return -1;
}
}
}
cnt = send(client, buff, msg->len + 2, 0);
if (cnt == SOCKET_ERROR)
{
Sleep(10);
cnt = send(client, buff, msg->len + 2, 0);
if (cnt == SOCKET_ERROR)
{
Sleep(10);
cnt = send(client, buff, msg->len + 2, 0);
if (cnt == SOCKET_ERROR)
{
printf("tcp send error [dns = %s code = %d]\n", dns_ip[sel % DNS_SERV_NUM], WSAGetLastError());
free(msg);
closesocket(client);
return -1;
}
}
}
cnt = recv(client, buff, BUFF_SIZE, 0);
if (cnt == SOCKET_ERROR)
{
Sleep(10);
cnt = recv(client, buff, BUFF_SIZE, 0);
if (cnt == SOCKET_ERROR)
{
Sleep(10);
cnt = recv(client, buff, BUFF_SIZE, 0);
if (cnt == SOCKET_ERROR)
{
printf("tcp recv error [dns = %s code = %d]\n", dns_ip[sel % DNS_SERV_NUM], WSAGetLastError());
free(msg);
closesocket(client);
return -1;
}
}
}
if (sendto(serv, buf, cnt - 2, 0, (struct sockaddr *)&(msg->from_addr), sizeof(struct sockaddr)) == SOCKET_ERROR)
{
Sleep(10);
if (sendto(serv, buf, cnt - 2, 0, (struct sockaddr *)&(msg->from_addr), sizeof(struct sockaddr)) == SOCKET_ERROR)
{
Sleep(10);
if (sendto(serv, buf, cnt - 2, 0, (struct sockaddr *)&(msg->from_addr), sizeof(struct sockaddr)) == SOCKET_ERROR)
{
printf("udp send error [dns = %s code = %d]\n", dns_ip[sel % DNS_SERV_NUM], WSAGetLastError());
free(msg);
closesocket(client);
return -1;
}
}
}
free(msg);
closesocket(client);
return 0;
}