forked from DNSPod/dnspod-sr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
323 lines (291 loc) · 6.66 KB
/
net.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Copyright (c) 2006-2012, DNSPod Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the FreeBSD Project.
*/
#include "net.h"
//reserved ip
//10.0.0.0:8
//172.16.0.0:12
//192.168.0.0:16
//0.0.0.0
//255.255.255.255
//127.0.0.0:8
//224.0.0.0:18
int check_client_addr(struct sockaddr_in *addr)
{
return 0;
}
//add fd to backdoor
//only 1 udp fd at first
int add_backdoor(int fd)
{
int epfd,ret;
struct epoll_event ev;
epfd = epoll_create(BACK_EVENT);
if(epfd < 0)
dns_error(0,"epoll bd");
ev.data.fd = fd;
ev.events = EPOLLIN; //with out EPOLLET
ret = epoll_ctl(epfd,EPOLL_CTL_ADD,ev.data.fd,&ev);
if(ret < 0)
dns_error(0,"epoll add udp backdoor");
return epfd;
}
int set_recv_timeout(int fd,int sec,int usec)
{
int ret;
struct timeval tv;
tv.tv_sec = sec;
tv.tv_usec = usec;
ret = setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(char*)&tv,sizeof(struct timeval));
return ret;
}
int create_socket(int port,int proto,uchar *addr)
{
int fd = -1,pt = -1;
struct sockaddr_in srv;
if(proto == UDP)
pt = SOCK_DGRAM;
else if(proto == TCP)
pt = SOCK_STREAM;
fd = socket(AF_INET,pt,0);
if(fd <= 0)
return -1;
srv.sin_family = AF_INET;
if(addr == NULL)
srv.sin_addr.s_addr = htonl(INADDR_ANY);
else
inet_aton(addr,&srv.sin_addr);
srv.sin_port = htons(port);
if(bind(fd,(SA*)&srv,sizeof(srv)) < 0)
{
perror("bind:");
return -1;
}
if(proto == SOCK_STREAM)
listen(fd,512);
return fd;
}
int connect_to(struct sockinfo *si)
{
int ret = 0;
//printf("CONN!!!\n");
socklen_t len = sizeof(struct sockaddr_in);
ret = connect(si->fd,(SA*)&si->addr,len);
if(ret < 0)
{
if(errno == EINPROGRESS)
return 0;
printf("%d,%d,",si->fd,errno);
perror("conn");
return -1;
}
return 0;
}
int tcp_write_info(struct sockinfo *ri,int vi) //for dns only
{
int i,ret;
ret = send(ri->fd,ri->buf,ri->buflen,MSG_NOSIGNAL);
if(ret < 0)
{
printf("%d,",errno);
perror("tcp send");
}
if(vi == 1)
{
printf("fd is %d\n",ri->fd);
for(i = 0;i < ri->buflen;i ++)
printf("%x,",ri->buf[i]);
printf("\n");
}
return ret;
}
int udp_write_info(struct sockinfo *ri,int vi)
{
int i,ret;
socklen_t len;
if(vi)
{
dbg_print_addr((struct sockaddr_in*)&(ri->addr));
for(i = 0;i < ri->buflen;i ++)
printf("%x,",ri->buf[i]);
printf("\n");
}
len = sizeof(struct sockaddr_in);
((struct sockaddr_in*)&(ri->addr))->sin_family = AF_INET;
ret = sendto(ri->fd,ri->buf,ri->buflen,0,(SA*)&(ri->addr),len);
if(ret < 0)
{
perror("write udp");
printf("len %u,fd %d\n",len,ri->fd);
dbg_print_addr((struct sockaddr_in*)&(ri->addr));
for(i = 0;i < ri->buflen;i ++)
printf("%x,",ri->buf[i]);
printf("\n");
}
return ret;
}
int tcp_read_dns_msg(struct sockinfo *si,uint max,int vi) //for dns only.
{
int ret = 0,i,tp,rcvnum;
int len;
uchar buf[4] = {0};
ushort le = 0;
tp = recv(si->fd,buf,2,0);
if(tp < 0)
{
printf("%d,", si->fd);
perror("tp");
return -1;
}
if(tp == 0) //peer closed
return 0;
le = *(ushort*)buf;
le = ntohs(le);
if(le > max)
{
printf("too large %d,%u,%d\n",si->fd,le,max);
return -1;
}
while(ret < le) //should set time out here
{
rcvnum = recv(si->fd,si->buf + ret,si->buflen - ret,0);
if(rcvnum < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK)
continue;
printf("tcp data %d,%d,%d",si->fd,le,ret);
perror("tcp read");
return -1;
}
if(rcvnum == 0)
{
ret = -1;
break;
}
ret += rcvnum;
}
return ret;
}
int udp_read_msg(struct sockinfo *si,int visible)
{
int ret,i;
socklen_t len = sizeof(struct sockaddr_in);
ret = recvfrom(si->fd,si->buf,si->buflen,0,(SA*)&(si->addr),&len);
if(ret < 0)
{
//perror("read udp");
return ret;
}
//printf("%d,",ret);
//dbg_print_addr(&si->addr);
if(visible)
{
for(i = 0;i < ret;i ++)
printf("%x,",si->buf[i]);
printf("\n");
}
return ret;
}
int set_sock_buff(int fd,int m)
{
int ret;
int bufsize = m * 1024 * 1024; //1m
if(fd <= 0)
return -1;
ret = setsockopt(fd,SOL_SOCKET,SO_RCVBUF,
(const uchar*)&bufsize,sizeof(int));
return ret;
}
int set_non_block(int fd)
{
int opt = fcntl(fd,F_GETFL,0);
if(opt < 0)
return -1;
opt |= O_NONBLOCK;
return (fcntl(fd,F_SETFL,opt));
}
int make_bin_from_str(uchar *bin,const uchar *str)
{
int i,n;
uchar val = 0;
for(i = 0;i < 4;i ++)
{
while((str[0] != '.') && (str[0] != 0))
{
val = val * 10 + str[0] - '0';
str ++;
}
str ++; //jump '.'
bin[0] = val;
val = 0;
bin ++; //next digit
}
return 0;
}
int make_addr_from_bin(struct sockaddr_in *addr,uchar *data)
{
uchar ipv4[16] = {0};
int idx = 0;
int i;
ushort val = 0;
if(data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 0)
return -1;
for(i = 0;i < 4;i ++)
{
val = (ushort)data[i];
if(val > 99)
{
ipv4[idx] = val / 100 + '0';
idx ++;
}
if(val > 9)
{
ipv4[idx] = (val % 100) / 10 + '0';
idx ++;
}
ipv4[idx] = val % 10 + '0';
idx ++;
ipv4[idx] = '.';
idx ++;
}
ipv4[idx - 1] = 0;
i = inet_aton(ipv4,&addr->sin_addr);
return 0;
}
//---------------------debug-------------------------------
int dbg_print_addr(struct sockaddr_in *addr)
{
int port;
uchar str[16],*ret;
uint i;
if(addr == NULL)
{
printf("null addr\n");
return 0;
}
i = addr->sin_addr.s_addr;
printf("%u.%u.%u.%u\n",i % (256),i / 256 % 256,i / 256 / 256 % 256,i / 256 / 256 / 256);
return 0;
}