From b801ff6703be2fdde9cc2629ebb03f270ea73f5e Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 2 Mar 2020 04:58:22 +0000 Subject: [PATCH] implemented soa rr encoding in dnc --- mio/bin/t01.c | 11 +++++++--- mio/lib/dns-cli.c | 22 +++++++++++++------- mio/lib/dns.c | 53 ++++++++++++++++++++++++++++++++++++++++++----- mio/lib/mio-dns.h | 2 +- 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/mio/bin/t01.c b/mio/bin/t01.c index 5a483da..9253296 100644 --- a/mio/bin/t01.c +++ b/mio/bin/t01.c @@ -957,7 +957,7 @@ for (i = 0; i < 5; i++) //mio_bcstrtoskad (mio, "[fe80::c7e2:bd6e:1209:ac1b%eno1]:1153", &servaddr); dnc = mio_svc_dnc_start (mio, &servaddr, MIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */ -#if 0 +#if 1 { mio_dns_bqr_t qrs[] = { @@ -971,13 +971,18 @@ for (i = 0; i < 5; i++) mio_ip4ad_t rrdata_a = { { 4, 3, 2, 1 } }; mio_ip6ad_t rrdata_aaaa = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, }}; + mio_dns_brrd_soa_t miflux_soa_data = + { + "ns9.dnszi.com", "root.dnszi.com", 2019091905, 43200, 3600, 1209600, 1100 + }; + mio_dns_brr_t rrs[] = { { MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_RRT_A, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(rrdata_a), &rrdata_a }, { MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_RRT_AAAA, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(rrdata_aaaa), &rrdata_aaaa }, { MIO_DNS_RR_PART_ANSWER, "miflux.com", MIO_DNS_RRT_NS, MIO_DNS_RRC_IN, 86400, 0, "ns1.miflux.com" }, - { MIO_DNS_RR_PART_ANSWER, "miflux.com", MIO_DNS_RRT_NS, MIO_DNS_RRC_IN, 86400, 0, "ns2.miflux.com" }, //, - //{ MIO_DNS_RR_PART_AUTHORITY, "miflux.com", MIO_DNS_RRT_SOA, MIO_DNS_RRC_IN, 86400, 0, "ns2.miflux.com" }, //, + { MIO_DNS_RR_PART_ANSWER, "miflux.com", MIO_DNS_RRT_NS, MIO_DNS_RRC_IN, 86400, 0, "ns2.miflux.com" }, + { MIO_DNS_RR_PART_AUTHORITY, "miflux.com", MIO_DNS_RRT_SOA, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(miflux_soa_data), &miflux_soa_data }, //, //{ MIO_DNS_RR_PART_ANSERT, "www.miflux.com", MIO_DNS_RRT_CNAME, MIO_DNS_RRC_IN, 60, 15, "code.miflux.com" } }; diff --git a/mio/lib/dns-cli.c b/mio/lib/dns-cli.c index 39f42eb..965efc2 100644 --- a/mio/lib/dns-cli.c +++ b/mio/lib/dns-cli.c @@ -47,7 +47,14 @@ struct mio_svc_dnc_t mio_ntime_t send_tmout; mio_ntime_t reply_tmout; /* default reply timeout */ - mio_oow_t reply_tmout_max_tries; + + /* For a question sent out, it may wait for a corresponding answer. + * if max_tries is greater than 0, sending and waiting is limited + * to this value over udp and to 1 over tcp. if max_tries is 0, + * it sends out the question but never waits for a response. + * For a non-question message sent out, it never waits for a response + * regardless of max_tries. */ + mio_oow_t max_tries; mio_oow_t seq; mio_dns_msg_t* pending_req; @@ -131,7 +138,7 @@ static mio_dns_msg_t* make_dns_msg (mio_svc_dnc_t* dnc, mio_dns_bhdr_t* bdns, mi msgxtn->on_done = on_done; msgxtn->wtmout = dnc->send_tmout; msgxtn->rtmout = dnc->reply_tmout; - msgxtn->rmaxtries = dnc->reply_tmout_max_tries; + msgxtn->rmaxtries = dnc->max_tries; msgxtn->rtries = 0; msgxtn->servaddr = dnc->serv_addr; @@ -344,7 +351,7 @@ static int on_tcp_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, con goto finalize; } - MIO_ASSERT (mio, msgxtn->pending == 1); + MIO_ASSERT (mio, msgxtn->pending != 0); } else { @@ -566,7 +573,8 @@ static int on_udp_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, } //////////////////////// -pkt->tc = 1; +// for simple testing without actual truncated dns response +//pkt->tc = 1; //////////////////////// if (MIO_UNLIKELY(pkt->tc)) { @@ -705,7 +713,7 @@ static void on_udp_disconnect (mio_dev_sck_t* dev) } } -mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const mio_skad_t* bind_addr, const mio_ntime_t* send_tmout, const mio_ntime_t* reply_tmout, mio_oow_t reply_tmout_max_tries) +mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const mio_skad_t* bind_addr, const mio_ntime_t* send_tmout, const mio_ntime_t* reply_tmout, mio_oow_t max_tries) { mio_svc_dnc_t* dnc = MIO_NULL; mio_dev_sck_make_t mkinfo; @@ -719,7 +727,7 @@ mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const dnc->serv_addr = *serv_addr; dnc->send_tmout = *send_tmout; dnc->reply_tmout = *reply_tmout; - dnc->reply_tmout_max_tries = reply_tmout_max_tries; + dnc->max_tries = max_tries; MIO_MEMSET (&mkinfo, 0, MIO_SIZEOF(mkinfo)); switch (mio_skad_family(serv_addr)) @@ -798,7 +806,7 @@ mio_dns_msg_t* mio_svc_dnc_sendmsg (mio_svc_dnc_t* dnc, mio_dns_bhdr_t* bdns, mi msg = make_dns_msg(dnc, bdns, qr, qr_count, rr, rr_count, edns, on_done, xtnsize); if (!msg) return MIO_NULL; - if (send_dns_msg(dnc, msg, prefer_tcp) <= -1) /* TODO: determine to prefer tcp or not before sending */ + if (send_dns_msg(dnc, msg, prefer_tcp) <= -1) { release_dns_msg (dnc, msg); return MIO_NULL; diff --git a/mio/lib/dns.c b/mio/lib/dns.c index 934d8f4..90f9bf9 100644 --- a/mio/lib/dns.c +++ b/mio/lib/dns.c @@ -467,16 +467,16 @@ void mio_dns_free_packet_info (mio_t* mio, mio_dns_pkt_info_t* pi) static int encode_rrdata_in_dns_msg (mio_t* mio, const mio_dns_brr_t* rr, mio_uint16_t* dxlen, void* dptr) { - mio_oow_t xlen; + mio_oow_t xlen; /* actual data length after encoding */ switch (rr->rrtype) { case MIO_DNS_RRT_A: - if (rr->dlen != MIO_SIZEOF(mio_ip4ad_t)) goto inval; + if (MIO_UNLIKELY(rr->dlen != MIO_SIZEOF(mio_ip4ad_t))) goto inval; goto verbatim; case MIO_DNS_RRT_AAAA: - if (rr->dlen != MIO_SIZEOF(mio_ip6ad_t)) goto inval; + if (MIO_UNLIKELY(rr->dlen != MIO_SIZEOF(mio_ip6ad_t))) goto inval; goto verbatim; case MIO_DNS_RRT_CNAME: @@ -493,7 +493,7 @@ static int encode_rrdata_in_dns_msg (mio_t* mio, const mio_dns_brr_t* rr, mio_ui xlen = to_dn(rr->dptr, dptr); else xlen = to_dn_capa(rr->dptr); - if (xlen <= 0) goto inval; + if (MIO_UNLIKELY(xlen <= 0)) goto inval; break; #if 0 @@ -510,9 +510,52 @@ static int encode_rrdata_in_dns_msg (mio_t* mio, const mio_dns_brr_t* rr, mio_ui break; case MIO_DNS_RRT_SOA: + { /* soa */ - xlen = rr->dlen; + mio_dns_brrd_soa_t* soa; + mio_oow_t tmp; + + if (MIO_UNLIKELY(rr->dlen != MIO_SIZEOF(mio_dns_brrd_soa_t))) goto inval; + + soa = (mio_dns_brrd_soa_t*)rr->dptr; + xlen = 0; + if (dptr) + { + mio_uint32_t ti; + + tmp = to_dn(soa->mname, (mio_uint8_t*)dptr + xlen); + if (MIO_UNLIKELY(tmp <= 0)) goto inval; + xlen += tmp; + + tmp = to_dn(soa->rname, (mio_uint8_t*)dptr + xlen); + if (MIO_UNLIKELY(tmp <= 0)) goto inval; + xlen += tmp; + + ti = mio_ntoh32(soa->serial); + MIO_MEMCPY((mio_uint8_t*)dptr + xlen, &ti, MIO_SIZEOF(ti)); xlen += MIO_SIZEOF(ti); + ti = mio_ntoh32(soa->refresh); + MIO_MEMCPY((mio_uint8_t*)dptr + xlen, &ti, MIO_SIZEOF(ti)); xlen += MIO_SIZEOF(ti); + ti = mio_ntoh32(soa->retry); + MIO_MEMCPY((mio_uint8_t*)dptr + xlen, &ti, MIO_SIZEOF(ti)); xlen += MIO_SIZEOF(ti); + ti = mio_ntoh32(soa->expire); + MIO_MEMCPY((mio_uint8_t*)dptr + xlen, &ti, MIO_SIZEOF(ti)); xlen += MIO_SIZEOF(ti); + ti = mio_ntoh32(soa->minimum); + MIO_MEMCPY((mio_uint8_t*)dptr + xlen, &ti, MIO_SIZEOF(ti)); xlen += MIO_SIZEOF(ti); + } + else + { + tmp = to_dn_capa(soa->mname); + if (MIO_UNLIKELY(tmp <= 0)) goto inval; + xlen += tmp; + + tmp = to_dn_capa(soa->rname); + if (MIO_UNLIKELY(tmp <= 0)) goto inval; + xlen += tmp; + + xlen += 20; + } break; + } case MIO_DNS_RRT_TXT: case MIO_DNS_RRT_NULL: diff --git a/mio/lib/mio-dns.h b/mio/lib/mio-dns.h index cb705a1..012fbcc 100644 --- a/mio/lib/mio-dns.h +++ b/mio/lib/mio-dns.h @@ -448,7 +448,7 @@ MIO_EXPORT mio_svc_dnc_t* mio_svc_dnc_start ( const mio_skad_t* bind_addr, /* optional. can be MIO_NULL */ const mio_ntime_t* send_tmout, /* required */ const mio_ntime_t* reply_tmout, /* required */ - mio_oow_t reply_tmout_max_tries + mio_oow_t max_tries /* required */ ); MIO_EXPORT void mio_svc_dnc_stop (