some code for edns

This commit is contained in:
hyung-hwan 2019-04-09 09:45:02 +00:00
parent 9cc53fc237
commit 9217fcec55
3 changed files with 52 additions and 18 deletions

View File

@ -28,11 +28,13 @@
#include <mio-sck.h> #include <mio-sck.h>
#include "mio-prv.h" #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')) #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; 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 struct mio_dnss_t
{ {
MIO_SVC_HEADERS; MIO_SVC_HEADERS;
MIO_DNS_HEADERS;
}; };
struct mio_dnsc_t struct mio_dnsc_t
{ {
MIO_SVC_HEADERS; MIO_SVC_HEADERS;
MIO_DNS_HEADERS;
mio_dev_sck_t* sck; mio_dev_sck_t* sck;
mio_sckaddr_t serveraddr; 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); 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_t* mio = dnsc->mio;
mio_oow_t dnlen, msgbufsz, i; 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++) for (i = 0; i < qr_count; i++)
{ {
/* dnlen includes the ending <zero> */ /* dnlen includes the ending <zero> */
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) if (dnlen <= 0)
{ {
release_req_msg (dnsc, msg); 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); 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; mio_oow_t match_count = 0;
for (i = 0; i < rr_count; i++) for (i = 0; i < rr_count; i++)
{ {
if (rr[i].part == rr_sect) 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) if (dnlen <= 0)
{ {
release_req_msg (dnsc, msg); 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_dns_msg_t* msg;
mio_oow_t msglen; 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; if (!msg) return -1;
msg->rd = 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_dns_msg_t* msg;
mio_oow_t msglen; 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; if (!msg) return -1;
msg->rd = 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_dns_msg_t* msg;
mio_oow_t msglen; 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; if (!msg) return -1;
msg->rd = 1; msg->rd = 1;

View File

@ -820,9 +820,9 @@ for (i = 0; i < 5; i++)
}; };
mio_dns_brr_t rrs[] = 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_RR_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_RR_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_AUTHORITY, "dns.miflux.com", MIO_DNS_QTYPE_NS, MIO_DNS_QCLASS_IN, 86400, 0, MIO_NULL }
}; };
mio_dnsc_sendreq (dnsc, qrs, MIO_COUNTOF(qrs)); mio_dnsc_sendreq (dnsc, qrs, MIO_COUNTOF(qrs));

View File

@ -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_dnss_t mio_dnss_t;
typedef struct mio_dnsc_t mio_dnsc_t; typedef struct mio_dnsc_t mio_dnsc_t;
/* breakdown of question record */
struct mio_dns_bqr_t struct mio_dns_bqr_t
{ {
mio_bch_t* qname; mio_bch_t* qname;
@ -181,14 +182,16 @@ struct mio_dns_bqr_t
}; };
typedef struct mio_dns_bqr_t mio_dns_bqr_t; typedef struct mio_dns_bqr_t mio_dns_bqr_t;
enum mio_dns_rr_part_t enum mio_dns_rr_part_t
{ {
MIO_DNS_RRR_PART_ANSWER, MIO_DNS_RR_PART_ANSWER,
MIO_DNS_RRR_PART_AUTHORITY, MIO_DNS_RR_PART_AUTHORITY,
MIO_DNS_RRR_PART_ADDITIONAL MIO_DNS_RR_PART_ADDITIONAL
}; };
typedef enum mio_dns_rr_part_t mio_dns_rr_part_t; typedef enum mio_dns_rr_part_t mio_dns_rr_part_t;
/* breakdown of resource record */
struct mio_dns_brr_t struct mio_dns_brr_t
{ {
mio_dns_rr_part_t part; mio_dns_rr_part_t part;
@ -201,6 +204,24 @@ struct mio_dns_brr_t
}; };
typedef struct mio_dns_brr_t 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) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif