Skip to content

Commit

Permalink
Merge pull request #193 from NLnetLabs/features/edns-for-testns
Browse files Browse the repository at this point in the history
add EDNS support for ldns-testns
  • Loading branch information
TCY16 authored Nov 11, 2022
2 parents e400fa9 + bee23f1 commit 689403f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
19 changes: 17 additions & 2 deletions examples/ldns-testpkts.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ entry_add_reply(struct entry* entry)
pkt->packet_sleep = 0;
pkt->reply = ldns_pkt_new();
pkt->reply_from_hex = NULL;
pkt->raw_ednsdata = NULL;
/* link at end */
while(*p)
p = &((*p)->next);
Expand Down Expand Up @@ -324,11 +325,11 @@ data_buffer2wire(ldns_buffer *data_buffer)
size_t wirelen;
uint8_t *data_wire = (uint8_t *) ldns_buffer_begin(data_buffer);
uint8_t *wire = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN);

hexbuf = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN);
for (data_buf_pos = 0; data_buf_pos < ldns_buffer_position(data_buffer); data_buf_pos++) {
c = (int) data_wire[data_buf_pos];

if (state < 2 && !isascii(c)) {
/*verbose("non ascii character found in file: (%d) switching to raw mode\n", c);*/
state = 2;
Expand Down Expand Up @@ -502,14 +503,28 @@ read_entry(FILE* in, const char* name, int *lineno, uint32_t* default_ttl,
ldns_buffer_printf(hex_data_buffer, line);
} else if(str_keyword(&parse, "HEX_EDNSDATA_BEGIN")) {
hex_ednsdata_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN);

reading_hex_ednsdata = true;
} else if(str_keyword(&parse, "HEX_EDNSDATA_END")) {
ldns_buffer* edns = NULL;
if (!reading_hex_ednsdata) {
error("%s line %d: HEX_EDNSDATA_END read but no"
"HEX_EDNSDATA_BEGIN keyword seen", name, *lineno);
}
reading_hex_ednsdata = false;

edns = data_buffer2wire(hex_ednsdata_buffer);

/* add read-in EDNS directly to the reply */
ldns_pkt_set_edns_data(cur_reply->reply,
ldns_rdf_new_frm_data(LDNS_RDF_TYPE_UNKNOWN,
ldns_buffer_limit(edns),
ldns_buffer_begin(edns)));

/* store raw EDNS for matching */
cur_reply->raw_ednsdata = data_buffer2wire(hex_ednsdata_buffer);

ldns_buffer_free(edns);
ldns_buffer_free(hex_ednsdata_buffer);
hex_ednsdata_buffer = NULL;
} else if(reading_hex_ednsdata) {
Expand Down
7 changes: 7 additions & 0 deletions host2str.c
Original file line number Diff line number Diff line change
Expand Up @@ -2274,11 +2274,16 @@ ldns_edns_subnet2buffer_str(ldns_buffer* output, uint8_t* data, size_t len)
{
uint16_t family;
uint8_t source, scope;

ldns_buffer_printf(output, "; CLIENT SUBNET: ");

if(len < 4) {
ldns_buffer_printf(output, "malformed subnet ");
ldns_edns_hex_data2buffer_str(output, data, len);
return ldns_buffer_status(output);
}


family = ldns_read_uint16(data);
source = data[2];
scope = data[3];
Expand Down Expand Up @@ -2746,6 +2751,7 @@ ldns_pkt2buffer_str_fmt(ldns_buffer *output,
ldns_buffer_printf(output, "\n");

ldns_buffer_printf(output, ";; ADDITIONAL SECTION:\n");

for (i = 0; i < ldns_pkt_arcount(pkt); i++) {
status = ldns_rr2buffer_str_fmt(output, fmt,
ldns_rr_list_rr(
Expand Down Expand Up @@ -2788,6 +2794,7 @@ ldns_pkt2buffer_str_fmt(ldns_buffer *output,
} else {
ldns_buffer_printf(output, ";; Data: ");
(void)ldns_rdf2buffer_str(output, ldns_pkt_edns_data(pkt));
ldns_buffer_printf(output, "\n");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ pkt_edns_data2edns_option_list(const ldns_rdf *edns_data)
ldns_edns_option* edns;
uint8_t *data;

if (pos + 4 > max) { /* make sure the header is */
if (pos + 4 > max) { /* make sure the header fits */
ldns_edns_option_list_deep_free(edns_list);
return NULL;
}
Expand Down

0 comments on commit 689403f

Please sign in to comment.