… | |
… | |
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 | |
21 | |
22 | // TODO: EDNS0 option to increase dns mtu? |
22 | // TODO: EDNS0 option to increase dns mtu? |
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
24 | // to ensure no buffer overflows or similar problems. |
24 | // to ensure no buffer overflows or similar problems. |
… | |
… | |
461 | r4 = r5 = r6 = r7 = 0; |
461 | r4 = r5 = r6 = r7 = 0; |
462 | } |
462 | } |
463 | |
463 | |
464 | bool dns_cfg::valid () |
464 | bool dns_cfg::valid () |
465 | { |
465 | { |
|
|
466 | // although the protocol itself allows for some configurability, |
|
|
467 | // only the following encoding/decoding settings are implemented. |
466 | return id1 == 'G' |
468 | return id1 == 'G' |
467 | && id2 == 'V' |
469 | && id2 == 'V' |
468 | && id3 == 'P' |
470 | && id3 == 'P' |
469 | && id4 == 'E' |
471 | && id4 == 'E' |
470 | && seq_cdc == 26 |
472 | && seq_cdc == 26 |
… | |
… | |
557 | vector<dns_rcv *> rcvpq; |
559 | vector<dns_rcv *> rcvpq; |
558 | |
560 | |
559 | byte_stream rcvdq; int rcvseq; int repseq; |
561 | byte_stream rcvdq; int rcvseq; int repseq; |
560 | byte_stream snddq; int sndseq; |
562 | byte_stream snddq; int sndseq; |
561 | |
563 | |
562 | void time_cb (time_watcher &w); time_watcher tw; |
564 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
563 | void receive_rep (dns_rcv *r); |
565 | void receive_rep (dns_rcv *r); |
564 | |
566 | |
565 | dns_connection (connection *c); |
567 | dns_connection (connection *c); |
566 | ~dns_connection (); |
568 | ~dns_connection (); |
567 | }; |
569 | }; |
… | |
… | |
586 | : dns (dns) |
588 | : dns (dns) |
587 | { |
589 | { |
588 | timeout = 0; |
590 | timeout = 0; |
589 | retry = 0; |
591 | retry = 0; |
590 | seqno = 0; |
592 | seqno = 0; |
591 | sent = NOW; |
593 | sent = ev_now (); |
592 | stdhdr = false; |
594 | stdhdr = false; |
593 | |
595 | |
594 | pkt = new dns_packet; |
596 | pkt = new dns_packet; |
595 | |
597 | |
596 | pkt->id = next_id (); |
598 | pkt->id = next_id (); |
… | |
… | |
627 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
629 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
628 | { |
630 | { |
629 | stdhdr = true; |
631 | stdhdr = true; |
630 | this->seqno = seqno; |
632 | this->seqno = seqno; |
631 | |
633 | |
632 | timeout = NOW + INITIAL_TIMEOUT; |
634 | timeout = ev_now () + INITIAL_TIMEOUT; |
633 | |
635 | |
634 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
636 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
635 | pkt->qdcount = htons (1); |
637 | pkt->qdcount = htons (1); |
636 | |
638 | |
637 | int offs = 6*2; |
639 | int offs = 6*2; |
… | |
… | |
672 | pkt->len = offs; |
674 | pkt->len = offs; |
673 | } |
675 | } |
674 | |
676 | |
675 | void dns_snd::gen_syn_req () |
677 | void dns_snd::gen_syn_req () |
676 | { |
678 | { |
677 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
679 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
678 | |
680 | |
679 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
681 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
680 | pkt->qdcount = htons (1); |
682 | pkt->qdcount = htons (1); |
681 | |
683 | |
682 | int offs = 6 * 2; |
684 | int offs = 6 * 2; |
… | |
… | |
748 | |
750 | |
749 | void dns_connection::receive_rep (dns_rcv *r) |
751 | void dns_connection::receive_rep (dns_rcv *r) |
750 | { |
752 | { |
751 | if (r->datalen) |
753 | if (r->datalen) |
752 | { |
754 | { |
753 | last_received = NOW; |
755 | last_received = ev_now (); |
754 | tw.trigger (); |
756 | tw (); |
755 | |
757 | |
756 | poll_interval = send_interval; |
758 | poll_interval = send_interval; |
757 | } |
759 | } |
758 | else |
760 | else |
759 | { |
761 | { |
… | |
… | |
769 | |
771 | |
770 | // find next packet |
772 | // find next packet |
771 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
773 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
772 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
774 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
773 | { |
775 | { |
|
|
776 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
774 | // enter the packet into our input stream |
777 | // enter the packet into our input stream |
775 | r = *i; |
778 | r = *i; |
776 | |
779 | |
777 | // remove the oldest packet, look forward, as it's oldest first |
780 | // remove the oldest packet, look forward, as it's oldest first |
778 | 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) |
779 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
782 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
780 | { |
783 | { |
|
|
784 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
781 | delete *j; |
785 | delete *j; |
782 | rcvpq.erase (j); |
786 | rcvpq.erase (j); |
783 | break; |
787 | break; |
784 | } |
788 | } |
785 | |
789 | |
… | |
… | |
1013 | { |
1017 | { |
1014 | dns_connection *dns = (*i)->dns; |
1018 | dns_connection *dns = (*i)->dns; |
1015 | connection *c = dns->c; |
1019 | connection *c = dns->c; |
1016 | int seqno = (*i)->seqno; |
1020 | int seqno = (*i)->seqno; |
1017 | u8 data[MAXSIZE], *datap = data; |
1021 | u8 data[MAXSIZE], *datap = data; |
|
|
1022 | //printf ("rcv pkt %x\n", seqno);//D |
1018 | |
1023 | |
1019 | if ((*i)->retry) |
1024 | if ((*i)->retry) |
1020 | { |
1025 | { |
1021 | dns->send_interval *= 1.01; |
1026 | dns->send_interval *= 1.01; |
1022 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1027 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1027 | #if 0 |
1032 | #if 0 |
1028 | dns->send_interval *= 0.999; |
1033 | dns->send_interval *= 0.999; |
1029 | #endif |
1034 | #endif |
1030 | // the latency surely puts an upper bound on |
1035 | // the latency surely puts an upper bound on |
1031 | // the minimum send interval |
1036 | // the minimum send interval |
1032 | double latency = NOW - (*i)->sent; |
1037 | double latency = ev_now () - (*i)->sent; |
1033 | |
1038 | |
1034 | if (latency < dns->min_latency) |
1039 | if (latency < dns->min_latency) |
1035 | dns->min_latency = latency; |
1040 | dns->min_latency = latency; |
1036 | |
1041 | |
1037 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1042 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
… | |
… | |
1151 | break; |
1156 | break; |
1152 | } |
1157 | } |
1153 | } |
1158 | } |
1154 | |
1159 | |
1155 | void |
1160 | void |
1156 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1161 | vpn::dnsv4_ev (ev::io &w, int revents) |
1157 | { |
1162 | { |
1158 | if (revents & EVENT_READ) |
1163 | if (revents & EV_READ) |
1159 | { |
1164 | { |
1160 | dns_packet *pkt = new dns_packet; |
1165 | dns_packet *pkt = new dns_packet; |
1161 | struct sockaddr_in sa; |
1166 | struct sockaddr_in sa; |
1162 | socklen_t sa_len = sizeof (sa); |
1167 | socklen_t sa_len = sizeof (sa); |
1163 | |
1168 | |
… | |
… | |
1189 | |
1194 | |
1190 | if (!c->dns) |
1195 | if (!c->dns) |
1191 | c->dns = new dns_connection (c); |
1196 | c->dns = new dns_connection (c); |
1192 | |
1197 | |
1193 | if (c->dns->snddq.put (pkt)) |
1198 | if (c->dns->snddq.put (pkt)) |
1194 | c->dns->tw.trigger (); |
1199 | c->dns->tw (); |
1195 | |
1200 | |
1196 | // always return true even if the buffer overflows |
1201 | // always return true even if the buffer overflows |
1197 | return true; |
1202 | return true; |
1198 | } |
1203 | } |
1199 | |
1204 | |
… | |
… | |
1204 | } |
1209 | } |
1205 | |
1210 | |
1206 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1211 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1207 | |
1212 | |
1208 | void |
1213 | void |
1209 | dns_connection::time_cb (time_watcher &w) |
1214 | dns_connection::time_cb (ev::timer &w, int revents) |
1210 | { |
1215 | { |
1211 | // servers have to be polled |
1216 | // servers have to be polled |
1212 | if (THISNODE->dns_port) |
1217 | if (THISNODE->dns_port) |
1213 | return; |
1218 | return; |
1214 | |
1219 | |
1215 | // check for timeouts and (re)transmit |
1220 | // check for timeouts and (re)transmit |
1216 | tstamp next = NOW + poll_interval; |
1221 | tstamp next = ev::now () + poll_interval; |
1217 | dns_snd *send = 0; |
1222 | dns_snd *send = 0; |
1218 | |
1223 | |
1219 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1224 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1220 | i != vpn->dns_sndpq.end (); |
1225 | i != vpn->dns_sndpq.end (); |
1221 | ++i) |
1226 | ++i) |
1222 | { |
1227 | { |
1223 | dns_snd *r = *i; |
1228 | dns_snd *r = *i; |
1224 | |
1229 | |
1225 | if (r->timeout <= NOW) |
1230 | if (r->timeout <= ev_now ()) |
1226 | { |
1231 | { |
1227 | if (!send) |
1232 | if (!send) |
1228 | { |
1233 | { |
1229 | send = r; |
1234 | send = r; |
1230 | |
1235 | |
1231 | r->retry++; |
1236 | r->retry++; |
1232 | r->timeout = NOW + (r->retry * min_latency * conf.dns_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 |
1233 | |
1239 | |
1234 | // the following code changes the query section a bit, forcing |
1240 | // the following code changes the query section a bit, forcing |
1235 | // the forwarder to generate a new request |
1241 | // the forwarder to generate a new request |
1236 | if (r->stdhdr) |
1242 | if (r->stdhdr) |
1237 | { |
|
|
1238 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1239 | //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); |
1240 | } |
|
|
1241 | } |
1244 | } |
1242 | } |
1245 | } |
1243 | else |
1246 | else |
1244 | NEXT (r->timeout); |
1247 | NEXT (r->timeout); |
1245 | } |
1248 | } |
… | |
… | |
1259 | } |
1262 | } |
1260 | } |
1263 | } |
1261 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1264 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1262 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1265 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1263 | { |
1266 | { |
1264 | if (last_sent + send_interval <= NOW) |
1267 | if (last_sent + send_interval <= ev_now ()) |
1265 | { |
1268 | { |
1266 | //printf ("sending data request etc.\n"); //D |
1269 | //printf ("sending data request etc.\n"); //D |
1267 | if (!snddq.empty () || last_received + 1. > NOW) |
1270 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1268 | { |
1271 | { |
1269 | poll_interval = send_interval; |
1272 | poll_interval = send_interval; |
1270 | NEXT (NOW + send_interval); |
1273 | NEXT (ev_now () + send_interval); |
1271 | } |
1274 | } |
1272 | |
1275 | |
1273 | send = new dns_snd (this); |
1276 | send = new dns_snd (this); |
1274 | send->gen_stream_req (sndseq, snddq); |
1277 | send->gen_stream_req (sndseq, snddq); |
1275 | send->timeout = NOW + min_latency * conf.dns_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 |
1276 | |
1280 | |
1277 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1281 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1278 | } |
1282 | } |
1279 | else |
1283 | else |
1280 | NEXT (last_sent + send_interval); |
1284 | NEXT (last_sent + send_interval); |
… | |
… | |
1284 | vpn->dns_sndpq.push_back (send); |
1288 | vpn->dns_sndpq.push_back (send); |
1285 | } |
1289 | } |
1286 | |
1290 | |
1287 | if (send) |
1291 | if (send) |
1288 | { |
1292 | { |
1289 | last_sent = NOW; |
1293 | last_sent = ev_now (); |
1290 | sendto (vpn->dnsv4_fd, |
1294 | sendto (vpn->dnsv4_fd, |
1291 | send->pkt->at (0), send->pkt->len, 0, |
1295 | send->pkt->at (0), send->pkt->len, 0, |
1292 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1296 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1293 | } |
1297 | } |
1294 | |
1298 | |
1295 | 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)", |
1296 | poll_interval, send_interval, next - NOW, |
1300 | poll_interval, send_interval, next - ev_now (), |
1297 | vpn->dns_sndpq.size (), snddq.size (), |
1301 | vpn->dns_sndpq.size (), snddq.size (), |
1298 | rcvpq.size ()); |
1302 | rcvpq.size ()); |
1299 | |
1303 | |
1300 | // 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 |
1301 | // doesn't seem to happen anymore |
1305 | // doesn't seem to happen anymore |
1302 | if (next < NOW + 0.001) |
1306 | if (next < ev_now () + 0.001) |
1303 | next = NOW + 0.1; |
1307 | next = ev_now () + 0.1; |
1304 | |
1308 | |
1305 | w.start (next); |
1309 | w.start (next - ev_now ()); |
1306 | } |
1310 | } |
1307 | |
1311 | |
1308 | #endif |
1312 | #endif |
1309 | |
1313 | |