… | |
… | |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
266 | |
266 | |
267 | ///////////////////////////////////////////////////////////////////////////// |
267 | ///////////////////////////////////////////////////////////////////////////// |
268 | |
268 | |
269 | #define HDRSIZE 6 |
269 | #define HDRSIZE 6 |
270 | |
270 | |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
272 | { |
272 | { |
273 | seqno &= SEQNO_MASK; |
273 | seqno &= SEQNO_MASK; |
274 | |
274 | |
275 | u8 hdr[3] = { |
275 | u8 hdr[3] = { |
… | |
… | |
409 | struct dns_cfg |
409 | struct dns_cfg |
410 | { |
410 | { |
411 | static int next_uid; |
411 | static int next_uid; |
412 | |
412 | |
413 | u8 id1, id2, id3, id4; |
413 | u8 id1, id2, id3, id4; |
|
|
414 | |
414 | u8 version; |
415 | u8 version; |
|
|
416 | u8 flags; |
415 | u8 rrtype; |
417 | u8 rrtype; |
416 | u8 flags; |
|
|
417 | u8 def_ttl; |
418 | u8 def_ttl; |
418 | u8 rcv_cdc; |
419 | |
419 | u8 snd_cdc; |
|
|
420 | u16 max_size; |
|
|
421 | u16 client; |
420 | u16 client; |
422 | u16 uid; // to make request unique |
421 | u16 uid; // to make request unique |
423 | |
422 | |
424 | u8 reserved[8]; |
423 | u16 max_size; |
|
|
424 | u8 seq_cdc; |
|
|
425 | u8 req_cdc; |
|
|
426 | |
|
|
427 | u8 rep_cdc; |
|
|
428 | u8 r2, r3, r4; |
|
|
429 | |
|
|
430 | u8 r5, r6, r7, r8; |
425 | |
431 | |
426 | void reset (int clientid); |
432 | void reset (int clientid); |
427 | bool valid (); |
433 | bool valid (); |
428 | }; |
434 | }; |
429 | |
435 | |
… | |
… | |
438 | |
444 | |
439 | version = 1; |
445 | version = 1; |
440 | |
446 | |
441 | rrtype = RR_TYPE_TXT; |
447 | rrtype = RR_TYPE_TXT; |
442 | flags = 0; |
448 | flags = 0; |
443 | def_ttl = 1; |
449 | def_ttl = 0; |
|
|
450 | seq_cdc = 26; |
|
|
451 | req_cdc = 62; |
444 | rcv_cdc = 0; |
452 | rep_cdc = 0; |
445 | snd_cdc = 62; |
|
|
446 | max_size = ntohs (MAX_PKT_SIZE); |
453 | max_size = ntohs (MAX_PKT_SIZE); |
447 | client = ntohs (clientid); |
454 | client = ntohs (clientid); |
448 | uid = next_uid++; |
455 | uid = next_uid++; |
449 | |
456 | |
450 | memset (reserved, 0, 8); |
457 | r2 = r3 = r4 = 0; |
|
|
458 | r4 = r5 = r6 = r7 = 0; |
451 | } |
459 | } |
452 | |
460 | |
453 | bool dns_cfg::valid () |
461 | bool dns_cfg::valid () |
454 | { |
462 | { |
455 | return id1 == 'G' |
463 | return id1 == 'G' |
456 | && id2 == 'V' |
464 | && id2 == 'V' |
457 | && id3 == 'P' |
465 | && id3 == 'P' |
458 | && id4 == 'E' |
466 | && id4 == 'E' |
|
|
467 | && seq_cdc == 26 |
|
|
468 | && req_cdc == 62 |
|
|
469 | && rep_cdc == 0 |
459 | && version == 1 |
470 | && version == 1 |
460 | && flags == 0 |
|
|
461 | && rcv_cdc == 0 |
|
|
462 | && snd_cdc == 62 |
|
|
463 | && max_size == ntohs (MAX_PKT_SIZE); |
471 | && max_size == ntohs (MAX_PKT_SIZE); |
464 | } |
472 | } |
465 | |
473 | |
466 | struct dns_packet : net_packet |
474 | struct dns_packet : net_packet |
467 | { |
475 | { |
468 | u16 id; |
476 | u16 id; |
469 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
477 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
470 | u16 qdcount, ancount, nscount, arcount; |
478 | u16 qdcount, ancount, nscount, arcount; |
471 | |
479 | |
472 | u8 data[MAXSIZE - 6 * 2]; |
480 | u8 data [MAXSIZE - 6 * 2]; |
473 | |
481 | |
474 | int decode_label (char *data, int size, int &offs); |
482 | int decode_label (char *data, int size, int &offs); |
475 | }; |
483 | }; |
476 | |
484 | |
477 | int dns_packet::decode_label (char *data, int size, int &offs) |
485 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
663 | } |
671 | } |
664 | |
672 | |
665 | void dns_snd::gen_syn_req () |
673 | void dns_snd::gen_syn_req () |
666 | { |
674 | { |
667 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
675 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
668 | |
|
|
669 | printf ("send syn\n");//D |
|
|
670 | |
676 | |
671 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
677 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
672 | pkt->qdcount = htons (1); |
678 | pkt->qdcount = htons (1); |
673 | |
679 | |
674 | int offs = 6 * 2; |
680 | int offs = 6 * 2; |
… | |
… | |
807 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
813 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
808 | |
814 | |
809 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
815 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
810 | && pkt.qdcount == htons (1)) |
816 | && pkt.qdcount == htons (1)) |
811 | { |
817 | { |
812 | char qname[MAXSIZE]; |
818 | char qname [MAXSIZE]; |
813 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
819 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
814 | |
820 | |
815 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
821 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
816 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
822 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
817 | |
823 | |
… | |
… | |
824 | |
830 | |
825 | int dlen = strlen (THISNODE->domain); |
831 | int dlen = strlen (THISNODE->domain); |
826 | |
832 | |
827 | if (qclass == RR_CLASS_IN |
833 | if (qclass == RR_CLASS_IN |
828 | && qlen > dlen + 1 |
834 | && qlen > dlen + 1 |
829 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
835 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
830 | { |
836 | { |
831 | // now generate reply |
837 | // now generate reply |
832 | pkt.ancount = htons (1); // one answer RR |
838 | pkt.ancount = htons (1); // one answer RR |
833 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
839 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
834 | |
840 | |
… | |
… | |
875 | // new packet, queue |
881 | // new packet, queue |
876 | rcv = new dns_rcv (seqno, data, datalen); |
882 | rcv = new dns_rcv (seqno, data, datalen); |
877 | dns->receive_rep (rcv); |
883 | dns->receive_rep (rcv); |
878 | } |
884 | } |
879 | |
885 | |
|
|
886 | { |
880 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
887 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
881 | |
888 | |
882 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
889 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
883 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
890 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
884 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
891 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
885 | pkt [offs++] = 0; pkt [offs++] = 0; |
892 | pkt [offs++] = 0; pkt [offs++] = 0; |
886 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
893 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
887 | |
894 | |
888 | int rdlen_offs = offs += 2; |
895 | int rdlen_offs = offs += 2; |
889 | |
896 | |
890 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
897 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
891 | // bind doesn't compress well, so reduce further by one label length |
898 | // bind doesn't compress well, so reduce further by one label length |
892 | dlen -= qlen; |
899 | dlen -= qlen; |
893 | |
900 | |
894 | if (dns) |
901 | if (dns) |
895 | { |
902 | { |
896 | // only put data into in-order sequence packets, if |
903 | // only put data into in-order sequence packets, if |
897 | // we receive out-of-order packets we generate empty |
904 | // we receive out-of-order packets we generate empty |
898 | // replies |
905 | // replies |
899 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
906 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
900 | { |
907 | { |
901 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
908 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
902 | |
909 | |
903 | if (txtlen > dns->snddq.size ()) |
910 | if (txtlen > dns->snddq.size ()) |
904 | txtlen = dns->snddq.size (); |
911 | txtlen = dns->snddq.size (); |
905 | |
912 | |
906 | pkt[offs++] = txtlen; |
913 | pkt[offs++] = txtlen; |
907 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
914 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
908 | offs += txtlen; |
915 | offs += txtlen; |
909 | dns->snddq.remove (txtlen); |
916 | dns->snddq.remove (txtlen); |
910 | |
917 | |
911 | dlen -= txtlen + 1; |
918 | dlen -= txtlen + 1; |
912 | } |
919 | } |
913 | |
920 | |
914 | // avoid empty TXT rdata |
921 | // avoid empty TXT rdata |
915 | if (offs == rdlen_offs) |
922 | if (offs == rdlen_offs) |
916 | pkt[offs++] = 0; |
923 | pkt[offs++] = 0; |
917 | |
924 | |
918 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
925 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
919 | } |
926 | } |
920 | else |
927 | else |
921 | { |
928 | { |
922 | // send RST |
929 | // send RST |
923 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
930 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
924 | pkt [offs++] = CMD_IP_RST; |
931 | pkt [offs++] = CMD_IP_RST; |
925 | } |
932 | } |
926 | |
933 | |
927 | int rdlen = offs - rdlen_offs; |
934 | int rdlen = offs - rdlen_offs; |
928 | |
935 | |
929 | pkt [rdlen_offs - 2] = rdlen >> 8; |
936 | pkt [rdlen_offs - 2] = rdlen >> 8; |
930 | pkt [rdlen_offs - 1] = rdlen; |
937 | pkt [rdlen_offs - 1] = rdlen; |
931 | |
938 | |
932 | if (dns) |
939 | if (dns) |
933 | { |
940 | { |
934 | // now update dns_rcv copy |
941 | // now update dns_rcv copy |
935 | rcv->pkt->len = offs; |
942 | rcv->pkt->len = offs; |
936 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
943 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
937 | } |
944 | } |
|
|
945 | } |
938 | |
946 | |
939 | duplicate_request: ; |
947 | duplicate_request: ; |
940 | } |
948 | } |
941 | else |
949 | else |
942 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
950 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
954 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
962 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
955 | pkt [offs++] = 0; pkt [offs++] = 0; |
963 | pkt [offs++] = 0; pkt [offs++] = 0; |
956 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
964 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
957 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
965 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
958 | |
966 | |
959 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
967 | slog (L_INFO, _("DNS: client %d connects"), client); |
960 | |
968 | |
961 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
969 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
962 | pkt [offs++] = CMD_IP_REJ; |
970 | pkt [offs++] = CMD_IP_REJ; |
963 | |
971 | |
964 | if (0 < client && client <= conns.size ()) |
972 | if (0 < client && client <= conns.size ()) |
… | |
… | |
1146 | |
1154 | |
1147 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1155 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1148 | |
1156 | |
1149 | if (pkt->len > 0) |
1157 | if (pkt->len > 0) |
1150 | { |
1158 | { |
1151 | if (THISNODE->dns_port) |
1159 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1160 | dnsv4_client (*pkt); |
|
|
1161 | else |
1152 | { |
1162 | { |
1153 | dnsv4_server (*pkt); |
1163 | dnsv4_server (*pkt); |
1154 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1164 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1155 | } |
1165 | } |
1156 | else |
|
|
1157 | dnsv4_client (*pkt); |
|
|
1158 | |
1166 | |
1159 | delete pkt; |
1167 | delete pkt; |
1160 | } |
1168 | } |
1161 | } |
1169 | } |
1162 | } |
1170 | } |
… | |
… | |
1238 | { |
1246 | { |
1239 | if (vpn->dns_sndpq.empty ()) |
1247 | if (vpn->dns_sndpq.empty ()) |
1240 | { |
1248 | { |
1241 | send = new dns_snd (this); |
1249 | send = new dns_snd (this); |
1242 | |
1250 | |
1243 | printf ("new conn %p %d\n", this, c->conf->id);//D |
|
|
1244 | cfg.reset (THISNODE->id); |
1251 | cfg.reset (THISNODE->id); |
1245 | send->gen_syn_req (); |
1252 | send->gen_syn_req (); |
1246 | } |
1253 | } |
1247 | } |
1254 | } |
1248 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1255 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |