… | |
… | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
16 | |
16 | |
17 | 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 |
18 | along with gvpe; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
19 | 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 |
20 | */ |
20 | */ |
|
|
21 | |
|
|
22 | // TODO: EDNS0 option to increase dns mtu? |
|
|
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
|
|
24 | // to ensure no buffer overflows or similar problems. |
21 | |
25 | |
22 | #include "config.h" |
26 | #include "config.h" |
23 | |
27 | |
24 | #if ENABLE_DNS |
28 | #if ENABLE_DNS |
25 | |
29 | |
… | |
… | |
47 | #include "vpn.h" |
51 | #include "vpn.h" |
48 | |
52 | |
49 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
53 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
50 | #define ACTIVITY_INTERVAL 5. |
54 | #define ACTIVITY_INTERVAL 5. |
51 | |
55 | |
52 | #define TIMEOUT_FACTOR 8. |
|
|
53 | |
|
|
54 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
56 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
55 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
57 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
56 | |
58 | |
57 | #define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests |
|
|
58 | #define MAX_SEND_INTERVAL 2. // optimistic? |
59 | #define MAX_SEND_INTERVAL 2. // optimistic? |
59 | |
60 | |
60 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
|
|
61 | #define MAX_OUTSTANDING 100 // max. outstanding requests |
|
|
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
63 | #define MAX_BACKLOG (32*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
64 | |
63 | |
65 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
64 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
66 | // 240 leaves about 4 bytes of server reply data |
65 | // 240 leaves about 4 bytes of server reply data |
67 | // every request byte less give room for two reply bytes |
66 | // every request byte less give room for two reply bytes |
68 | |
67 | |
69 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_MASK 0x3fff |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
… | |
… | |
462 | r4 = r5 = r6 = r7 = 0; |
461 | r4 = r5 = r6 = r7 = 0; |
463 | } |
462 | } |
464 | |
463 | |
465 | bool dns_cfg::valid () |
464 | bool dns_cfg::valid () |
466 | { |
465 | { |
|
|
466 | // although the protocol itself allows for some configurability, |
|
|
467 | // only the following encoding/decoding settings are implemented. |
467 | return id1 == 'G' |
468 | return id1 == 'G' |
468 | && id2 == 'V' |
469 | && id2 == 'V' |
469 | && id3 == 'P' |
470 | && id3 == 'P' |
470 | && id4 == 'E' |
471 | && id4 == 'E' |
471 | && seq_cdc == 26 |
472 | && seq_cdc == 26 |
… | |
… | |
770 | |
771 | |
771 | // find next packet |
772 | // find next packet |
772 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
773 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
773 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
774 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
774 | { |
775 | { |
|
|
776 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
775 | // enter the packet into our input stream |
777 | // enter the packet into our input stream |
776 | r = *i; |
778 | r = *i; |
777 | |
779 | |
778 | // remove the oldest packet, look forward, as it's oldest first |
780 | // remove the oldest packet, look forward, as it's oldest first |
779 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
781 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
780 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
782 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
781 | { |
783 | { |
|
|
784 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
782 | delete *j; |
785 | delete *j; |
783 | rcvpq.erase (j); |
786 | rcvpq.erase (j); |
784 | break; |
787 | break; |
785 | } |
788 | } |
786 | |
789 | |
… | |
… | |
1014 | { |
1017 | { |
1015 | dns_connection *dns = (*i)->dns; |
1018 | dns_connection *dns = (*i)->dns; |
1016 | connection *c = dns->c; |
1019 | connection *c = dns->c; |
1017 | int seqno = (*i)->seqno; |
1020 | int seqno = (*i)->seqno; |
1018 | u8 data[MAXSIZE], *datap = data; |
1021 | u8 data[MAXSIZE], *datap = data; |
|
|
1022 | //printf ("rcv pkt %x\n", seqno);//D |
1019 | |
1023 | |
1020 | if ((*i)->retry) |
1024 | if ((*i)->retry) |
1021 | { |
1025 | { |
1022 | dns->send_interval *= 1.01; |
1026 | dns->send_interval *= 1.01; |
1023 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1027 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1033 | double latency = NOW - (*i)->sent; |
1037 | double latency = NOW - (*i)->sent; |
1034 | |
1038 | |
1035 | if (latency < dns->min_latency) |
1039 | if (latency < dns->min_latency) |
1036 | dns->min_latency = latency; |
1040 | dns->min_latency = latency; |
1037 | |
1041 | |
1038 | if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) |
1042 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1039 | dns->send_interval = dns->min_latency * LATENCY_FACTOR; |
1043 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
1040 | |
1044 | |
1041 | if (dns->send_interval < MIN_SEND_INTERVAL) |
1045 | if (dns->send_interval < conf.dns_send_interval) |
1042 | dns->send_interval = MIN_SEND_INTERVAL; |
1046 | dns->send_interval = conf.dns_send_interval; |
1043 | } |
1047 | } |
1044 | |
1048 | |
1045 | delete *i; |
1049 | delete *i; |
1046 | dns_sndpq.erase (i); |
1050 | dns_sndpq.erase (i); |
1047 | |
1051 | |
… | |
… | |
1228 | if (!send) |
1232 | if (!send) |
1229 | { |
1233 | { |
1230 | send = r; |
1234 | send = r; |
1231 | |
1235 | |
1232 | r->retry++; |
1236 | r->retry++; |
1233 | r->timeout = NOW + (r->retry * min_latency * TIMEOUT_FACTOR); |
1237 | r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor); |
1234 | printf ("TO %d %f\n", r->retry, r->timeout - NOW);//D |
1238 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - NOW);//D |
1235 | |
1239 | |
1236 | // the following code changes the query section a bit, forcing |
1240 | // the following code changes the query section a bit, forcing |
1237 | // the forwarder to generate a new request |
1241 | // the forwarder to generate a new request |
1238 | if (r->stdhdr) |
1242 | if (r->stdhdr) |
1239 | { |
|
|
1240 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1241 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1243 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1242 | } |
|
|
1243 | } |
1244 | } |
1244 | } |
1245 | } |
1245 | else |
1246 | else |
1246 | NEXT (r->timeout); |
1247 | NEXT (r->timeout); |
1247 | } |
1248 | } |
… | |
… | |
1258 | |
1259 | |
1259 | cfg.reset (THISNODE->id); |
1260 | cfg.reset (THISNODE->id); |
1260 | send->gen_syn_req (); |
1261 | send->gen_syn_req (); |
1261 | } |
1262 | } |
1262 | } |
1263 | } |
1263 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1264 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1264 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1265 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1265 | { |
1266 | { |
1266 | if (last_sent + send_interval <= NOW) |
1267 | if (last_sent + send_interval <= NOW) |
1267 | { |
1268 | { |
1268 | //printf ("sending data request etc.\n"); //D |
1269 | //printf ("sending data request etc.\n"); //D |
… | |
… | |
1272 | NEXT (NOW + send_interval); |
1273 | NEXT (NOW + send_interval); |
1273 | } |
1274 | } |
1274 | |
1275 | |
1275 | send = new dns_snd (this); |
1276 | send = new dns_snd (this); |
1276 | send->gen_stream_req (sndseq, snddq); |
1277 | send->gen_stream_req (sndseq, snddq); |
1277 | send->timeout = NOW + min_latency * TIMEOUT_FACTOR; |
1278 | send->timeout = NOW + min_latency * conf.dns_timeout_factor; |
|
|
1279 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - NOW, min_latency, conf.dns_timeout_factor);//D |
1278 | |
1280 | |
1279 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1281 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1280 | } |
1282 | } |
1281 | else |
1283 | else |
1282 | NEXT (last_sent + send_interval); |
1284 | NEXT (last_sent + send_interval); |
… | |
… | |
1298 | poll_interval, send_interval, next - NOW, |
1300 | poll_interval, send_interval, next - NOW, |
1299 | vpn->dns_sndpq.size (), snddq.size (), |
1301 | vpn->dns_sndpq.size (), snddq.size (), |
1300 | rcvpq.size ()); |
1302 | rcvpq.size ()); |
1301 | |
1303 | |
1302 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1304 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
|
|
1305 | // doesn't seem to happen anymore |
1303 | if (next < NOW + 0.001) |
1306 | if (next < NOW + 0.001) |
1304 | next = NOW + 0.1; |
1307 | next = NOW + 0.1; |
1305 | |
1308 | |
1306 | w.start (next); |
1309 | w.start (next); |
1307 | } |
1310 | } |