… | |
… | |
559 | vector<dns_rcv *> rcvpq; |
559 | vector<dns_rcv *> rcvpq; |
560 | |
560 | |
561 | byte_stream rcvdq; int rcvseq; int repseq; |
561 | byte_stream rcvdq; int rcvseq; int repseq; |
562 | byte_stream snddq; int sndseq; |
562 | byte_stream snddq; int sndseq; |
563 | |
563 | |
564 | void time_cb (time_watcher &w); time_watcher tw; |
564 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
565 | void receive_rep (dns_rcv *r); |
565 | void receive_rep (dns_rcv *r); |
566 | |
566 | |
567 | dns_connection (connection *c); |
567 | dns_connection (connection *c); |
568 | ~dns_connection (); |
568 | ~dns_connection (); |
569 | }; |
569 | }; |
… | |
… | |
588 | : dns (dns) |
588 | : dns (dns) |
589 | { |
589 | { |
590 | timeout = 0; |
590 | timeout = 0; |
591 | retry = 0; |
591 | retry = 0; |
592 | seqno = 0; |
592 | seqno = 0; |
593 | sent = NOW; |
593 | sent = ev::ev_now (); |
594 | stdhdr = false; |
594 | stdhdr = false; |
595 | |
595 | |
596 | pkt = new dns_packet; |
596 | pkt = new dns_packet; |
597 | |
597 | |
598 | pkt->id = next_id (); |
598 | pkt->id = next_id (); |
… | |
… | |
629 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
629 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | { |
630 | { |
631 | stdhdr = true; |
631 | stdhdr = true; |
632 | this->seqno = seqno; |
632 | this->seqno = seqno; |
633 | |
633 | |
634 | timeout = NOW + INITIAL_TIMEOUT; |
634 | timeout = ev::ev_now () + INITIAL_TIMEOUT; |
635 | |
635 | |
636 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
636 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->qdcount = htons (1); |
637 | pkt->qdcount = htons (1); |
638 | |
638 | |
639 | int offs = 6*2; |
639 | int offs = 6*2; |
… | |
… | |
674 | pkt->len = offs; |
674 | pkt->len = offs; |
675 | } |
675 | } |
676 | |
676 | |
677 | void dns_snd::gen_syn_req () |
677 | void dns_snd::gen_syn_req () |
678 | { |
678 | { |
679 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
679 | timeout = ev::ev_now () + INITIAL_SYN_TIMEOUT; |
680 | |
680 | |
681 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
681 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->qdcount = htons (1); |
682 | pkt->qdcount = htons (1); |
683 | |
683 | |
684 | int offs = 6 * 2; |
684 | int offs = 6 * 2; |
… | |
… | |
750 | |
750 | |
751 | void dns_connection::receive_rep (dns_rcv *r) |
751 | void dns_connection::receive_rep (dns_rcv *r) |
752 | { |
752 | { |
753 | if (r->datalen) |
753 | if (r->datalen) |
754 | { |
754 | { |
755 | last_received = NOW; |
755 | last_received = ev::ev_now (); |
756 | tw.trigger (); |
756 | tw (); |
757 | |
757 | |
758 | poll_interval = send_interval; |
758 | poll_interval = send_interval; |
759 | } |
759 | } |
760 | else |
760 | else |
761 | { |
761 | { |
… | |
… | |
771 | |
771 | |
772 | // find next packet |
772 | // find next packet |
773 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
773 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
774 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
774 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
775 | { |
775 | { |
|
|
776 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
776 | // enter the packet into our input stream |
777 | // enter the packet into our input stream |
777 | r = *i; |
778 | r = *i; |
778 | |
779 | |
779 | // remove the oldest packet, look forward, as it's oldest first |
780 | // remove the oldest packet, look forward, as it's oldest first |
780 | 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) |
781 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
782 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
782 | { |
783 | { |
|
|
784 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
783 | delete *j; |
785 | delete *j; |
784 | rcvpq.erase (j); |
786 | rcvpq.erase (j); |
785 | break; |
787 | break; |
786 | } |
788 | } |
787 | |
789 | |
… | |
… | |
1015 | { |
1017 | { |
1016 | dns_connection *dns = (*i)->dns; |
1018 | dns_connection *dns = (*i)->dns; |
1017 | connection *c = dns->c; |
1019 | connection *c = dns->c; |
1018 | int seqno = (*i)->seqno; |
1020 | int seqno = (*i)->seqno; |
1019 | u8 data[MAXSIZE], *datap = data; |
1021 | u8 data[MAXSIZE], *datap = data; |
|
|
1022 | //printf ("rcv pkt %x\n", seqno);//D |
1020 | |
1023 | |
1021 | if ((*i)->retry) |
1024 | if ((*i)->retry) |
1022 | { |
1025 | { |
1023 | dns->send_interval *= 1.01; |
1026 | dns->send_interval *= 1.01; |
1024 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1027 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1029 | #if 0 |
1032 | #if 0 |
1030 | dns->send_interval *= 0.999; |
1033 | dns->send_interval *= 0.999; |
1031 | #endif |
1034 | #endif |
1032 | // the latency surely puts an upper bound on |
1035 | // the latency surely puts an upper bound on |
1033 | // the minimum send interval |
1036 | // the minimum send interval |
1034 | double latency = NOW - (*i)->sent; |
1037 | double latency = ev::ev_now () - (*i)->sent; |
1035 | |
1038 | |
1036 | if (latency < dns->min_latency) |
1039 | if (latency < dns->min_latency) |
1037 | dns->min_latency = latency; |
1040 | dns->min_latency = latency; |
1038 | |
1041 | |
1039 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1042 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
… | |
… | |
1153 | break; |
1156 | break; |
1154 | } |
1157 | } |
1155 | } |
1158 | } |
1156 | |
1159 | |
1157 | void |
1160 | void |
1158 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1161 | vpn::dnsv4_ev (ev::io &w, int revents) |
1159 | { |
1162 | { |
1160 | if (revents & EVENT_READ) |
1163 | if (revents & EV_READ) |
1161 | { |
1164 | { |
1162 | dns_packet *pkt = new dns_packet; |
1165 | dns_packet *pkt = new dns_packet; |
1163 | struct sockaddr_in sa; |
1166 | struct sockaddr_in sa; |
1164 | socklen_t sa_len = sizeof (sa); |
1167 | socklen_t sa_len = sizeof (sa); |
1165 | |
1168 | |
… | |
… | |
1191 | |
1194 | |
1192 | if (!c->dns) |
1195 | if (!c->dns) |
1193 | c->dns = new dns_connection (c); |
1196 | c->dns = new dns_connection (c); |
1194 | |
1197 | |
1195 | if (c->dns->snddq.put (pkt)) |
1198 | if (c->dns->snddq.put (pkt)) |
1196 | c->dns->tw.trigger (); |
1199 | c->dns->tw (); |
1197 | |
1200 | |
1198 | // always return true even if the buffer overflows |
1201 | // always return true even if the buffer overflows |
1199 | return true; |
1202 | return true; |
1200 | } |
1203 | } |
1201 | |
1204 | |
… | |
… | |
1206 | } |
1209 | } |
1207 | |
1210 | |
1208 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1211 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1209 | |
1212 | |
1210 | void |
1213 | void |
1211 | dns_connection::time_cb (time_watcher &w) |
1214 | dns_connection::time_cb (ev::timer &w, int revents) |
1212 | { |
1215 | { |
1213 | // servers have to be polled |
1216 | // servers have to be polled |
1214 | if (THISNODE->dns_port) |
1217 | if (THISNODE->dns_port) |
1215 | return; |
1218 | return; |
1216 | |
1219 | |
1217 | // check for timeouts and (re)transmit |
1220 | // check for timeouts and (re)transmit |
1218 | tstamp next = NOW + poll_interval; |
1221 | tstamp next = ev::now () + poll_interval; |
1219 | dns_snd *send = 0; |
1222 | dns_snd *send = 0; |
1220 | |
1223 | |
1221 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1224 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1222 | i != vpn->dns_sndpq.end (); |
1225 | i != vpn->dns_sndpq.end (); |
1223 | ++i) |
1226 | ++i) |
1224 | { |
1227 | { |
1225 | dns_snd *r = *i; |
1228 | dns_snd *r = *i; |
1226 | |
1229 | |
1227 | if (r->timeout <= NOW) |
1230 | if (r->timeout <= ev::ev_now ()) |
1228 | { |
1231 | { |
1229 | if (!send) |
1232 | if (!send) |
1230 | { |
1233 | { |
1231 | send = r; |
1234 | send = r; |
1232 | |
1235 | |
1233 | r->retry++; |
1236 | r->retry++; |
1234 | r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor); |
1237 | r->timeout = ev::ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1238 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev::ev_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 | } |
… | |
… | |
1261 | } |
1262 | } |
1262 | } |
1263 | } |
1263 | else if (vpn->dns_sndpq.size () < conf.dns_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 <= ev::ev_now ()) |
1267 | { |
1268 | { |
1268 | //printf ("sending data request etc.\n"); //D |
1269 | //printf ("sending data request etc.\n"); //D |
1269 | if (!snddq.empty () || last_received + 1. > NOW) |
1270 | if (!snddq.empty () || last_received + 1. > ev::ev_now ()) |
1270 | { |
1271 | { |
1271 | poll_interval = send_interval; |
1272 | poll_interval = send_interval; |
1272 | NEXT (NOW + send_interval); |
1273 | NEXT (ev::ev_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 * conf.dns_timeout_factor; |
1278 | send->timeout = ev::ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1279 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev::ev_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); |
… | |
… | |
1286 | vpn->dns_sndpq.push_back (send); |
1288 | vpn->dns_sndpq.push_back (send); |
1287 | } |
1289 | } |
1288 | |
1290 | |
1289 | if (send) |
1291 | if (send) |
1290 | { |
1292 | { |
1291 | last_sent = NOW; |
1293 | last_sent = ev::ev_now (); |
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::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::ev_now (), we have a problem |
1303 | // doesn't seem to happen anymore |
1305 | // doesn't seem to happen anymore |
1304 | if (next < NOW + 0.001) |
1306 | if (next < ev::ev_now () + 0.001) |
1305 | next = NOW + 0.1; |
1307 | next = ev::ev_now () + 0.1; |
1306 | |
1308 | |
1307 | w.start (next); |
1309 | w.start (next - ev::ev_now ()); |
1308 | } |
1310 | } |
1309 | |
1311 | |
1310 | #endif |
1312 | #endif |
1311 | |
1313 | |