… | |
… | |
42 | #include <unistd.h> |
42 | #include <unistd.h> |
43 | #include <fcntl.h> |
43 | #include <fcntl.h> |
44 | |
44 | |
45 | #include <map> |
45 | #include <map> |
46 | |
46 | |
|
|
47 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
47 | #include <gmp.h> |
48 | #include <gmp.h> |
48 | |
49 | |
49 | #include "netcompat.h" |
50 | #include "netcompat.h" |
50 | |
51 | |
51 | #include "vpn.h" |
52 | #include "vpn.h" |
… | |
… | |
559 | vector<dns_rcv *> rcvpq; |
560 | vector<dns_rcv *> rcvpq; |
560 | |
561 | |
561 | byte_stream rcvdq; int rcvseq; int repseq; |
562 | byte_stream rcvdq; int rcvseq; int repseq; |
562 | byte_stream snddq; int sndseq; |
563 | byte_stream snddq; int sndseq; |
563 | |
564 | |
564 | void time_cb (time_watcher &w); time_watcher tw; |
565 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
565 | void receive_rep (dns_rcv *r); |
566 | void receive_rep (dns_rcv *r); |
566 | |
567 | |
567 | dns_connection (connection *c); |
568 | dns_connection (connection *c); |
568 | ~dns_connection (); |
569 | ~dns_connection (); |
569 | }; |
570 | }; |
… | |
… | |
588 | : dns (dns) |
589 | : dns (dns) |
589 | { |
590 | { |
590 | timeout = 0; |
591 | timeout = 0; |
591 | retry = 0; |
592 | retry = 0; |
592 | seqno = 0; |
593 | seqno = 0; |
593 | sent = NOW; |
594 | sent = ev_now (); |
594 | stdhdr = false; |
595 | stdhdr = false; |
595 | |
596 | |
596 | pkt = new dns_packet; |
597 | pkt = new dns_packet; |
597 | |
598 | |
598 | pkt->id = next_id (); |
599 | pkt->id = next_id (); |
… | |
… | |
629 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | { |
631 | { |
631 | stdhdr = true; |
632 | stdhdr = true; |
632 | this->seqno = seqno; |
633 | this->seqno = seqno; |
633 | |
634 | |
634 | timeout = NOW + INITIAL_TIMEOUT; |
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
635 | |
636 | |
636 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->qdcount = htons (1); |
638 | pkt->qdcount = htons (1); |
638 | |
639 | |
639 | int offs = 6*2; |
640 | int offs = 6*2; |
… | |
… | |
674 | pkt->len = offs; |
675 | pkt->len = offs; |
675 | } |
676 | } |
676 | |
677 | |
677 | void dns_snd::gen_syn_req () |
678 | void dns_snd::gen_syn_req () |
678 | { |
679 | { |
679 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
680 | |
681 | |
681 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->qdcount = htons (1); |
683 | pkt->qdcount = htons (1); |
683 | |
684 | |
684 | int offs = 6 * 2; |
685 | int offs = 6 * 2; |
… | |
… | |
724 | |
725 | |
725 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
726 | : c (c) |
727 | : c (c) |
727 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
728 | , snddq (MAX_BACKLOG) |
729 | , snddq (MAX_BACKLOG) |
729 | , tw (this, &dns_connection::time_cb) |
|
|
730 | { |
730 | { |
|
|
731 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
732 | |
731 | vpn = c->vpn; |
733 | vpn = c->vpn; |
732 | |
734 | |
733 | established = false; |
735 | established = false; |
734 | |
736 | |
735 | rcvseq = repseq = sndseq = 0; |
737 | rcvseq = repseq = sndseq = 0; |
… | |
… | |
750 | |
752 | |
751 | void dns_connection::receive_rep (dns_rcv *r) |
753 | void dns_connection::receive_rep (dns_rcv *r) |
752 | { |
754 | { |
753 | if (r->datalen) |
755 | if (r->datalen) |
754 | { |
756 | { |
755 | last_received = NOW; |
757 | last_received = ev_now (); |
756 | tw.trigger (); |
758 | tw (); |
757 | |
759 | |
758 | poll_interval = send_interval; |
760 | poll_interval = send_interval; |
759 | } |
761 | } |
760 | else |
762 | else |
761 | { |
763 | { |
… | |
… | |
1032 | #if 0 |
1034 | #if 0 |
1033 | dns->send_interval *= 0.999; |
1035 | dns->send_interval *= 0.999; |
1034 | #endif |
1036 | #endif |
1035 | // the latency surely puts an upper bound on |
1037 | // the latency surely puts an upper bound on |
1036 | // the minimum send interval |
1038 | // the minimum send interval |
1037 | double latency = NOW - (*i)->sent; |
1039 | double latency = ev_now () - (*i)->sent; |
1038 | |
1040 | |
1039 | if (latency < dns->min_latency) |
1041 | if (latency < dns->min_latency) |
1040 | dns->min_latency = latency; |
1042 | dns->min_latency = latency; |
1041 | |
1043 | |
1042 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1044 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
… | |
… | |
1156 | break; |
1158 | break; |
1157 | } |
1159 | } |
1158 | } |
1160 | } |
1159 | |
1161 | |
1160 | void |
1162 | void |
1161 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1163 | vpn::dnsv4_ev (ev::io &w, int revents) |
1162 | { |
1164 | { |
1163 | if (revents & EVENT_READ) |
1165 | if (revents & EV_READ) |
1164 | { |
1166 | { |
1165 | dns_packet *pkt = new dns_packet; |
1167 | dns_packet *pkt = new dns_packet; |
1166 | struct sockaddr_in sa; |
1168 | struct sockaddr_in sa; |
1167 | socklen_t sa_len = sizeof (sa); |
1169 | socklen_t sa_len = sizeof (sa); |
1168 | |
1170 | |
… | |
… | |
1194 | |
1196 | |
1195 | if (!c->dns) |
1197 | if (!c->dns) |
1196 | c->dns = new dns_connection (c); |
1198 | c->dns = new dns_connection (c); |
1197 | |
1199 | |
1198 | if (c->dns->snddq.put (pkt)) |
1200 | if (c->dns->snddq.put (pkt)) |
1199 | c->dns->tw.trigger (); |
1201 | c->dns->tw (); |
1200 | |
1202 | |
1201 | // always return true even if the buffer overflows |
1203 | // always return true even if the buffer overflows |
1202 | return true; |
1204 | return true; |
1203 | } |
1205 | } |
1204 | |
1206 | |
… | |
… | |
1209 | } |
1211 | } |
1210 | |
1212 | |
1211 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1213 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1212 | |
1214 | |
1213 | void |
1215 | void |
1214 | dns_connection::time_cb (time_watcher &w) |
1216 | dns_connection::time_cb (ev::timer &w, int revents) |
1215 | { |
1217 | { |
1216 | // servers have to be polled |
1218 | // servers have to be polled |
1217 | if (THISNODE->dns_port) |
1219 | if (THISNODE->dns_port) |
1218 | return; |
1220 | return; |
1219 | |
1221 | |
1220 | // check for timeouts and (re)transmit |
1222 | // check for timeouts and (re)transmit |
1221 | tstamp next = NOW + poll_interval; |
1223 | tstamp next = ev::now () + poll_interval; |
1222 | dns_snd *send = 0; |
1224 | dns_snd *send = 0; |
1223 | |
1225 | |
1224 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1226 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1225 | i != vpn->dns_sndpq.end (); |
1227 | i != vpn->dns_sndpq.end (); |
1226 | ++i) |
1228 | ++i) |
1227 | { |
1229 | { |
1228 | dns_snd *r = *i; |
1230 | dns_snd *r = *i; |
1229 | |
1231 | |
1230 | if (r->timeout <= NOW) |
1232 | if (r->timeout <= ev_now ()) |
1231 | { |
1233 | { |
1232 | if (!send) |
1234 | if (!send) |
1233 | { |
1235 | { |
1234 | send = r; |
1236 | send = r; |
1235 | |
1237 | |
1236 | r->retry++; |
1238 | r->retry++; |
1237 | r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor); |
1239 | 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 - NOW);//D |
1240 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1239 | |
1241 | |
1240 | // the following code changes the query section a bit, forcing |
1242 | // the following code changes the query section a bit, forcing |
1241 | // the forwarder to generate a new request |
1243 | // the forwarder to generate a new request |
1242 | if (r->stdhdr) |
1244 | if (r->stdhdr) |
1243 | 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); |
… | |
… | |
1262 | } |
1264 | } |
1263 | } |
1265 | } |
1264 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1266 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1265 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1267 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1266 | { |
1268 | { |
1267 | if (last_sent + send_interval <= NOW) |
1269 | if (last_sent + send_interval <= ev_now ()) |
1268 | { |
1270 | { |
1269 | //printf ("sending data request etc.\n"); //D |
1271 | //printf ("sending data request etc.\n"); //D |
1270 | if (!snddq.empty () || last_received + 1. > NOW) |
1272 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1271 | { |
1273 | { |
1272 | poll_interval = send_interval; |
1274 | poll_interval = send_interval; |
1273 | NEXT (NOW + send_interval); |
1275 | NEXT (ev_now () + send_interval); |
1274 | } |
1276 | } |
1275 | |
1277 | |
1276 | send = new dns_snd (this); |
1278 | send = new dns_snd (this); |
1277 | send->gen_stream_req (sndseq, snddq); |
1279 | send->gen_stream_req (sndseq, snddq); |
1278 | send->timeout = NOW + min_latency * conf.dns_timeout_factor; |
1280 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
1279 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - NOW, min_latency, conf.dns_timeout_factor);//D |
1281 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1280 | |
1282 | |
1281 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1283 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1282 | } |
1284 | } |
1283 | else |
1285 | else |
1284 | NEXT (last_sent + send_interval); |
1286 | NEXT (last_sent + send_interval); |
… | |
… | |
1288 | vpn->dns_sndpq.push_back (send); |
1290 | vpn->dns_sndpq.push_back (send); |
1289 | } |
1291 | } |
1290 | |
1292 | |
1291 | if (send) |
1293 | if (send) |
1292 | { |
1294 | { |
1293 | last_sent = NOW; |
1295 | last_sent = ev_now (); |
1294 | sendto (vpn->dnsv4_fd, |
1296 | sendto (vpn->dnsv4_fd, |
1295 | send->pkt->at (0), send->pkt->len, 0, |
1297 | send->pkt->at (0), send->pkt->len, 0, |
1296 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1298 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1297 | } |
1299 | } |
1298 | |
1300 | |
1299 | 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)", |
1300 | poll_interval, send_interval, next - NOW, |
1302 | poll_interval, send_interval, next - ev_now (), |
1301 | vpn->dns_sndpq.size (), snddq.size (), |
1303 | vpn->dns_sndpq.size (), snddq.size (), |
1302 | rcvpq.size ()); |
1304 | rcvpq.size ()); |
1303 | |
1305 | |
1304 | // 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 |
1305 | // doesn't seem to happen anymore |
1307 | // doesn't seem to happen anymore |
1306 | if (next < NOW + 0.001) |
1308 | if (next < ev_now () + 0.001) |
1307 | next = NOW + 0.1; |
1309 | next = ev_now () + 0.1; |
1308 | |
1310 | |
1309 | w.start (next); |
1311 | w.start (next - ev_now ()); |
1310 | } |
1312 | } |
1311 | |
1313 | |
1312 | #endif |
1314 | #endif |
1313 | |
1315 | |