… | |
… | |
47 | #include "vpn.h" |
47 | #include "vpn.h" |
48 | |
48 | |
49 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
49 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
50 | #define ACTIVITY_INTERVAL 5. |
50 | #define ACTIVITY_INTERVAL 5. |
51 | |
51 | |
|
|
52 | #define TIMEOUT_FACTOR 2. |
|
|
53 | |
52 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
54 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
53 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
55 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
54 | |
56 | |
55 | #define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests |
57 | #define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests |
56 | #define MAX_SEND_INTERVAL 2. // optimistic? |
58 | #define MAX_SEND_INTERVAL 2. // optimistic? |
57 | |
59 | |
58 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
60 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
59 | #define MAX_OUTSTANDING 2 // max. outstanding requests |
61 | #define MAX_OUTSTANDING 100 // max. outstanding requests |
60 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
63 | #define MAX_BACKLOG (32*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | |
64 | |
63 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
65 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
64 | // 240 leaves about 4 bytes of server reply data |
66 | // 240 leaves about 4 bytes of server reply data |
65 | // every two request bytes less give room for one reply byte |
67 | // every two request bytes less give room for one reply byte |
66 | |
68 | |
67 | #define SEQNO_MASK 0x3fff |
69 | #define SEQNO_MASK 0x0fff |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | |
71 | |
70 | #define MAX_LBL_SIZE 63 |
72 | #define MAX_LBL_SIZE 63 |
71 | #define MAX_PKT_SIZE 512 |
73 | #define MAX_PKT_SIZE 512 |
72 | |
74 | |
… | |
… | |
553 | double min_latency; |
555 | double min_latency; |
554 | double poll_interval, send_interval; |
556 | double poll_interval, send_interval; |
555 | |
557 | |
556 | vector<dns_rcv *> rcvpq; |
558 | vector<dns_rcv *> rcvpq; |
557 | |
559 | |
558 | byte_stream rcvdq; int rcvseq; |
560 | byte_stream rcvdq; int rcvseq; int repseq; |
559 | byte_stream snddq; int sndseq; |
561 | byte_stream snddq; int sndseq; |
560 | |
562 | |
561 | void time_cb (time_watcher &w); time_watcher tw; |
563 | void time_cb (time_watcher &w); time_watcher tw; |
562 | void receive_rep (dns_rcv *r); |
564 | void receive_rep (dns_rcv *r); |
563 | |
565 | |
… | |
… | |
720 | ///////////////////////////////////////////////////////////////////////////// |
722 | ///////////////////////////////////////////////////////////////////////////// |
721 | |
723 | |
722 | dns_connection::dns_connection (connection *c) |
724 | dns_connection::dns_connection (connection *c) |
723 | : c (c) |
725 | : c (c) |
724 | , rcvdq (MAX_BACKLOG * 2) |
726 | , rcvdq (MAX_BACKLOG * 2) |
725 | , snddq (MAX_BACKLOG * 2) |
727 | , snddq (MAX_BACKLOG) |
726 | , tw (this, &dns_connection::time_cb) |
728 | , tw (this, &dns_connection::time_cb) |
727 | { |
729 | { |
728 | vpn = c->vpn; |
730 | vpn = c->vpn; |
729 | |
731 | |
730 | established = false; |
732 | established = false; |
731 | |
733 | |
732 | rcvseq = sndseq = 0; |
734 | rcvseq = repseq = sndseq = 0; |
733 | |
735 | |
734 | last_sent = last_received = 0; |
736 | last_sent = last_received = 0; |
735 | poll_interval = 0.5; // starting here |
737 | poll_interval = 0.5; // starting here |
736 | send_interval = 0.5; // starting rate |
738 | send_interval = 0.5; // starting rate |
737 | min_latency = INITIAL_TIMEOUT; |
739 | min_latency = INITIAL_TIMEOUT; |
… | |
… | |
855 | if (0 < client && client <= conns.size ()) |
857 | if (0 < client && client <= conns.size ()) |
856 | { |
858 | { |
857 | connection *c = conns [client - 1]; |
859 | connection *c = conns [client - 1]; |
858 | dns_connection *dns = c->dns; |
860 | dns_connection *dns = c->dns; |
859 | dns_rcv *rcv; |
861 | dns_rcv *rcv; |
860 | bool in_seq; |
|
|
861 | |
862 | |
862 | if (dns) |
863 | if (dns) |
863 | { |
864 | { |
864 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
865 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
865 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
866 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
… | |
… | |
876 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
877 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
877 | |
878 | |
878 | goto duplicate_request; |
879 | goto duplicate_request; |
879 | } |
880 | } |
880 | |
881 | |
881 | in_seq = dns->rcvseq == seqno; |
|
|
882 | |
|
|
883 | // new packet, queue |
882 | // new packet, queue |
884 | rcv = new dns_rcv (seqno, data, datalen); |
883 | rcv = new dns_rcv (seqno, data, datalen); |
885 | dns->receive_rep (rcv); |
884 | dns->receive_rep (rcv); |
886 | } |
885 | } |
887 | |
886 | |
… | |
… | |
904 | dlen -= qlen; |
903 | dlen -= qlen; |
905 | |
904 | |
906 | // only put data into in-order sequence packets, if |
905 | // only put data into in-order sequence packets, if |
907 | // we receive out-of-order packets we generate empty |
906 | // we receive out-of-order packets we generate empty |
908 | // replies |
907 | // replies |
909 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
908 | //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D |
|
|
909 | if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) |
910 | { |
910 | { |
|
|
911 | dns->repseq = seqno; |
|
|
912 | |
|
|
913 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
914 | { |
911 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
915 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
912 | |
916 | |
913 | if (txtlen > dns->snddq.size ()) |
917 | if (txtlen > dns->snddq.size ()) |
914 | txtlen = dns->snddq.size (); |
918 | txtlen = dns->snddq.size (); |
915 | |
919 | |
916 | pkt[offs++] = txtlen; |
920 | pkt[offs++] = txtlen; |
917 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
921 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
918 | offs += txtlen; |
922 | offs += txtlen; |
919 | dns->snddq.remove (txtlen); |
923 | dns->snddq.remove (txtlen); |
920 | |
924 | |
921 | dlen -= txtlen + 1; |
925 | dlen -= txtlen + 1; |
|
|
926 | } |
922 | } |
927 | } |
923 | |
928 | |
924 | // avoid empty TXT rdata |
929 | // avoid completely empty TXT rdata |
925 | if (offs == rdlen_offs) |
930 | if (offs == rdlen_offs) |
926 | pkt[offs++] = 0; |
931 | pkt[offs++] = 0; |
927 | |
932 | |
928 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
933 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
929 | } |
934 | } |
… | |
… | |
1184 | connection *c = conns [client - 1]; |
1189 | connection *c = conns [client - 1]; |
1185 | |
1190 | |
1186 | if (!c->dns) |
1191 | if (!c->dns) |
1187 | c->dns = new dns_connection (c); |
1192 | c->dns = new dns_connection (c); |
1188 | |
1193 | |
1189 | if (!c->dns->snddq.put (pkt)) |
1194 | if (c->dns->snddq.put (pkt)) |
1190 | return false; |
|
|
1191 | |
|
|
1192 | c->dns->tw.trigger (); |
1195 | c->dns->tw.trigger (); |
1193 | |
1196 | |
|
|
1197 | // always return true even if the buffer overflows |
1194 | return true; |
1198 | return true; |
1195 | } |
1199 | } |
1196 | |
1200 | |
1197 | void |
1201 | void |
1198 | connection::dnsv4_reset_connection () |
1202 | connection::dnsv4_reset_connection () |
… | |
… | |
1224 | if (!send) |
1228 | if (!send) |
1225 | { |
1229 | { |
1226 | send = r; |
1230 | send = r; |
1227 | |
1231 | |
1228 | r->retry++; |
1232 | r->retry++; |
1229 | r->timeout = NOW + (r->retry * min_latency * 8.); |
1233 | r->timeout = NOW + (r->retry * min_latency * TIMEOUT_FACTOR); |
1230 | |
1234 | |
1231 | // the following code changes the query section a bit, forcing |
1235 | // the following code changes the query section a bit, forcing |
1232 | // the forwarder to generate a new request |
1236 | // the forwarder to generate a new request |
1233 | if (r->stdhdr) |
1237 | if (r->stdhdr) |
1234 | { |
1238 | { |
… | |
… | |
1267 | NEXT (NOW + send_interval); |
1271 | NEXT (NOW + send_interval); |
1268 | } |
1272 | } |
1269 | |
1273 | |
1270 | send = new dns_snd (this); |
1274 | send = new dns_snd (this); |
1271 | send->gen_stream_req (sndseq, snddq); |
1275 | send->gen_stream_req (sndseq, snddq); |
1272 | send->timeout = NOW + min_latency * 8.; |
1276 | send->timeout = NOW + min_latency * TIMEOUT_FACTOR; |
1273 | |
1277 | |
1274 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1278 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1275 | } |
1279 | } |
1276 | else |
1280 | else |
1277 | NEXT (last_sent + send_interval); |
1281 | NEXT (last_sent + send_interval); |
… | |
… | |
1282 | } |
1286 | } |
1283 | |
1287 | |
1284 | if (send) |
1288 | if (send) |
1285 | { |
1289 | { |
1286 | last_sent = NOW; |
1290 | last_sent = NOW; |
|
|
1291 | if (rand () & 15 != 0)//D |
1287 | sendto (vpn->dnsv4_fd, |
1292 | sendto (vpn->dnsv4_fd, |
1288 | send->pkt->at (0), send->pkt->len, 0, |
1293 | send->pkt->at (0), send->pkt->len, 0, |
1289 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1294 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1290 | } |
1295 | } |
1291 | |
1296 | |