… | |
… | |
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 | |
… | |
… | |
38 | #include <unistd.h> |
42 | #include <unistd.h> |
39 | #include <fcntl.h> |
43 | #include <fcntl.h> |
40 | |
44 | |
41 | #include <map> |
45 | #include <map> |
42 | |
46 | |
|
|
47 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
43 | #include <gmp.h> |
48 | #include <gmp.h> |
44 | |
49 | |
45 | #include "netcompat.h" |
50 | #include "netcompat.h" |
46 | |
51 | |
47 | #include "vpn.h" |
52 | #include "vpn.h" |
… | |
… | |
457 | r4 = r5 = r6 = r7 = 0; |
462 | r4 = r5 = r6 = r7 = 0; |
458 | } |
463 | } |
459 | |
464 | |
460 | bool dns_cfg::valid () |
465 | bool dns_cfg::valid () |
461 | { |
466 | { |
|
|
467 | // although the protocol itself allows for some configurability, |
|
|
468 | // only the following encoding/decoding settings are implemented. |
462 | return id1 == 'G' |
469 | return id1 == 'G' |
463 | && id2 == 'V' |
470 | && id2 == 'V' |
464 | && id3 == 'P' |
471 | && id3 == 'P' |
465 | && id4 == 'E' |
472 | && id4 == 'E' |
466 | && seq_cdc == 26 |
473 | && seq_cdc == 26 |
… | |
… | |
553 | vector<dns_rcv *> rcvpq; |
560 | vector<dns_rcv *> rcvpq; |
554 | |
561 | |
555 | byte_stream rcvdq; int rcvseq; int repseq; |
562 | byte_stream rcvdq; int rcvseq; int repseq; |
556 | byte_stream snddq; int sndseq; |
563 | byte_stream snddq; int sndseq; |
557 | |
564 | |
558 | void time_cb (time_watcher &w); time_watcher tw; |
565 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
559 | void receive_rep (dns_rcv *r); |
566 | void receive_rep (dns_rcv *r); |
560 | |
567 | |
561 | dns_connection (connection *c); |
568 | dns_connection (connection *c); |
562 | ~dns_connection (); |
569 | ~dns_connection (); |
563 | }; |
570 | }; |
… | |
… | |
582 | : dns (dns) |
589 | : dns (dns) |
583 | { |
590 | { |
584 | timeout = 0; |
591 | timeout = 0; |
585 | retry = 0; |
592 | retry = 0; |
586 | seqno = 0; |
593 | seqno = 0; |
587 | sent = NOW; |
594 | sent = ev_now (); |
588 | stdhdr = false; |
595 | stdhdr = false; |
589 | |
596 | |
590 | pkt = new dns_packet; |
597 | pkt = new dns_packet; |
591 | |
598 | |
592 | pkt->id = next_id (); |
599 | pkt->id = next_id (); |
… | |
… | |
623 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
624 | { |
631 | { |
625 | stdhdr = true; |
632 | stdhdr = true; |
626 | this->seqno = seqno; |
633 | this->seqno = seqno; |
627 | |
634 | |
628 | timeout = NOW + INITIAL_TIMEOUT; |
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
629 | |
636 | |
630 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
631 | pkt->qdcount = htons (1); |
638 | pkt->qdcount = htons (1); |
632 | |
639 | |
633 | int offs = 6*2; |
640 | int offs = 6*2; |
… | |
… | |
668 | pkt->len = offs; |
675 | pkt->len = offs; |
669 | } |
676 | } |
670 | |
677 | |
671 | void dns_snd::gen_syn_req () |
678 | void dns_snd::gen_syn_req () |
672 | { |
679 | { |
673 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
674 | |
681 | |
675 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
676 | pkt->qdcount = htons (1); |
683 | pkt->qdcount = htons (1); |
677 | |
684 | |
678 | int offs = 6 * 2; |
685 | int offs = 6 * 2; |
… | |
… | |
718 | |
725 | |
719 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
720 | : c (c) |
727 | : c (c) |
721 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
722 | , snddq (MAX_BACKLOG) |
729 | , snddq (MAX_BACKLOG) |
723 | , tw (this, &dns_connection::time_cb) |
|
|
724 | { |
730 | { |
|
|
731 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
732 | |
725 | vpn = c->vpn; |
733 | vpn = c->vpn; |
726 | |
734 | |
727 | established = false; |
735 | established = false; |
728 | |
736 | |
729 | rcvseq = repseq = sndseq = 0; |
737 | rcvseq = repseq = sndseq = 0; |
… | |
… | |
744 | |
752 | |
745 | void dns_connection::receive_rep (dns_rcv *r) |
753 | void dns_connection::receive_rep (dns_rcv *r) |
746 | { |
754 | { |
747 | if (r->datalen) |
755 | if (r->datalen) |
748 | { |
756 | { |
749 | last_received = NOW; |
757 | last_received = ev_now (); |
750 | tw.trigger (); |
758 | tw (); |
751 | |
759 | |
752 | poll_interval = send_interval; |
760 | poll_interval = send_interval; |
753 | } |
761 | } |
754 | else |
762 | else |
755 | { |
763 | { |
… | |
… | |
765 | |
773 | |
766 | // find next packet |
774 | // find next packet |
767 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
775 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
768 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
776 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
769 | { |
777 | { |
|
|
778 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
770 | // enter the packet into our input stream |
779 | // enter the packet into our input stream |
771 | r = *i; |
780 | r = *i; |
772 | |
781 | |
773 | // remove the oldest packet, look forward, as it's oldest first |
782 | // remove the oldest packet, look forward, as it's oldest first |
774 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
783 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
775 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
784 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
776 | { |
785 | { |
|
|
786 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
777 | delete *j; |
787 | delete *j; |
778 | rcvpq.erase (j); |
788 | rcvpq.erase (j); |
779 | break; |
789 | break; |
780 | } |
790 | } |
781 | |
791 | |
… | |
… | |
1009 | { |
1019 | { |
1010 | dns_connection *dns = (*i)->dns; |
1020 | dns_connection *dns = (*i)->dns; |
1011 | connection *c = dns->c; |
1021 | connection *c = dns->c; |
1012 | int seqno = (*i)->seqno; |
1022 | int seqno = (*i)->seqno; |
1013 | u8 data[MAXSIZE], *datap = data; |
1023 | u8 data[MAXSIZE], *datap = data; |
|
|
1024 | //printf ("rcv pkt %x\n", seqno);//D |
1014 | |
1025 | |
1015 | if ((*i)->retry) |
1026 | if ((*i)->retry) |
1016 | { |
1027 | { |
1017 | dns->send_interval *= 1.01; |
1028 | dns->send_interval *= 1.01; |
1018 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1029 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1023 | #if 0 |
1034 | #if 0 |
1024 | dns->send_interval *= 0.999; |
1035 | dns->send_interval *= 0.999; |
1025 | #endif |
1036 | #endif |
1026 | // the latency surely puts an upper bound on |
1037 | // the latency surely puts an upper bound on |
1027 | // the minimum send interval |
1038 | // the minimum send interval |
1028 | double latency = NOW - (*i)->sent; |
1039 | double latency = ev_now () - (*i)->sent; |
1029 | |
1040 | |
1030 | if (latency < dns->min_latency) |
1041 | if (latency < dns->min_latency) |
1031 | dns->min_latency = latency; |
1042 | dns->min_latency = latency; |
1032 | |
1043 | |
1033 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1044 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
… | |
… | |
1147 | break; |
1158 | break; |
1148 | } |
1159 | } |
1149 | } |
1160 | } |
1150 | |
1161 | |
1151 | void |
1162 | void |
1152 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1163 | vpn::dnsv4_ev (ev::io &w, int revents) |
1153 | { |
1164 | { |
1154 | if (revents & EVENT_READ) |
1165 | if (revents & EV_READ) |
1155 | { |
1166 | { |
1156 | dns_packet *pkt = new dns_packet; |
1167 | dns_packet *pkt = new dns_packet; |
1157 | struct sockaddr_in sa; |
1168 | struct sockaddr_in sa; |
1158 | socklen_t sa_len = sizeof (sa); |
1169 | socklen_t sa_len = sizeof (sa); |
1159 | |
1170 | |
… | |
… | |
1185 | |
1196 | |
1186 | if (!c->dns) |
1197 | if (!c->dns) |
1187 | c->dns = new dns_connection (c); |
1198 | c->dns = new dns_connection (c); |
1188 | |
1199 | |
1189 | if (c->dns->snddq.put (pkt)) |
1200 | if (c->dns->snddq.put (pkt)) |
1190 | c->dns->tw.trigger (); |
1201 | c->dns->tw (); |
1191 | |
1202 | |
1192 | // always return true even if the buffer overflows |
1203 | // always return true even if the buffer overflows |
1193 | return true; |
1204 | return true; |
1194 | } |
1205 | } |
1195 | |
1206 | |
… | |
… | |
1200 | } |
1211 | } |
1201 | |
1212 | |
1202 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1213 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1203 | |
1214 | |
1204 | void |
1215 | void |
1205 | dns_connection::time_cb (time_watcher &w) |
1216 | dns_connection::time_cb (ev::timer &w, int revents) |
1206 | { |
1217 | { |
1207 | // servers have to be polled |
1218 | // servers have to be polled |
1208 | if (THISNODE->dns_port) |
1219 | if (THISNODE->dns_port) |
1209 | return; |
1220 | return; |
1210 | |
1221 | |
1211 | // check for timeouts and (re)transmit |
1222 | // check for timeouts and (re)transmit |
1212 | tstamp next = NOW + poll_interval; |
1223 | tstamp next = ev::now () + poll_interval; |
1213 | dns_snd *send = 0; |
1224 | dns_snd *send = 0; |
1214 | |
1225 | |
1215 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1226 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1216 | i != vpn->dns_sndpq.end (); |
1227 | i != vpn->dns_sndpq.end (); |
1217 | ++i) |
1228 | ++i) |
1218 | { |
1229 | { |
1219 | dns_snd *r = *i; |
1230 | dns_snd *r = *i; |
1220 | |
1231 | |
1221 | if (r->timeout <= NOW) |
1232 | if (r->timeout <= ev_now ()) |
1222 | { |
1233 | { |
1223 | if (!send) |
1234 | if (!send) |
1224 | { |
1235 | { |
1225 | send = r; |
1236 | send = r; |
1226 | |
1237 | |
1227 | r->retry++; |
1238 | r->retry++; |
1228 | r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor); |
1239 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1240 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1229 | |
1241 | |
1230 | // the following code changes the query section a bit, forcing |
1242 | // the following code changes the query section a bit, forcing |
1231 | // the forwarder to generate a new request |
1243 | // the forwarder to generate a new request |
1232 | if (r->stdhdr) |
1244 | if (r->stdhdr) |
1233 | { |
|
|
1234 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1235 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1245 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1236 | } |
|
|
1237 | } |
1246 | } |
1238 | } |
1247 | } |
1239 | else |
1248 | else |
1240 | NEXT (r->timeout); |
1249 | NEXT (r->timeout); |
1241 | } |
1250 | } |
… | |
… | |
1255 | } |
1264 | } |
1256 | } |
1265 | } |
1257 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1266 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1258 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1267 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1259 | { |
1268 | { |
1260 | if (last_sent + send_interval <= NOW) |
1269 | if (last_sent + send_interval <= ev_now ()) |
1261 | { |
1270 | { |
1262 | //printf ("sending data request etc.\n"); //D |
1271 | //printf ("sending data request etc.\n"); //D |
1263 | if (!snddq.empty () || last_received + 1. > NOW) |
1272 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1264 | { |
1273 | { |
1265 | poll_interval = send_interval; |
1274 | poll_interval = send_interval; |
1266 | NEXT (NOW + send_interval); |
1275 | NEXT (ev_now () + send_interval); |
1267 | } |
1276 | } |
1268 | |
1277 | |
1269 | send = new dns_snd (this); |
1278 | send = new dns_snd (this); |
1270 | send->gen_stream_req (sndseq, snddq); |
1279 | send->gen_stream_req (sndseq, snddq); |
1271 | send->timeout = NOW + min_latency * conf.dns_timeout_factor; |
1280 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1281 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1272 | |
1282 | |
1273 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1283 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1274 | } |
1284 | } |
1275 | else |
1285 | else |
1276 | NEXT (last_sent + send_interval); |
1286 | NEXT (last_sent + send_interval); |
… | |
… | |
1280 | vpn->dns_sndpq.push_back (send); |
1290 | vpn->dns_sndpq.push_back (send); |
1281 | } |
1291 | } |
1282 | |
1292 | |
1283 | if (send) |
1293 | if (send) |
1284 | { |
1294 | { |
1285 | last_sent = NOW; |
1295 | last_sent = ev_now (); |
1286 | sendto (vpn->dnsv4_fd, |
1296 | sendto (vpn->dnsv4_fd, |
1287 | send->pkt->at (0), send->pkt->len, 0, |
1297 | send->pkt->at (0), send->pkt->len, 0, |
1288 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1298 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1289 | } |
1299 | } |
1290 | |
1300 | |
1291 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1301 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1292 | poll_interval, send_interval, next - NOW, |
1302 | poll_interval, send_interval, next - ev_now (), |
1293 | vpn->dns_sndpq.size (), snddq.size (), |
1303 | vpn->dns_sndpq.size (), snddq.size (), |
1294 | rcvpq.size ()); |
1304 | rcvpq.size ()); |
1295 | |
1305 | |
1296 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1306 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
1297 | // doesn't seem to happen anymore |
1307 | // doesn't seem to happen anymore |
1298 | if (next < NOW + 0.001) |
1308 | if (next < ev_now () + 0.001) |
1299 | next = NOW + 0.1; |
1309 | next = ev_now () + 0.1; |
1300 | |
1310 | |
1301 | w.start (next); |
1311 | w.start (next - ev_now ()); |
1302 | } |
1312 | } |
1303 | |
1313 | |
1304 | #endif |
1314 | #endif |
1305 | |
1315 | |