From 083d398631fca4ded51da79d1baefa0500b18676 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Mon, 18 Apr 2016 23:51:31 +0200 Subject: [PATCH] Only copy questions when creating reply In particular, there may be additional records which should not be copied PUBLISHED_FROM=6b348868cb62d7b3fc4df0e935ffd5a31a314a08 --- docs/c-api/dns.h/items.json | 2 +- docs/c-api/dns.h/mg_dns_copy_body.md | 15 -------------- docs/c-api/dns.h/mg_dns_copy_questions.md | 15 ++++++++++++++ .../captive_dns_server/captive_dns_server.c | 20 +++++++++++++------ mongoose.c | 13 ++++++++---- mongoose.h | 4 ++-- 6 files changed, 41 insertions(+), 28 deletions(-) delete mode 100644 docs/c-api/dns.h/mg_dns_copy_body.md create mode 100644 docs/c-api/dns.h/mg_dns_copy_questions.md diff --git a/docs/c-api/dns.h/items.json b/docs/c-api/dns.h/items.json index aa3452e7..8d21dd80 100644 --- a/docs/c-api/dns.h/items.json +++ b/docs/c-api/dns.h/items.json @@ -18,7 +18,7 @@ }, { "type": "markdown", - "name": "mg_dns_copy_body.md" + "name": "mg_dns_copy_questions.md" }, { "type": "markdown", diff --git a/docs/c-api/dns.h/mg_dns_copy_body.md b/docs/c-api/dns.h/mg_dns_copy_body.md deleted file mode 100644 index b2ff61e6..00000000 --- a/docs/c-api/dns.h/mg_dns_copy_body.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: "mg_dns_copy_body()" -decl_name: "mg_dns_copy_body" -symbol_kind: "func" -signature: | - int mg_dns_copy_body(struct mbuf *io, struct mg_dns_message *msg); ---- - -Append already encoded body from an existing message. - -This is useful when generating a DNS reply message which includes -all question records. - -Return number of appened bytes. - diff --git a/docs/c-api/dns.h/mg_dns_copy_questions.md b/docs/c-api/dns.h/mg_dns_copy_questions.md new file mode 100644 index 00000000..9fc2091f --- /dev/null +++ b/docs/c-api/dns.h/mg_dns_copy_questions.md @@ -0,0 +1,15 @@ +--- +title: "mg_dns_copy_questions()" +decl_name: "mg_dns_copy_questions" +symbol_kind: "func" +signature: | + int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg); +--- + +Append already encoded questions from an existing message. + +This is useful when generating a DNS reply message which includes +all question records. + +Return number of appened bytes. + diff --git a/examples/captive_dns_server/captive_dns_server.c b/examples/captive_dns_server/captive_dns_server.c index ac5781c2..0f170435 100644 --- a/examples/captive_dns_server/captive_dns_server.c +++ b/examples/captive_dns_server/captive_dns_server.c @@ -23,28 +23,35 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { int i; switch (ev) { - case MG_DNS_MESSAGE: + case MG_DNS_MESSAGE: { + struct mbuf reply_buf; + mbuf_init(&reply_buf, 512); msg = (struct mg_dns_message *) ev_data; - reply = mg_dns_create_reply(&nc->send_mbuf, msg); + reply = mg_dns_create_reply(&reply_buf, msg); for (i = 0; i < msg->num_questions; i++) { + char rname[256]; rr = &msg->questions[i]; + mg_dns_uncompress_name(msg, &rr->name, rname, sizeof(rname) - 1); + LOG(LL_INFO, ("Q type %d name %s", rr->rtype, rname)); if (rr->rtype == MG_DNS_A_RECORD) { - mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 3600, - &s_our_ip_addr, 4); + mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 10, &s_our_ip_addr, + 4); } } /* * We don't set the error flag even if there were no answers - * maching the MG_DNS_A_RECORD query type. - * This indicates that we have (syntetic) answers for MG_DNS_A_RECORD. + * matching the MG_DNS_A_RECORD query type. + * This indicates that we have (synthetic) answers for MG_DNS_A_RECORD. * See http://goo.gl/QWvufr for a distinction between NXDOMAIN and NODATA. */ mg_dns_send_reply(nc, &reply); nc->flags |= MG_F_SEND_AND_CLOSE; + mbuf_free(&reply_buf); break; + } } } @@ -55,6 +62,7 @@ int main(int argc, char *argv[]) { mg_mgr_init(&mgr, NULL); s_our_ip_addr = inet_addr("127.0.0.1"); + cs_log_set_level(LL_INFO); /* Parse command line arguments */ for (i = 1; i < argc; i++) { diff --git a/mongoose.c b/mongoose.c index b33b42c5..859c30ff 100644 --- a/mongoose.c +++ b/mongoose.c @@ -8841,9 +8841,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos, return mbuf_insert(io, pos, &header, sizeof(header)); } -int mg_dns_copy_body(struct mbuf *io, struct mg_dns_message *msg) { - return mbuf_append(io, msg->pkt.p + sizeof(struct mg_dns_header), - msg->pkt.len - sizeof(struct mg_dns_header)); +int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) { + unsigned char *begin, *end; + struct mg_dns_resource_record *last_q; + if (msg->num_questions <= 0) return 0; + begin = (unsigned char *) msg->pkt.p + sizeof(struct mg_dns_header); + last_q = &msg->questions[msg->num_questions - 1]; + end = (unsigned char *) last_q->name.p + last_q->name.len + 4; + return mbuf_append(io, begin, end - begin); } static int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) { @@ -9150,7 +9155,7 @@ struct mg_dns_reply mg_dns_create_reply(struct mbuf *io, /* reply + recursion allowed */ msg->flags |= 0x8080; - mg_dns_copy_body(io, msg); + mg_dns_copy_questions(io, msg); msg->num_answers = 0; return rep; diff --git a/mongoose.h b/mongoose.h index 9626e4d8..4063847d 100644 --- a/mongoose.h +++ b/mongoose.h @@ -3253,14 +3253,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos, struct mg_dns_message *msg); /* - * Append already encoded body from an existing message. + * Append already encoded questions from an existing message. * * This is useful when generating a DNS reply message which includes * all question records. * * Return number of appened bytes. */ -int mg_dns_copy_body(struct mbuf *io, struct mg_dns_message *msg); +int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg); /* * Encode and append a DNS resource record to an IO buffer.