From f5f8b9e2b4532dde352914a973e147f3e084ba4c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 20 Feb 2020 10:07:12 +0000 Subject: [PATCH] enhanced mio_equal_skads() --- mio/bin/t01.c | 37 +++++++++++++++++++++++++++---------- mio/lib/dns.c | 32 +++++++++++++++++++++++++------- mio/lib/mio-skad.h | 3 ++- mio/lib/sck.c | 2 +- mio/lib/skad.c | 41 +++++++++++++++++++++++++++++++++++++---- 5 files changed, 92 insertions(+), 23 deletions(-) diff --git a/mio/bin/t01.c b/mio/bin/t01.c index fa40827..bcabda1 100644 --- a/mio/bin/t01.c +++ b/mio/bin/t01.c @@ -616,26 +616,32 @@ static void on_dnc_resolve(mio_svc_dnc_t* dnc, mio_dns_msg_t* reqmsg, mio_errnum { case MIO_DNS_RRT_A: { - struct in6_addr ia; - char buf[128]; - memcpy (&ia.s6_addr, brr->dptr, brr->dlen); - printf ("^^^ GOT REPLY........................ %d ", brr->dlen); - printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, MIO_COUNTOF(buf))); + struct in_addr ia; + memcpy (&ia.s_addr, brr->dptr, brr->dlen); + printf ("^^^ GOT REPLY A........................ %d ", brr->dlen); + printf ("[%s]", inet_ntoa(ia)); printf ("\n"); goto done; } case MIO_DNS_RRT_AAAA: { - struct in_addr ia; - memcpy (&ia.s_addr, brr->dptr, brr->dlen); - printf ("^^^ GOT REPLY........................ %d ", brr->dlen); - printf ("[%s]", inet_ntoa(ia)); + struct in6_addr ia; + char buf[128]; + memcpy (&ia.s6_addr, brr->dptr, brr->dlen); + printf ("^^^ GOT REPLY AAAA........................ %d ", brr->dlen); + printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, MIO_COUNTOF(buf))); printf ("\n"); goto done; } case MIO_DNS_RRT_CNAME: printf ("^^^ GOT REPLY.... CNAME [%s] %d\n", brr->dptr, (int)brr->dlen); goto done; + case MIO_DNS_RRT_NS: + printf ("^^^ GOT REPLY.... NS [%s] %d\n", brr->dptr, (int)brr->dlen); + goto done; + case MIO_DNS_RRT_PTR: + printf ("^^^ GOT REPLY.... PTR [%s] %d\n", brr->dptr, (int)brr->dlen); + goto done; default: goto no_valid_reply; } @@ -925,8 +931,11 @@ for (i = 0; i < 5; i++) reply_tmout.nsec = 0; - mio_bcstrtoskad (mio, "8.8.8.8:53", &servaddr); + //mio_bcstrtoskad (mio, "8.8.8.8:53", &servaddr); + mio_bcstrtoskad (mio, "[fe80::c7e2:bd6e:1209:ac1b]: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 { mio_dns_bqr_t qrs[] = { @@ -993,7 +1002,14 @@ for (i = 0; i < 5; i++) mio_svc_dnc_sendreq (dnc, &qhdr, &qrs[0], &qedns, MIO_NULL, 0); mio_svc_dnc_sendmsg (dnc, &rhdr, qrs, MIO_COUNTOF(qrs), rrs, MIO_COUNTOF(rrs), &qedns, MIO_NULL, 0); } +#endif +if (!mio_svc_dnc_resolve(dnc, "a.wild.com", MIO_DNS_RRT_A, 0, on_dnc_resolve, 0)) +{ + printf ("resolve attempt failure ---> a.wild.com\n"); +} + +#if 0 if (!mio_svc_dnc_resolve(dnc, "www.microsoft.com", MIO_DNS_RRT_CNAME, 0, on_dnc_resolve, 0)) { printf ("resolve attempt failure ---> code.miflux.com\n"); @@ -1015,6 +1031,7 @@ if (!mio_svc_dnc_resolve(dnc, "google.com", MIO_DNS_RRT_SOA, MIO_SVC_DNC_RESOLVE { printf ("resolve attempt failure ---> code.miflux.com\n"); } +#endif mio_loop (mio); diff --git a/mio/lib/dns.c b/mio/lib/dns.c index d5bd93d..8cf9eee 100644 --- a/mio/lib/dns.c +++ b/mio/lib/dns.c @@ -244,7 +244,8 @@ static int dnc_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, { mio_dns_pkt_t* reqpkt = mio_dns_msg_to_pkt(reqmsg); dnc_dns_msg_xtn_t* msgxtn = dnc_dns_msg_getxtn(reqmsg); - if (dev == (mio_dev_sck_t*)msgxtn->dev && pkt->id == reqpkt->id && mio_equal_skads(&msgxtn->servaddr, srcaddr)) + + if (dev == (mio_dev_sck_t*)msgxtn->dev && pkt->id == reqpkt->id && mio_equal_skads(&msgxtn->servaddr, srcaddr, 0)) { MIO_DEBUG1 (mio, "received dns response...id %d\n", id); if (MIO_LIKELY(msgxtn->on_reply)) @@ -270,7 +271,7 @@ static void dnc_on_reply_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob MIO_ASSERT (mio, msgxtn->rtmridx == MIO_TMRIDX_INVALID); -MIO_DEBUG0 (mio, "unable to receive dns response in time...\n"); +MIO_DEBUG0 (mio, "*** TIMEOUT ==> unable to receive dns response in time...\n"); if (msgxtn->rtries < msgxtn->rmaxtries) { mio_ntime_t* tmout; @@ -384,7 +385,20 @@ mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const dnc->reply_tmout_max_tries = reply_tmout_max_tries; MIO_MEMSET (&mkinfo, 0, MIO_SIZEOF(mkinfo)); - mkinfo.type = MIO_DEV_SCK_UDP4; /* or UDP6 depending on the binding address */ + switch (mio_skad_family(serv_addr)) + { + case MIO_AF_INET: + mkinfo.type = MIO_DEV_SCK_UDP4; + break; + + case MIO_AF_INET6: + mkinfo.type = MIO_DEV_SCK_UDP6; + break; + + default: + mio_seterrnum (mio, MIO_EINVAL); + goto oops; + } mkinfo.on_write = dnc_on_write; mkinfo.on_read = dnc_on_read; mkinfo.on_connect = dnc_on_connect; @@ -490,11 +504,15 @@ static void on_dnc_resolve (mio_svc_dnc_t* dnc, mio_dns_msg_t* reqmsg, mio_errnu mio_dns_pkt_info_t* pi = MIO_NULL; dnc_dns_msg_resolve_xtn_t* reqmsgxtn = dnc_dns_msg_resolve_getxtn(reqmsg); - MIO_ASSERT (mio, dlen >= MIO_SIZEOF(*pkt)); /* this is guaranteed by the dnc_on_read() */ - pkt = (mio_dns_pkt_t*)data; - if (pkt->tc && (reqmsgxtn->flags & MIO_SVC_DNC_RESOLVE_FLAG_TCP_IF_TC)) /* truncated */ + if (data) { - /* TODO: */ + MIO_ASSERT (mio, dlen >= MIO_SIZEOF(*pkt)); /* this is guaranteed by the dnc_on_read() */ + + pkt = (mio_dns_pkt_t*)data; + if (pkt->tc && (reqmsgxtn->flags & MIO_SVC_DNC_RESOLVE_FLAG_TCP_IF_TC)) /* truncated */ + { + /* TODO: */ + } } if (!(reqmsgxtn->flags & MIO_SVC_DNC_RESOLVE_FLAG_BRIEF)) diff --git a/mio/lib/mio-skad.h b/mio/lib/mio-skad.h index a05dba1..da1729f 100644 --- a/mio/lib/mio-skad.h +++ b/mio/lib/mio-skad.h @@ -175,7 +175,8 @@ MIO_EXPORT void mio_clear_skad ( MIO_EXPORT int mio_equal_skads ( const mio_skad_t* addr1, - const mio_skad_t* addr2 + const mio_skad_t* addr2, + int strict ); #if defined(__cplusplus) diff --git a/mio/lib/sck.c b/mio/lib/sck.c index c84c7b7..6186564 100644 --- a/mio/lib/sck.c +++ b/mio/lib/sck.c @@ -1237,7 +1237,7 @@ accept_done: clidev->orgdstaddr = rdev->localaddr; #endif - if (!mio_equal_skads(&clidev->orgdstaddr, &clidev->localaddr)) + if (!mio_equal_skads(&clidev->orgdstaddr, &clidev->localaddr, 0)) { clidev->state |= MIO_DEV_SCK_INTERCEPTED; } diff --git a/mio/lib/skad.c b/mio/lib/skad.c index b601087..50a5414 100644 --- a/mio/lib/skad.c +++ b/mio/lib/skad.c @@ -1438,9 +1438,42 @@ void mio_clear_skad (mio_skad_t* _skad) skad->sa.sa_family = MIO_AF_UNSPEC; } -int mio_equal_skads (const mio_skad_t* addr1, const mio_skad_t* addr2) +int mio_equal_skads (const mio_skad_t* addr1, const mio_skad_t* addr2, int strict) { - return mio_skad_family(addr1) == mio_skad_family(addr2) && - mio_skad_size(addr1) == mio_skad_size(addr2) && - MIO_MEMCMP(addr1, addr2, mio_skad_size(addr1)) == 0; + int f1; + + if ((f1 = mio_skad_family(addr1)) != mio_skad_family(addr2) || + mio_skad_size(addr1) != mio_skad_size(addr2)) return 0; + + switch (f1) + { + case AF_INET: + return ((struct sockaddr_in*)addr1)->sin_addr.s_addr == ((struct sockaddr_in*)addr2)->sin_addr.s_addr && + ((struct sockaddr_in*)addr1)->sin_port == ((struct sockaddr_in*)addr2)->sin_port; + + #if defined(AF_INET6) + case AF_INET6: + + if (strict) + { + /* don't care about scope id */ + return MIO_MEMCMP(&((struct sockaddr_in6*)addr1)->sin6_addr, &((struct sockaddr_in6*)addr2)->sin6_addr, MIO_SIZEOF(((struct sockaddr_in6*)addr2)->sin6_addr)) == 0 && + ((struct sockaddr_in6*)addr1)->sin6_port == ((struct sockaddr_in6*)addr2)->sin6_port && + ((struct sockaddr_in6*)addr1)->sin6_scope_id == ((struct sockaddr_in6*)addr2)->sin6_scope_id; + } + else + { + return MIO_MEMCMP(&((struct sockaddr_in6*)addr1)->sin6_addr, &((struct sockaddr_in6*)addr2)->sin6_addr, MIO_SIZEOF(((struct sockaddr_in6*)addr2)->sin6_addr)) == 0 && + ((struct sockaddr_in6*)addr1)->sin6_port == ((struct sockaddr_in6*)addr2)->sin6_port; + } + #endif + + #if defined(AF_UNIX) + case AF_UNIX: + return mio_comp_bcstr(((struct sockaddr_un*)addr1)->sun_path, ((struct sockaddr_un*)addr2)->sun_path) == 0; + #endif + + default: + return MIO_MEMCMP(addr1, addr2, mio_skad_size(addr1)) == 0; + } }