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. |
… | |
… | |
100 | ~tcp_connection (); |
101 | ~tcp_connection (); |
101 | }; |
102 | }; |
102 | |
103 | |
103 | void tcp_si_map::cleaner_cb (time_watcher &w) |
104 | void tcp_si_map::cleaner_cb (time_watcher &w) |
104 | { |
105 | { |
105 | w.at = NOW + 600; |
106 | w.start (NOW + 600); |
|
|
107 | |
106 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
108 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
107 | |
109 | |
108 | for (iterator i = begin (); i != end(); ) |
110 | for (iterator i = begin (); i != end(); ) |
109 | if (i->second->last_activity >= to) |
111 | if (i->second->last_activity >= to) |
110 | ++i; |
112 | ++i; |
… | |
… | |
116 | } |
118 | } |
117 | |
119 | |
118 | void |
120 | void |
119 | vpn::tcpv4_ev (io_watcher &w, short revents) |
121 | vpn::tcpv4_ev (io_watcher &w, short revents) |
120 | { |
122 | { |
121 | if (revents & (POLLIN | POLLERR)) |
123 | if (revents & EVENT_READ) |
122 | { |
124 | { |
123 | struct sockaddr_in sa; |
125 | struct sockaddr_in sa; |
124 | socklen_t sa_len = sizeof (sa); |
126 | socklen_t sa_len = sizeof (sa); |
125 | int len; |
127 | int len; |
126 | |
128 | |
… | |
… | |
198 | void |
200 | void |
199 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
201 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
200 | { |
202 | { |
201 | last_activity = NOW; |
203 | last_activity = NOW; |
202 | |
204 | |
203 | if (revents & (POLLERR | POLLHUP)) |
205 | if (revents & EVENT_WRITE) |
204 | { |
|
|
205 | error (); |
|
|
206 | return; |
|
|
207 | } |
|
|
208 | |
|
|
209 | if (revents & POLLOUT) |
|
|
210 | { |
206 | { |
211 | if (state == CONNECTING) |
207 | if (state == CONNECTING) |
212 | { |
208 | { |
213 | state = ESTABLISHED; |
209 | state = ESTABLISHED; |
214 | set (POLLIN); |
210 | set (EVENT_READ); |
215 | #if ENABLE_HTTP_PROXY |
211 | #if ENABLE_HTTP_PROXY |
216 | if (::conf.proxy_host && ::conf.proxy_port) |
212 | if (::conf.proxy_host && ::conf.proxy_port) |
217 | { |
213 | { |
218 | state = CONNECTING_PROXY; |
214 | state = CONNECTING_PROXY; |
|
|
215 | |
219 | write (fd, proxy_req, proxy_req_len); |
216 | if (write (fd, proxy_req, proxy_req_len) == 0) |
|
|
217 | { |
|
|
218 | error (); |
|
|
219 | return; |
|
|
220 | } |
|
|
221 | |
220 | free (proxy_req); proxy_req = 0; |
222 | free (proxy_req); proxy_req = 0; |
221 | } |
223 | } |
222 | #endif |
224 | #endif |
223 | } |
225 | } |
224 | else if (state == ESTABLISHED) |
226 | else if (state == ESTABLISHED) |
… | |
… | |
227 | { |
229 | { |
228 | if (write_packet ()) |
230 | if (write_packet ()) |
229 | { |
231 | { |
230 | delete w_pkt; w_pkt = 0; |
232 | delete w_pkt; w_pkt = 0; |
231 | |
233 | |
232 | set (POLLIN); |
234 | set (EVENT_READ); |
233 | } |
235 | } |
234 | } |
236 | } |
235 | else |
237 | else |
236 | set (POLLIN); |
238 | set (EVENT_READ); |
237 | } |
239 | } |
238 | else |
240 | else |
239 | set (POLLIN); |
241 | set (EVENT_READ); |
240 | } |
242 | } |
241 | |
243 | |
242 | if (revents & POLLIN) |
244 | if (revents & EVENT_READ) |
243 | { |
245 | { |
244 | if (state == ESTABLISHED) |
246 | if (state == ESTABLISHED) |
245 | for (;;) |
247 | for (;;) |
246 | { |
248 | { |
247 | if (!r_pkt) |
249 | if (!r_pkt) |
… | |
… | |
281 | break; |
283 | break; |
282 | } |
284 | } |
283 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
285 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
284 | break; |
286 | break; |
285 | |
287 | |
|
|
288 | // len == 0 <-> EOF |
286 | error (); |
289 | error (); |
287 | break; |
290 | break; |
288 | } |
291 | } |
289 | #if ENABLE_HTTP_PROXY |
292 | #if ENABLE_HTTP_PROXY |
290 | else if (state == CONNECTING_PROXY) |
293 | else if (state == CONNECTING_PROXY) |
… | |
… | |
385 | |
388 | |
386 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
389 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
387 | || errno == EINPROGRESS) |
390 | || errno == EINPROGRESS) |
388 | { |
391 | { |
389 | state = CONNECTING; |
392 | state = CONNECTING; |
390 | start (fd, POLLOUT); |
393 | start (fd, EVENT_WRITE); |
391 | } |
394 | } |
392 | else |
395 | else |
393 | close (fd); |
396 | close (fd); |
394 | } |
397 | } |
395 | } |
398 | } |
… | |
… | |
414 | else |
417 | else |
415 | { |
418 | { |
416 | w_pkt = new vpn_packet; |
419 | w_pkt = new vpn_packet; |
417 | w_pkt->set (*pkt); |
420 | w_pkt->set (*pkt); |
418 | |
421 | |
419 | set (POLLIN | POLLOUT); |
422 | set (EVENT_READ | EVENT_WRITE); |
420 | } |
423 | } |
421 | } |
424 | } |
422 | } |
425 | } |
423 | |
426 | |
424 | return state != ERROR; |
427 | return state != ERROR; |
… | |
… | |
460 | } |
463 | } |
461 | else |
464 | else |
462 | { |
465 | { |
463 | active = false; |
466 | active = false; |
464 | state = ESTABLISHED; |
467 | state = ESTABLISHED; |
465 | start (fd, POLLIN); |
468 | start (fd, EVENT_READ); |
466 | } |
469 | } |
467 | } |
470 | } |
468 | |
471 | |
469 | tcp_connection::~tcp_connection () |
472 | tcp_connection::~tcp_connection () |
470 | { |
473 | { |