Only copy questions when creating reply

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

PUBLISHED_FROM=6b348868cb62d7b3fc4df0e935ffd5a31a314a08
This commit is contained in:
Deomid Ryabkov 2016-04-18 23:51:31 +02:00 committed by rojer
parent 466183113f
commit 083d398631
6 changed files with 41 additions and 28 deletions

View File

@ -18,7 +18,7 @@
},
{
"type": "markdown",
"name": "mg_dns_copy_body.md"
"name": "mg_dns_copy_questions.md"
},
{
"type": "markdown",

View File

@ -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.

View File

@ -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.

View File

@ -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++) {

View File

@ -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;

View File

@ -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.