changed to remember the server address for each dns message sent

This commit is contained in:
hyung-hwan 2020-02-20 06:21:53 +00:00
parent 3d20fc7421
commit e19490a505
5 changed files with 29 additions and 17 deletions

View File

@ -365,7 +365,7 @@ int main (int argc, char* argv[])
ts->tally = 0; ts->tally = 0;
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); 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); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = 0; tcp_conn.options = 0;
if (mio_dev_sck_connect(tcpsvr, &tcp_conn) <= -1) if (mio_dev_sck_connect(tcpsvr, &tcp_conn) <= -1)

View File

@ -429,7 +429,7 @@ static void send_icmp (mio_dev_sck_t* dev, mio_uint16_t seq)
mio_icmphdr_t* icmphdr; mio_icmphdr_t* icmphdr;
mio_uint8_t buf[512]; 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)); memset(buf, 0, MIO_SIZEOF(buf));
icmphdr = (mio_icmphdr_t*)buf; icmphdr = (mio_icmphdr_t*)buf;
@ -789,7 +789,7 @@ int main (int argc, char* argv[])
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn));
/* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */ /* 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); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL; tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL;
@ -817,7 +817,7 @@ int main (int argc, char* argv[])
ts->tally = 0; ts->tally = 0;
memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind)); 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; tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR;
if (mio_dev_sck_bind(tcp[1],&tcp_bind) <= -1) 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)); 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 |*/; tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR /*| MIO_DEV_SCK_BIND_REUSEPORT |*/;
#if defined(USE_SSL) #if defined(USE_SSL)
tcp_bind.options |= MIO_DEV_SCK_BIND_SSL; tcp_bind.options |= MIO_DEV_SCK_BIND_SSL;
@ -925,8 +925,8 @@ for (i = 0; i < 5; i++)
reply_tmout.nsec = 0; reply_tmout.nsec = 0;
mio_bcharstoskad (mio, "1.1.1.1:53", 10, &servaddr); mio_bcstrtoskad (mio, "8.8.8.8:53", &servaddr);
dnc = mio_svc_dnc_start (mio, &servaddr, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */ 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[] = mio_dns_bqr_t qrs[] =
{ {
@ -1073,7 +1073,7 @@ int main (int argc, char* argv[])
ts->tally = 0; ts->tally = 0;
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); 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); MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = 0; tcp_conn.options = 0;

View File

@ -133,6 +133,7 @@ struct dnc_dns_msg_xtn_t
mio_tmridx_t rtmridx; mio_tmridx_t rtmridx;
mio_dns_msg_t* prev; mio_dns_msg_t* prev;
mio_dns_msg_t* next; mio_dns_msg_t* next;
mio_skad_t servaddr;
mio_svc_dnc_on_reply_t on_reply; mio_svc_dnc_on_reply_t on_reply;
mio_ntime_t wtmout; mio_ntime_t wtmout;
mio_ntime_t rtmout; 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); mio_dns_pkt_t* reqpkt = mio_dns_msg_to_pkt(reqmsg);
dnc_dns_msg_xtn_t* msgxtn = dnc_dns_msg_getxtn(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); MIO_DEBUG1 (mio, "received dns response...id %d\n", id);
if (MIO_LIKELY(msgxtn->on_reply)) 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; mio_ntime_t* tmout;
tmout = MIO_IS_POS_NTIME(&msgxtn->wtmout)? &msgxtn->wtmout: MIO_NULL; 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)) if (MIO_LIKELY(msgxtn->on_reply))
msgxtn->on_reply (dnc, reqmsg, MIO_ETMOUT, MIO_NULL, 0); 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_svc_dnc_t* dnc = MIO_NULL;
mio_dev_sck_make_t mkinfo; 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_sck_xtn_t*)mio_dev_sck_getxtn(dnc->sck);
xtn->dnc = dnc; xtn->dnc = dnc;
/* TODO: bind if requested */ if (bind_addr) /* TODO: get mio_dev_sck_bind_t? instead of bind_addr? */
/*if (mio_dev_sck_bind(dev, ....) <= -1) goto oops;*/ {
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); MIO_SVC_REGISTER (mio, (mio_svc_t*)dnc);
return 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->rtmout = dnc->reply_tmout;
msgxtn->rmaxtries = dnc->reply_tmout_max_tries; msgxtn->rmaxtries = dnc->reply_tmout_max_tries;
msgxtn->rtries = 0; msgxtn->rtries = 0;
msgxtn->servaddr = dnc->serv_addr;
/* TODO: optionally, override dnc->serv_addr and use the target address passed as a parameter */ /* 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; 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); release_dns_msg (dnc, msg);
return MIO_NULL; return MIO_NULL;

View File

@ -444,9 +444,10 @@ extern "C" {
MIO_EXPORT mio_svc_dnc_t* mio_svc_dnc_start ( MIO_EXPORT mio_svc_dnc_t* mio_svc_dnc_start (
mio_t* mio, mio_t* mio,
const mio_skad_t* serv_addr, const mio_skad_t* serv_addr, /* required */
const mio_ntime_t* send_tmout, const mio_skad_t* bind_addr, /* optional. can be MIO_NULL */
const mio_ntime_t* reply_tmout, const mio_ntime_t* send_tmout, /* required */
const mio_ntime_t* reply_tmout, /* required */
mio_oow_t reply_tmout_max_tries mio_oow_t reply_tmout_max_tries
); );

View File

@ -106,6 +106,10 @@ MIO_EXPORT int mio_bcharstoskad (
mio_skad_t* skad 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_EXPORT mio_oow_t mio_skadtoucstr (
mio_t* mio, mio_t* mio,
const mio_skad_t* skad, const mio_skad_t* skad,