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