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 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" |
|
|
45 | |
|
|
46 | #if ENABLE_HTTP_PROXY |
|
|
47 | # include "conf.h" |
|
|
48 | #endif |
47 | |
49 | |
48 | struct tcp_connection; |
50 | struct tcp_connection; |
49 | |
51 | |
50 | struct lt_sockinfo |
52 | struct lt_sockinfo |
51 | { |
53 | { |
… | |
… | |
68 | struct tcp_connection : io_watcher { |
70 | struct tcp_connection : io_watcher { |
69 | tstamp last_activity; |
71 | tstamp last_activity; |
70 | const sockinfo si; |
72 | const sockinfo si; |
71 | vpn &v; |
73 | vpn &v; |
72 | bool active; // this connection has been actively established |
74 | bool active; // this connection has been actively established |
73 | enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; |
75 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
74 | |
76 | |
75 | vpn_packet *r_pkt; |
77 | vpn_packet *r_pkt; |
76 | u32 r_len, r_ofs; |
78 | u32 r_len, r_ofs; |
77 | |
79 | |
78 | vpn_packet *w_pkt; |
80 | vpn_packet *w_pkt; |
79 | u32 w_len, w_ofs; |
81 | u32 w_len, w_ofs; |
|
|
82 | |
|
|
83 | #if ENABLE_HTTP_PROXY |
|
|
84 | char *proxy_req; |
|
|
85 | int proxy_req_len; |
|
|
86 | #endif |
80 | |
87 | |
81 | void tcpv4_ev (io_watcher &w, short revents); |
88 | void tcpv4_ev (io_watcher &w, short revents); |
82 | |
89 | |
83 | bool send_packet (vpn_packet *pkt, int tos); |
90 | bool send_packet (vpn_packet *pkt, int tos); |
84 | bool write_packet (); |
91 | bool write_packet (); |
… | |
… | |
150 | i = info->second; |
157 | i = info->second; |
151 | |
158 | |
152 | return i->send_packet (pkt, tos); |
159 | return i->send_packet (pkt, tos); |
153 | } |
160 | } |
154 | |
161 | |
155 | void tcp_connection::error () |
|
|
156 | { |
|
|
157 | if (fd >= 0) |
|
|
158 | { |
|
|
159 | close (fd); |
|
|
160 | fd = -1; |
|
|
161 | } |
|
|
162 | |
|
|
163 | delete r_pkt; r_pkt = 0; |
|
|
164 | delete w_pkt; w_pkt = 0; |
|
|
165 | |
|
|
166 | stop (); |
|
|
167 | state = active ? IDLE : ERROR; |
|
|
168 | } |
|
|
169 | |
|
|
170 | bool |
162 | bool |
171 | tcp_connection::write_packet () |
163 | tcp_connection::write_packet () |
172 | { |
164 | { |
173 | ssize_t len; |
165 | ssize_t len; |
174 | |
166 | |
175 | if (w_ofs < 2) |
167 | if (w_ofs < 2) |
176 | { |
168 | { |
177 | u16 plen = htons (w_pkt->len); |
169 | u16 plen = htons (w_pkt->len); |
178 | |
170 | |
179 | iovec vec[2]; |
171 | iovec vec[2]; |
|
|
172 | //TODO: char* is the right type? hardly... |
180 | vec[0].iov_base = ((u8 *)&plen) + w_ofs; |
173 | vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs; |
181 | vec[0].iov_len = 2 - w_ofs; |
174 | vec[0].iov_len = 2 - w_ofs; |
182 | vec[1].iov_base = &((*w_pkt)[0]); |
175 | vec[1].iov_base = (char *)&((*w_pkt)[0]); |
183 | vec[1].iov_len = w_len - 2; |
176 | vec[1].iov_len = w_len - 2; |
184 | |
177 | |
185 | len = writev (fd, vec, 2); |
178 | len = writev (fd, vec, 2); |
186 | } |
179 | } |
187 | else |
180 | else |
… | |
… | |
218 | { |
211 | { |
219 | if (state == CONNECTING) |
212 | if (state == CONNECTING) |
220 | { |
213 | { |
221 | state = ESTABLISHED; |
214 | state = ESTABLISHED; |
222 | set (POLLIN); |
215 | set (POLLIN); |
|
|
216 | #if ENABLE_HTTP_PROXY |
|
|
217 | if (::conf.proxy_host && ::conf.proxy_port) |
|
|
218 | { |
|
|
219 | state = CONNECTING_PROXY; |
|
|
220 | write (fd, proxy_req, proxy_req_len); |
|
|
221 | free (proxy_req); proxy_req = 0; |
|
|
222 | } |
|
|
223 | #endif |
223 | } |
224 | } |
224 | else if (state == ESTABLISHED) |
225 | else if (state == ESTABLISHED) |
225 | { |
226 | { |
226 | if (w_pkt) |
227 | if (w_pkt) |
227 | { |
228 | { |
… | |
… | |
239 | set (POLLIN); |
240 | set (POLLIN); |
240 | } |
241 | } |
241 | |
242 | |
242 | if (revents & POLLIN) |
243 | if (revents & POLLIN) |
243 | { |
244 | { |
|
|
245 | if (state == ESTABLISHED) |
244 | for (;;) |
246 | for (;;) |
245 | { |
247 | { |
246 | if (!r_pkt) |
248 | if (!r_pkt) |
247 | { |
249 | { |
248 | r_pkt = new vpn_packet; |
250 | r_pkt = new vpn_packet; |
249 | r_ofs = 0; |
251 | r_ofs = 0; |
250 | r_len = 2; // header |
252 | r_len = 2; // header |
251 | } |
253 | } |
252 | |
254 | |
253 | ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
255 | ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
254 | |
256 | |
255 | if (len > 0) |
257 | if (len > 0) |
256 | { |
258 | { |
257 | r_len -= len; |
259 | r_len -= len; |
258 | r_ofs += len; |
260 | r_ofs += len; |
259 | |
261 | |
260 | if (r_len == 0) |
262 | if (r_len == 0) |
|
|
263 | { |
|
|
264 | if (r_ofs == 2) |
|
|
265 | { |
|
|
266 | r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
|
|
267 | r_pkt->len = r_len; |
|
|
268 | |
|
|
269 | if (r_len > 0 && r_len < MAXSIZE) |
|
|
270 | continue; |
|
|
271 | } |
|
|
272 | else |
|
|
273 | { |
|
|
274 | v.recv_vpn_packet (r_pkt, si); |
|
|
275 | delete r_pkt; |
|
|
276 | r_pkt = 0; |
|
|
277 | |
|
|
278 | continue; |
|
|
279 | } |
|
|
280 | } |
|
|
281 | else |
|
|
282 | break; |
|
|
283 | } |
|
|
284 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
|
|
285 | break; |
|
|
286 | |
|
|
287 | error (); |
|
|
288 | break; |
|
|
289 | } |
|
|
290 | #if ENABLE_HTTP_PROXY |
|
|
291 | else if (state == CONNECTING_PROXY) |
|
|
292 | { |
|
|
293 | fcntl (fd, F_SETFL, 0); |
|
|
294 | char r[1024]; |
|
|
295 | int i; |
|
|
296 | bool emptyline = false; |
|
|
297 | |
|
|
298 | // we do a blocking read of the response, to hell with it |
|
|
299 | for (i = 0; i < 1023; i++) |
|
|
300 | { |
|
|
301 | int l = read (fd, &r[i], 1); |
|
|
302 | |
|
|
303 | if (l <= 0) |
261 | { |
304 | { |
262 | if (r_ofs == 2) |
305 | error (); |
263 | { |
306 | return; |
264 | r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
|
|
265 | r_pkt->len = r_len; |
|
|
266 | |
|
|
267 | if (r_len > 0 && r_len < MAXSIZE) |
|
|
268 | continue; |
|
|
269 | } |
307 | } |
|
|
308 | |
|
|
309 | if (r[i] == '\012') |
|
|
310 | { |
|
|
311 | if (emptyline) |
|
|
312 | break; |
270 | else |
313 | else |
271 | { |
|
|
272 | v.recv_vpn_packet (r_pkt, si); |
|
|
273 | delete r_pkt; |
|
|
274 | r_pkt = 0; |
|
|
275 | |
|
|
276 | continue; |
314 | emptyline = true; |
277 | } |
315 | } |
|
|
316 | else if (r[i] != '\015') |
|
|
317 | emptyline = false; |
|
|
318 | } |
|
|
319 | |
|
|
320 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
321 | |
|
|
322 | if (i < 12) |
|
|
323 | { |
|
|
324 | slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"), |
|
|
325 | (const char *)si); |
|
|
326 | error (); |
|
|
327 | } |
|
|
328 | else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/' |
|
|
329 | || r[5] != '1' // http-major |
|
|
330 | || r[9] != '2') // response |
|
|
331 | { |
|
|
332 | slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"), |
|
|
333 | (const char *)si, r); |
|
|
334 | error (); |
|
|
335 | } |
|
|
336 | else |
|
|
337 | state = ESTABLISHED; |
|
|
338 | } |
|
|
339 | #endif |
|
|
340 | } |
|
|
341 | } |
|
|
342 | |
|
|
343 | bool |
|
|
344 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
|
|
345 | { |
|
|
346 | last_activity = NOW; |
|
|
347 | |
|
|
348 | if (state == IDLE) |
|
|
349 | { |
|
|
350 | // woaw, the first lost packet ;) |
|
|
351 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
|
352 | |
|
|
353 | if (fd >= 0) |
|
|
354 | { |
|
|
355 | const sockinfo *csi = &si; |
|
|
356 | |
|
|
357 | #if ENABLE_HTTP_PROXY |
|
|
358 | sockinfo psi; |
|
|
359 | |
|
|
360 | if (::conf.proxy_host && ::conf.proxy_port) |
|
|
361 | { |
|
|
362 | psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4); |
|
|
363 | |
|
|
364 | if (psi.valid ()) |
|
|
365 | { |
|
|
366 | csi = ψ |
|
|
367 | |
|
|
368 | proxy_req_len = asprintf (&proxy_req, |
|
|
369 | "CONNECT %s:%d HTTP/1.0\015\012" |
|
|
370 | "%s%s%s" // optional proxy-auth |
|
|
371 | "\015\012", |
|
|
372 | si.ntoa (), |
|
|
373 | ntohs (si.port), |
|
|
374 | ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "", |
|
|
375 | ::conf.proxy_auth ? ::conf.proxy_auth : "", |
|
|
376 | ::conf.proxy_auth ? "\015\012" : ""); |
|
|
377 | |
278 | } |
378 | } |
279 | else |
379 | else |
280 | break; |
380 | slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"), |
|
|
381 | ::conf.proxy_host); |
|
|
382 | } |
|
|
383 | #endif |
281 | } |
384 | |
282 | else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
|
|
283 | break; |
|
|
284 | |
|
|
285 | error (); |
|
|
286 | break; |
|
|
287 | } |
|
|
288 | } |
|
|
289 | } |
|
|
290 | |
|
|
291 | bool |
|
|
292 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
|
|
293 | { |
|
|
294 | last_activity = NOW; |
|
|
295 | |
|
|
296 | if (state == IDLE) |
|
|
297 | { |
|
|
298 | // woaw, the first lost packet ;) |
|
|
299 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
|
300 | |
|
|
301 | if (fd >= 0) |
|
|
302 | { |
|
|
303 | fcntl (fd, F_SETFL, O_NONBLOCK); |
385 | fcntl (fd, F_SETFL, O_NONBLOCK); |
304 | |
386 | |
305 | if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 |
387 | if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
306 | || errno == EINPROGRESS) |
388 | || errno == EINPROGRESS) |
307 | { |
389 | { |
308 | state = CONNECTING; |
390 | state = CONNECTING; |
309 | start (fd, POLLOUT); |
391 | start (fd, POLLOUT); |
310 | } |
392 | } |
… | |
… | |
318 | // right thing to do, not using tcp *is* the right thing to do. |
400 | // right thing to do, not using tcp *is* the right thing to do. |
319 | if (!w_pkt) |
401 | if (!w_pkt) |
320 | { |
402 | { |
321 | // how this maps to the underlying tcp packets we don't know |
403 | // how this maps to the underlying tcp packets we don't know |
322 | // and we don't care. at least we tried ;) |
404 | // and we don't care. at least we tried ;) |
|
|
405 | #if defined(SOL_IP) && defined(IP_TOS) |
323 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
406 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
407 | #endif |
324 | |
408 | |
325 | w_pkt = pkt; |
409 | w_pkt = pkt; |
326 | w_ofs = 0; |
410 | w_ofs = 0; |
327 | w_len = pkt->len + 2; // length + size header |
411 | w_len = pkt->len + 2; // length + size header |
328 | |
412 | |
… | |
… | |
337 | } |
421 | } |
338 | } |
422 | } |
339 | } |
423 | } |
340 | |
424 | |
341 | return state != ERROR; |
425 | return state != ERROR; |
|
|
426 | } |
|
|
427 | |
|
|
428 | void tcp_connection::error () |
|
|
429 | { |
|
|
430 | if (fd >= 0) |
|
|
431 | { |
|
|
432 | close (fd); |
|
|
433 | fd = -1; |
|
|
434 | } |
|
|
435 | |
|
|
436 | delete r_pkt; r_pkt = 0; |
|
|
437 | delete w_pkt; w_pkt = 0; |
|
|
438 | #if ENABLE_HTTP_PROXY |
|
|
439 | free (proxy_req); proxy_req = 0; |
|
|
440 | #endif |
|
|
441 | |
|
|
442 | stop (); |
|
|
443 | state = active ? IDLE : ERROR; |
342 | } |
444 | } |
343 | |
445 | |
344 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
446 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
345 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
447 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
346 | { |
448 | { |
347 | last_activity = NOW; |
449 | last_activity = NOW; |
348 | r_pkt = 0; |
450 | r_pkt = 0; |
349 | w_pkt = 0; |
451 | w_pkt = 0; |
350 | fd = fd_; |
452 | fd = fd_; |
|
|
453 | #if ENABLE_HTTP_PROXY |
|
|
454 | proxy_req = 0; |
|
|
455 | #endif |
351 | |
456 | |
352 | if (fd < 0) |
457 | if (fd < 0) |
353 | { |
458 | { |
354 | active = true; |
459 | active = true; |
355 | state = IDLE; |
460 | state = IDLE; |