… | |
… | |
57 | { |
57 | { |
58 | return *a < *b; |
58 | return *a < *b; |
59 | } |
59 | } |
60 | }; |
60 | }; |
61 | |
61 | |
62 | 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> |
|
|
63 | { |
63 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
64 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
64 | |
65 | |
65 | tcp_si_map () |
66 | tcp_si_map () |
66 | : cleaner(this, &tcp_si_map::cleaner_cb) |
|
|
67 | { |
67 | { |
|
|
68 | cleaner.set<tcp_si_map, &tcp_si_map::cleaner_cb> (this); |
68 | cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
69 | cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
69 | } |
70 | } |
70 | |
71 | |
71 | } tcp_si; |
72 | } tcp_si; |
72 | |
73 | |
73 | struct tcp_connection : ev::io { |
74 | struct tcp_connection : ev::io |
|
|
75 | { |
|
|
76 | int tos; |
74 | tstamp last_activity; |
77 | tstamp last_activity; |
75 | const sockinfo si; |
78 | const sockinfo si; |
76 | vpn &v; |
79 | vpn &v; |
77 | bool active; // this connection has been actively established |
80 | bool active; // this connection has been actively established |
78 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
81 | enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
… | |
… | |
104 | ~tcp_connection (); |
107 | ~tcp_connection (); |
105 | }; |
108 | }; |
106 | |
109 | |
107 | void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
110 | void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
108 | { |
111 | { |
109 | tstamp to = ev::ev_now () - ::conf.keepalive - 30 - 60; |
112 | tstamp to = ev_now () - ::conf.keepalive - 30 - 60; |
110 | |
113 | |
111 | for (iterator i = begin (); i != end(); ) |
114 | for (iterator i = begin (); i != end(); ) |
112 | if (i->second->last_activity >= to) |
115 | if (i->second->last_activity >= to) |
113 | ++i; |
116 | ++i; |
114 | else |
117 | else |
… | |
… | |
201 | } |
204 | } |
202 | |
205 | |
203 | void |
206 | void |
204 | tcp_connection::tcpv4_ev (ev::io &w, int revents) |
207 | tcp_connection::tcpv4_ev (ev::io &w, int revents) |
205 | { |
208 | { |
206 | last_activity = ev::ev_now (); |
209 | last_activity = ev_now (); |
207 | |
210 | |
208 | if (revents & EV_WRITE) |
211 | if (revents & EV_WRITE) |
209 | { |
212 | { |
210 | if (state == CONNECTING) |
213 | if (state == CONNECTING) |
211 | { |
214 | { |
… | |
… | |
346 | } |
349 | } |
347 | |
350 | |
348 | bool |
351 | bool |
349 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
352 | tcp_connection::send_packet (vpn_packet *pkt, int tos) |
350 | { |
353 | { |
351 | last_activity = ev::ev_now (); |
354 | last_activity = ev_now (); |
352 | |
355 | |
353 | if (state == IDLE) |
356 | if (state == IDLE) |
354 | { |
357 | { |
355 | // woaw, the first lost packet ;) |
358 | // woaw, the first lost packet ;) |
356 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
359 | fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
… | |
… | |
409 | if (!w_pkt) |
412 | if (!w_pkt) |
410 | { |
413 | { |
411 | // how this maps to the underlying tcp packets we don't know |
414 | // how this maps to the underlying tcp packets we don't know |
412 | // and we don't care. at least we tried ;) |
415 | // and we don't care. at least we tried ;) |
413 | #if defined(SOL_IP) && defined(IP_TOS) |
416 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
417 | if (tos != this->tos) |
|
|
418 | { |
|
|
419 | this->tos = tos; |
414 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
420 | setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
421 | } |
415 | #endif |
422 | #endif |
416 | |
423 | |
417 | w_pkt = pkt; |
424 | w_pkt = pkt; |
418 | w_ofs = 0; |
425 | w_ofs = 0; |
419 | w_len = pkt->len + 2; // length + size header |
426 | w_len = pkt->len + 2; // length + size header |
… | |
… | |
438 | stop (); |
445 | stop (); |
439 | |
446 | |
440 | if (fd >= 0) |
447 | if (fd >= 0) |
441 | { |
448 | { |
442 | close (fd); |
449 | close (fd); |
|
|
450 | tos = -1; |
443 | fd = -1; |
451 | fd = -1; |
444 | } |
452 | } |
445 | |
453 | |
446 | delete r_pkt; r_pkt = 0; |
454 | delete r_pkt; r_pkt = 0; |
447 | delete w_pkt; w_pkt = 0; |
455 | delete w_pkt; w_pkt = 0; |
448 | #if ENABLE_HTTP_PROXY |
456 | #if ENABLE_HTTP_PROXY |
… | |
… | |
451 | |
459 | |
452 | state = active ? IDLE : ERROR; |
460 | state = active ? IDLE : ERROR; |
453 | } |
461 | } |
454 | |
462 | |
455 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
463 | tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
456 | : v(v_), si(si_), ev::io(this, &tcp_connection::tcpv4_ev) |
464 | : v(v_), si(si_) |
457 | { |
465 | { |
|
|
466 | set<tcp_connection, &tcp_connection::tcpv4_ev> (this); |
|
|
467 | |
458 | last_activity = ev::ev_now (); |
468 | last_activity = ev_now (); |
459 | r_pkt = 0; |
469 | r_pkt = 0; |
460 | w_pkt = 0; |
470 | w_pkt = 0; |
|
|
471 | tos = -1; |
461 | fd = fd_; |
472 | fd = fd_; |
462 | #if ENABLE_HTTP_PROXY |
473 | #if ENABLE_HTTP_PROXY |
463 | proxy_req = 0; |
474 | proxy_req = 0; |
464 | #endif |
475 | #endif |
465 | |
476 | |