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