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-2004 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | 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 | 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 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
27 | |
28 | |
28 | #include <cstring> |
29 | #include <cstring> |
29 | |
30 | |
30 | #include <sys/types.h> |
31 | #include <sys/types.h> |
31 | #include <sys/socket.h> |
32 | #include <sys/socket.h> |
32 | #include <sys/poll.h> |
|
|
33 | #include <sys/wait.h> |
33 | #include <sys/wait.h> |
34 | #include <netinet/in.h> |
|
|
35 | #include <sys/uio.h> |
34 | #include <sys/uio.h> |
36 | #include <arpa/inet.h> |
|
|
37 | #include <errno.h> |
35 | #include <errno.h> |
38 | #include <time.h> |
36 | #include <time.h> |
39 | #include <unistd.h> |
37 | #include <unistd.h> |
|
|
38 | #include <fcntl.h> |
40 | |
39 | |
41 | #include <map> |
40 | #include <map> |
42 | #include <unistd.h> |
41 | |
43 | #include <fcntl.h> |
42 | #include "netcompat.h" |
44 | #include <sys/poll.h> |
|
|
45 | |
43 | |
46 | #include "vpn.h" |
44 | #include "vpn.h" |
47 | |
45 | |
48 | #if ENABLE_HTTP_PROXY |
46 | #if ENABLE_HTTP_PROXY |
49 | # include "conf.h" |
47 | # include "conf.h" |
… | |
… | |
103 | ~tcp_connection (); |
101 | ~tcp_connection (); |
104 | }; |
102 | }; |
105 | |
103 | |
106 | void tcp_si_map::cleaner_cb (time_watcher &w) |
104 | void tcp_si_map::cleaner_cb (time_watcher &w) |
107 | { |
105 | { |
108 | w.at = NOW + 600; |
106 | w.start (NOW + 600); |
|
|
107 | |
109 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
108 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
110 | |
109 | |
111 | for (iterator i = begin (); i != end(); ) |
110 | for (iterator i = begin (); i != end(); ) |
112 | if (i->second->last_activity >= to) |
111 | if (i->second->last_activity >= to) |
113 | ++i; |
112 | ++i; |
… | |
… | |
119 | } |
118 | } |
120 | |
119 | |
121 | void |
120 | void |
122 | vpn::tcpv4_ev (io_watcher &w, short revents) |
121 | vpn::tcpv4_ev (io_watcher &w, short revents) |
123 | { |
122 | { |
124 | if (revents & (POLLIN | POLLERR)) |
123 | if (revents & EVENT_READ) |
125 | { |
124 | { |
126 | struct sockaddr_in sa; |
125 | struct sockaddr_in sa; |
127 | socklen_t sa_len = sizeof (sa); |
126 | socklen_t sa_len = sizeof (sa); |
128 | int len; |
127 | int len; |
129 | |
128 | |
… | |
… | |
201 | void |
200 | void |
202 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
201 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
203 | { |
202 | { |
204 | last_activity = NOW; |
203 | last_activity = NOW; |
205 | |
204 | |
206 | if (revents & (POLLERR | POLLHUP)) |
205 | if (revents & EVENT_WRITE) |
207 | { |
|
|
208 | error (); |
|
|
209 | return; |
|
|
210 | } |
|
|
211 | |
|
|
212 | if (revents & POLLOUT) |
|
|
213 | { |
206 | { |
214 | if (state == CONNECTING) |
207 | if (state == CONNECTING) |
215 | { |
208 | { |
216 | state = ESTABLISHED; |
209 | state = ESTABLISHED; |
217 | set (POLLIN); |
210 | set (EVENT_READ); |
218 | #if ENABLE_HTTP_PROXY |
211 | #if ENABLE_HTTP_PROXY |
219 | if (::conf.proxy_host && ::conf.proxy_port) |
212 | if (::conf.proxy_host && ::conf.proxy_port) |
220 | { |
213 | { |
221 | state = CONNECTING_PROXY; |
214 | state = CONNECTING_PROXY; |
|
|
215 | |
222 | write (fd, proxy_req, proxy_req_len); |
216 | if (write (fd, proxy_req, proxy_req_len) == 0) |
|
|
217 | { |
|
|
218 | error (); |
|
|
219 | return; |
|
|
220 | } |
|
|
221 | |
223 | free (proxy_req); proxy_req = 0; |
222 | free (proxy_req); proxy_req = 0; |
224 | } |
223 | } |
225 | #endif |
224 | #endif |
226 | } |
225 | } |
227 | else if (state == ESTABLISHED) |
226 | else if (state == ESTABLISHED) |
… | |
… | |
230 | { |
229 | { |
231 | if (write_packet ()) |
230 | if (write_packet ()) |
232 | { |
231 | { |
233 | delete w_pkt; w_pkt = 0; |
232 | delete w_pkt; w_pkt = 0; |
234 | |
233 | |
235 | set (POLLIN); |
234 | set (EVENT_READ); |
236 | } |
235 | } |
237 | } |
236 | } |
238 | else |
237 | else |
239 | set (POLLIN); |
238 | set (EVENT_READ); |
240 | } |
239 | } |
241 | else |
240 | else |
242 | set (POLLIN); |
241 | set (EVENT_READ); |
243 | } |
242 | } |
244 | |
243 | |
245 | if (revents & POLLIN) |
244 | if (revents & EVENT_READ) |
246 | { |
245 | { |
247 | if (state == ESTABLISHED) |
246 | if (state == ESTABLISHED) |
248 | for (;;) |
247 | for (;;) |
249 | { |
248 | { |
250 | if (!r_pkt) |
249 | if (!r_pkt) |
… | |
… | |
284 | break; |
283 | break; |
285 | } |
284 | } |
286 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
285 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
287 | break; |
286 | break; |
288 | |
287 | |
|
|
288 | // len == 0 <-> EOF |
289 | error (); |
289 | error (); |
290 | break; |
290 | break; |
291 | } |
291 | } |
292 | #if ENABLE_HTTP_PROXY |
292 | #if ENABLE_HTTP_PROXY |
293 | else if (state == CONNECTING_PROXY) |
293 | else if (state == CONNECTING_PROXY) |
… | |
… | |
388 | |
388 | |
389 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
389 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
390 | || errno == EINPROGRESS) |
390 | || errno == EINPROGRESS) |
391 | { |
391 | { |
392 | state = CONNECTING; |
392 | state = CONNECTING; |
393 | start (fd, POLLOUT); |
393 | start (fd, EVENT_WRITE); |
394 | } |
394 | } |
395 | else |
395 | else |
396 | close (fd); |
396 | close (fd); |
397 | } |
397 | } |
398 | } |
398 | } |
… | |
… | |
417 | else |
417 | else |
418 | { |
418 | { |
419 | w_pkt = new vpn_packet; |
419 | w_pkt = new vpn_packet; |
420 | w_pkt->set (*pkt); |
420 | w_pkt->set (*pkt); |
421 | |
421 | |
422 | set (POLLIN | POLLOUT); |
422 | set (EVENT_READ | EVENT_WRITE); |
423 | } |
423 | } |
424 | } |
424 | } |
425 | } |
425 | } |
426 | |
426 | |
427 | return state != ERROR; |
427 | return state != ERROR; |
… | |
… | |
463 | } |
463 | } |
464 | else |
464 | else |
465 | { |
465 | { |
466 | active = false; |
466 | active = false; |
467 | state = ESTABLISHED; |
467 | state = ESTABLISHED; |
468 | start (fd, POLLIN); |
468 | start (fd, EVENT_READ); |
469 | } |
469 | } |
470 | } |
470 | } |
471 | |
471 | |
472 | tcp_connection::~tcp_connection () |
472 | tcp_connection::~tcp_connection () |
473 | { |
473 | { |