From 9217fcec5510733084627405b2b1ffeb1c685cb3 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 9 Apr 2019 09:45:02 +0000 Subject: [PATCH] some code for edns --- mio/lib/dns.c | 29 +++++++++++++++++++++-------- mio/lib/main.c | 6 +++--- mio/lib/mio-dns.h | 35 ++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/mio/lib/dns.c b/mio/lib/dns.c index 580e6c6..d2b5664 100644 --- a/mio/lib/dns.c +++ b/mio/lib/dns.c @@ -28,11 +28,13 @@ #include #include "mio-prv.h" +typedef struct mio_dns_t mio_dns_t; + /* ----------------------------------------------------------------------- */ #define DN_AT_END(ptr) (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) -static mio_oow_t to_dn (const mio_bch_t* str, mio_uint8_t* buf, mio_oow_t bufsz) +static mio_oow_t to_dn (mio_dns_t* dns, const mio_bch_t* str, mio_uint8_t* buf, mio_oow_t bufsz) { mio_uint8_t* bp = buf, * be = buf + bufsz; @@ -97,14 +99,25 @@ static mio_oow_t dn_length (mio_uint8_t* ptr, mio_oow_t len) /* ----------------------------------------------------------------------- */ +/*#define MIO_DNS_HEADERS mio_htl_t* dncache;*/ +#define MIO_DNS_HEADERS void* dncache; + +struct mio_dns_t +{ + MIO_SVC_HEADERS; + MIO_DNS_HEADERS; +}; + struct mio_dnss_t { MIO_SVC_HEADERS; + MIO_DNS_HEADERS; }; struct mio_dnsc_t { MIO_SVC_HEADERS; + MIO_DNS_HEADERS; mio_dev_sck_t* sck; mio_sckaddr_t serveraddr; @@ -216,7 +229,7 @@ static void release_req_msg (mio_dnsc_t* dnsc, mio_dns_msg_t* msg) mio_freemem (mio, msgbuf); } -static mio_dns_msg_t* build_req_msg (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oow_t qr_count, mio_dns_brr_t* rr, mio_oow_t rr_count, mio_oow_t* xmsglen) +static mio_dns_msg_t* build_req_msg (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oow_t qr_count, mio_dns_brr_t* rr, mio_oow_t rr_count, mio_dns_bedns_t* edns, mio_oow_t* xmsglen) { mio_t* mio = dnsc->mio; mio_oow_t dnlen, msgbufsz, i; @@ -257,7 +270,7 @@ static mio_dns_msg_t* build_req_msg (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oo for (i = 0; i < qr_count; i++) { /* dnlen includes the ending */ - dnlen = to_dn(qr[i].qname, dn, mio_count_bcstr(qr[i].qname) + 2); + dnlen = to_dn((mio_dns_t*)dnsc, qr[i].qname, dn, mio_count_bcstr(qr[i].qname) + 2); if (dnlen <= 0) { release_req_msg (dnsc, msg); @@ -272,14 +285,14 @@ static mio_dns_msg_t* build_req_msg (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oo dn = (mio_uint8_t*)(qrtr + 1); } - for (rr_sect = MIO_DNS_RRR_PART_ANSWER; rr_sect <= MIO_DNS_RRR_PART_ADDITIONAL;) + for (rr_sect = MIO_DNS_RR_PART_ANSWER; rr_sect <= MIO_DNS_RR_PART_ADDITIONAL;) { mio_oow_t match_count = 0; for (i = 0; i < rr_count; i++) { if (rr[i].part == rr_sect) { - dnlen = to_dn(rr[i].qname, dn, mio_count_bcstr(rr[i].qname) + 2); + dnlen = to_dn((mio_dns_t*)dnsc, rr[i].qname, dn, mio_count_bcstr(rr[i].qname) + 2); if (dnlen <= 0) { release_req_msg (dnsc, msg); @@ -330,7 +343,7 @@ static MIO_INLINE int send_req_with_single_rr (mio_dnsc_t* dnsc, mio_dns_bqr_t* mio_dns_msg_t* msg; mio_oow_t msglen; - msg = build_req_msg(dnsc, qr, 1, MIO_NULL, 0, &msglen); + msg = build_req_msg(dnsc, qr, 1, MIO_NULL, 0, MIO_NULL, &msglen); if (!msg) return -1; msg->rd = 1; @@ -359,7 +372,7 @@ int mio_dnsc_sendreq (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oow_t count) mio_dns_msg_t* msg; mio_oow_t msglen; - msg = build_req_msg(dnsc, qr, count, &msglen); + msg = build_req_msg(dnsc, qr, count, MIO_NULL, &msglen); if (!msg) return -1; msg->rd = 1; @@ -379,7 +392,7 @@ int mio_dnsc_sendrep (mio_dnsc_t* dnsc, mio_dns_bqr_t* qr, mio_oow_t qr_count, m mio_dns_msg_t* msg; mio_oow_t msglen; - msg = build_req_msg(dnsc, qr, qr_count, rr, rr_count, &msglen); + msg = build_req_msg(dnsc, qr, qr_count, rr, rr_count, MIO_NULL, &msglen); if (!msg) return -1; msg->rd = 1; diff --git a/mio/lib/main.c b/mio/lib/main.c index a8fdbc7..2760d80 100644 --- a/mio/lib/main.c +++ b/mio/lib/main.c @@ -820,9 +820,9 @@ for (i = 0; i < 5; i++) }; mio_dns_brr_t rrs[] = { - { MIO_DNS_RRR_PART_ANSWER, "code.miflux.com", MIO_DNS_QTYPE_A, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL }, - { MIO_DNS_RRR_PART_ANSWER, "code.miflux.com", MIO_DNS_QTYPE_AAAA, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL }, - { MIO_DNS_RRR_PART_AUTHORITY, "dns.miflux.com", MIO_DNS_QTYPE_NS, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL } + { MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_QTYPE_A, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL }, + { MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_QTYPE_AAAA, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL }, + { MIO_DNS_RR_PART_AUTHORITY, "dns.miflux.com", MIO_DNS_QTYPE_NS, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL } }; mio_dnsc_sendreq (dnsc, qrs, MIO_COUNTOF(qrs)); diff --git a/mio/lib/mio-dns.h b/mio/lib/mio-dns.h index 97e0812..b2b4b99 100644 --- a/mio/lib/mio-dns.h +++ b/mio/lib/mio-dns.h @@ -173,6 +173,7 @@ typedef struct mio_dns_rrtr_t mio_dns_rrtr_t; typedef struct mio_dnss_t mio_dnss_t; typedef struct mio_dnsc_t mio_dnsc_t; +/* breakdown of question record */ struct mio_dns_bqr_t { mio_bch_t* qname; @@ -181,17 +182,19 @@ struct mio_dns_bqr_t }; typedef struct mio_dns_bqr_t mio_dns_bqr_t; + enum mio_dns_rr_part_t { - MIO_DNS_RRR_PART_ANSWER, - MIO_DNS_RRR_PART_AUTHORITY, - MIO_DNS_RRR_PART_ADDITIONAL + MIO_DNS_RR_PART_ANSWER, + MIO_DNS_RR_PART_AUTHORITY, + MIO_DNS_RR_PART_ADDITIONAL }; typedef enum mio_dns_rr_part_t mio_dns_rr_part_t; +/* breakdown of resource record */ struct mio_dns_brr_t { - mio_dns_rr_part_t part; + mio_dns_rr_part_t part; mio_bch_t* qname; mio_uint16_t qtype; mio_uint16_t qclass; @@ -201,6 +204,24 @@ struct mio_dns_brr_t }; typedef struct mio_dns_brr_t mio_dns_brr_t; +struct mio_dns_bedns_opt_t +{ + mio_uint16_t dlen; + void* dptr; +}; +typedef struct mio_dns_bedns_opt_t mio_dns_bedns_opt_t; + +struct mio_dns_bedns_t +{ + mio_uint16_t uplen; /* udp payload len */ + mio_uint8_t version; + + mio_oow_t olen; + mio_dns_bedns_opt_t* optr; +}; +typedef struct mio_dns_bedns_t mio_dns_bedns_t; + + #if defined(__cplusplus) extern "C" { #endif @@ -215,15 +236,15 @@ MIO_EXPORT void mio_dnsc_stop ( MIO_EXPORT int mio_dnsc_sendreq ( mio_dnsc_t* dnsc, - mio_dns_bqr_t* qr, + mio_dns_bqr_t* qr, mio_oow_t qr_count ); MIO_EXPORT int mio_dnsc_sendrep ( mio_dnsc_t* dnsc, - mio_dns_bqr_t* qr, + mio_dns_bqr_t* qr, mio_oow_t qr_count, - mio_dns_brr_t* rr, + mio_dns_brr_t* rr, mio_oow_t rr_count );