質問編集履歴
1
コードを追加いたしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,3 +17,541 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
![イメージ説明](60d49fd8880b4e8b076b23b2a416830a.jpeg)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```ここに言語を入力
|
24
|
+
|
25
|
+
#include "inc.h"
|
26
|
+
|
27
|
+
#include "base.h"
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
/* •ø•§•‡•¢•¶•»ª˛¥÷(…√) */
|
32
|
+
|
33
|
+
#define TIMEOUT_SEC (30)
|
34
|
+
|
35
|
+
#define TIMEOUT_SEC_U (5)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
/* •—•È•·°º•ø•’•°•§•Î•—•π */
|
40
|
+
|
41
|
+
#define PARAM_PATH "/etc/dnsproxy.ini"
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
/* DNS•µ°º•–•«°º•ø */
|
46
|
+
|
47
|
+
#define DNS_SERVER_HOST "192.168.0.10"
|
48
|
+
|
49
|
+
char DnsServerHost[512]=DNS_SERVER_HOST;
|
50
|
+
|
51
|
+
#define DNS_SERVER_PORT "20053"
|
52
|
+
|
53
|
+
char DnsServerPort[512]=DNS_SERVER_PORT;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
#define BIG(a,b) ((a)>=(b)?(a):(b))
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
/* •◊•Ì•»•ø•§•◊ */
|
62
|
+
|
63
|
+
static int MsgLogTcpQuery(JOB_DATA *job_data,char *buf,int size);
|
64
|
+
|
65
|
+
static int MsgLogTcpResponse(JOB_DATA *job_data,char *buf,int size);
|
66
|
+
|
67
|
+
static int MsgLogUdpQuery(JOB_DATA *job_data,char *buf,int size);
|
68
|
+
|
69
|
+
static int MsgLogUdpResponse(JOB_DATA *job_data,char *buf,int size);
|
70
|
+
|
71
|
+
static void do_timeout(int sig);
|
72
|
+
|
73
|
+
static int set_timeout(int sec);
|
74
|
+
|
75
|
+
static int reset_timeout();
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
/* •—•È•·°º•ø∆…§flπ˛§fl */
|
80
|
+
|
81
|
+
int ReadParam()
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
GetPrivateProfileString("DnsProxy","DnsServerHost",DNS_SERVER_HOST,DnsServerHost,sizeof(DnsServerHost),PARAM_PATH);
|
86
|
+
|
87
|
+
GetPrivateProfileString("DnsProxy","DnsServerPort",DNS_SERVER_PORT,DnsServerPort,sizeof(DnsServerPort),PARAM_PATH);
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
lprintf("DnsServer=%s:%s\n",DnsServerHost,DnsServerPort);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
return(0);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
/* •◊•Ì•≠•∑ΩËÕ˝º¬π‘ */
|
102
|
+
|
103
|
+
int DoProxy(JOB_DATA *job_data)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
int ret;
|
108
|
+
|
109
|
+
int count=0;
|
110
|
+
|
111
|
+
int width;
|
112
|
+
|
113
|
+
struct timeval timeout;
|
114
|
+
|
115
|
+
fd_set readOK;
|
116
|
+
|
117
|
+
int end=0;
|
118
|
+
|
119
|
+
char buf[4097];
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
set_timeout(TIMEOUT_SEC);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
job_data->soc_server=-1;
|
128
|
+
|
129
|
+
strcpy(job_data->host,"");
|
130
|
+
|
131
|
+
strcpy(job_data->port,"");
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
job_data->soc_server=ClientSocket(DnsServerHost,DnsServerPort,-1);
|
136
|
+
|
137
|
+
if(job_data->soc_server==-1){
|
138
|
+
|
139
|
+
lprintf("%s:%s§À¿‹¬≥§«§≠§ §§\n",DnsServerHost,DnsServerPort);
|
140
|
+
|
141
|
+
return(-1);
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
while(1){
|
148
|
+
|
149
|
+
count++;
|
150
|
+
|
151
|
+
/* select()Õ—•fi•π•Ø∫Ó¿Æ */
|
152
|
+
|
153
|
+
FD_ZERO(&readOK);
|
154
|
+
|
155
|
+
FD_SET(job_data->soc,&readOK);
|
156
|
+
|
157
|
+
width=job_data->soc+1;
|
158
|
+
|
159
|
+
FD_SET(job_data->soc_server,&readOK);
|
160
|
+
|
161
|
+
width=BIG(job_data->soc_server+1,width);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
timeout.tv_sec=32767;
|
166
|
+
|
167
|
+
timeout.tv_usec=0;
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
switch(select(width,&readOK,NULL,NULL,&timeout)){
|
172
|
+
|
173
|
+
case -1:
|
174
|
+
|
175
|
+
if(errno!=EINTR){
|
176
|
+
|
177
|
+
lperror("select");
|
178
|
+
|
179
|
+
/* ≥‰§Íπ˛§fl∞ ≥∞§Œ•®•È°º§ŒæÏπÁ§œΩ™Œª */
|
180
|
+
|
181
|
+
end=1;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
break;
|
186
|
+
|
187
|
+
case 0:
|
188
|
+
|
189
|
+
end=1;
|
190
|
+
|
191
|
+
break;
|
192
|
+
|
193
|
+
default:
|
194
|
+
|
195
|
+
if(FD_ISSET(job_data->soc,&readOK)){
|
196
|
+
|
197
|
+
/* •Ø•È•§•¢•Û•»•Ï•«•£ */
|
198
|
+
|
199
|
+
/* •Ø•È•§•¢•Û•»§´§ÈºıøÆ */
|
200
|
+
|
201
|
+
ret=recv(job_data->soc,buf,sizeof(buf)-1,0);
|
202
|
+
|
203
|
+
if(ret>0){
|
204
|
+
|
205
|
+
MsgLogTcpQuery(job_data,buf,ret);
|
206
|
+
|
207
|
+
/* •µ°º•–§À¡˜øÆ */
|
208
|
+
|
209
|
+
SendSize(job_data->soc_server,buf,ret);
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
if(ret<=0){
|
214
|
+
|
215
|
+
end=1;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
if(FD_ISSET(job_data->soc_server,&readOK)){
|
222
|
+
|
223
|
+
/* •µ°º•–•Ï•«•£ */
|
224
|
+
|
225
|
+
/* •µ°º•–§´§ÈºıøÆ */
|
226
|
+
|
227
|
+
ret=recv(job_data->soc_server,buf,sizeof(buf)-1,0);
|
228
|
+
|
229
|
+
if(ret>0){
|
230
|
+
|
231
|
+
MsgLogTcpResponse(job_data,buf,ret);
|
232
|
+
|
233
|
+
/* •Ø•È•§•¢•Û•»§À¡˜øÆ */
|
234
|
+
|
235
|
+
SendSize(job_data->soc,buf,ret);
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
if(ret<=0){
|
240
|
+
|
241
|
+
end=1;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
break;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
if(end){
|
252
|
+
|
253
|
+
break;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
if(job_data->soc_server!=-1){
|
262
|
+
|
263
|
+
/* •Ω•±•√•»•Ø•Ì°º•∫ */
|
264
|
+
|
265
|
+
SocketClose(job_data->soc_server);
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
/* •ø•§•‡•¢•¶•»•Í•ª•√•» */
|
272
|
+
|
273
|
+
reset_timeout();
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
return(0);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
/* •Ì•∞Ω–Œœ¥ÿœ¢ */
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
static int MsgLogTcpQuery(JOB_DATA *job_data,char *buf,int size)
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
char msg[512];
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
sprintf(msg,"TCP:Query(%s)[%d]<<<<<<<<<<\n",job_data->client_address,getpid());
|
296
|
+
|
297
|
+
log_out(msg,buf+2,size-2);
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
return(0);
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
static int MsgLogTcpResponse(JOB_DATA *job_data,char *buf,int size)
|
308
|
+
|
309
|
+
{
|
310
|
+
|
311
|
+
char msg[512];
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
sprintf(msg,"TCP:Response(%s)[%d]>>>>>>>>>>\n",job_data->client_address,getpid());
|
316
|
+
|
317
|
+
log_out(msg,buf+2,size-2);
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
return(0);
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
static int MsgLogUdpQuery(JOB_DATA *job_data,char *buf,int size)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
char msg[512];
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
sprintf(msg,"UDP:Query(%s)[%d]<<<<<<<<<<\n",job_data->client_address,getpid());
|
336
|
+
|
337
|
+
log_out(msg,buf,size);
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return(0);
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
static int MsgLogUdpResponse(JOB_DATA *job_data,char *buf,int size)
|
348
|
+
|
349
|
+
{
|
350
|
+
|
351
|
+
char msg[512];
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
sprintf(msg,"UDP:Response(%s)[%d]>>>>>>>>>>\n",job_data->client_address,getpid());
|
356
|
+
|
357
|
+
log_out(msg,buf,size);
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
return(0);
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
/* •◊•Ì•≠•∑ΩËÕ˝º¬π‘(UDP) */
|
368
|
+
|
369
|
+
int DoProxyU(JOB_DATA *job_data)
|
370
|
+
|
371
|
+
{
|
372
|
+
|
373
|
+
struct sockaddr_in from,to,server;
|
374
|
+
|
375
|
+
struct in_addr addr;
|
376
|
+
|
377
|
+
int fromlen,size,tolen,ret,size2;
|
378
|
+
|
379
|
+
char adr[80];
|
380
|
+
|
381
|
+
union {
|
382
|
+
|
383
|
+
HEADER hde;
|
384
|
+
|
385
|
+
u_char buf[PACKETSZ];
|
386
|
+
|
387
|
+
}buf,buf2;
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
set_timeout(TIMEOUT_SEC_U);
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
/* •Ø•È•§•¢•Û•»§´§ÈºıøÆ */
|
396
|
+
|
397
|
+
fromlen=sizeof(from);
|
398
|
+
|
399
|
+
size=recvfrom(job_data->soc,buf.buf,sizeof(buf),0,&from,&fromlen);
|
400
|
+
|
401
|
+
if(size<=0||size>512){
|
402
|
+
|
403
|
+
return(-1);
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
strcpy(job_data->client_address,inet_ntoa(from.sin_addr));
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
MsgLogUdpQuery(job_data,buf.buf,size);
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
if(buf.hde.qr==1){
|
416
|
+
|
417
|
+
/* ÷≈˙§ŒæÏπÁ:¡˜øƧŒ§fl */
|
418
|
+
|
419
|
+
job_data->soc_server=ClientSocketU(DnsServerHost,DnsServerPort,-1,&to,&tolen);
|
420
|
+
|
421
|
+
if(job_data->soc_server==-1){
|
422
|
+
|
423
|
+
lprintf("%s:%s§À¿‹¬≥§«§≠§ §§\n",DnsServerHost,DnsServerPort);
|
424
|
+
|
425
|
+
return(-1);
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
ret=sendto(job_data->soc_server,buf.buf,size,0,&to,tolen);
|
432
|
+
|
433
|
+
strcpy(adr,inet_ntoa(from.sin_addr));
|
434
|
+
|
435
|
+
SocketClose(job_data->soc_server);
|
436
|
+
|
437
|
+
return(1);
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
else{
|
442
|
+
|
443
|
+
/* ɧ§πÁ§Ô§ª§ŒæÏπÁ */
|
444
|
+
|
445
|
+
if(atoaddr(DnsServerHost,&addr)==0){
|
446
|
+
|
447
|
+
lprintf("atoaddr():error\n");
|
448
|
+
|
449
|
+
return(-1);
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
memset((char *)&server,0,sizeof(server));
|
454
|
+
|
455
|
+
server.sin_family=AF_INET;
|
456
|
+
|
457
|
+
server.sin_port=atoport(DnsServerPort,"udp");
|
458
|
+
|
459
|
+
server.sin_addr=addr;
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
_res.nscount=1;
|
464
|
+
|
465
|
+
memcpy(&_res.nsaddr_list[0],&server,sizeof(struct sockaddr_in));
|
466
|
+
|
467
|
+
_res.options&=~RES_RECURSE;
|
468
|
+
|
469
|
+
_res.retry=1;
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
/* •µ°º•–§Àɧ§πÁ§Ô§ª¡˜øÆ°¶±˛≈˙ºıøÆ */
|
474
|
+
|
475
|
+
size2=res_send(buf.buf,size,buf2.buf,PACKETSZ);
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
MsgLogUdpResponse(job_data,buf2.buf,size2);
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
if(size2<=0){
|
484
|
+
|
485
|
+
return(-1);
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
if(buf.hde.id!=buf2.hde.id){
|
490
|
+
|
491
|
+
return(-1);
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
/* •Ø•È•§•¢•Û•»§À±˛≈˙ */
|
498
|
+
|
499
|
+
ret=sendto(job_data->soc,buf2.buf,size2,0,&from,fromlen);
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
return(0);
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
/* •ø•§•‡•¢•¶•»ΩËÕ˝¥ÿœ¢ */
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
static void do_timeout(int sig)
|
516
|
+
|
517
|
+
{
|
518
|
+
|
519
|
+
exit(0);
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
static int set_timeout(int sec)
|
526
|
+
|
527
|
+
{
|
528
|
+
|
529
|
+
signal(SIGALRM,do_timeout);
|
530
|
+
|
531
|
+
alarm(sec);
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
return(0);
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
static int reset_timeout()
|
542
|
+
|
543
|
+
{
|
544
|
+
|
545
|
+
alarm(0);
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
return(0);
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
```
|