Commit 083d3986 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by rojer

Only copy questions when creating reply

In particular, there may be additional records which should not be copied

PUBLISHED_FROM=6b348868cb62d7b3fc4df0e935ffd5a31a314a08
parent 46618311
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
}, },
{ {
"type": "markdown", "type": "markdown",
"name": "mg_dns_copy_body.md" "name": "mg_dns_copy_questions.md"
}, },
{ {
"type": "markdown", "type": "markdown",
......
--- ---
title: "mg_dns_copy_body()" title: "mg_dns_copy_questions()"
decl_name: "mg_dns_copy_body" decl_name: "mg_dns_copy_questions"
symbol_kind: "func" symbol_kind: "func"
signature: | signature: |
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);
--- ---
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 This is useful when generating a DNS reply message which includes
all question records. all question records.
......
...@@ -23,28 +23,35 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { ...@@ -23,28 +23,35 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
int i; int i;
switch (ev) { 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; 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++) { for (i = 0; i < msg->num_questions; i++) {
char rname[256];
rr = &msg->questions[i]; 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) { if (rr->rtype == MG_DNS_A_RECORD) {
mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 3600, mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 10, &s_our_ip_addr,
&s_our_ip_addr, 4); 4);
} }
} }
/* /*
* We don't set the error flag even if there were no answers * We don't set the error flag even if there were no answers
* maching the MG_DNS_A_RECORD query type. * matching the MG_DNS_A_RECORD query type.
* This indicates that we have (syntetic) answers for MG_DNS_A_RECORD. * This indicates that we have (synthetic) answers for MG_DNS_A_RECORD.
* See http://goo.gl/QWvufr for a distinction between NXDOMAIN and NODATA. * See http://goo.gl/QWvufr for a distinction between NXDOMAIN and NODATA.
*/ */
mg_dns_send_reply(nc, &reply); mg_dns_send_reply(nc, &reply);
nc->flags |= MG_F_SEND_AND_CLOSE; nc->flags |= MG_F_SEND_AND_CLOSE;
mbuf_free(&reply_buf);
break; break;
}
} }
} }
...@@ -55,6 +62,7 @@ int main(int argc, char *argv[]) { ...@@ -55,6 +62,7 @@ int main(int argc, char *argv[]) {
mg_mgr_init(&mgr, NULL); mg_mgr_init(&mgr, NULL);
s_our_ip_addr = inet_addr("127.0.0.1"); s_our_ip_addr = inet_addr("127.0.0.1");
cs_log_set_level(LL_INFO);
/* Parse command line arguments */ /* Parse command line arguments */
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
......
...@@ -8841,9 +8841,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos, ...@@ -8841,9 +8841,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos,
return mbuf_insert(io, pos, &header, sizeof(header)); return mbuf_insert(io, pos, &header, sizeof(header));
} }
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) {
return mbuf_append(io, msg->pkt.p + sizeof(struct mg_dns_header), unsigned char *begin, *end;
msg->pkt.len - sizeof(struct mg_dns_header)); 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) { 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, ...@@ -9150,7 +9155,7 @@ struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
/* reply + recursion allowed */ /* reply + recursion allowed */
msg->flags |= 0x8080; msg->flags |= 0x8080;
mg_dns_copy_body(io, msg); mg_dns_copy_questions(io, msg);
msg->num_answers = 0; msg->num_answers = 0;
return rep; return rep;
......
...@@ -3253,14 +3253,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos, ...@@ -3253,14 +3253,14 @@ int mg_dns_insert_header(struct mbuf *io, size_t pos,
struct mg_dns_message *msg); 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 * This is useful when generating a DNS reply message which includes
* all question records. * all question records.
* *
* Return number of appened bytes. * 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. * Encode and append a DNS resource record to an IO buffer.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment