-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunclient.cpp
295 lines (224 loc) · 7.9 KB
/
tunclient.cpp
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
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <signal.h>
#include <time.h>
#include "protocol.h"
//more code borrowed from ncat
void subexec(char * cmdexec[],int * childpid, int * childin,int * childout) {
int child_stdin[2];
int child_stdout[2];
int pid;
if (pipe(child_stdin) == -1 || pipe(child_stdout) == -1) {
perror("Can't create child pipes");
exit(1);
}
if ( (pid = fork()) < 0) {
perror("fork error");
exit(1);
} else if ( pid == 0 ) {
close(child_stdin[1]);
close(child_stdout[0]);
if(dup2(child_stdin[0], STDIN_FILENO) < 0) {
perror("dup2 failed");
exit(1);
}
if(dup2(child_stdout[1], STDOUT_FILENO) < 0) {
perror("dup2 failed");
exit(1);
}
execvp(cmdexec[0],cmdexec);
std::cerr << "error starting command " << cmdexec[0] << std::endl;
exit(1);
/* do child stuff */
} else {
close(child_stdin[0]);
close(child_stdout[1]);
/* do parent stuff */
*childin = child_stdin[1];
*childout = child_stdout[0];
}
}
static int max(int a, int b) {
if (a > b) {
return a;
}
return b;
}
uint64_t getNow() {
struct timespec ts;
if(clock_gettime(CLOCK_REALTIME,&ts)) {
std::cerr << "error failed to get system time\n.";
exit(1);
}
return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
}
void proxy_forever(Protocol & p, std::vector<uint8_t> & initialProtoData ,int protoin,int protoout,int datain, int dataout) {
int maxfd = datain;
maxfd = max(datain,maxfd);
maxfd = max(dataout,maxfd);
maxfd = max(protoout,maxfd);
maxfd = max(protoin,maxfd);
int64_t now = getNow();
std::vector<uint8_t> out;
std::vector<uint8_t> bufferedProtocolData;
std::vector<uint8_t> bufferedData;
bufferedProtocolData = initialProtoData;
uint8_t buff[256];
for (;;) {
fd_set readfds;
fd_set writefds;
fd_set errfds;
int r, n_r,n_w;
now = getNow();
if (p.getState() == STATE_UNINIT) {
std::cerr << "Connection terminated." << std::endl;
break;
}
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&errfds);
int doDataIn = p.readyForData();
int doBufferedOut = bufferedData.size() > 0;
int doBufferedProtoOut = bufferedProtocolData.size() > 0;
FD_SET(protoin, &readfds);
if(doDataIn) {
FD_SET(datain, &readfds);
}
if(doBufferedProtoOut) {
FD_SET(protoout,&writefds);
}
if(doBufferedOut) {
FD_SET(dataout,&writefds);
}
FD_SET(datain,&errfds);
FD_SET(dataout,&errfds);
FD_SET(protoin,&errfds);
FD_SET(protoout,&errfds);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000;
r = select(maxfd + 1, &readfds, &writefds, &errfds, &tv);
if (r == -1) {
break;
}
if(doDataIn) {
if (FD_ISSET(datain, &readfds)) {
if(!p.readyForData()) {
std::cerr << "BUG: bad assertion. not ready for data." << std::endl;
exit(1);
}
n_r = read(datain, buff, sizeof(buff));
if (n_r <= 0) {
break;
}
out = p.sendData(std::vector<uint8_t>(buff,buff+n_r),now);
bufferedProtocolData.insert(bufferedProtocolData.end(),out.begin(),out.end());
}
}
if (FD_ISSET(protoin, &readfds)) {
n_r = read(protoin, buff, sizeof(buff));
if (n_r <= 0) {
break;
}
out = std::vector<uint8_t>(buff,buff+n_r);
std::pair<std::vector<uint8_t>,std::vector<uint8_t> > eventRet;
eventRet = p.dataEvent(out,now,true);
bufferedProtocolData.insert(bufferedProtocolData.end(),eventRet.first.begin(),eventRet.first.end());
if(eventRet.second.size()) {
bufferedData.insert(bufferedData.end(),eventRet.second.begin(),eventRet.second.end());
}
}
if(doBufferedOut) {
if (FD_ISSET(dataout, &writefds)) {
if(!bufferedData.size()) {
std::cerr << "BUG: bad assertion. not for sending buffered data." << std::endl;
exit(1);
}
n_w = write(dataout,&bufferedData.front(),bufferedData.size());
if(n_w <= 0) {
break;
}
//inefficient due to vector realloc but w.e. , our cpu is faster than data link
bufferedData.erase(bufferedData.begin(),bufferedData.begin()+n_w);
}
}
if(doBufferedProtoOut) {
if (FD_ISSET(protoout, &writefds)) {
if(!bufferedProtocolData.size()) {
std::cerr << "BUG: bad assertion. not for sending protocol buffered data." << std::endl;
exit(1);
}
n_w = write(protoout,&bufferedProtocolData.front(),bufferedProtocolData.size());
if(n_w <= 0) {
break;
}
//inefficient due to vector realloc but w.e. , our cpu is faster than data link
bufferedProtocolData.erase(bufferedProtocolData.begin(),bufferedProtocolData.begin()+n_w);
}
}
if( FD_ISSET(datain,&errfds)
|| FD_ISSET(dataout,&errfds)
|| FD_ISSET(protoin,&errfds)
|| FD_ISSET(protoout,&errfds) ) {
std::cerr << "Closing - fd error\n";
break;
}
out = p.timerEvent(now);
bufferedProtocolData.insert(bufferedProtocolData.end(),out.begin(),out.end());
}
std::cerr << "closing connection\n";
close(protoout);
close(protoin);
close(datain);
close(dataout);
exit(0);
}
int
main(int argc, char *argv[]) {
int opt;
int server = 0;
while ((opt = getopt(argc, argv, "s")) != -1) {
switch (opt) {
case 's':
server = 1;
break;
default: /* '?' */
std::cerr << "Bad arguments." << std::endl;
exit(EXIT_FAILURE);
}
}
int childpid,childin,childout;
Protocol p;
if(!server) {
subexec(&argv[optind],&childpid,&childin,&childout);
std::vector<uint8_t> initVec;
initVec = p.connect(getNow());
proxy_forever(p,initVec,childout,childin,STDIN_FILENO,STDOUT_FILENO);
} else {
int n_r;
uint8_t buff[4096];
p.listen();
std::cerr << "listening for connection.\n";
while(1) {
n_r = read(STDIN_FILENO,buff,sizeof(buff));
if(n_r <= 0) {
std::cerr << "std in abruptly closed" << std::endl;
exit(1);
}
std::vector<uint8_t> out(buff,buff+n_r);
out = p.dataEvent(out,getNow()).first;
if(p.getState() != STATE_LISTENING) {
std::cerr << "Connection established\n";
subexec(&argv[optind],&childpid,&childin,&childout);
proxy_forever(p,out,STDIN_FILENO,STDOUT_FILENO,childout,childin);
return 0;
}
}
}
return 0;
}