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
|
/*
* tcpproxy
*
* tcpproxy is a simple tcp connection proxy which combines the
* features of rinetd and 6tunnel. tcpproxy supports IPv4 and
* IPv6 and also supports connections from IPv6 to IPv4
* endpoints and vice versa.
*
*
* Copyright (C) 2010-2011 Christian Pointner <equinox@spreadspace.org>
*
* This file is part of tcpproxy.
*
* tcpproxy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* tcpproxy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include "datatypes.h"
#include "options.h"
#include "string_list.h"
#include "log.h"
#include "daemon.h"
#include "listener.h"
#include "clients.h"
extern FILE *yyin;
extern void yyinit(options_t* opt, listeners_t* listeners);
extern int yyparse(void);
int main_loop(options_t* opt, listeners_t* listeners)
{
log_printf(INFO, "entering main loop");
int sig_fd = signal_init();
if(sig_fd < 0)
return -1;
clients_t clients;
int return_value = clients_init(&clients, opt->buffer_size_);
while(!return_value) {
fd_set readfds, writefds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(sig_fd, &readfds);
int nfds = sig_fd;
listener_read_fds(listeners, &readfds, &nfds);
clients_read_fds(&clients, &readfds, &nfds);
clients_write_fds(&clients, &writefds, &nfds);
int ret = select(nfds + 1, &readfds, &writefds, NULL, NULL);
if(ret == -1 && errno != EINTR) {
log_printf(ERROR, "select returned with error: %s", strerror(errno));
return_value = -1;
break;
}
if(!ret || ret == -1)
continue;
if(FD_ISSET(sig_fd, &readfds)) {
if(signal_handle()) {
return_value = 1;
break;
}
}
return_value = listener_handle_accept(listeners, &clients, &readfds);
if(return_value) break;
return_value = clients_write(&clients, &writefds);
if(return_value) break;
return_value = clients_read(&clients, &readfds);
}
clients_clear(&clients);
signal_stop();
return return_value;
}
int main(int argc, char* argv[])
{
log_init();
options_t opt;
int ret = options_parse(&opt, argc, argv);
if(ret) {
if(ret > 0) {
fprintf(stderr, "syntax error near: %s\n\n", argv[ret]);
}
if(ret == -2) {
fprintf(stderr, "memory error on options_parse, exitting\n");
}
if(ret == -3) {
options_print_version();
}
if(ret != -2 && ret != -3)
options_print_usage();
if(ret == -1 || ret == -3)
ret = 0;
options_clear(&opt);
log_close();
exit(ret);
}
slist_element_t* tmp = opt.log_targets_.first_;
while(tmp) {
ret = log_add_target(tmp->data_);
if(ret) {
switch(ret) {
case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break;
case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", (char*)(tmp->data_)); break;
case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", (char*)(tmp->data_)); break;
default: fprintf(stderr, "syntax error near: '%s', exitting\n", (char*)(tmp->data_)); break;
}
options_clear(&opt);
log_close();
exit(ret);
}
tmp = tmp->next_;
}
log_printf(NOTICE, "just started...");
options_parse_post(&opt);
listeners_t listeners;
ret = listener_init(&listeners);
if(ret) {
options_clear(&opt);
log_close();
exit(-1);
}
if(opt.local_port_) {
ret = listener_add(&listeners, opt.local_addr_, opt.lresolv_type_, opt.local_port_, opt.remote_addr_, opt.rresolv_type_, opt.remote_port_, opt.source_addr_);
if(ret) {
listener_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
} else {
yyin = fopen(opt.config_file_, "r");
if(!yyin) {
log_printf(ERROR, "can't open config file %s: %s", opt.config_file_, strerror(errno));
listener_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
yyinit(&opt, &listeners);
yyparse();
}
priv_info_t priv;
if(opt.username_)
if(priv_init(&priv, opt.username_, opt.groupname_)) {
listener_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
FILE* pid_file = NULL;
if(opt.pid_file_) {
pid_file = fopen(opt.pid_file_, "w");
if(!pid_file) {
log_printf(WARNING, "unable to open pid file: %s", strerror(errno));
}
}
if(opt.chroot_dir_)
if(do_chroot(opt.chroot_dir_)) {
listener_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
if(opt.username_)
if(priv_drop(&priv)) {
listener_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
if(opt.daemonize_) {
pid_t oldpid = getpid();
daemonize();
log_printf(INFO, "running in background now (old pid: %d)", oldpid);
}
if(pid_file) {
pid_t pid = getpid();
fprintf(pid_file, "%d", pid);
fclose(pid_file);
}
ret = main_loop(&opt, &listeners);
listener_clear(&listeners);
options_clear(&opt);
if(!ret)
log_printf(NOTICE, "normal shutdown");
else if(ret < 0)
log_printf(NOTICE, "shutdown after error");
else
log_printf(NOTICE, "shutdown after signal");
log_close();
return ret;
}
|