create response header from wireformat

This commit is contained in:
Willem Toorop 2015-02-11 16:59:47 +01:00
parent f01ed133f5
commit 6a16a56717
1 changed files with 32 additions and 98 deletions

View File

@ -185,100 +185,44 @@ convert_rdf_to_str(ldns_rdf * rdf)
return ldns_rdf2str(rdf); return ldns_rdf2str(rdf);
} }
#define SET_WIRE_INT(X,Y) if (getdns_dict_set_int(result, #X , (int) \
GLDNS_ ## Y ## _WIRE(netreq->response))) break
#define SET_WIRE_CNT(X,Y) if (getdns_dict_set_int(result, #X , (int) \
GLDNS_ ## Y (netreq->response))) break
/* create the header dict */ /* create the header dict */
static struct getdns_dict * static struct getdns_dict *
create_reply_header_dict(struct getdns_context *context, ldns_pkt * reply) create_reply_header_dict(getdns_context *context, getdns_network_req *netreq)
{ {
/* { "id": 23456, "qr": 1, "opcode": 0, ... }, */ /* { "id": 23456, "qr": 1, "opcode": 0, ... }, */
int r = 0;
struct getdns_dict *result = getdns_dict_create_with_context(context); struct getdns_dict *result = getdns_dict_create_with_context(context);
if (!result) {
if (!result)
return NULL; return NULL;
}
/* cheat since we know GETDNS_RETURN_GOOD == 0 */
do { do {
r = getdns_dict_set_int(result, GETDNS_STR_KEY_ID, SET_WIRE_INT(id, ID);
ldns_pkt_id(reply)); SET_WIRE_INT(qr, QR);
if (r != GETDNS_RETURN_GOOD) SET_WIRE_INT(aa, AA);
break; SET_WIRE_INT(tc, TC);
SET_WIRE_INT(rd, RD);
SET_WIRE_INT(cd, CD);
SET_WIRE_INT(ra, RA);
SET_WIRE_INT(ad, AD);
SET_WIRE_INT(opcode, OPCODE);
SET_WIRE_INT(rcode, RCODE);
SET_WIRE_INT(z, Z);
/* set bits - seems like this could be macro-ified*/ SET_WIRE_CNT(qdcount, QDCOUNT);
r = getdns_dict_set_int(result, GETDNS_STR_KEY_QR, SET_WIRE_CNT(ancount, ANCOUNT);
(int) ldns_pkt_qr(reply)); SET_WIRE_CNT(nscount, NSCOUNT);
if (r != GETDNS_RETURN_GOOD) SET_WIRE_CNT(arcount, ARCOUNT);
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_AA, return result;
(int) ldns_pkt_aa(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_TC,
(int) ldns_pkt_tc(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_RD,
(int) ldns_pkt_rd(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_CD,
(int) ldns_pkt_cd(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_RA,
(int) ldns_pkt_ra(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_AD,
(int) ldns_pkt_ad(reply));
if (r != GETDNS_RETURN_GOOD)
break;
/* codes */
r = getdns_dict_set_int(result, GETDNS_STR_KEY_OPCODE,
(int) ldns_pkt_get_opcode(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_RCODE,
(int) ldns_pkt_get_rcode(reply));
if (r != GETDNS_RETURN_GOOD)
break;
/* default z to 0 */
r = getdns_dict_set_int(result, GETDNS_STR_KEY_Z, 0);
if (r != GETDNS_RETURN_GOOD)
break;
/* counts */
r = getdns_dict_set_int(result, GETDNS_STR_KEY_QDCOUNT,
(int) ldns_pkt_qdcount(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_ANCOUNT,
(int) ldns_pkt_ancount(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_NSCOUNT,
(int) ldns_pkt_nscount(reply));
if (r != GETDNS_RETURN_GOOD)
break;
r = getdns_dict_set_int(result, GETDNS_STR_KEY_ARCOUNT,
(int) ldns_pkt_arcount(reply));
} while (0); } while (0);
if (r != 0) {
getdns_dict_destroy(result); getdns_dict_destroy(result);
result = NULL; return NULL;
}
return result;
} }
/* helper to convert an rr_list to getdns_list. /* helper to convert an rr_list to getdns_list.
@ -424,7 +368,7 @@ create_reply_dict(struct getdns_context *context, getdns_network_req * req,
/* header */ /* header */
do { do {
subdict = create_reply_header_dict(context, reply); subdict = create_reply_header_dict(context, req);
r = getdns_dict_set_dict(result, GETDNS_STR_KEY_HEADER, subdict); r = getdns_dict_set_dict(result, GETDNS_STR_KEY_HEADER, subdict);
getdns_dict_destroy(subdict); getdns_dict_destroy(subdict);
if (r != GETDNS_RETURN_GOOD) { if (r != GETDNS_RETURN_GOOD) {
@ -555,6 +499,7 @@ create_getdns_response(getdns_dns_req *completed_request)
char *canonical_name = NULL; char *canonical_name = NULL;
getdns_return_t r = 0; getdns_return_t r = 0;
int nreplies = 0, nanswers = 0, nsecure = 0, ninsecure = 0, nbogus = 0; int nreplies = 0, nanswers = 0, nsecure = 0, ninsecure = 0, nbogus = 0;
struct getdns_bindata full_data;
/* info (bools) about dns_req */ /* info (bools) about dns_req */
int dnssec_return_status; int dnssec_return_status;
@ -630,17 +575,6 @@ create_getdns_response(getdns_dns_req *completed_request)
continue; continue;
else if (completed_request->dnssec_return_only_secure && ! netreq->secure) else if (completed_request->dnssec_return_only_secure && ! netreq->secure)
continue; continue;
}
struct getdns_bindata full_data;
full_data.data = NULL;
full_data.size = 0;
ldns_pkt *pkt = netreq->result;
ldns_status s =
ldns_pkt2wire(&(full_data.data), pkt, &(full_data.size));
if (s != LDNS_STATUS_OK) {
// break inner
r = GETDNS_RETURN_MEMORY_ERROR;
break;
} }
size_t idx = 0; size_t idx = 0;
/* reply tree */ /* reply tree */
@ -681,13 +615,13 @@ create_getdns_response(getdns_dns_req *completed_request)
/* buffer */ /* buffer */
r = getdns_list_add_item(replies_full, &idx); r = getdns_list_add_item(replies_full, &idx);
if (r != GETDNS_RETURN_GOOD) { if (r != GETDNS_RETURN_GOOD) {
free(full_data.data);
// break inner while // break inner while
break; break;
} }
full_data.data = netreq->response;
full_data.size = netreq->response_len;
r = getdns_list_set_bindata(replies_full, idx, r = getdns_list_set_bindata(replies_full, idx,
&full_data); &full_data);
free(full_data.data);
if (r != GETDNS_RETURN_GOOD) { if (r != GETDNS_RETURN_GOOD) {
free(full_data.data); free(full_data.data);
// break inner while // break inner while