… | |
… | |
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 2. |
|
|
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 240 // 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 two request bytes less give room for one reply byte |
66 | // every request byte less give room for two reply bytes |
68 | |
67 | |
69 | #define SEQNO_MASK 0x0fff |
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) ) |
71 | |
70 | |
72 | #define MAX_LBL_SIZE 63 |
71 | #define MAX_LBL_SIZE 63 |
73 | #define MAX_PKT_SIZE 512 |
72 | #define MAX_PKT_SIZE 512 |
74 | |
73 | |
… | |
… | |
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 |
… | |
… | |
558 | vector<dns_rcv *> rcvpq; |
559 | vector<dns_rcv *> rcvpq; |
559 | |
560 | |
560 | byte_stream rcvdq; int rcvseq; int repseq; |
561 | byte_stream rcvdq; int rcvseq; int repseq; |
561 | byte_stream snddq; int sndseq; |
562 | byte_stream snddq; int sndseq; |
562 | |
563 | |
563 | void time_cb (time_watcher &w); time_watcher tw; |
564 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
564 | void receive_rep (dns_rcv *r); |
565 | void receive_rep (dns_rcv *r); |
565 | |
566 | |
566 | dns_connection (connection *c); |
567 | dns_connection (connection *c); |
567 | ~dns_connection (); |
568 | ~dns_connection (); |
568 | }; |
569 | }; |
… | |
… | |
587 | : dns (dns) |
588 | : dns (dns) |
588 | { |
589 | { |
589 | timeout = 0; |
590 | timeout = 0; |
590 | retry = 0; |
591 | retry = 0; |
591 | seqno = 0; |
592 | seqno = 0; |
592 | sent = NOW; |
593 | sent = ev_now (); |
593 | stdhdr = false; |
594 | stdhdr = false; |
594 | |
595 | |
595 | pkt = new dns_packet; |
596 | pkt = new dns_packet; |
596 | |
597 | |
597 | pkt->id = next_id (); |
598 | pkt->id = next_id (); |
… | |
… | |
628 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
629 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
629 | { |
630 | { |
630 | stdhdr = true; |
631 | stdhdr = true; |
631 | this->seqno = seqno; |
632 | this->seqno = seqno; |
632 | |
633 | |
633 | timeout = NOW + INITIAL_TIMEOUT; |
634 | timeout = ev_now () + INITIAL_TIMEOUT; |
634 | |
635 | |
635 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
636 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
636 | pkt->qdcount = htons (1); |
637 | pkt->qdcount = htons (1); |
637 | |
638 | |
638 | int offs = 6*2; |
639 | int offs = 6*2; |
… | |
… | |
673 | pkt->len = offs; |
674 | pkt->len = offs; |
674 | } |
675 | } |
675 | |
676 | |
676 | void dns_snd::gen_syn_req () |
677 | void dns_snd::gen_syn_req () |
677 | { |
678 | { |
678 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
679 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
679 | |
680 | |
680 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
681 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
681 | pkt->qdcount = htons (1); |
682 | pkt->qdcount = htons (1); |
682 | |
683 | |
683 | int offs = 6 * 2; |
684 | int offs = 6 * 2; |
… | |
… | |
749 | |
750 | |
750 | void dns_connection::receive_rep (dns_rcv *r) |
751 | void dns_connection::receive_rep (dns_rcv *r) |
751 | { |
752 | { |
752 | if (r->datalen) |
753 | if (r->datalen) |
753 | { |
754 | { |
754 | last_received = NOW; |
755 | last_received = ev_now (); |
755 | tw.trigger (); |
756 | tw (); |
756 | |
757 | |
757 | poll_interval = send_interval; |
758 | poll_interval = send_interval; |
758 | } |
759 | } |
759 | else |
760 | else |
760 | { |
761 | { |
… | |
… | |
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 | |
… | |
… | |
793 | } |
796 | } |
794 | |
797 | |
795 | while (vpn_packet *pkt = rcvdq.get ()) |
798 | while (vpn_packet *pkt = rcvdq.get ()) |
796 | { |
799 | { |
797 | sockinfo si; |
800 | sockinfo si; |
798 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
801 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
799 | |
802 | |
800 | vpn->recv_vpn_packet (pkt, si); |
803 | vpn->recv_vpn_packet (pkt, si); |
801 | |
804 | |
802 | delete pkt; |
805 | delete pkt; |
803 | } |
806 | } |
… | |
… | |
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) |
… | |
… | |
1028 | #if 0 |
1032 | #if 0 |
1029 | dns->send_interval *= 0.999; |
1033 | dns->send_interval *= 0.999; |
1030 | #endif |
1034 | #endif |
1031 | // the latency surely puts an upper bound on |
1035 | // the latency surely puts an upper bound on |
1032 | // the minimum send interval |
1036 | // the minimum send interval |
1033 | double latency = NOW - (*i)->sent; |
1037 | double latency = ev_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 | |
… | |
… | |
1152 | break; |
1156 | break; |
1153 | } |
1157 | } |
1154 | } |
1158 | } |
1155 | |
1159 | |
1156 | void |
1160 | void |
1157 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1161 | vpn::dnsv4_ev (ev::io &w, int revents) |
1158 | { |
1162 | { |
1159 | if (revents & EVENT_READ) |
1163 | if (revents & EV_READ) |
1160 | { |
1164 | { |
1161 | dns_packet *pkt = new dns_packet; |
1165 | dns_packet *pkt = new dns_packet; |
1162 | struct sockaddr_in sa; |
1166 | struct sockaddr_in sa; |
1163 | socklen_t sa_len = sizeof (sa); |
1167 | socklen_t sa_len = sizeof (sa); |
1164 | |
1168 | |
… | |
… | |
1180 | } |
1184 | } |
1181 | |
1185 | |
1182 | bool |
1186 | bool |
1183 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1187 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1184 | { |
1188 | { |
1185 | int client = ntohs (si.port); |
1189 | int client = ntohl (si.host); |
1186 | |
1190 | |
1187 | assert (0 < client && client <= conns.size ()); |
1191 | assert (0 < client && client <= conns.size ()); |
1188 | |
1192 | |
1189 | connection *c = conns [client - 1]; |
1193 | connection *c = conns [client - 1]; |
1190 | |
1194 | |
1191 | if (!c->dns) |
1195 | if (!c->dns) |
1192 | c->dns = new dns_connection (c); |
1196 | c->dns = new dns_connection (c); |
1193 | |
1197 | |
1194 | if (c->dns->snddq.put (pkt)) |
1198 | if (c->dns->snddq.put (pkt)) |
1195 | c->dns->tw.trigger (); |
1199 | c->dns->tw (); |
1196 | |
1200 | |
1197 | // always return true even if the buffer overflows |
1201 | // always return true even if the buffer overflows |
1198 | return true; |
1202 | return true; |
1199 | } |
1203 | } |
1200 | |
1204 | |
… | |
… | |
1205 | } |
1209 | } |
1206 | |
1210 | |
1207 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1211 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1208 | |
1212 | |
1209 | void |
1213 | void |
1210 | dns_connection::time_cb (time_watcher &w) |
1214 | dns_connection::time_cb (ev::timer &w, int revents) |
1211 | { |
1215 | { |
1212 | // servers have to be polled |
1216 | // servers have to be polled |
1213 | if (THISNODE->dns_port) |
1217 | if (THISNODE->dns_port) |
1214 | return; |
1218 | return; |
1215 | |
1219 | |
1216 | // check for timeouts and (re)transmit |
1220 | // check for timeouts and (re)transmit |
1217 | tstamp next = NOW + poll_interval; |
1221 | tstamp next = ev::now () + poll_interval; |
1218 | dns_snd *send = 0; |
1222 | dns_snd *send = 0; |
1219 | |
1223 | |
1220 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1224 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1221 | i != vpn->dns_sndpq.end (); |
1225 | i != vpn->dns_sndpq.end (); |
1222 | ++i) |
1226 | ++i) |
1223 | { |
1227 | { |
1224 | dns_snd *r = *i; |
1228 | dns_snd *r = *i; |
1225 | |
1229 | |
1226 | if (r->timeout <= NOW) |
1230 | if (r->timeout <= ev_now ()) |
1227 | { |
1231 | { |
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 = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1238 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1234 | |
1239 | |
1235 | // the following code changes the query section a bit, forcing |
1240 | // the following code changes the query section a bit, forcing |
1236 | // the forwarder to generate a new request |
1241 | // the forwarder to generate a new request |
1237 | if (r->stdhdr) |
1242 | if (r->stdhdr) |
1238 | { |
|
|
1239 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1240 | //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); |
1241 | } |
|
|
1242 | } |
1244 | } |
1243 | } |
1245 | } |
1244 | else |
1246 | else |
1245 | NEXT (r->timeout); |
1247 | NEXT (r->timeout); |
1246 | } |
1248 | } |
… | |
… | |
1257 | |
1259 | |
1258 | cfg.reset (THISNODE->id); |
1260 | cfg.reset (THISNODE->id); |
1259 | send->gen_syn_req (); |
1261 | send->gen_syn_req (); |
1260 | } |
1262 | } |
1261 | } |
1263 | } |
1262 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1264 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1263 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1265 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1264 | { |
1266 | { |
1265 | if (last_sent + send_interval <= NOW) |
1267 | if (last_sent + send_interval <= ev_now ()) |
1266 | { |
1268 | { |
1267 | //printf ("sending data request etc.\n"); //D |
1269 | //printf ("sending data request etc.\n"); //D |
1268 | if (!snddq.empty () || last_received + 1. > NOW) |
1270 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1269 | { |
1271 | { |
1270 | poll_interval = send_interval; |
1272 | poll_interval = send_interval; |
1271 | NEXT (NOW + send_interval); |
1273 | NEXT (ev_now () + send_interval); |
1272 | } |
1274 | } |
1273 | |
1275 | |
1274 | send = new dns_snd (this); |
1276 | send = new dns_snd (this); |
1275 | send->gen_stream_req (sndseq, snddq); |
1277 | send->gen_stream_req (sndseq, snddq); |
1276 | send->timeout = NOW + min_latency * TIMEOUT_FACTOR; |
1278 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1279 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1277 | |
1280 | |
1278 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1281 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1279 | } |
1282 | } |
1280 | else |
1283 | else |
1281 | NEXT (last_sent + send_interval); |
1284 | NEXT (last_sent + send_interval); |
… | |
… | |
1285 | vpn->dns_sndpq.push_back (send); |
1288 | vpn->dns_sndpq.push_back (send); |
1286 | } |
1289 | } |
1287 | |
1290 | |
1288 | if (send) |
1291 | if (send) |
1289 | { |
1292 | { |
1290 | last_sent = NOW; |
1293 | last_sent = ev_now (); |
1291 | if (rand () & 15 != 0)//D |
|
|
1292 | sendto (vpn->dnsv4_fd, |
1294 | sendto (vpn->dnsv4_fd, |
1293 | send->pkt->at (0), send->pkt->len, 0, |
1295 | send->pkt->at (0), send->pkt->len, 0, |
1294 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1296 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1295 | } |
1297 | } |
1296 | |
1298 | |
1297 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1299 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1298 | poll_interval, send_interval, next - NOW, |
1300 | poll_interval, send_interval, next - ev_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 < ev_now (), we have a problem |
|
|
1305 | // doesn't seem to happen anymore |
1303 | if (next < NOW + 0.001) |
1306 | if (next < ev_now () + 0.001) |
1304 | next = NOW + 0.1; |
1307 | next = ev_now () + 0.1; |
1305 | |
1308 | |
1306 | w.start (next); |
1309 | w.start (next - ev_now ()); |
1307 | } |
1310 | } |
1308 | |
1311 | |
1309 | #endif |
1312 | #endif |
1310 | |
1313 | |