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