1 | /* |
1 | /* |
2 | vpn_tcp.C -- handle the tcp part of the protocol. |
2 | vpn_tcp.C -- handle the tcp part of the protocol. |
|
|
3 | Copyright (C) 2003-2007 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
8 | 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 |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include "config.h" |
22 | #include "config.h" |
20 | |
23 | |
21 | #if ENABLE_TCP |
24 | #if ENABLE_TCP |
22 | |
25 | |
23 | // tcp processing is extremely ugly, since the vpe protocol is simply |
26 | // tcp processing is extremely ugly, since the gvpe protocol is simply |
24 | // designed for unreliable datagram networks. tcp is implemented by |
27 | // designed for unreliable datagram networks. tcp is implemented by |
25 | // multiplexing packets over tcp. errors are completely ignored, as we |
28 | // multiplexing packets over tcp. errors are completely ignored, as we |
26 | // rely on the higher level protocol to time out and reconnect. |
29 | // rely on the higher level layers to time out and reconnect. |
27 | |
30 | |
28 | #include <cstring> |
31 | #include <cstring> |
29 | |
32 | |
30 | #include <sys/types.h> |
33 | #include <sys/types.h> |
31 | #include <sys/socket.h> |
34 | #include <sys/socket.h> |
… | |
… | |
54 | { |
57 | { |
55 | return *a < *b; |
58 | return *a < *b; |
56 | } |
59 | } |
57 | }; |
60 | }; |
58 | |
61 | |
59 | struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> { |
62 | struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> |
60 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
63 | { |
|
|
64 | inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
61 | |
65 | |
62 | tcp_si_map () |
66 | tcp_si_map () |
63 | : cleaner(this, &tcp_si_map::cleaner_cb) |
|
|
64 | { |
67 | { |
65 | cleaner.start (0); |
68 | ev_default_loop (0); |
|
|
69 | cleaner.set<tcp_si_map, &tcp_si_map::cleaner_cb> (this); |
|
|
70 | cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
66 | } |
71 | } |
|
|
72 | |
67 | } tcp_si; |
73 | } tcp_si; |
68 | |
74 | |
69 | struct tcp_connection : io_watcher { |
75 | struct tcp_connection : ev::io |
|
|
76 | { |
|
|
77 | int tos; |
70 | tstamp last_activity; |
78 | tstamp last_activity; |
71 | const sockinfo si; |
79 | const sockinfo si; |
72 | vpn &v; |
80 | vpn &v; |
73 | bool active; // this connection has been actively established |
81 | bool active; // this connection has been actively established |
74 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
82 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
… | |
… | |
82 | #if ENABLE_HTTP_PROXY |
90 | #if ENABLE_HTTP_PROXY |
83 | char *proxy_req; |
91 | char *proxy_req; |
84 | int proxy_req_len; |
92 | int proxy_req_len; |
85 | #endif |
93 | #endif |
86 | |
94 | |
87 | void tcpv4_ev (io_watcher &w, short revents); |
95 | inline void tcpv4_ev (ev::io &w, int revents); |
88 | |
96 | |
89 | bool send_packet (vpn_packet *pkt, int tos); |
97 | bool send_packet (vpn_packet *pkt, int tos); |
90 | bool write_packet (); |
98 | bool write_packet (); |
91 | |
99 | |
92 | void error (); // abort conenction && cleanup |
100 | void error (); // abort conenction && cleanup |
93 | |
101 | |
94 | operator tcp_si_map::value_type() |
102 | operator tcp_si_map::value_type() |
95 | { |
103 | { |
96 | return tcp_si_map::value_type (&si, this); |
104 | return tcp_si_map::value_type (&si, this); |
97 | } |
105 | } |
98 | |
106 | |
99 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
107 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
100 | ~tcp_connection (); |
108 | ~tcp_connection (); |
101 | }; |
109 | }; |
102 | |
110 | |
103 | void tcp_si_map::cleaner_cb (time_watcher &w) |
111 | void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
104 | { |
112 | { |
105 | w.at = NOW + 600; |
|
|
106 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
113 | tstamp to = ev_now () - ::conf.keepalive - 30 - 60; |
107 | |
114 | |
108 | for (iterator i = begin (); i != end(); ) |
115 | for (iterator i = begin (); i != end(); ) |
109 | if (i->second->last_activity >= to) |
116 | if (i->second->last_activity >= to) |
110 | ++i; |
117 | ++i; |
111 | else |
118 | else |
112 | { |
119 | { |
|
|
120 | delete i->second; |
113 | erase (i); |
121 | erase (i); |
114 | i = begin (); |
122 | i = begin (); |
115 | } |
123 | } |
116 | } |
124 | } |
117 | |
125 | |
118 | void |
126 | void |
119 | vpn::tcpv4_ev (io_watcher &w, short revents) |
127 | vpn::tcpv4_ev (ev::io &w, int revents) |
120 | { |
128 | { |
121 | if (revents & (POLLIN | POLLERR)) |
129 | if (revents & EV_READ) |
122 | { |
130 | { |
123 | struct sockaddr_in sa; |
131 | struct sockaddr_in sa; |
124 | socklen_t sa_len = sizeof (sa); |
132 | socklen_t sa_len = sizeof (sa); |
125 | int len; |
133 | int len; |
126 | |
134 | |
127 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
135 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
128 | |
136 | |
129 | if (fd >= 0) |
137 | if (fd >= 0) |
130 | { |
138 | { |
|
|
139 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
140 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
141 | |
131 | sockinfo si(sa, PROT_TCPv4); |
142 | sockinfo si(sa, PROT_TCPv4); |
132 | |
143 | |
133 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
144 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
134 | |
|
|
135 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
136 | |
145 | |
137 | tcp_connection *i = new tcp_connection (fd, si, *this); |
146 | tcp_connection *i = new tcp_connection (fd, si, *this); |
138 | tcp_si.insert (*i); |
147 | tcp_si.insert (*i); |
139 | } |
148 | } |
140 | } |
149 | } |
… | |
… | |
194 | return false; |
203 | return false; |
195 | } |
204 | } |
196 | } |
205 | } |
197 | |
206 | |
198 | void |
207 | void |
199 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
208 | tcp_connection::tcpv4_ev (ev::io &w, int revents) |
200 | { |
209 | { |
201 | last_activity = NOW; |
210 | last_activity = ev_now (); |
202 | |
211 | |
203 | if (revents & (POLLERR | POLLHUP)) |
212 | if (revents & EV_WRITE) |
204 | { |
|
|
205 | error (); |
|
|
206 | return; |
|
|
207 | } |
|
|
208 | |
|
|
209 | if (revents & POLLOUT) |
|
|
210 | { |
213 | { |
211 | if (state == CONNECTING) |
214 | if (state == CONNECTING) |
212 | { |
215 | { |
213 | state = ESTABLISHED; |
216 | state = ESTABLISHED; |
214 | set (POLLIN); |
217 | set (EV_READ); |
215 | #if ENABLE_HTTP_PROXY |
218 | #if ENABLE_HTTP_PROXY |
216 | if (::conf.proxy_host && ::conf.proxy_port) |
219 | if (::conf.proxy_host && ::conf.proxy_port) |
217 | { |
220 | { |
218 | state = CONNECTING_PROXY; |
221 | state = CONNECTING_PROXY; |
|
|
222 | |
219 | write (fd, proxy_req, proxy_req_len); |
223 | if (write (fd, proxy_req, proxy_req_len) == 0) |
|
|
224 | { |
|
|
225 | error (); |
|
|
226 | return; |
|
|
227 | } |
|
|
228 | |
220 | free (proxy_req); proxy_req = 0; |
229 | free (proxy_req); proxy_req = 0; |
221 | } |
230 | } |
222 | #endif |
231 | #endif |
223 | } |
232 | } |
224 | else if (state == ESTABLISHED) |
233 | else if (state == ESTABLISHED) |
… | |
… | |
227 | { |
236 | { |
228 | if (write_packet ()) |
237 | if (write_packet ()) |
229 | { |
238 | { |
230 | delete w_pkt; w_pkt = 0; |
239 | delete w_pkt; w_pkt = 0; |
231 | |
240 | |
232 | set (POLLIN); |
241 | set (EV_READ); |
233 | } |
242 | } |
234 | } |
243 | } |
235 | else |
244 | else |
236 | set (POLLIN); |
245 | set (EV_READ); |
237 | } |
246 | } |
238 | else |
247 | else |
239 | set (POLLIN); |
248 | set (EV_READ); |
240 | } |
249 | } |
241 | |
250 | |
242 | if (revents & POLLIN) |
251 | if (revents & EV_READ) |
243 | { |
252 | { |
244 | if (state == ESTABLISHED) |
253 | if (state == ESTABLISHED) |
245 | for (;;) |
254 | for (;;) |
246 | { |
255 | { |
247 | if (!r_pkt) |
256 | if (!r_pkt) |
… | |
… | |
281 | break; |
290 | break; |
282 | } |
291 | } |
283 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
292 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
284 | break; |
293 | break; |
285 | |
294 | |
|
|
295 | // len == 0 <-> EOF |
286 | error (); |
296 | error (); |
287 | break; |
297 | break; |
288 | } |
298 | } |
289 | #if ENABLE_HTTP_PROXY |
299 | #if ENABLE_HTTP_PROXY |
290 | else if (state == CONNECTING_PROXY) |
300 | else if (state == CONNECTING_PROXY) |
… | |
… | |
340 | } |
350 | } |
341 | |
351 | |
342 | bool |
352 | bool |
343 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
353 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
344 | { |
354 | { |
345 | last_activity = NOW; |
355 | last_activity = ev_now (); |
346 | |
356 | |
347 | if (state == IDLE) |
357 | if (state == IDLE) |
348 | { |
358 | { |
349 | // woaw, the first lost packet ;) |
359 | // woaw, the first lost packet ;) |
350 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
360 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
… | |
… | |
384 | fcntl (fd, F_SETFL, O_NONBLOCK); |
394 | fcntl (fd, F_SETFL, O_NONBLOCK); |
385 | |
395 | |
386 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
396 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
387 | || errno == EINPROGRESS) |
397 | || errno == EINPROGRESS) |
388 | { |
398 | { |
|
|
399 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
400 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
401 | |
389 | state = CONNECTING; |
402 | state = CONNECTING; |
390 | start (fd, POLLOUT); |
403 | start (fd, EV_WRITE); |
391 | } |
404 | } |
392 | else |
405 | else |
393 | close (fd); |
406 | close (fd); |
394 | } |
407 | } |
395 | } |
408 | } |
… | |
… | |
400 | if (!w_pkt) |
413 | if (!w_pkt) |
401 | { |
414 | { |
402 | // how this maps to the underlying tcp packets we don't know |
415 | // how this maps to the underlying tcp packets we don't know |
403 | // and we don't care. at least we tried ;) |
416 | // and we don't care. at least we tried ;) |
404 | #if defined(SOL_IP) && defined(IP_TOS) |
417 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
418 | if (tos != this->tos) |
|
|
419 | { |
|
|
420 | this->tos = tos; |
405 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
421 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
422 | } |
406 | #endif |
423 | #endif |
407 | |
424 | |
408 | w_pkt = pkt; |
425 | w_pkt = pkt; |
409 | w_ofs = 0; |
426 | w_ofs = 0; |
410 | w_len = pkt->len + 2; // length + size header |
427 | w_len = pkt->len + 2; // length + size header |
… | |
… | |
414 | else |
431 | else |
415 | { |
432 | { |
416 | w_pkt = new vpn_packet; |
433 | w_pkt = new vpn_packet; |
417 | w_pkt->set (*pkt); |
434 | w_pkt->set (*pkt); |
418 | |
435 | |
419 | set (POLLIN | POLLOUT); |
436 | set (EV_READ | EV_WRITE); |
420 | } |
437 | } |
421 | } |
438 | } |
422 | } |
439 | } |
423 | |
440 | |
424 | return state != ERROR; |
441 | return state != ERROR; |
425 | } |
442 | } |
426 | |
443 | |
427 | void tcp_connection::error () |
444 | void tcp_connection::error () |
428 | { |
445 | { |
|
|
446 | stop (); |
|
|
447 | |
429 | if (fd >= 0) |
448 | if (fd >= 0) |
430 | { |
449 | { |
431 | close (fd); |
450 | close (fd); |
|
|
451 | tos = -1; |
432 | fd = -1; |
452 | fd = -1; |
433 | } |
453 | } |
434 | |
454 | |
435 | delete r_pkt; r_pkt = 0; |
455 | delete r_pkt; r_pkt = 0; |
436 | delete w_pkt; w_pkt = 0; |
456 | delete w_pkt; w_pkt = 0; |
437 | #if ENABLE_HTTP_PROXY |
457 | #if ENABLE_HTTP_PROXY |
438 | free (proxy_req); proxy_req = 0; |
458 | free (proxy_req); proxy_req = 0; |
439 | #endif |
459 | #endif |
440 | |
460 | |
441 | stop (); |
|
|
442 | state = active ? IDLE : ERROR; |
461 | state = active ? IDLE : ERROR; |
443 | } |
462 | } |
444 | |
463 | |
445 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
464 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
446 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
465 | : v(v_), si(si_) |
447 | { |
466 | { |
|
|
467 | set<tcp_connection, &tcp_connection::tcpv4_ev> (this); |
|
|
468 | |
448 | last_activity = NOW; |
469 | last_activity = ev_now (); |
449 | r_pkt = 0; |
470 | r_pkt = 0; |
450 | w_pkt = 0; |
471 | w_pkt = 0; |
|
|
472 | tos = -1; |
451 | fd = fd_; |
473 | fd = fd_; |
452 | #if ENABLE_HTTP_PROXY |
474 | #if ENABLE_HTTP_PROXY |
453 | proxy_req = 0; |
475 | proxy_req = 0; |
454 | #endif |
476 | #endif |
455 | |
477 | |
… | |
… | |
460 | } |
482 | } |
461 | else |
483 | else |
462 | { |
484 | { |
463 | active = false; |
485 | active = false; |
464 | state = ESTABLISHED; |
486 | state = ESTABLISHED; |
465 | start (fd, POLLIN); |
487 | start (fd, EV_READ); |
466 | } |
488 | } |
467 | } |
489 | } |
468 | |
490 | |
469 | tcp_connection::~tcp_connection () |
491 | tcp_connection::~tcp_connection () |
470 | { |
492 | { |