1 |
pcg |
1.1 |
/* |
2 |
|
|
vpn_tcp.C -- handle the tcp part of the protocol. |
3 |
|
|
|
4 |
|
|
This program is free software; you can redistribute it and/or modify |
5 |
|
|
it under the terms of the GNU General Public License as published by |
6 |
|
|
the Free Software Foundation; either version 2 of the License, or |
7 |
|
|
(at your option) any later version. |
8 |
|
|
|
9 |
|
|
This program is distributed in the hope that it will be useful, |
10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
|
|
GNU General Public License for more details. |
13 |
|
|
|
14 |
|
|
You should have received a copy of the GNU General Public License |
15 |
|
|
along with this program; if not, write to the Free Software |
16 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include "config.h" |
20 |
|
|
|
21 |
|
|
#if ENABLE_TCP |
22 |
|
|
|
23 |
|
|
// tcp processing is extremely ugly, since the vpe protocol is simply |
24 |
|
|
// designed for unreliable datagram networks. tcp is implemented by |
25 |
|
|
// multiplexing packets over tcp. errors are completely ignored, as we |
26 |
|
|
// rely on the higher level protocol to time out and reconnect. |
27 |
|
|
|
28 |
|
|
#include <cstring> |
29 |
|
|
|
30 |
|
|
#include <sys/types.h> |
31 |
|
|
#include <sys/socket.h> |
32 |
|
|
#include <sys/poll.h> |
33 |
|
|
#include <sys/wait.h> |
34 |
|
|
#include <netinet/in.h> |
35 |
|
|
#include <sys/uio.h> |
36 |
|
|
#include <arpa/inet.h> |
37 |
|
|
#include <errno.h> |
38 |
|
|
#include <time.h> |
39 |
|
|
#include <unistd.h> |
40 |
|
|
|
41 |
|
|
#include <map> |
42 |
|
|
#include <unistd.h> |
43 |
|
|
#include <fcntl.h> |
44 |
|
|
#include <sys/poll.h> |
45 |
|
|
|
46 |
|
|
#include "vpn.h" |
47 |
|
|
|
48 |
pcg |
1.4 |
#if ENABLE_HTTP_PROXY |
49 |
|
|
# include "conf.h" |
50 |
|
|
#endif |
51 |
|
|
|
52 |
pcg |
1.1 |
struct tcp_connection; |
53 |
|
|
|
54 |
|
|
struct lt_sockinfo |
55 |
|
|
{ |
56 |
|
|
bool operator()(const sockinfo *a, const sockinfo *b) const |
57 |
|
|
{ |
58 |
|
|
return *a < *b; |
59 |
|
|
} |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> { |
63 |
|
|
void cleaner_cb (time_watcher &w); time_watcher cleaner; |
64 |
|
|
|
65 |
|
|
tcp_si_map () |
66 |
|
|
: cleaner(this, &tcp_si_map::cleaner_cb) |
67 |
|
|
{ |
68 |
|
|
cleaner.start (0); |
69 |
|
|
} |
70 |
|
|
} tcp_si; |
71 |
|
|
|
72 |
|
|
struct tcp_connection : io_watcher { |
73 |
|
|
tstamp last_activity; |
74 |
|
|
const sockinfo si; |
75 |
|
|
vpn &v; |
76 |
|
|
bool active; // this connection has been actively established |
77 |
pcg |
1.4 |
enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
78 |
pcg |
1.1 |
|
79 |
|
|
vpn_packet *r_pkt; |
80 |
|
|
u32 r_len, r_ofs; |
81 |
|
|
|
82 |
pcg |
1.3 |
vpn_packet *w_pkt; |
83 |
|
|
u32 w_len, w_ofs; |
84 |
|
|
|
85 |
pcg |
1.4 |
#if ENABLE_HTTP_PROXY |
86 |
|
|
char *proxy_req; |
87 |
|
|
int proxy_req_len; |
88 |
|
|
#endif |
89 |
|
|
|
90 |
pcg |
1.1 |
void tcpv4_ev (io_watcher &w, short revents); |
91 |
|
|
|
92 |
pcg |
1.2 |
bool send_packet (vpn_packet *pkt, int tos); |
93 |
pcg |
1.3 |
bool write_packet (); |
94 |
pcg |
1.1 |
|
95 |
|
|
void error (); // abort conenction && cleanup |
96 |
|
|
|
97 |
|
|
operator tcp_si_map::value_type() |
98 |
|
|
{ |
99 |
|
|
return tcp_si_map::value_type (&si, this); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
103 |
|
|
~tcp_connection (); |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
void tcp_si_map::cleaner_cb (time_watcher &w) |
107 |
|
|
{ |
108 |
|
|
w.at = NOW + 600; |
109 |
|
|
tstamp to = NOW - ::conf.keepalive - 30 - 60; |
110 |
|
|
|
111 |
|
|
for (iterator i = begin (); i != end(); ) |
112 |
|
|
if (i->second->last_activity >= to) |
113 |
|
|
++i; |
114 |
|
|
else |
115 |
|
|
{ |
116 |
|
|
erase (i); |
117 |
|
|
i = begin (); |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
void |
122 |
|
|
vpn::tcpv4_ev (io_watcher &w, short revents) |
123 |
|
|
{ |
124 |
|
|
if (revents & (POLLIN | POLLERR)) |
125 |
|
|
{ |
126 |
|
|
struct sockaddr_in sa; |
127 |
|
|
socklen_t sa_len = sizeof (sa); |
128 |
|
|
int len; |
129 |
|
|
|
130 |
|
|
int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
131 |
|
|
|
132 |
|
|
if (fd >= 0) |
133 |
|
|
{ |
134 |
|
|
sockinfo si(sa, PROT_TCPv4); |
135 |
|
|
|
136 |
|
|
slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
137 |
|
|
|
138 |
|
|
fcntl (fd, F_SETFL, O_NONBLOCK); |
139 |
|
|
|
140 |
|
|
tcp_connection *i = new tcp_connection (fd, si, *this); |
141 |
|
|
tcp_si.insert (*i); |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
pcg |
1.2 |
bool |
147 |
pcg |
1.1 |
vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
148 |
|
|
{ |
149 |
|
|
tcp_si_map::iterator info = tcp_si.find (&si); |
150 |
|
|
|
151 |
|
|
tcp_connection *i; |
152 |
|
|
|
153 |
|
|
if (info == tcp_si.end ()) |
154 |
|
|
{ |
155 |
|
|
i = new tcp_connection (-1, si, *this); |
156 |
|
|
tcp_si.insert (*i); |
157 |
|
|
} |
158 |
|
|
else |
159 |
|
|
i = info->second; |
160 |
|
|
|
161 |
pcg |
1.2 |
return i->send_packet (pkt, tos); |
162 |
pcg |
1.1 |
} |
163 |
|
|
|
164 |
pcg |
1.3 |
bool |
165 |
|
|
tcp_connection::write_packet () |
166 |
|
|
{ |
167 |
|
|
ssize_t len; |
168 |
|
|
|
169 |
|
|
if (w_ofs < 2) |
170 |
|
|
{ |
171 |
|
|
u16 plen = htons (w_pkt->len); |
172 |
|
|
|
173 |
|
|
iovec vec[2]; |
174 |
|
|
vec[0].iov_base = ((u8 *)&plen) + w_ofs; |
175 |
|
|
vec[0].iov_len = 2 - w_ofs; |
176 |
|
|
vec[1].iov_base = &((*w_pkt)[0]); |
177 |
|
|
vec[1].iov_len = w_len - 2; |
178 |
|
|
|
179 |
|
|
len = writev (fd, vec, 2); |
180 |
|
|
} |
181 |
|
|
else |
182 |
|
|
len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len); |
183 |
|
|
|
184 |
|
|
if (len > 0) |
185 |
|
|
{ |
186 |
|
|
w_ofs += len; |
187 |
|
|
w_len -= len; |
188 |
|
|
|
189 |
|
|
return w_len == 0; |
190 |
|
|
} |
191 |
|
|
else if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
192 |
|
|
return false; |
193 |
|
|
else |
194 |
|
|
{ |
195 |
|
|
error (); |
196 |
|
|
return false; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
pcg |
1.1 |
void |
201 |
|
|
tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
202 |
|
|
{ |
203 |
|
|
last_activity = NOW; |
204 |
|
|
|
205 |
|
|
if (revents & (POLLERR | POLLHUP)) |
206 |
|
|
{ |
207 |
pcg |
1.3 |
error (); |
208 |
|
|
return; |
209 |
pcg |
1.1 |
} |
210 |
|
|
|
211 |
pcg |
1.3 |
if (revents & POLLOUT) |
212 |
|
|
{ |
213 |
|
|
if (state == CONNECTING) |
214 |
|
|
{ |
215 |
|
|
state = ESTABLISHED; |
216 |
|
|
set (POLLIN); |
217 |
pcg |
1.4 |
#if ENABLE_HTTP_PROXY |
218 |
|
|
if (::conf.proxy_host && ::conf.proxy_port) |
219 |
|
|
{ |
220 |
|
|
state = CONNECTING_PROXY; |
221 |
|
|
write (fd, proxy_req, proxy_req_len); |
222 |
|
|
free (proxy_req); proxy_req = 0; |
223 |
|
|
} |
224 |
|
|
#endif |
225 |
pcg |
1.3 |
} |
226 |
|
|
else if (state == ESTABLISHED) |
227 |
|
|
{ |
228 |
|
|
if (w_pkt) |
229 |
|
|
{ |
230 |
|
|
if (write_packet ()) |
231 |
|
|
{ |
232 |
|
|
delete w_pkt; w_pkt = 0; |
233 |
|
|
|
234 |
|
|
set (POLLIN); |
235 |
|
|
} |
236 |
|
|
} |
237 |
|
|
else |
238 |
|
|
set (POLLIN); |
239 |
|
|
} |
240 |
|
|
else |
241 |
|
|
set (POLLIN); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
if (revents & POLLIN) |
245 |
pcg |
1.1 |
{ |
246 |
pcg |
1.4 |
if (state == ESTABLISHED) |
247 |
|
|
for (;;) |
248 |
|
|
{ |
249 |
|
|
if (!r_pkt) |
250 |
|
|
{ |
251 |
|
|
r_pkt = new vpn_packet; |
252 |
|
|
r_ofs = 0; |
253 |
|
|
r_len = 2; // header |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
257 |
|
|
|
258 |
|
|
if (len > 0) |
259 |
|
|
{ |
260 |
|
|
r_len -= len; |
261 |
|
|
r_ofs += len; |
262 |
|
|
|
263 |
|
|
if (r_len == 0) |
264 |
|
|
{ |
265 |
|
|
if (r_ofs == 2) |
266 |
|
|
{ |
267 |
|
|
r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
268 |
|
|
r_pkt->len = r_len; |
269 |
|
|
|
270 |
|
|
if (r_len > 0 && r_len < MAXSIZE) |
271 |
|
|
continue; |
272 |
|
|
} |
273 |
|
|
else |
274 |
|
|
{ |
275 |
|
|
v.recv_vpn_packet (r_pkt, si); |
276 |
|
|
delete r_pkt; |
277 |
|
|
r_pkt = 0; |
278 |
|
|
|
279 |
|
|
continue; |
280 |
|
|
} |
281 |
|
|
} |
282 |
|
|
else |
283 |
|
|
break; |
284 |
|
|
} |
285 |
|
|
else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
286 |
|
|
break; |
287 |
|
|
|
288 |
|
|
error (); |
289 |
|
|
break; |
290 |
|
|
} |
291 |
|
|
#if ENABLE_HTTP_PROXY |
292 |
|
|
else if (state == CONNECTING_PROXY) |
293 |
pcg |
1.1 |
{ |
294 |
pcg |
1.4 |
fcntl (fd, F_SETFL, 0); |
295 |
|
|
char r[1024]; |
296 |
|
|
int i; |
297 |
|
|
bool emptyline = false; |
298 |
pcg |
1.1 |
|
299 |
pcg |
1.4 |
// we do a blocking read of the response, to hell with it |
300 |
|
|
for (i = 0; i < 1023; i++) |
301 |
pcg |
1.1 |
{ |
302 |
pcg |
1.4 |
int l = read (fd, &r[i], 1); |
303 |
pcg |
1.1 |
|
304 |
pcg |
1.4 |
if (l <= 0) |
305 |
pcg |
1.1 |
{ |
306 |
pcg |
1.4 |
error (); |
307 |
|
|
return; |
308 |
|
|
} |
309 |
pcg |
1.1 |
|
310 |
pcg |
1.4 |
if (r[i] == '\012') |
311 |
|
|
{ |
312 |
|
|
if (emptyline) |
313 |
|
|
break; |
314 |
pcg |
1.1 |
else |
315 |
pcg |
1.4 |
emptyline = true; |
316 |
pcg |
1.1 |
} |
317 |
pcg |
1.4 |
else if (r[i] != '\015') |
318 |
|
|
emptyline = false; |
319 |
pcg |
1.1 |
} |
320 |
|
|
|
321 |
pcg |
1.4 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
322 |
|
|
|
323 |
|
|
if (i < 12) |
324 |
|
|
{ |
325 |
pcg |
1.6 |
slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"), |
326 |
pcg |
1.5 |
(const char *)si); |
327 |
pcg |
1.4 |
error (); |
328 |
|
|
} |
329 |
|
|
else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/' |
330 |
|
|
|| r[5] != '1' // http-major |
331 |
|
|
|| r[9] != '2') // response |
332 |
|
|
{ |
333 |
pcg |
1.6 |
slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"), |
334 |
pcg |
1.5 |
(const char *)si, r); |
335 |
pcg |
1.4 |
error (); |
336 |
|
|
} |
337 |
|
|
else |
338 |
|
|
state = ESTABLISHED; |
339 |
pcg |
1.1 |
} |
340 |
pcg |
1.4 |
#endif |
341 |
pcg |
1.1 |
} |
342 |
|
|
} |
343 |
|
|
|
344 |
pcg |
1.2 |
bool |
345 |
pcg |
1.1 |
tcp_connection::send_packet (vpn_packet *pkt, int tos) |
346 |
|
|
{ |
347 |
|
|
last_activity = NOW; |
348 |
|
|
|
349 |
|
|
if (state == IDLE) |
350 |
|
|
{ |
351 |
|
|
// woaw, the first lost packet ;) |
352 |
|
|
fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
353 |
|
|
|
354 |
|
|
if (fd >= 0) |
355 |
|
|
{ |
356 |
pcg |
1.4 |
const sockinfo *csi = &si; |
357 |
|
|
|
358 |
|
|
#if ENABLE_HTTP_PROXY |
359 |
|
|
sockinfo psi; |
360 |
|
|
|
361 |
|
|
if (::conf.proxy_host && ::conf.proxy_port) |
362 |
|
|
{ |
363 |
|
|
psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4); |
364 |
|
|
|
365 |
|
|
if (psi.valid ()) |
366 |
|
|
{ |
367 |
|
|
csi = ψ |
368 |
|
|
|
369 |
|
|
proxy_req_len = asprintf (&proxy_req, |
370 |
|
|
"CONNECT %s:%d HTTP/1.0\015\012" |
371 |
|
|
"%s%s%s" // optional proxy-auth |
372 |
|
|
"\015\012", |
373 |
|
|
si.ntoa (), |
374 |
|
|
ntohs (si.port), |
375 |
|
|
::conf.proxy_auth ? "Proxy-Authorization: Basic " : "", |
376 |
|
|
::conf.proxy_auth ? ::conf.proxy_auth : "", |
377 |
|
|
::conf.proxy_auth ? "\015\012" : ""); |
378 |
|
|
|
379 |
|
|
} |
380 |
|
|
else |
381 |
|
|
slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"), |
382 |
|
|
::conf.proxy_host); |
383 |
|
|
} |
384 |
|
|
#endif |
385 |
|
|
|
386 |
pcg |
1.1 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
387 |
pcg |
1.4 |
|
388 |
|
|
if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
389 |
pcg |
1.1 |
|| errno == EINPROGRESS) |
390 |
|
|
{ |
391 |
|
|
state = CONNECTING; |
392 |
|
|
start (fd, POLLOUT); |
393 |
|
|
} |
394 |
|
|
else |
395 |
|
|
close (fd); |
396 |
|
|
} |
397 |
|
|
} |
398 |
|
|
else if (state == ESTABLISHED) |
399 |
|
|
{ |
400 |
pcg |
1.3 |
// drop packet if the tcp write buffer is full. this *is* the |
401 |
|
|
// right thing to do, not using tcp *is* the right thing to do. |
402 |
|
|
if (!w_pkt) |
403 |
|
|
{ |
404 |
|
|
// how this maps to the underlying tcp packets we don't know |
405 |
|
|
// and we don't care. at least we tried ;) |
406 |
|
|
setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
407 |
|
|
|
408 |
|
|
w_pkt = pkt; |
409 |
|
|
w_ofs = 0; |
410 |
|
|
w_len = pkt->len + 2; // length + size header |
411 |
|
|
|
412 |
|
|
if (write_packet ()) |
413 |
|
|
w_pkt = 0; |
414 |
|
|
else |
415 |
|
|
{ |
416 |
|
|
w_pkt = new vpn_packet; |
417 |
|
|
w_pkt->set (*pkt); |
418 |
pcg |
1.1 |
|
419 |
pcg |
1.3 |
set (POLLIN | POLLOUT); |
420 |
|
|
} |
421 |
|
|
} |
422 |
pcg |
1.1 |
} |
423 |
pcg |
1.2 |
|
424 |
|
|
return state != ERROR; |
425 |
pcg |
1.1 |
} |
426 |
|
|
|
427 |
pcg |
1.4 |
void tcp_connection::error () |
428 |
|
|
{ |
429 |
|
|
if (fd >= 0) |
430 |
|
|
{ |
431 |
|
|
close (fd); |
432 |
|
|
fd = -1; |
433 |
|
|
} |
434 |
|
|
|
435 |
|
|
delete r_pkt; r_pkt = 0; |
436 |
|
|
delete w_pkt; w_pkt = 0; |
437 |
|
|
#if ENABLE_HTTP_PROXY |
438 |
|
|
free (proxy_req); proxy_req = 0; |
439 |
|
|
#endif |
440 |
|
|
|
441 |
|
|
stop (); |
442 |
|
|
state = active ? IDLE : ERROR; |
443 |
|
|
} |
444 |
|
|
|
445 |
pcg |
1.1 |
tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
446 |
|
|
: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
447 |
|
|
{ |
448 |
|
|
last_activity = NOW; |
449 |
|
|
r_pkt = 0; |
450 |
pcg |
1.3 |
w_pkt = 0; |
451 |
pcg |
1.1 |
fd = fd_; |
452 |
pcg |
1.4 |
#if ENABLE_HTTP_PROXY |
453 |
|
|
proxy_req = 0; |
454 |
|
|
#endif |
455 |
pcg |
1.1 |
|
456 |
|
|
if (fd < 0) |
457 |
|
|
{ |
458 |
|
|
active = true; |
459 |
|
|
state = IDLE; |
460 |
|
|
} |
461 |
|
|
else |
462 |
|
|
{ |
463 |
|
|
active = false; |
464 |
|
|
state = ESTABLISHED; |
465 |
|
|
start (fd, POLLIN); |
466 |
|
|
} |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
tcp_connection::~tcp_connection () |
470 |
|
|
{ |
471 |
|
|
error (); |
472 |
|
|
} |
473 |
|
|
|
474 |
|
|
#endif |
475 |
|
|
|