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 | |
|
|
48 | #if ENABLE_HTTP_PROXY |
|
|
49 | # include "conf.h" |
|
|
50 | #endif |
47 | |
51 | |
48 | struct tcp_connection; |
52 | struct tcp_connection; |
49 | |
53 | |
50 | struct lt_sockinfo |
54 | struct lt_sockinfo |
51 | { |
55 | { |
… | |
… | |
53 | { |
57 | { |
54 | return *a < *b; |
58 | return *a < *b; |
55 | } |
59 | } |
56 | }; |
60 | }; |
57 | |
61 | |
58 | 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> |
59 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
63 | { |
|
|
64 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
60 | |
65 | |
61 | tcp_si_map () |
66 | tcp_si_map () |
62 | : cleaner(this, &tcp_si_map::cleaner_cb) |
|
|
63 | { |
67 | { |
64 | cleaner.start (0); |
68 | cleaner.set<tcp_si_map, &tcp_si_map::cleaner_cb> (this); |
|
|
69 | cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
65 | } |
70 | } |
|
|
71 | |
66 | } tcp_si; |
72 | } tcp_si; |
67 | |
73 | |
68 | struct tcp_connection : io_watcher { |
74 | struct tcp_connection : ev::io |
|
|
75 | { |
|
|
76 | int tos; |
69 | tstamp last_activity; |
77 | tstamp last_activity; |
70 | const sockinfo si; |
78 | const sockinfo si; |
71 | vpn &v; |
79 | vpn &v; |
72 | bool active; // this connection has been actively established |
80 | bool active; // this connection has been actively established |
73 | enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; |
81 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
74 | |
82 | |
75 | vpn_packet *r_pkt; |
83 | vpn_packet *r_pkt; |
76 | u32 r_len, r_ofs; |
84 | u32 r_len, r_ofs; |
77 | |
85 | |
|
|
86 | vpn_packet *w_pkt; |
|
|
87 | u32 w_len, w_ofs; |
|
|
88 | |
|
|
89 | #if ENABLE_HTTP_PROXY |
|
|
90 | char *proxy_req; |
|
|
91 | int proxy_req_len; |
|
|
92 | #endif |
|
|
93 | |
78 | void tcpv4_ev (io_watcher &w, short revents); |
94 | void tcpv4_ev (ev::io &w, int revents); |
79 | |
95 | |
80 | void send_packet (vpn_packet *pkt, int tos); |
96 | bool send_packet (vpn_packet *pkt, int tos); |
|
|
97 | bool write_packet (); |
81 | |
98 | |
82 | void error (); // abort conenction && cleanup |
99 | void error (); // abort conenction && cleanup |
83 | |
100 | |
84 | operator tcp_si_map::value_type() |
101 | operator tcp_si_map::value_type() |
85 | { |
102 | { |
86 | return tcp_si_map::value_type (&si, this); |
103 | return tcp_si_map::value_type (&si, this); |
87 | } |
104 | } |
88 | |
105 | |
89 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
106 | tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
90 | ~tcp_connection (); |
107 | ~tcp_connection (); |
91 | }; |
108 | }; |
92 | |
109 | |
93 | void tcp_si_map::cleaner_cb (time_watcher &w) |
110 | void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
94 | { |
111 | { |
95 | w.at = NOW + 600; |
|
|
96 | tstamp to = NOW - ::conf.keepalive - 30 - 60; |
112 | tstamp to = ev_now () - ::conf.keepalive - 30 - 60; |
97 | |
113 | |
98 | for (iterator i = begin (); i != end(); ) |
114 | for (iterator i = begin (); i != end(); ) |
99 | if (i->second->last_activity >= to) |
115 | if (i->second->last_activity >= to) |
100 | ++i; |
116 | ++i; |
101 | else |
117 | else |
102 | { |
118 | { |
|
|
119 | delete i->second; |
103 | erase (i); |
120 | erase (i); |
104 | i = begin (); |
121 | i = begin (); |
105 | } |
122 | } |
106 | } |
123 | } |
107 | |
124 | |
108 | void |
125 | void |
109 | vpn::tcpv4_ev (io_watcher &w, short revents) |
126 | vpn::tcpv4_ev (ev::io &w, int revents) |
110 | { |
127 | { |
111 | if (revents & (POLLIN | POLLERR)) |
128 | if (revents & EV_READ) |
112 | { |
129 | { |
113 | struct sockaddr_in sa; |
130 | struct sockaddr_in sa; |
114 | socklen_t sa_len = sizeof (sa); |
131 | socklen_t sa_len = sizeof (sa); |
115 | int len; |
132 | int len; |
116 | |
133 | |
117 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
134 | int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
118 | |
135 | |
119 | if (fd >= 0) |
136 | if (fd >= 0) |
120 | { |
137 | { |
|
|
138 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
139 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
140 | |
121 | sockinfo si(sa, PROT_TCPv4); |
141 | sockinfo si(sa, PROT_TCPv4); |
122 | |
142 | |
123 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
143 | slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
124 | |
|
|
125 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
126 | |
144 | |
127 | tcp_connection *i = new tcp_connection (fd, si, *this); |
145 | tcp_connection *i = new tcp_connection (fd, si, *this); |
128 | tcp_si.insert (*i); |
146 | tcp_si.insert (*i); |
129 | } |
147 | } |
130 | } |
148 | } |
131 | } |
149 | } |
132 | |
150 | |
133 | void |
151 | bool |
134 | vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
152 | vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
135 | { |
153 | { |
136 | tcp_si_map::iterator info = tcp_si.find (&si); |
154 | tcp_si_map::iterator info = tcp_si.find (&si); |
137 | |
155 | |
138 | tcp_connection *i; |
156 | tcp_connection *i; |
… | |
… | |
143 | tcp_si.insert (*i); |
161 | tcp_si.insert (*i); |
144 | } |
162 | } |
145 | else |
163 | else |
146 | i = info->second; |
164 | i = info->second; |
147 | |
165 | |
148 | i->send_packet (pkt, tos); |
166 | return i->send_packet (pkt, tos); |
149 | } |
167 | } |
150 | |
168 | |
151 | void tcp_connection::error () |
169 | bool |
|
|
170 | tcp_connection::write_packet () |
152 | { |
171 | { |
153 | if (fd >= 0) |
172 | ssize_t len; |
154 | { |
173 | |
155 | close (fd); |
174 | if (w_ofs < 2) |
156 | fd = -1; |
|
|
157 | } |
175 | { |
|
|
176 | u16 plen = htons (w_pkt->len); |
158 | |
177 | |
159 | delete r_pkt; |
178 | iovec vec[2]; |
160 | r_pkt = 0; |
179 | //TODO: char* is the right type? hardly... |
|
|
180 | vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs; |
|
|
181 | vec[0].iov_len = 2 - w_ofs; |
|
|
182 | vec[1].iov_base = (char *)&((*w_pkt)[0]); |
|
|
183 | vec[1].iov_len = w_len - 2; |
161 | |
184 | |
162 | stop (); |
185 | len = writev (fd, vec, 2); |
163 | state = active ? IDLE : ERROR; |
186 | } |
|
|
187 | else |
|
|
188 | len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len); |
|
|
189 | |
|
|
190 | if (len > 0) |
|
|
191 | { |
|
|
192 | w_ofs += len; |
|
|
193 | w_len -= len; |
|
|
194 | |
|
|
195 | return w_len == 0; |
|
|
196 | } |
|
|
197 | else if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
|
|
198 | return false; |
|
|
199 | else |
|
|
200 | { |
|
|
201 | error (); |
|
|
202 | return false; |
|
|
203 | } |
164 | } |
204 | } |
165 | |
205 | |
166 | void |
206 | void |
167 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
207 | tcp_connection::tcpv4_ev (ev::io &w, int revents) |
168 | { |
208 | { |
169 | last_activity = NOW; |
209 | last_activity = ev_now (); |
170 | |
210 | |
171 | if (revents & (POLLERR | POLLHUP)) |
211 | if (revents & EV_WRITE) |
172 | error (); |
|
|
173 | else if (revents & POLLOUT && state == CONNECTING) |
|
|
174 | { |
212 | { |
|
|
213 | if (state == CONNECTING) |
|
|
214 | { |
175 | state = ESTABLISHED; |
215 | state = ESTABLISHED; |
176 | set (POLLIN); |
216 | set (EV_READ); |
177 | } |
217 | #if ENABLE_HTTP_PROXY |
178 | |
218 | if (::conf.proxy_host && ::conf.proxy_port) |
179 | else if (revents & POLLIN) |
|
|
180 | { |
|
|
181 | for (;;) |
|
|
182 | { |
|
|
183 | if (!r_pkt) |
|
|
184 | { |
|
|
185 | r_pkt = new vpn_packet; |
|
|
186 | r_ofs = 0; |
|
|
187 | r_len = 2; // header |
|
|
188 | } |
219 | { |
|
|
220 | state = CONNECTING_PROXY; |
189 | |
221 | |
190 | ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
222 | if (write (fd, proxy_req, proxy_req_len) == 0) |
191 | |
|
|
192 | if (len > 0) |
|
|
193 | { |
|
|
194 | r_len -= len; |
|
|
195 | r_ofs += len; |
|
|
196 | |
|
|
197 | if (r_len == 0) |
|
|
198 | { |
223 | { |
|
|
224 | error (); |
|
|
225 | return; |
|
|
226 | } |
|
|
227 | |
|
|
228 | free (proxy_req); proxy_req = 0; |
|
|
229 | } |
|
|
230 | #endif |
|
|
231 | } |
|
|
232 | else if (state == ESTABLISHED) |
|
|
233 | { |
|
|
234 | if (w_pkt) |
|
|
235 | { |
|
|
236 | if (write_packet ()) |
|
|
237 | { |
|
|
238 | delete w_pkt; w_pkt = 0; |
|
|
239 | |
|
|
240 | set (EV_READ); |
|
|
241 | } |
|
|
242 | } |
|
|
243 | else |
|
|
244 | set (EV_READ); |
|
|
245 | } |
|
|
246 | else |
|
|
247 | set (EV_READ); |
|
|
248 | } |
|
|
249 | |
|
|
250 | if (revents & EV_READ) |
|
|
251 | { |
|
|
252 | if (state == ESTABLISHED) |
|
|
253 | for (;;) |
|
|
254 | { |
|
|
255 | if (!r_pkt) |
|
|
256 | { |
|
|
257 | r_pkt = new vpn_packet; |
|
|
258 | r_ofs = 0; |
|
|
259 | r_len = 2; // header |
|
|
260 | } |
|
|
261 | |
|
|
262 | ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
|
|
263 | |
|
|
264 | if (len > 0) |
|
|
265 | { |
|
|
266 | r_len -= len; |
|
|
267 | r_ofs += len; |
|
|
268 | |
|
|
269 | if (r_len == 0) |
|
|
270 | { |
199 | if (r_ofs == 2) |
271 | if (r_ofs == 2) |
200 | { |
272 | { |
201 | r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
273 | r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
202 | r_pkt->len = r_len; |
274 | r_pkt->len = r_len; |
203 | |
275 | |
204 | if (r_len > 0 && r_len < MAXSIZE) |
276 | if (r_len > 0 && r_len < MAXSIZE) |
|
|
277 | continue; |
|
|
278 | } |
|
|
279 | else |
|
|
280 | { |
|
|
281 | v.recv_vpn_packet (r_pkt, si); |
|
|
282 | delete r_pkt; |
|
|
283 | r_pkt = 0; |
|
|
284 | |
205 | continue; |
285 | continue; |
206 | } |
286 | } |
|
|
287 | } |
|
|
288 | else |
|
|
289 | break; |
|
|
290 | } |
|
|
291 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
|
|
292 | break; |
|
|
293 | |
|
|
294 | // len == 0 <-> EOF |
|
|
295 | error (); |
|
|
296 | break; |
|
|
297 | } |
|
|
298 | #if ENABLE_HTTP_PROXY |
|
|
299 | else if (state == CONNECTING_PROXY) |
|
|
300 | { |
|
|
301 | fcntl (fd, F_SETFL, 0); |
|
|
302 | char r[1024]; |
|
|
303 | int i; |
|
|
304 | bool emptyline = false; |
|
|
305 | |
|
|
306 | // we do a blocking read of the response, to hell with it |
|
|
307 | for (i = 0; i < 1023; i++) |
|
|
308 | { |
|
|
309 | int l = read (fd, &r[i], 1); |
|
|
310 | |
|
|
311 | if (l <= 0) |
|
|
312 | { |
|
|
313 | error (); |
|
|
314 | return; |
|
|
315 | } |
|
|
316 | |
|
|
317 | if (r[i] == '\012') |
|
|
318 | { |
|
|
319 | if (emptyline) |
|
|
320 | break; |
207 | else |
321 | else |
208 | { |
|
|
209 | v.recv_vpn_packet (r_pkt, si); |
|
|
210 | delete r_pkt; |
|
|
211 | r_pkt = 0; |
|
|
212 | |
|
|
213 | continue; |
322 | emptyline = true; |
214 | } |
|
|
215 | } |
323 | } |
|
|
324 | else if (r[i] != '\015') |
|
|
325 | emptyline = false; |
216 | |
326 | } |
|
|
327 | |
|
|
328 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
329 | |
|
|
330 | if (i < 12) |
217 | } |
331 | { |
218 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
332 | slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"), |
219 | return; |
333 | (const char *)si); |
220 | |
|
|
221 | error (); |
334 | error (); |
222 | return; |
|
|
223 | } |
335 | } |
|
|
336 | else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/' |
|
|
337 | || r[5] != '1' // http-major |
|
|
338 | || r[9] != '2') // response |
|
|
339 | { |
|
|
340 | slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"), |
|
|
341 | (const char *)si, r); |
|
|
342 | error (); |
|
|
343 | } |
|
|
344 | else |
|
|
345 | state = ESTABLISHED; |
|
|
346 | } |
|
|
347 | #endif |
224 | } |
348 | } |
225 | } |
349 | } |
226 | |
350 | |
227 | void |
351 | bool |
228 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
352 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
229 | { |
353 | { |
230 | last_activity = NOW; |
354 | last_activity = ev_now (); |
231 | |
355 | |
232 | if (state == IDLE) |
356 | if (state == IDLE) |
233 | { |
357 | { |
234 | // woaw, the first lost packet ;) |
358 | // woaw, the first lost packet ;) |
235 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
359 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
236 | |
360 | |
237 | if (fd >= 0) |
361 | if (fd >= 0) |
238 | { |
362 | { |
|
|
363 | const sockinfo *csi = &si; |
|
|
364 | |
|
|
365 | #if ENABLE_HTTP_PROXY |
|
|
366 | sockinfo psi; |
|
|
367 | |
|
|
368 | if (::conf.proxy_host && ::conf.proxy_port) |
|
|
369 | { |
|
|
370 | psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4); |
|
|
371 | |
|
|
372 | if (psi.valid ()) |
|
|
373 | { |
|
|
374 | csi = ψ |
|
|
375 | |
|
|
376 | proxy_req_len = asprintf (&proxy_req, |
|
|
377 | "CONNECT %s:%d HTTP/1.0\015\012" |
|
|
378 | "%s%s%s" // optional proxy-auth |
|
|
379 | "\015\012", |
|
|
380 | si.ntoa (), |
|
|
381 | ntohs (si.port), |
|
|
382 | ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "", |
|
|
383 | ::conf.proxy_auth ? ::conf.proxy_auth : "", |
|
|
384 | ::conf.proxy_auth ? "\015\012" : ""); |
|
|
385 | |
|
|
386 | } |
|
|
387 | else |
|
|
388 | slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"), |
|
|
389 | ::conf.proxy_host); |
|
|
390 | } |
|
|
391 | #endif |
|
|
392 | |
239 | fcntl (fd, F_SETFL, O_NONBLOCK); |
393 | fcntl (fd, F_SETFL, O_NONBLOCK); |
240 | |
394 | |
241 | if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 |
395 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
242 | || errno == EINPROGRESS) |
396 | || errno == EINPROGRESS) |
243 | { |
397 | { |
|
|
398 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
399 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
400 | |
244 | state = CONNECTING; |
401 | state = CONNECTING; |
245 | start (fd, POLLOUT); |
402 | start (fd, EV_WRITE); |
246 | } |
403 | } |
247 | else |
404 | else |
248 | close (fd); |
405 | close (fd); |
249 | } |
406 | } |
250 | } |
407 | } |
251 | else if (state == ESTABLISHED) |
408 | else if (state == ESTABLISHED) |
252 | { |
409 | { |
|
|
410 | // drop packet if the tcp write buffer is full. this *is* the |
|
|
411 | // right thing to do, not using tcp *is* the right thing to do. |
|
|
412 | if (!w_pkt) |
|
|
413 | { |
253 | // how this maps to the underlying tcp packet we don't know |
414 | // how this maps to the underlying tcp packets we don't know |
254 | // and we don't care. at least we tried ;) |
415 | // and we don't care. at least we tried ;) |
|
|
416 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
417 | if (tos != this->tos) |
|
|
418 | { |
|
|
419 | this->tos = tos; |
255 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
420 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
421 | } |
|
|
422 | #endif |
256 | |
423 | |
257 | // we use none of the advantages of tcp; if an error occurs, just drop |
424 | w_pkt = pkt; |
258 | // (this happens when a tcp connection gets stuck, too, which might not be |
425 | w_ofs = 0; |
259 | // the wisest thing to do.. either drop packet (too late) or make sure |
426 | w_len = pkt->len + 2; // length + size header |
260 | // it gets delivered) |
|
|
261 | u16 len = htons (pkt->len); |
|
|
262 | |
427 | |
263 | iovec vec[2]; |
428 | if (write_packet ()) |
264 | vec[0].iov_base = &len; |
429 | w_pkt = 0; |
265 | vec[0].iov_len = sizeof len; |
430 | else |
266 | vec[1].iov_base = &((*pkt)[0]); |
431 | { |
267 | vec[1].iov_len = pkt->len; |
432 | w_pkt = new vpn_packet; |
|
|
433 | w_pkt->set (*pkt); |
|
|
434 | |
|
|
435 | set (EV_READ | EV_WRITE); |
|
|
436 | } |
268 | |
437 | } |
269 | if (sizeof (u16) + pkt->len != writev (fd, vec, 2)) |
438 | } |
270 | error (); |
439 | |
|
|
440 | return state != ERROR; |
|
|
441 | } |
|
|
442 | |
|
|
443 | void tcp_connection::error () |
|
|
444 | { |
|
|
445 | stop (); |
|
|
446 | |
|
|
447 | if (fd >= 0) |
271 | } |
448 | { |
|
|
449 | close (fd); |
|
|
450 | tos = -1; |
|
|
451 | fd = -1; |
|
|
452 | } |
|
|
453 | |
|
|
454 | delete r_pkt; r_pkt = 0; |
|
|
455 | delete w_pkt; w_pkt = 0; |
|
|
456 | #if ENABLE_HTTP_PROXY |
|
|
457 | free (proxy_req); proxy_req = 0; |
|
|
458 | #endif |
|
|
459 | |
|
|
460 | state = active ? IDLE : ERROR; |
272 | } |
461 | } |
273 | |
462 | |
274 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
463 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
275 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
464 | : v(v_), si(si_) |
276 | { |
465 | { |
|
|
466 | set<tcp_connection, &tcp_connection::tcpv4_ev> (this); |
|
|
467 | |
277 | last_activity = NOW; |
468 | last_activity = ev_now (); |
278 | r_pkt = 0; |
469 | r_pkt = 0; |
|
|
470 | w_pkt = 0; |
|
|
471 | tos = -1; |
279 | fd = fd_; |
472 | fd = fd_; |
|
|
473 | #if ENABLE_HTTP_PROXY |
|
|
474 | proxy_req = 0; |
|
|
475 | #endif |
280 | |
476 | |
281 | if (fd < 0) |
477 | if (fd < 0) |
282 | { |
478 | { |
283 | active = true; |
479 | active = true; |
284 | state = IDLE; |
480 | state = IDLE; |
285 | } |
481 | } |
286 | else |
482 | else |
287 | { |
483 | { |
288 | active = false; |
484 | active = false; |
289 | state = ESTABLISHED; |
485 | state = ESTABLISHED; |
290 | start (fd, POLLIN); |
486 | start (fd, EV_READ); |
291 | } |
487 | } |
292 | } |
488 | } |
293 | |
489 | |
294 | tcp_connection::~tcp_connection () |
490 | tcp_connection::~tcp_connection () |
295 | { |
491 | { |