1 char *
wdns_message_to_str(wdns_message_t * m)2 wdns_message_to_str(wdns_message_t *m)
3 {
4 	const char *opcode;
5 	const char *rcode;
6 	char *ret;
7 	size_t retsz;
8 	ubuf *u;
9 
10 	u = ubuf_new();
11 
12 	ubuf_add_cstr(u, ";; ->>HEADER<<- ");
13 
14 	opcode = wdns_opcode_to_str(WDNS_FLAGS_OPCODE(*m));
15 	if (opcode != NULL)
16 		ubuf_add_fmt(u, "opcode: %s", opcode);
17 	else
18 		ubuf_add_fmt(u, "opcode: %hu", WDNS_FLAGS_OPCODE(*m));
19 
20 	rcode = wdns_rcode_to_str(WDNS_FLAGS_RCODE(*m));
21 	if (rcode != NULL)
22 		ubuf_add_fmt(u, ", rcode: %s", rcode);
23 	else
24 		ubuf_add_fmt(u, ", rcode: %hu", WDNS_FLAGS_RCODE(*m));
25 
26 	ubuf_add_fmt(u,
27 		     ", id: %hu\n"
28 		     ";; flags:%s%s%s%s%s%s%s; "
29 		     "QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
30 		     m->id,
31 		     WDNS_FLAGS_QR(*m) ? " qr" : "",
32 		     WDNS_FLAGS_AA(*m) ? " aa" : "",
33 		     WDNS_FLAGS_TC(*m) ? " tc" : "",
34 		     WDNS_FLAGS_RD(*m) ? " rd" : "",
35 		     WDNS_FLAGS_RA(*m) ? " ra" : "",
36 		     WDNS_FLAGS_AD(*m) ? " ad" : "",
37 		     WDNS_FLAGS_CD(*m) ? " cd" : "",
38 		     m->sections[0].n_rrs,
39 		     m->sections[1].n_rrs,
40 		     m->sections[2].n_rrs,
41 		     m->sections[3].n_rrs
42 	);
43 
44 	ubuf_add_cstr(u, "\n;; QUESTION SECTION:\n");
45 	_wdns_rrset_array_to_ubuf(u, &m->sections[WDNS_MSG_SEC_QUESTION], WDNS_MSG_SEC_QUESTION);
46 
47 	ubuf_add_cstr(u, "\n;; ANSWER SECTION:\n");
48 	_wdns_rrset_array_to_ubuf(u, &m->sections[WDNS_MSG_SEC_ANSWER], WDNS_MSG_SEC_ANSWER);
49 
50 	ubuf_add_cstr(u, "\n;; AUTHORITY SECTION:\n");
51 	_wdns_rrset_array_to_ubuf(u, &m->sections[WDNS_MSG_SEC_AUTHORITY], WDNS_MSG_SEC_AUTHORITY);
52 
53 	ubuf_add_cstr(u, "\n;; ADDITIONAL SECTION:\n");
54 	_wdns_rrset_array_to_ubuf(u, &m->sections[WDNS_MSG_SEC_ADDITIONAL], WDNS_MSG_SEC_ADDITIONAL);
55 
56 	ubuf_cterm(u);
57 	ubuf_detach(u, (uint8_t **) &ret, &retsz);
58 	ubuf_destroy(&u);
59 	return (ret);
60 }
61