From e0a3dc38a623659fdf585129b6ec78a436fc4559 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 17 Aug 2011 21:51:40 +0100 Subject: [PATCH] WIP dns transport packets etc. --- dns-transp-client.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ dns-transp-common.h | 6 ++++++ dns-transp-server.c | 11 ++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 dns-transp-client.c diff --git a/dns-transp-client.c b/dns-transp-client.c new file mode 100644 index 0000000..deec7b6 --- /dev/null +++ b/dns-transp-client.c @@ -0,0 +1,48 @@ +/**/ + +uint8_t qid[2]; + +#define PKT_PREPEND_START(pe, size, body) do{ \ + assert((pe)->ptr - (pe)->buf >= (size)); \ + const uint8_t *pkt_prepend_orig = (pe)->ptr; \ + uint8_t *pkt = (pe)->ptr -= (size); \ + { body; } \ + assert(pkt == pkt_prepend_orig); \ + }while(0) + +#define PKT_ADDB(byte) (*pkt++ = (byte)) +#define PKT_ADDW(word) (*pkt++ = ((w) >> 8), *pkt++ = (w)) + +#define DNS_HDRLEN (6*2) + +void dnsqueryenc_start(struct dnspacketenc *pe, + int mydompathlen, const char *mydompath) { + dnspacketenc_start(pe); + + PKT_PREPEND(pe, 4, { + PKT_ADDW(QTYPE_CNAME); + PKT_ADDW(QCLASS_IN); + }); + + return dnsdomainenc_start(&pe->dom, pe->buf, + pe->ptr - pe->buf - DNS_HDRLEN, + mydompathlen, mydompath); +} + +void dnsqueryenc_finish(uint16_t qid, const uint8_t **result, int *reslen) { + pe->ptr = dnsdomainenc_getresult(&pe->dom); + + PKT_PREPEND(pe, DNS_HDRLEN, { + PKT_ADDW(qid); + PKT_ADDB(0x00); /* QR=0(q); Opcode=QUERY; !AA,!TC,!RD */ + PKT_ADDB(0x00); /* !RA; Z; RCODE=NOERROR */ + PKT_ADDW(0x01); /* QDCOUNT=1 */ + PKT_ADDW(0x00); /* ANCOUNT=0 */ + PKT_ADDW(0x00); /* NSCOUNT=0 */ + PKT_ADDW(0x00); *hdr++ = 0x00; /* ARCOUNT=0 */ + }); + + *result = pe->ptr; + *reslen = (pe->buf + MAX_DNSPACKET_BYTES) - pe->ptr; +} + diff --git a/dns-transp-common.h b/dns-transp-common.h index 53f6a73..9b25f26 100644 --- a/dns-transp-common.h +++ b/dns-transp-common.h @@ -36,6 +36,12 @@ struct dnsdomainenc { int labremain; }; +struct dnspacketenc { + uint8_t buf[MAX_DNSPACKET_BYTES]; + uint8_t *ptr; + struct dnsdomainenc dom; +}; + /* * The only legal calling sequence is this: * start diff --git a/dns-transp-server.c b/dns-transp-server.c index fd52b8a..b822932 100644 --- a/dns-transp-server.c +++ b/dns-transp-server.c @@ -114,4 +114,13 @@ struct dns_server { * 4a. create assoc packet * look up {clientaddr,clientnonce} in clientinit * if not then create an assoc - * + */ + + + + +void incoming_packet(const uint8_t pkt, int len) { + if (len < DNS_HDRLEN) + return badpkt("shorter than dns header"); + + uint16_t id = -- 2.30.2