… | |
… | |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
55 | |
55 | |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
57 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
57 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
58 | |
58 | |
|
|
59 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
59 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
60 | #define MAX_OUTSTANDING 20 // max. outstanding requests |
60 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #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 |
62 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | |
63 | |
63 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
64 | #define MAX_DOMAIN_SIZE 200 // 255 is legal limit, but bind doesn't compress well |
64 | // 240 leaves about 4 bytes of server reply data |
65 | // 240 leaves about 4 bytes of server reply data |
65 | // every two request bytes less give room for one reply byte |
66 | // every two request bytes less give room for one reply byte |
66 | |
67 | |
67 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
… | |
… | |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
266 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
266 | |
267 | |
267 | ///////////////////////////////////////////////////////////////////////////// |
268 | ///////////////////////////////////////////////////////////////////////////// |
268 | |
269 | |
269 | #define HDRSIZE 6 |
270 | #define HDRSIZE 6 |
270 | |
271 | |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
272 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
272 | { |
273 | { |
273 | seqno &= SEQNO_MASK; |
274 | seqno &= SEQNO_MASK; |
274 | |
275 | |
275 | u8 hdr[3] = { |
276 | u8 hdr[3] = { |
… | |
… | |
409 | struct dns_cfg |
410 | struct dns_cfg |
410 | { |
411 | { |
411 | static int next_uid; |
412 | static int next_uid; |
412 | |
413 | |
413 | u8 id1, id2, id3, id4; |
414 | u8 id1, id2, id3, id4; |
|
|
415 | |
414 | u8 version; |
416 | u8 version; |
|
|
417 | u8 flags; |
415 | u8 rrtype; |
418 | u8 rrtype; |
416 | u8 flags; |
|
|
417 | u8 def_ttl; |
419 | u8 def_ttl; |
418 | u8 rcv_cdc; |
420 | |
419 | u8 snd_cdc; |
|
|
420 | u16 max_size; |
|
|
421 | u16 client; |
421 | u16 client; |
422 | u16 uid; // to make request unique |
422 | u16 uid; // to make request unique |
423 | |
423 | |
424 | u8 reserved[8]; |
424 | u16 max_size; |
|
|
425 | u8 seq_cdc; |
|
|
426 | u8 req_cdc; |
|
|
427 | |
|
|
428 | u8 rep_cdc; |
|
|
429 | u8 r2, r3, r4; |
|
|
430 | |
|
|
431 | u8 r5, r6, r7, r8; |
425 | |
432 | |
426 | void reset (int clientid); |
433 | void reset (int clientid); |
427 | bool valid (); |
434 | bool valid (); |
428 | }; |
435 | }; |
429 | |
436 | |
… | |
… | |
438 | |
445 | |
439 | version = 1; |
446 | version = 1; |
440 | |
447 | |
441 | rrtype = RR_TYPE_TXT; |
448 | rrtype = RR_TYPE_TXT; |
442 | flags = 0; |
449 | flags = 0; |
443 | def_ttl = 1; |
450 | def_ttl = 0; |
|
|
451 | seq_cdc = 26; |
|
|
452 | req_cdc = 62; |
444 | rcv_cdc = 0; |
453 | rep_cdc = 0; |
445 | snd_cdc = 62; |
|
|
446 | max_size = ntohs (MAX_PKT_SIZE); |
454 | max_size = ntohs (MAX_PKT_SIZE); |
447 | client = ntohs (clientid); |
455 | client = ntohs (clientid); |
448 | uid = next_uid++; |
456 | uid = next_uid++; |
449 | |
457 | |
450 | memset (reserved, 0, 8); |
458 | r2 = r3 = r4 = 0; |
|
|
459 | r4 = r5 = r6 = r7 = 0; |
451 | } |
460 | } |
452 | |
461 | |
453 | bool dns_cfg::valid () |
462 | bool dns_cfg::valid () |
454 | { |
463 | { |
455 | return id1 == 'G' |
464 | return id1 == 'G' |
456 | && id2 == 'V' |
465 | && id2 == 'V' |
457 | && id3 == 'P' |
466 | && id3 == 'P' |
458 | && id4 == 'E' |
467 | && id4 == 'E' |
|
|
468 | && seq_cdc == 26 |
|
|
469 | && req_cdc == 62 |
|
|
470 | && rep_cdc == 0 |
459 | && version == 1 |
471 | && version == 1 |
460 | && flags == 0 |
|
|
461 | && rcv_cdc == 0 |
|
|
462 | && snd_cdc == 62 |
|
|
463 | && max_size == ntohs (MAX_PKT_SIZE); |
472 | && max_size == ntohs (MAX_PKT_SIZE); |
464 | } |
473 | } |
465 | |
474 | |
466 | struct dns_packet : net_packet |
475 | struct dns_packet : net_packet |
467 | { |
476 | { |
468 | u16 id; |
477 | u16 id; |
469 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
478 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
470 | u16 qdcount, ancount, nscount, arcount; |
479 | u16 qdcount, ancount, nscount, arcount; |
471 | |
480 | |
472 | u8 data[MAXSIZE - 6 * 2]; |
481 | u8 data [MAXSIZE - 6 * 2]; |
473 | |
482 | |
474 | int decode_label (char *data, int size, int &offs); |
483 | int decode_label (char *data, int size, int &offs); |
475 | }; |
484 | }; |
476 | |
485 | |
477 | int dns_packet::decode_label (char *data, int size, int &offs) |
486 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
539 | |
548 | |
540 | bool established; |
549 | bool established; |
541 | |
550 | |
542 | tstamp last_received; |
551 | tstamp last_received; |
543 | tstamp last_sent; |
552 | tstamp last_sent; |
544 | double last_latency; |
553 | double min_latency; |
545 | double poll_interval, send_interval; |
554 | double poll_interval, send_interval; |
546 | |
555 | |
547 | vector<dns_rcv *> rcvpq; |
556 | vector<dns_rcv *> rcvpq; |
548 | |
557 | |
549 | byte_stream rcvdq; int rcvseq; |
558 | byte_stream rcvdq; int rcvseq; |
… | |
… | |
723 | rcvseq = sndseq = 0; |
732 | rcvseq = sndseq = 0; |
724 | |
733 | |
725 | last_sent = last_received = 0; |
734 | last_sent = last_received = 0; |
726 | poll_interval = MIN_POLL_INTERVAL; |
735 | poll_interval = MIN_POLL_INTERVAL; |
727 | send_interval = 0.5; // starting rate |
736 | send_interval = 0.5; // starting rate |
728 | last_latency = INITIAL_TIMEOUT; |
737 | min_latency = INITIAL_TIMEOUT; |
729 | } |
738 | } |
730 | |
739 | |
731 | dns_connection::~dns_connection () |
740 | dns_connection::~dns_connection () |
732 | { |
741 | { |
733 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
742 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
… | |
… | |
746 | poll_interval = send_interval; |
755 | poll_interval = send_interval; |
747 | } |
756 | } |
748 | else |
757 | else |
749 | { |
758 | { |
750 | poll_interval *= 1.5; |
759 | poll_interval *= 1.5; |
|
|
760 | |
751 | if (poll_interval > MAX_POLL_INTERVAL) |
761 | if (poll_interval > MAX_POLL_INTERVAL) |
752 | poll_interval = MAX_POLL_INTERVAL; |
762 | poll_interval = MAX_POLL_INTERVAL; |
753 | } |
763 | } |
754 | |
764 | |
755 | rcvpq.push_back (r); |
765 | rcvpq.push_back (r); |
… | |
… | |
805 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
815 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
806 | |
816 | |
807 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
817 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
808 | && pkt.qdcount == htons (1)) |
818 | && pkt.qdcount == htons (1)) |
809 | { |
819 | { |
810 | char qname[MAXSIZE]; |
820 | char qname [MAXSIZE]; |
811 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
821 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
812 | |
822 | |
813 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
823 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
814 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
824 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
815 | |
825 | |
… | |
… | |
822 | |
832 | |
823 | int dlen = strlen (THISNODE->domain); |
833 | int dlen = strlen (THISNODE->domain); |
824 | |
834 | |
825 | if (qclass == RR_CLASS_IN |
835 | if (qclass == RR_CLASS_IN |
826 | && qlen > dlen + 1 |
836 | && qlen > dlen + 1 |
827 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
837 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
828 | { |
838 | { |
829 | // now generate reply |
839 | // now generate reply |
830 | pkt.ancount = htons (1); // one answer RR |
840 | pkt.ancount = htons (1); // one answer RR |
831 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
841 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
832 | |
842 | |
… | |
… | |
954 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
964 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
955 | pkt [offs++] = 0; pkt [offs++] = 0; |
965 | pkt [offs++] = 0; pkt [offs++] = 0; |
956 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
966 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
957 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
967 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
958 | |
968 | |
959 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
969 | slog (L_INFO, _("DNS: client %d connects"), client); |
960 | |
970 | |
961 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
971 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
962 | pkt [offs++] = CMD_IP_REJ; |
972 | pkt [offs++] = CMD_IP_REJ; |
963 | |
973 | |
964 | if (0 < client && client <= conns.size ()) |
974 | if (0 < client && client <= conns.size ()) |
… | |
… | |
1016 | dns->send_interval = MIN_SEND_INTERVAL; |
1026 | dns->send_interval = MIN_SEND_INTERVAL; |
1017 | |
1027 | |
1018 | // the latency surely puts an upper bound on |
1028 | // the latency surely puts an upper bound on |
1019 | // the minimum send interval |
1029 | // the minimum send interval |
1020 | double latency = NOW - (*i)->sent; |
1030 | double latency = NOW - (*i)->sent; |
|
|
1031 | |
|
|
1032 | if (latency < dns->min_latency) |
1021 | dns->last_latency = latency; |
1033 | dns->min_latency = latency; |
1022 | |
1034 | |
1023 | if (dns->send_interval > latency) |
1035 | if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) |
1024 | dns->send_interval = latency; |
1036 | dns->send_interval = dns->min_latency * LATENCY_FACTOR; |
1025 | } |
1037 | } |
1026 | |
1038 | |
1027 | delete *i; |
1039 | delete *i; |
1028 | dns_sndpq.erase (i); |
1040 | dns_sndpq.erase (i); |
1029 | |
1041 | |
… | |
… | |
1146 | |
1158 | |
1147 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1159 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1148 | |
1160 | |
1149 | if (pkt->len > 0) |
1161 | if (pkt->len > 0) |
1150 | { |
1162 | { |
1151 | if (THISNODE->dns_port) |
1163 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1164 | dnsv4_client (*pkt); |
|
|
1165 | else |
1152 | { |
1166 | { |
1153 | dnsv4_server (*pkt); |
1167 | dnsv4_server (*pkt); |
1154 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1168 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1155 | } |
1169 | } |
1156 | else |
|
|
1157 | dnsv4_client (*pkt); |
|
|
1158 | |
1170 | |
1159 | delete pkt; |
1171 | delete pkt; |
1160 | } |
1172 | } |
1161 | } |
1173 | } |
1162 | } |
1174 | } |
… | |
… | |
1211 | if (!send) |
1223 | if (!send) |
1212 | { |
1224 | { |
1213 | send = r; |
1225 | send = r; |
1214 | |
1226 | |
1215 | r->retry++; |
1227 | r->retry++; |
1216 | r->timeout = NOW + (r->retry * last_latency * 8.); |
1228 | r->timeout = NOW + (r->retry * min_latency * 8.); |
1217 | |
1229 | |
1218 | // the following code changes the query section a bit, forcing |
1230 | // the following code changes the query section a bit, forcing |
1219 | // the forwarder to generate a new request |
1231 | // the forwarder to generate a new request |
1220 | if (r->stdhdr) |
1232 | if (r->stdhdr) |
1221 | { |
1233 | { |
… | |
… | |
1238 | { |
1250 | { |
1239 | if (vpn->dns_sndpq.empty ()) |
1251 | if (vpn->dns_sndpq.empty ()) |
1240 | { |
1252 | { |
1241 | send = new dns_snd (this); |
1253 | send = new dns_snd (this); |
1242 | |
1254 | |
1243 | printf ("new conn %p %d\n", this, c->conf->id);//D |
|
|
1244 | cfg.reset (THISNODE->id); |
1255 | cfg.reset (THISNODE->id); |
1245 | send->gen_syn_req (); |
1256 | send->gen_syn_req (); |
1246 | } |
1257 | } |
1247 | } |
1258 | } |
1248 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1259 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1249 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1260 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1250 | { |
1261 | { |
1251 | //printf ("sending data request etc.\n"); //D |
1262 | //printf ("sending data request etc.\n"); //D |
1252 | if (!snddq.empty ()) |
1263 | if (!snddq.empty () || last_received + 1. > NOW) |
1253 | { |
1264 | { |
1254 | poll_interval = send_interval; |
1265 | poll_interval = send_interval; |
1255 | NEXT (NOW + send_interval); |
1266 | NEXT (NOW + send_interval); |
1256 | } |
1267 | } |
1257 | |
1268 | |
1258 | send = new dns_snd (this); |
1269 | send = new dns_snd (this); |
1259 | send->gen_stream_req (sndseq, snddq); |
1270 | send->gen_stream_req (sndseq, snddq); |
1260 | send->timeout = NOW + last_latency * 8.; |
1271 | send->timeout = NOW + min_latency * 8.; |
1261 | |
1272 | |
1262 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1273 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1263 | } |
1274 | } |
1264 | |
1275 | |
1265 | if (send) |
1276 | if (send) |