-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
143 lines (121 loc) · 3.65 KB
/
main.c
File metadata and controls
143 lines (121 loc) · 3.65 KB
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
#include "common.h"
void daemon_init() {
if (fork() > 0) {
exit(0);
}
setsid();
daemon_proc = 1;
openlog(PROG, LOG_PID, 0);
}
/* init threadpoll, db, cache etc */
threadpool thpool;
char *sql_config;
void server_init()
{
thpool = thpool_init(NR_THREADS);
if (!thpool)
err_quit("threadpool init failed");
signal(SIGPIPE, SIG_IGN);
#ifdef USEDB
char line[256];
FILE *f = fopen(sql_config, "r");
if (!f) {
err_quit("The project was compiled with -DUSEDB option,\n"
"Pass the config.txt file as an argument in the format:\n"
"odbc_srcname,dbuser,dbpasswd");
}
if (fgets(line, sizeof(line), f) == NULL) {
err_quit("Error reading %s\n", sql_config);
}
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
if (sqlinit(line) == -1)
err_quit("can't initialize odbc DB");
printf("ODBC: %s\n", line);
#endif
if (hashtbl_init(&dns_table, DNSTBL_SIZE, 4) == -1)
err_quit("Failed to initialize dns_table");
/* no dns6 cache for now
if (hashtbl_init(&dns6_table, DNSTBL_SIZE, 16) == -1)
err_quit("Failed to initialize dns6_table");
*/
if (!cache_initip(&cacheip, CACHEIP_INITSZ))
err_quit("Failed to initialize ip cache");
if (!cache_initapp(&cacheapp, CACHEAP_INITSZ))
err_quit("Failed to initialize app cache");
}
void usage(char *prog) {
err_quit("Usage: %s [-Ddvh] socks.cfg\n"
"\t-D\tdaemonize process\n"
"\t-d\tenables debug output\n"
"\t-v\tenables verbose output\n"
"\t-h\tprint this help and exit\n"
"\nlogging:\n"
"\t-v\terror\n"
"\t-vv\twarning\n"
"\t-vvv\tnotice\n"
"\t-vvvv\tinfo\n"
"\ndebug:\nthe program should be compiled with -DDEBUG\n"
"\t-d\t0 level\n"
"\t-dd\t1 level\n"
"\t-ddd\t2 level\n"
, prog);
}
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t clilen;
struct sockaddr_storage cliaddr;
const char *host, *service;
int opt;
int daemon = 0;
int debug = -1, verbose = LOG_CRIT;
while ((opt = getopt(argc, argv, "Ddvh")) != -1) {
switch (opt) {
case 'D':
daemon = 1;
break;
case 'd':
if (debug < 2)
debug++;
break;
case 'v':
if (verbose < LOG_INFO)
verbose++;
break;
case 'h':
default:
usage(argv[0]);
}
}
silk_log_level = verbose;
silk_debug_level = debug;
if (argc - optind > 1)
usage(argv[0]);
sql_config = argv[optind];
if (daemon)
daemon_init();
server_init();
host = NULL;
service = "1080";
listenfd = tcp_listen(host, service, NULL);
printf("Listening on:\t%s:%s\t%lu/%s\n", host ? host : "*", service, (unsigned long)getpid(), argv[0]);
printf("verbosity level: %s\ndebug level: (%s) %d level\n\n", syslog_lvl2str(verbose), debug == -1 ? "disabled" : "enabled", debug);
for (;;) {
clilen = sizeof(cliaddr);
if ((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen)) == -1) {
if (errno == EINTR)
continue;
else {
SILK_LOG(ERR, "accept error on fd(%d)", connfd);
continue;
}
}
/* nasty cast warning =) */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
thpool_add_work(thpool, &proxy_start, (void*)connfd);
#pragma GCC diagnostic pop
}
return 0;
}