From e19490a505487d2ee07f94a81cbda2ca9f1c9c82 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 20 Feb 2020 06:21:53 +0000 Subject: [PATCH] changed to remember the server address for each dns message sent --- mio/bin/execd.c | 2 +- mio/bin/t01.c | 14 +++++++------- mio/lib/dns.c | 19 +++++++++++++------ mio/lib/mio-dns.h | 7 ++++--- mio/lib/mio-skad.h | 4 ++++ 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/mio/bin/execd.c b/mio/bin/execd.c index ada2086..2f03ad4 100644 --- a/mio/bin/execd.c +++ b/mio/bin/execd.c @@ -365,7 +365,7 @@ int main (int argc, char* argv[]) ts->tally = 0; memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); - mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr); + mio_bcstrtoskad(mio, "127.0.0.1:9999", &tcp_conn.remoteaddr); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0); tcp_conn.options = 0; if (mio_dev_sck_connect(tcpsvr, &tcp_conn) <= -1) diff --git a/mio/bin/t01.c b/mio/bin/t01.c index 78967a8..fa40827 100644 --- a/mio/bin/t01.c +++ b/mio/bin/t01.c @@ -429,7 +429,7 @@ static void send_icmp (mio_dev_sck_t* dev, mio_uint16_t seq) mio_icmphdr_t* icmphdr; mio_uint8_t buf[512]; - mio_bcharstoskad (mio, "192.168.9.1", 11, &dstaddr); + mio_bcstrtoskad (mio, "192.168.9.1", &dstaddr); memset(buf, 0, MIO_SIZEOF(buf)); icmphdr = (mio_icmphdr_t*)buf; @@ -789,7 +789,7 @@ int main (int argc, char* argv[]) memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); /* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */ - mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr); + mio_bcstrtoskad(mio, "127.0.0.1:9999", &tcp_conn.remoteaddr); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0); tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL; @@ -817,7 +817,7 @@ int main (int argc, char* argv[]) ts->tally = 0; memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind)); - mio_bcharstoskad(mio, "0.0.0.0:1234", 14, &tcp_bind.localaddr); + mio_bcstrtoskad(mio, "0.0.0.0:1234", &tcp_bind.localaddr); tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR; if (mio_dev_sck_bind(tcp[1],&tcp_bind) <= -1) @@ -854,7 +854,7 @@ int main (int argc, char* argv[]) memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind)); - mio_bcharstoskad(mio, "0.0.0.0:1235", 14, &tcp_bind.localaddr); + mio_bcstrtoskad(mio, "0.0.0.0:1235", &tcp_bind.localaddr); tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR /*| MIO_DEV_SCK_BIND_REUSEPORT |*/; #if defined(USE_SSL) tcp_bind.options |= MIO_DEV_SCK_BIND_SSL; @@ -925,8 +925,8 @@ for (i = 0; i < 5; i++) reply_tmout.nsec = 0; - mio_bcharstoskad (mio, "1.1.1.1:53", 10, &servaddr); - dnc = mio_svc_dnc_start (mio, &servaddr, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */ + mio_bcstrtoskad (mio, "8.8.8.8:53", &servaddr); + dnc = mio_svc_dnc_start (mio, &servaddr, MIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */ { mio_dns_bqr_t qrs[] = { @@ -1073,7 +1073,7 @@ int main (int argc, char* argv[]) ts->tally = 0; memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); - mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr); + mio_bcstroskad(mio, "127.0.0.1:9999", &tcp_conn.remoteaddr); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0); tcp_conn.options = 0; diff --git a/mio/lib/dns.c b/mio/lib/dns.c index 87b8d97..d5bd93d 100644 --- a/mio/lib/dns.c +++ b/mio/lib/dns.c @@ -133,6 +133,7 @@ struct dnc_dns_msg_xtn_t mio_tmridx_t rtmridx; mio_dns_msg_t* prev; mio_dns_msg_t* next; + mio_skad_t servaddr; mio_svc_dnc_on_reply_t on_reply; mio_ntime_t wtmout; mio_ntime_t rtmout; @@ -243,7 +244,7 @@ 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) /* TODO: check the source address against the target address */ + if (dev == (mio_dev_sck_t*)msgxtn->dev && pkt->id == reqpkt->id && mio_equal_skads(&msgxtn->servaddr, srcaddr)) { MIO_DEBUG1 (mio, "received dns response...id %d\n", id); if (MIO_LIKELY(msgxtn->on_reply)) @@ -275,7 +276,7 @@ MIO_DEBUG0 (mio, "unable to receive dns response in time...\n"); mio_ntime_t* tmout; tmout = MIO_IS_POS_NTIME(&msgxtn->wtmout)? &msgxtn->wtmout: MIO_NULL; - if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(reqmsg), reqmsg->pktlen, tmout, reqmsg, &dnc->serv_addr) <= -1) + if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(reqmsg), reqmsg->pktlen, tmout, reqmsg, &msgxtn->servaddr) <= -1) { if (MIO_LIKELY(msgxtn->on_reply)) msgxtn->on_reply (dnc, reqmsg, MIO_ETMOUT, MIO_NULL, 0); @@ -366,7 +367,7 @@ static void dnc_on_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_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 reply_tmout_max_tries) { mio_svc_dnc_t* dnc = MIO_NULL; mio_dev_sck_make_t mkinfo; @@ -394,8 +395,13 @@ mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const xtn = (dnc_sck_xtn_t*)mio_dev_sck_getxtn(dnc->sck); xtn->dnc = dnc; - /* TODO: bind if requested */ - /*if (mio_dev_sck_bind(dev, ....) <= -1) goto oops;*/ + if (bind_addr) /* TODO: get mio_dev_sck_bind_t? instead of bind_addr? */ + { + mio_dev_sck_bind_t bi; + MIO_MEMSET (&bi, 0, MIO_SIZEOF(bi)); + bi.localaddr = *bind_addr; + if (mio_dev_sck_bind(dnc->sck, &bi) <= -1) goto oops; + } MIO_SVC_REGISTER (mio, (mio_svc_t*)dnc); return dnc; @@ -435,10 +441,11 @@ mio_dns_msg_t* mio_svc_dnc_sendmsg (mio_svc_dnc_t* dnc, mio_dns_bhdr_t* bdns, mi msgxtn->rtmout = dnc->reply_tmout; msgxtn->rmaxtries = dnc->reply_tmout_max_tries; msgxtn->rtries = 0; + msgxtn->servaddr = dnc->serv_addr; /* TODO: optionally, override dnc->serv_addr and use the target address passed as a parameter */ tmout = MIO_IS_POS_NTIME(&msgxtn->wtmout)? &msgxtn->wtmout: MIO_NULL; - if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(msg), msg->pktlen, tmout, msg, &dnc->serv_addr) <= -1) + if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(msg), msg->pktlen, tmout, msg, &msgxtn->servaddr) <= -1) { release_dns_msg (dnc, msg); return MIO_NULL; diff --git a/mio/lib/mio-dns.h b/mio/lib/mio-dns.h index 513c0c4..68560be 100644 --- a/mio/lib/mio-dns.h +++ b/mio/lib/mio-dns.h @@ -444,9 +444,10 @@ extern "C" { MIO_EXPORT mio_svc_dnc_t* mio_svc_dnc_start ( mio_t* mio, - const mio_skad_t* serv_addr, - const mio_ntime_t* send_tmout, - const mio_ntime_t* reply_tmout, + const mio_skad_t* serv_addr, /* required */ + 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 ); diff --git a/mio/lib/mio-skad.h b/mio/lib/mio-skad.h index 4c38645..a05dba1 100644 --- a/mio/lib/mio-skad.h +++ b/mio/lib/mio-skad.h @@ -106,6 +106,10 @@ MIO_EXPORT int mio_bcharstoskad ( mio_skad_t* skad ); +#define mio_bcstrtoskad(mio,str,skad) mio_bcharstoskad(mio, str, mio_count_bcstr(str), skad) +#define mio_ucstrtoskad(mio,str,skad) mio_ucharstoskad(mio, str, mio_count_ucstr(str), skad) + + MIO_EXPORT mio_oow_t mio_skadtoucstr ( mio_t* mio, const mio_skad_t* skad,