ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
(Generate patch)

Comparing gvpe/src/vpn_dns.C (file contents):
Revision 1.35 by pcg, Wed Mar 16 14:56:32 2005 UTC vs.
Revision 1.46 by pcg, Thu Dec 6 00:35:29 2007 UTC

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"
48 53
49#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data 54#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
50#define ACTIVITY_INTERVAL 5. 55#define ACTIVITY_INTERVAL 5.
51 56
52#define TIMEOUT_FACTOR 2.
53
54#define INITIAL_TIMEOUT 0.1 // retry timeouts 57#define INITIAL_TIMEOUT 0.1 // retry timeouts
55#define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn 58#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
56 59
57#define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests
58#define MAX_SEND_INTERVAL 2. // optimistic? 60#define MAX_SEND_INTERVAL 2. // optimistic?
59 61
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 62#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
63#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE 63#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
64 64
65#define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well 65#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 66// 240 leaves about 4 bytes of server reply data
462 r4 = r5 = r6 = r7 = 0; 462 r4 = r5 = r6 = r7 = 0;
463} 463}
464 464
465bool dns_cfg::valid () 465bool dns_cfg::valid ()
466{ 466{
467 // although the protocol itself allows for some configurability,
468 // only the following encoding/decoding settings are implemented.
467 return id1 == 'G' 469 return id1 == 'G'
468 && id2 == 'V' 470 && id2 == 'V'
469 && id3 == 'P' 471 && id3 == 'P'
470 && id4 == 'E' 472 && id4 == 'E'
471 && seq_cdc == 26 473 && seq_cdc == 26
558 vector<dns_rcv *> rcvpq; 560 vector<dns_rcv *> rcvpq;
559 561
560 byte_stream rcvdq; int rcvseq; int repseq; 562 byte_stream rcvdq; int rcvseq; int repseq;
561 byte_stream snddq; int sndseq; 563 byte_stream snddq; int sndseq;
562 564
563 void time_cb (time_watcher &w); time_watcher tw; 565 inline void time_cb (ev::timer &w, int revents); ev::timer tw;
564 void receive_rep (dns_rcv *r); 566 void receive_rep (dns_rcv *r);
565 567
566 dns_connection (connection *c); 568 dns_connection (connection *c);
567 ~dns_connection (); 569 ~dns_connection ();
568}; 570};
587: dns (dns) 589: dns (dns)
588{ 590{
589 timeout = 0; 591 timeout = 0;
590 retry = 0; 592 retry = 0;
591 seqno = 0; 593 seqno = 0;
592 sent = NOW; 594 sent = ev_now ();
593 stdhdr = false; 595 stdhdr = false;
594 596
595 pkt = new dns_packet; 597 pkt = new dns_packet;
596 598
597 pkt->id = next_id (); 599 pkt->id = next_id ();
628void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 630void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
629{ 631{
630 stdhdr = true; 632 stdhdr = true;
631 this->seqno = seqno; 633 this->seqno = seqno;
632 634
633 timeout = NOW + INITIAL_TIMEOUT; 635 timeout = ev_now () + INITIAL_TIMEOUT;
634 636
635 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 637 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
636 pkt->qdcount = htons (1); 638 pkt->qdcount = htons (1);
637 639
638 int offs = 6*2; 640 int offs = 6*2;
673 pkt->len = offs; 675 pkt->len = offs;
674} 676}
675 677
676void dns_snd::gen_syn_req () 678void dns_snd::gen_syn_req ()
677{ 679{
678 timeout = NOW + INITIAL_SYN_TIMEOUT; 680 timeout = ev_now () + INITIAL_SYN_TIMEOUT;
679 681
680 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 682 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
681 pkt->qdcount = htons (1); 683 pkt->qdcount = htons (1);
682 684
683 int offs = 6 * 2; 685 int offs = 6 * 2;
723 725
724dns_connection::dns_connection (connection *c) 726dns_connection::dns_connection (connection *c)
725: c (c) 727: c (c)
726, rcvdq (MAX_BACKLOG * 2) 728, rcvdq (MAX_BACKLOG * 2)
727, snddq (MAX_BACKLOG) 729, snddq (MAX_BACKLOG)
728, tw (this, &dns_connection::time_cb)
729{ 730{
731 tw.set<dns_connection, &dns_connection::time_cb> (this);
732
730 vpn = c->vpn; 733 vpn = c->vpn;
731 734
732 established = false; 735 established = false;
733 736
734 rcvseq = repseq = sndseq = 0; 737 rcvseq = repseq = sndseq = 0;
749 752
750void dns_connection::receive_rep (dns_rcv *r) 753void dns_connection::receive_rep (dns_rcv *r)
751{ 754{
752 if (r->datalen) 755 if (r->datalen)
753 { 756 {
754 last_received = NOW; 757 last_received = ev_now ();
755 tw.trigger (); 758 tw ();
756 759
757 poll_interval = send_interval; 760 poll_interval = send_interval;
758 } 761 }
759 else 762 else
760 { 763 {
770 773
771 // find next packet 774 // find next packet
772 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) 775 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
773 if (SEQNO_EQ (rcvseq, (*i)->seqno)) 776 if (SEQNO_EQ (rcvseq, (*i)->seqno))
774 { 777 {
778 //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D
775 // enter the packet into our input stream 779 // enter the packet into our input stream
776 r = *i; 780 r = *i;
777 781
778 // remove the oldest packet, look forward, as it's oldest first 782 // remove the oldest packet, look forward, as it's oldest first
779 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)
780 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) 784 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
781 { 785 {
786 //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D
782 delete *j; 787 delete *j;
783 rcvpq.erase (j); 788 rcvpq.erase (j);
784 break; 789 break;
785 } 790 }
786 791
1014 { 1019 {
1015 dns_connection *dns = (*i)->dns; 1020 dns_connection *dns = (*i)->dns;
1016 connection *c = dns->c; 1021 connection *c = dns->c;
1017 int seqno = (*i)->seqno; 1022 int seqno = (*i)->seqno;
1018 u8 data[MAXSIZE], *datap = data; 1023 u8 data[MAXSIZE], *datap = data;
1024 //printf ("rcv pkt %x\n", seqno);//D
1019 1025
1020 if ((*i)->retry) 1026 if ((*i)->retry)
1021 { 1027 {
1022 dns->send_interval *= 1.01; 1028 dns->send_interval *= 1.01;
1023 if (dns->send_interval > MAX_SEND_INTERVAL) 1029 if (dns->send_interval > MAX_SEND_INTERVAL)
1028#if 0 1034#if 0
1029 dns->send_interval *= 0.999; 1035 dns->send_interval *= 0.999;
1030#endif 1036#endif
1031 // the latency surely puts an upper bound on 1037 // the latency surely puts an upper bound on
1032 // the minimum send interval 1038 // the minimum send interval
1033 double latency = NOW - (*i)->sent; 1039 double latency = ev_now () - (*i)->sent;
1034 1040
1035 if (latency < dns->min_latency) 1041 if (latency < dns->min_latency)
1036 dns->min_latency = latency; 1042 dns->min_latency = latency;
1037 1043
1038 if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) 1044 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1039 dns->send_interval = dns->min_latency * LATENCY_FACTOR; 1045 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1040 1046
1041 if (dns->send_interval < MIN_SEND_INTERVAL) 1047 if (dns->send_interval < conf.dns_send_interval)
1042 dns->send_interval = MIN_SEND_INTERVAL; 1048 dns->send_interval = conf.dns_send_interval;
1043 } 1049 }
1044 1050
1045 delete *i; 1051 delete *i;
1046 dns_sndpq.erase (i); 1052 dns_sndpq.erase (i);
1047 1053
1152 break; 1158 break;
1153 } 1159 }
1154} 1160}
1155 1161
1156void 1162void
1157vpn::dnsv4_ev (io_watcher &w, short revents) 1163vpn::dnsv4_ev (ev::io &w, int revents)
1158{ 1164{
1159 if (revents & EVENT_READ) 1165 if (revents & EV_READ)
1160 { 1166 {
1161 dns_packet *pkt = new dns_packet; 1167 dns_packet *pkt = new dns_packet;
1162 struct sockaddr_in sa; 1168 struct sockaddr_in sa;
1163 socklen_t sa_len = sizeof (sa); 1169 socklen_t sa_len = sizeof (sa);
1164 1170
1190 1196
1191 if (!c->dns) 1197 if (!c->dns)
1192 c->dns = new dns_connection (c); 1198 c->dns = new dns_connection (c);
1193 1199
1194 if (c->dns->snddq.put (pkt)) 1200 if (c->dns->snddq.put (pkt))
1195 c->dns->tw.trigger (); 1201 c->dns->tw ();
1196 1202
1197 // always return true even if the buffer overflows 1203 // always return true even if the buffer overflows
1198 return true; 1204 return true;
1199} 1205}
1200 1206
1205} 1211}
1206 1212
1207#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1213#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1208 1214
1209void 1215void
1210dns_connection::time_cb (time_watcher &w) 1216dns_connection::time_cb (ev::timer &w, int revents)
1211{ 1217{
1212 // servers have to be polled 1218 // servers have to be polled
1213 if (THISNODE->dns_port) 1219 if (THISNODE->dns_port)
1214 return; 1220 return;
1215 1221
1216 // check for timeouts and (re)transmit 1222 // check for timeouts and (re)transmit
1217 tstamp next = NOW + poll_interval; 1223 tstamp next = ev::now () + poll_interval;
1218 dns_snd *send = 0; 1224 dns_snd *send = 0;
1219 1225
1220 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); 1226 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1221 i != vpn->dns_sndpq.end (); 1227 i != vpn->dns_sndpq.end ();
1222 ++i) 1228 ++i)
1223 { 1229 {
1224 dns_snd *r = *i; 1230 dns_snd *r = *i;
1225 1231
1226 if (r->timeout <= NOW) 1232 if (r->timeout <= ev_now ())
1227 { 1233 {
1228 if (!send) 1234 if (!send)
1229 { 1235 {
1230 send = r; 1236 send = r;
1231 1237
1232 r->retry++; 1238 r->retry++;
1233 r->timeout = NOW + (r->retry * min_latency * 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
1234 1241
1235 // the following code changes the query section a bit, forcing 1242 // the following code changes the query section a bit, forcing
1236 // the forwarder to generate a new request 1243 // the forwarder to generate a new request
1237 if (r->stdhdr) 1244 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); 1245 encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1241 }
1242 } 1246 }
1243 } 1247 }
1244 else 1248 else
1245 NEXT (r->timeout); 1249 NEXT (r->timeout);
1246 } 1250 }
1257 1261
1258 cfg.reset (THISNODE->id); 1262 cfg.reset (THISNODE->id);
1259 send->gen_syn_req (); 1263 send->gen_syn_req ();
1260 } 1264 }
1261 } 1265 }
1262 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING 1266 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1263 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) 1267 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1264 { 1268 {
1265 if (last_sent + send_interval <= NOW) 1269 if (last_sent + send_interval <= ev_now ())
1266 { 1270 {
1267 //printf ("sending data request etc.\n"); //D 1271 //printf ("sending data request etc.\n"); //D
1268 if (!snddq.empty () || last_received + 1. > NOW) 1272 if (!snddq.empty () || last_received + 1. > ev_now ())
1269 { 1273 {
1270 poll_interval = send_interval; 1274 poll_interval = send_interval;
1271 NEXT (NOW + send_interval); 1275 NEXT (ev_now () + send_interval);
1272 } 1276 }
1273 1277
1274 send = new dns_snd (this); 1278 send = new dns_snd (this);
1275 send->gen_stream_req (sndseq, snddq); 1279 send->gen_stream_req (sndseq, snddq);
1276 send->timeout = NOW + min_latency * 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
1277 1282
1278 sndseq = (sndseq + 1) & SEQNO_MASK; 1283 sndseq = (sndseq + 1) & SEQNO_MASK;
1279 } 1284 }
1280 else 1285 else
1281 NEXT (last_sent + send_interval); 1286 NEXT (last_sent + send_interval);
1285 vpn->dns_sndpq.push_back (send); 1290 vpn->dns_sndpq.push_back (send);
1286 } 1291 }
1287 1292
1288 if (send) 1293 if (send)
1289 { 1294 {
1290 last_sent = NOW; 1295 last_sent = ev_now ();
1291 sendto (vpn->dnsv4_fd, 1296 sendto (vpn->dnsv4_fd,
1292 send->pkt->at (0), send->pkt->len, 0, 1297 send->pkt->at (0), send->pkt->len, 0,
1293 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); 1298 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1294 } 1299 }
1295 1300
1296 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)",
1297 poll_interval, send_interval, next - NOW, 1302 poll_interval, send_interval, next - ev_now (),
1298 vpn->dns_sndpq.size (), snddq.size (), 1303 vpn->dns_sndpq.size (), snddq.size (),
1299 rcvpq.size ()); 1304 rcvpq.size ());
1300 1305
1301 // 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
1307 // doesn't seem to happen anymore
1302 if (next < NOW + 0.001) 1308 if (next < ev_now () + 0.001)
1303 next = NOW + 0.1; 1309 next = ev_now () + 0.1;
1304 1310
1305 w.start (next); 1311 w.start (next - ev_now ());
1306} 1312}
1307 1313
1308#endif 1314#endif
1309 1315

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines