forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·357 lines (295 loc) · 7.46 KB
/
main.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h> /* exit() */
#include <ctype.h> /* isspace() */
#ifndef NODOWNLOAD
#ifndef WIN32
#include <sys/types.h>
#include <sys/stat.h>
#ifdef DOWNLOAD_LIBCURL
#include <curl/curl.h>
#endif /* DOWNLOAD_LIBCURL */
#endif /* WIN32 */
#endif /* NODOWNLOAD */
#include "dnsrelated.h"
#include "common.h"
#include "utils.h"
#include "readconfig.h"
#include "querydnsinterface.h"
#include "request_response.h"
#include "debug.h"
#define VERSION__ "5.0.37"
#define PRINTM(...) if(ShowMassages == TRUE) printf(__VA_ARGS__);
static char *ConfigFile;
static BOOL DeamonMode;
int DaemonInit(void)
{
#ifdef WIN32
char *CmdLine = GetCommandLine();
char ModuleName[320];
char *itr;
char *NewArguments;
int ModuleNameLength;
BOOL StartUpStatus;
STARTUPINFO StartUpInfo;
PROCESS_INFORMATION ProcessInfo;
ModuleNameLength = GetModuleFileName(NULL, ModuleName, sizeof(ModuleName) - 1);
if( ModuleNameLength == 0 )
{
return 1;
} else {
ModuleName[sizeof(ModuleName) - 1] = '\0';
}
for(; isspace(*CmdLine); ++CmdLine);
if(*CmdLine == '"')
{
itr = strchr(++CmdLine, '"');
} else {
itr = strchr(CmdLine, ' ');
}
if( itr != NULL )
CmdLine = itr + 1;
else
return 1;
for(; isspace(*CmdLine); ++CmdLine);
NewArguments = SafeMalloc(strlen(ModuleName) + strlen(CmdLine) + 32);
strcpy(NewArguments, "\"");
strcat(NewArguments, ModuleName);
strcat(NewArguments, "\" ");
strcat(NewArguments, CmdLine);
itr = strstr(NewArguments + strlen(ModuleName) + 2, "-d");
while( itr != NULL )
{
*(itr + 1) = 'q';
itr = strstr(itr + 2, "-d");
}
StartUpInfo.cb = sizeof(StartUpInfo);
StartUpInfo.lpReserved = NULL;
StartUpInfo.lpDesktop = NULL;
StartUpInfo.lpTitle = NULL;
StartUpInfo.dwFlags = STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow = SW_HIDE;
StartUpInfo.cbReserved2 = 0;
StartUpInfo.lpReserved2 = NULL;
StartUpStatus = CreateProcess(NULL, NewArguments, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &StartUpInfo, &ProcessInfo);
SafeFree(NewArguments);
if( StartUpStatus != FALSE )
{
printf("deamon process pid : %d\n", (int)(ProcessInfo.dwProcessId));
exit(0);
} else {
return 1;
}
#else /* WIN32 */
pid_t pid;
if( (pid = fork()) < 0 )
{
return 1;
}
else
{
if(pid != 0)
{
printf("deamon process pid : %d\n", pid);
exit(0);
}
setsid();
umask(0); /* clear file mode creation mask */
close(0);
close(1);
close(2);
return 0;
}
#endif /* WIN32 */
}
void Test(const char *ServerAddress)
{
ThreadHandle t;
uint32_t Counter = 0;
struct TestServerArguments Args = {ServerAddress, &Counter};
if( ServerAddress == NULL )
{
printf("Please specify server address.\n");
return;
}
CREATE_THREAD(TestServer, &Args, t);
DETACH_THREAD(t);
while( TRUE )
{
SLEEP(1000);
printf("Requrests per second : %u\n", Counter);
Counter = 0;
}
}
int GetDefaultConfigureFile(char *out, int OutLength)
{
#ifdef WIN32
GetModulePath(out, OutLength);
strcat(out, "\\dnsforwarder.config");
#else
GetConfigDirectory(out);
strcat(out, "/config");
#endif
return 0;
}
#ifndef WIN32
void PrepareEnvironment(void)
{
char ConfigDirectory[2048];
GetConfigDirectory(ConfigDirectory);
if( mkdir(ConfigDirectory, S_IRWXU | S_IRGRP | S_IROTH) != 0 )
{
int ErrorNum = GET_LAST_ERROR();
char ErrorMessage[320];
ErrorMessage[0] = '\0';
GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
printf("mkdir : %s failed : %s\n", ConfigDirectory, ErrorMessage);
}
printf("Please put configure file into `%s' and rename it to `config'.\n", ConfigDirectory);
}
#endif
int ArgParse(int argc, char *argv_ori[], const char **Contexts)
{
char **argv = argv_ori;
++argv;
*Contexts = NULL;
while(*argv != NULL)
{
if(strcmp("-h", *argv) == 0)
{
printf("DNSforwarder by several people. Version "VERSION__" . License : GPL v3.\n Time of compilation : %s %s.\n\n", __DATE__, __TIME__);
printf("https://github.com/holmium/dnsforwarder\n\n");
printf("Usage : %s [args].\n", strrchr(argv_ori[0], PATH_SLASH_CH) == NULL ? argv_ori[0] : strrchr(argv_ori[0], PATH_SLASH_CH) + 1);
printf(" [args] is case sensitivity and can be zero or more (in any order) of:\n"
" -f <FILE> Use configuration <FILE> instead of the default one.\n"
" -q Quiet mode. Do not print any information.\n"
" -e Show only error messages.\n"
" -d Daemon mode. Running at background.\n"
#ifndef WIN32
"\n"
" -p Prepare needed environment.\n"
#endif
"\n"
" -h Show this help.\n"
"\n"
"Output format:\n"
" Date & Time [Udp|Tcp|Cache|Hosts|Refused|Blocked][Client IP][Queried type][Queried domain] : Message size\n"
" Results\n"
);
exit(0);
}
if(strcmp("-q", *argv) == 0)
{
ShowMassages = FALSE;
ErrorMessages = FALSE;
++argv;
continue;
}
if(strcmp("-e", *argv) == 0)
{
ShowMassages = FALSE;
++argv;
continue;
}
if(strcmp("-d", *argv) == 0)
{
DeamonMode = TRUE;
++argv;
continue;
}
if(strcmp("-f", *argv) == 0)
{
ConfigFile = *(++argv);
++argv;
continue;
}
if( strcmp("-T", *argv) == 0 )
{
ShowMassages = FALSE;
ErrorMessages = FALSE;
#ifdef WIN32
SetConsoleTitle("dnsforwarder - testing");
#endif
++argv;
Test(*argv);
exit(0);
++argv;
continue;
}
#ifndef WIN32
if( strcmp("-p", *argv) == 0 )
{
PrepareEnvironment();
exit(0);
++argv;
continue;
}
#endif
if( strcmp("-CONTEXT", *argv) == 0 )
{
*Contexts = *(++argv);
++argv;
continue;
}
PRINTM("Unrecognisable arg `%s'. Try `-h'.\n", *argv);
++argv;
}
return 0;
}
int main(int argc, char *argv[])
{
#ifdef WIN32
WSADATA wdata;
#endif
const char *Contexts = NULL;
#ifndef NODOWNLOAD
#ifdef WIN32
if(WSAStartup(MAKEWORD(2, 2), &wdata) != 0)
return -1;
#else
#ifdef DOWNLOAD_LIBCURL
curl_global_init(CURL_GLOBAL_ALL);
#endif /* DOWNLOAD_LIBCURL */
#endif /* WIN32 */
#endif /* NODOWNLOAD */
#ifdef WIN32
SetConsoleTitle("dnsforwarder");
#endif /* WIN32 */
ArgParse(argc, argv, &Contexts);
if( ConfigFile == NULL )
{
ConfigFile = malloc(320);
if( ConfigFile == NULL )
{
return -1;
}
GetDefaultConfigureFile(ConfigFile, 320);
}
PRINTM("DNSforwarder mainly by holmium. Version "VERSION__" . License : GPL v3.\nTime of compilation : %s %s.\n\n", __DATE__, __TIME__);
#ifndef WIN32
PRINTM("Please run `dnsforwarder -p' if something goes wrong.\n\n")
#endif
PRINTM("Configure File : %s\n\n", ConfigFile);
if( DeamonMode == TRUE )
{
if( DaemonInit() == 0 )
{
ShowMassages = FALSE;
ErrorMessages = FALSE;
} else {
printf("Daemon init failed, continuing on non-daemon mode.\n");
}
}
if( QueryDNSInterfaceInit(ConfigFile, Contexts) != 0 )
goto JustEnd;
putchar('\n');
if( QueryDNSInterfaceStart() != 0 )
goto JustEnd;
QueryDNSInterfaceWait();
JustEnd:
#ifdef WIN32
WSACleanup();
#endif
return 0;
}