… | |
… | |
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) |
… | |
… | |
805 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
813 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
806 | |
814 | |
807 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
815 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
808 | && pkt.qdcount == htons (1)) |
816 | && pkt.qdcount == htons (1)) |
809 | { |
817 | { |
810 | char qname[MAXSIZE]; |
818 | char qname [MAXSIZE]; |
811 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
819 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
812 | |
820 | |
813 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
821 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
814 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
822 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
815 | |
823 | |
… | |
… | |
822 | |
830 | |
823 | int dlen = strlen (THISNODE->domain); |
831 | int dlen = strlen (THISNODE->domain); |
824 | |
832 | |
825 | if (qclass == RR_CLASS_IN |
833 | if (qclass == RR_CLASS_IN |
826 | && qlen > dlen + 1 |
834 | && qlen > dlen + 1 |
827 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
835 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
828 | { |
836 | { |
829 | // now generate reply |
837 | // now generate reply |
830 | pkt.ancount = htons (1); // one answer RR |
838 | pkt.ancount = htons (1); // one answer RR |
831 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
839 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
832 | |
840 | |
… | |
… | |
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 |