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> |
32 | #include <sys/poll.h> |
|
|
33 | #include <sys/wait.h> |
35 | #include <sys/wait.h> |
34 | #include <netinet/in.h> |
|
|
35 | #include <sys/uio.h> |
36 | #include <sys/uio.h> |
36 | #include <arpa/inet.h> |
|
|
37 | #include <errno.h> |
37 | #include <errno.h> |
38 | #include <time.h> |
38 | #include <time.h> |
39 | #include <unistd.h> |
39 | #include <unistd.h> |
|
|
40 | #include <fcntl.h> |
40 | |
41 | |
41 | #include <map> |
42 | #include <map> |
42 | #include <unistd.h> |
43 | |
43 | #include <fcntl.h> |
44 | #include "netcompat.h" |
44 | #include <sys/poll.h> |
|
|
45 | |
45 | |
46 | #include "vpn.h" |
46 | #include "vpn.h" |
47 | |
47 | |
48 | #if ENABLE_HTTP_PROXY |
48 | #if ENABLE_HTTP_PROXY |
49 | # include "conf.h" |
49 | # include "conf.h" |
… | |
… | |
58 | return *a < *b; |
58 | return *a < *b; |
59 | } |
59 | } |
60 | }; |
60 | }; |
61 | |
61 | |
62 | 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> { |
63 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
63 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
64 | |
64 | |
65 | tcp_si_map () |
65 | tcp_si_map () |
66 | : cleaner(this, &tcp_si_map::cleaner_cb) |
66 | : cleaner(this, &tcp_si_map::cleaner_cb) |
67 | { |
67 | { |
68 | cleaner.start (0); |
68 | cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
69 | } |
69 | } |
|
|
70 | |
70 | } tcp_si; |
71 | } tcp_si; |
71 | |
72 | |
72 | struct tcp_connection : io_watcher { |
73 | struct tcp_connection : ev::io { |
73 | tstamp last_activity; |
74 | tstamp last_activity; |
74 | const sockinfo si; |
75 | const sockinfo si; |
75 | vpn &v; |
76 | vpn &v; |
76 | bool active; // this connection has been actively established |
77 | bool active; // this connection has been actively established |
77 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
78 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
… | |
… | |
85 | #if ENABLE_HTTP_PROXY |
86 | #if ENABLE_HTTP_PROXY |
86 | char *proxy_req; |
87 | char *proxy_req; |
87 | int proxy_req_len; |
88 | int proxy_req_len; |
88 | #endif |
89 | #endif |
89 | |
90 | |
90 | void tcpv4_ev (io_watcher &w, short revents); |
91 | void tcpv4_ev (ev::io &w, int revents); |
91 | |
92 | |
92 | bool send_packet (vpn_packet *pkt, int tos); |
93 | bool send_packet (vpn_packet *pkt, int tos); |
93 | bool write_packet (); |
94 | bool write_packet (); |
94 | |
95 | |
95 | void error (); // abort conenction && cleanup |
96 | void error (); // abort conenction && cleanup |
96 | |
97 | |
97 | operator tcp_si_map::value_type() |
98 | operator tcp_si_map::value_type() |
98 | { |
99 | { |
99 | return tcp_si_map::value_type (&si, this); |
100 | return tcp_si_map::value_type (&si, this); |
100 | } |
101 | } |
101 | |
102 | |
102 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
103 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
103 | ~tcp_connection (); |
104 | ~tcp_connection (); |
104 | }; |
105 | }; |
105 | |
106 | |
106 | void tcp_si_map::cleaner_cb (time_watcher &w) |
107 | void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
107 | { |
108 | { |
108 | w.at = NOW + 600; |
|
|
109 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
109 | tstamp to = ev::ev_now () - ::conf.keepalive - 30 - 60; |
110 | |
110 | |
111 | for (iterator i = begin (); i != end(); ) |
111 | for (iterator i = begin (); i != end(); ) |
112 | if (i->second->last_activity >= to) |
112 | if (i->second->last_activity >= to) |
113 | ++i; |
113 | ++i; |
114 | else |
114 | else |
115 | { |
115 | { |
|
|
116 | delete i->second; |
116 | erase (i); |
117 | erase (i); |
117 | i = begin (); |
118 | i = begin (); |
118 | } |
119 | } |
119 | } |
120 | } |
120 | |
121 | |
121 | void |
122 | void |
122 | vpn::tcpv4_ev (io_watcher &w, short revents) |
123 | vpn::tcpv4_ev (ev::io &w, int revents) |
123 | { |
124 | { |
124 | if (revents & (POLLIN | POLLERR)) |
125 | if (revents & EV_READ) |
125 | { |
126 | { |
126 | struct sockaddr_in sa; |
127 | struct sockaddr_in sa; |
127 | socklen_t sa_len = sizeof (sa); |
128 | socklen_t sa_len = sizeof (sa); |
128 | int len; |
129 | int len; |
129 | |
130 | |
130 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
131 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
131 | |
132 | |
132 | if (fd >= 0) |
133 | if (fd >= 0) |
133 | { |
134 | { |
|
|
135 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
136 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
137 | |
134 | sockinfo si(sa, PROT_TCPv4); |
138 | sockinfo si(sa, PROT_TCPv4); |
135 | |
139 | |
136 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
140 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
137 | |
|
|
138 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
139 | |
141 | |
140 | tcp_connection *i = new tcp_connection (fd, si, *this); |
142 | tcp_connection *i = new tcp_connection (fd, si, *this); |
141 | tcp_si.insert (*i); |
143 | tcp_si.insert (*i); |
142 | } |
144 | } |
143 | } |
145 | } |
… | |
… | |
169 | if (w_ofs < 2) |
171 | if (w_ofs < 2) |
170 | { |
172 | { |
171 | u16 plen = htons (w_pkt->len); |
173 | u16 plen = htons (w_pkt->len); |
172 | |
174 | |
173 | iovec vec[2]; |
175 | iovec vec[2]; |
|
|
176 | //TODO: char* is the right type? hardly... |
174 | vec[0].iov_base = ((u8 *)&plen) + w_ofs; |
177 | vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs; |
175 | vec[0].iov_len = 2 - w_ofs; |
178 | vec[0].iov_len = 2 - w_ofs; |
176 | vec[1].iov_base = &((*w_pkt)[0]); |
179 | vec[1].iov_base = (char *)&((*w_pkt)[0]); |
177 | vec[1].iov_len = w_len - 2; |
180 | vec[1].iov_len = w_len - 2; |
178 | |
181 | |
179 | len = writev (fd, vec, 2); |
182 | len = writev (fd, vec, 2); |
180 | } |
183 | } |
181 | else |
184 | else |
… | |
… | |
196 | return false; |
199 | return false; |
197 | } |
200 | } |
198 | } |
201 | } |
199 | |
202 | |
200 | void |
203 | void |
201 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
204 | tcp_connection::tcpv4_ev (ev::io &w, int revents) |
202 | { |
205 | { |
203 | last_activity = NOW; |
206 | last_activity = ev::ev_now (); |
204 | |
207 | |
205 | if (revents & (POLLERR | POLLHUP)) |
208 | if (revents & EV_WRITE) |
206 | { |
|
|
207 | error (); |
|
|
208 | return; |
|
|
209 | } |
|
|
210 | |
|
|
211 | if (revents & POLLOUT) |
|
|
212 | { |
209 | { |
213 | if (state == CONNECTING) |
210 | if (state == CONNECTING) |
214 | { |
211 | { |
215 | state = ESTABLISHED; |
212 | state = ESTABLISHED; |
216 | set (POLLIN); |
213 | set (EV_READ); |
217 | #if ENABLE_HTTP_PROXY |
214 | #if ENABLE_HTTP_PROXY |
218 | if (::conf.proxy_host && ::conf.proxy_port) |
215 | if (::conf.proxy_host && ::conf.proxy_port) |
219 | { |
216 | { |
220 | state = CONNECTING_PROXY; |
217 | state = CONNECTING_PROXY; |
|
|
218 | |
221 | write (fd, proxy_req, proxy_req_len); |
219 | if (write (fd, proxy_req, proxy_req_len) == 0) |
|
|
220 | { |
|
|
221 | error (); |
|
|
222 | return; |
|
|
223 | } |
|
|
224 | |
222 | free (proxy_req); proxy_req = 0; |
225 | free (proxy_req); proxy_req = 0; |
223 | } |
226 | } |
224 | #endif |
227 | #endif |
225 | } |
228 | } |
226 | else if (state == ESTABLISHED) |
229 | else if (state == ESTABLISHED) |
… | |
… | |
229 | { |
232 | { |
230 | if (write_packet ()) |
233 | if (write_packet ()) |
231 | { |
234 | { |
232 | delete w_pkt; w_pkt = 0; |
235 | delete w_pkt; w_pkt = 0; |
233 | |
236 | |
234 | set (POLLIN); |
237 | set (EV_READ); |
235 | } |
238 | } |
236 | } |
239 | } |
237 | else |
240 | else |
238 | set (POLLIN); |
241 | set (EV_READ); |
239 | } |
242 | } |
240 | else |
243 | else |
241 | set (POLLIN); |
244 | set (EV_READ); |
242 | } |
245 | } |
243 | |
246 | |
244 | if (revents & POLLIN) |
247 | if (revents & EV_READ) |
245 | { |
248 | { |
246 | if (state == ESTABLISHED) |
249 | if (state == ESTABLISHED) |
247 | for (;;) |
250 | for (;;) |
248 | { |
251 | { |
249 | if (!r_pkt) |
252 | if (!r_pkt) |
… | |
… | |
283 | break; |
286 | break; |
284 | } |
287 | } |
285 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
288 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
286 | break; |
289 | break; |
287 | |
290 | |
|
|
291 | // len == 0 <-> EOF |
288 | error (); |
292 | error (); |
289 | break; |
293 | break; |
290 | } |
294 | } |
291 | #if ENABLE_HTTP_PROXY |
295 | #if ENABLE_HTTP_PROXY |
292 | else if (state == CONNECTING_PROXY) |
296 | else if (state == CONNECTING_PROXY) |
… | |
… | |
342 | } |
346 | } |
343 | |
347 | |
344 | bool |
348 | bool |
345 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
349 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
346 | { |
350 | { |
347 | last_activity = NOW; |
351 | last_activity = ev::ev_now (); |
348 | |
352 | |
349 | if (state == IDLE) |
353 | if (state == IDLE) |
350 | { |
354 | { |
351 | // woaw, the first lost packet ;) |
355 | // woaw, the first lost packet ;) |
352 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
356 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
… | |
… | |
386 | fcntl (fd, F_SETFL, O_NONBLOCK); |
390 | fcntl (fd, F_SETFL, O_NONBLOCK); |
387 | |
391 | |
388 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
392 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
389 | || errno == EINPROGRESS) |
393 | || errno == EINPROGRESS) |
390 | { |
394 | { |
|
|
395 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
396 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
397 | |
391 | state = CONNECTING; |
398 | state = CONNECTING; |
392 | start (fd, POLLOUT); |
399 | start (fd, EV_WRITE); |
393 | } |
400 | } |
394 | else |
401 | else |
395 | close (fd); |
402 | close (fd); |
396 | } |
403 | } |
397 | } |
404 | } |
… | |
… | |
401 | // right thing to do, not using tcp *is* the right thing to do. |
408 | // right thing to do, not using tcp *is* the right thing to do. |
402 | if (!w_pkt) |
409 | if (!w_pkt) |
403 | { |
410 | { |
404 | // how this maps to the underlying tcp packets we don't know |
411 | // how this maps to the underlying tcp packets we don't know |
405 | // and we don't care. at least we tried ;) |
412 | // and we don't care. at least we tried ;) |
|
|
413 | #if defined(SOL_IP) && defined(IP_TOS) |
406 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
414 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
415 | #endif |
407 | |
416 | |
408 | w_pkt = pkt; |
417 | w_pkt = pkt; |
409 | w_ofs = 0; |
418 | w_ofs = 0; |
410 | w_len = pkt->len + 2; // length + size header |
419 | w_len = pkt->len + 2; // length + size header |
411 | |
420 | |
… | |
… | |
414 | else |
423 | else |
415 | { |
424 | { |
416 | w_pkt = new vpn_packet; |
425 | w_pkt = new vpn_packet; |
417 | w_pkt->set (*pkt); |
426 | w_pkt->set (*pkt); |
418 | |
427 | |
419 | set (POLLIN | POLLOUT); |
428 | set (EV_READ | EV_WRITE); |
420 | } |
429 | } |
421 | } |
430 | } |
422 | } |
431 | } |
423 | |
432 | |
424 | return state != ERROR; |
433 | return state != ERROR; |
425 | } |
434 | } |
426 | |
435 | |
427 | void tcp_connection::error () |
436 | void tcp_connection::error () |
428 | { |
437 | { |
|
|
438 | stop (); |
|
|
439 | |
429 | if (fd >= 0) |
440 | if (fd >= 0) |
430 | { |
441 | { |
431 | close (fd); |
442 | close (fd); |
432 | fd = -1; |
443 | fd = -1; |
433 | } |
444 | } |
… | |
… | |
436 | delete w_pkt; w_pkt = 0; |
447 | delete w_pkt; w_pkt = 0; |
437 | #if ENABLE_HTTP_PROXY |
448 | #if ENABLE_HTTP_PROXY |
438 | free (proxy_req); proxy_req = 0; |
449 | free (proxy_req); proxy_req = 0; |
439 | #endif |
450 | #endif |
440 | |
451 | |
441 | stop (); |
|
|
442 | state = active ? IDLE : ERROR; |
452 | state = active ? IDLE : ERROR; |
443 | } |
453 | } |
444 | |
454 | |
445 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
455 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
446 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
456 | : v(v_), si(si_), ev::io(this, &tcp_connection::tcpv4_ev) |
447 | { |
457 | { |
448 | last_activity = NOW; |
458 | last_activity = ev::ev_now (); |
449 | r_pkt = 0; |
459 | r_pkt = 0; |
450 | w_pkt = 0; |
460 | w_pkt = 0; |
451 | fd = fd_; |
461 | fd = fd_; |
452 | #if ENABLE_HTTP_PROXY |
462 | #if ENABLE_HTTP_PROXY |
453 | proxy_req = 0; |
463 | proxy_req = 0; |
… | |
… | |
460 | } |
470 | } |
461 | else |
471 | else |
462 | { |
472 | { |
463 | active = false; |
473 | active = false; |
464 | state = ESTABLISHED; |
474 | state = ESTABLISHED; |
465 | start (fd, POLLIN); |
475 | start (fd, EV_READ); |
466 | } |
476 | } |
467 | } |
477 | } |
468 | |
478 | |
469 | tcp_connection::~tcp_connection () |
479 | tcp_connection::~tcp_connection () |
470 | { |
480 | { |