1 #ifdef HAVE_ALLOCA_H
2 # include <alloca.h>
3 #endif
4 
5 #include <sys/types.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 #include <sys/socket.h>
9 #include <assert.h>
10 #include <ctype.h>
11 #include <inttypes.h>
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <strings.h>
18 
19 #include "wdns.h"
20 
21 #include "record_descr.h"
22 #include "libmy/b32_decode.h"
23 #include "libmy/b32_encode.h"
24 #include "libmy/b64_decode.h"
25 #include "libmy/b64_encode.h"
26 
27 #include "libmy/my_alloc.h"
28 #include "libmy/ubuf.h"
29 
30 #define load_net16(buf, out) do { \
31 	uint16_t _my_16; \
32 	memcpy(&_my_16, buf, sizeof(uint16_t)); \
33 	_my_16 = ntohs(_my_16); \
34 	*(out) = _my_16; \
35 } while (0)
36 
37 #define load_net32(buf, out) do { \
38 	uint32_t _my_32; \
39 	memcpy(&_my_32, buf, sizeof(uint32_t)); \
40 	_my_32 = ntohl(_my_32); \
41 	*(out) = _my_32; \
42 } while (0)
43 
44 /**
45  * Advance pointer p by sz bytes and update len.
46  */
47 #define WDNS_BUF_ADVANCE(p, len, sz) do { \
48 	p += sz; \
49 	len -= sz; \
50 } while (0)
51 
52 /**
53  * Read an 8 bit integer.
54  */
55 #define WDNS_BUF_GET8(dst, src) do { \
56 	memcpy(&dst, src, 1); \
57 	src++; \
58 } while (0)
59 
60 /**
61  * Read a 16 bit integer.
62  */
63 #define WDNS_BUF_GET16(dst, src) do { \
64 	memcpy(&dst, src, 2); \
65 	dst = ntohs(dst); \
66 	src += 2; \
67 } while (0)
68 
69 /**
70  * Read a 32 bit integer.
71  */
72 #define WDNS_BUF_GET32(dst, src) do { \
73 	memcpy(&dst, src, 4); \
74 	dst = ntohl(dst); \
75 	src += 4; \
76 } while (0)
77 
78 wdns_res
79 _wdns_insert_rr_rrset_array(wdns_rrset_array_t *a, wdns_rr_t *rr, unsigned sec);
80 
81 wdns_res
82 _wdns_parse_edns(wdns_message_t *m, wdns_rr_t *rr);
83 
84 wdns_res
85 _wdns_parse_rdata(wdns_rr_t *rr, const uint8_t *p, const uint8_t *eop,
86 		  const uint8_t *rdata, uint16_t rdlen);
87 
88 wdns_res
89 _wdns_parse_header(const uint8_t *p, size_t len, uint16_t *id, uint16_t *flags,
90 		   uint16_t *qdcount, uint16_t *ancount, uint16_t *nscount, uint16_t *arcount);
91 
92 wdns_res
93 _wdns_parse_message_rr(unsigned sec, const uint8_t *p, const uint8_t *eop, const uint8_t *data,
94 		       size_t *rrsz, wdns_rr_t *rr);
95 
96 void
97 _wdns_rdata_to_ubuf(ubuf *, const uint8_t *rdata, uint16_t rdlen,
98 		    uint16_t rrtype, uint16_t rrclass);
99 
100 wdns_res
101 _wdns_str_to_rdata_ubuf(ubuf *, const char * str,
102 		uint16_t rrtype, uint16_t rrclass);
103 
104 void
105 _wdns_rr_to_ubuf(ubuf *, wdns_rr_t *rr, unsigned sec);
106 
107 void
108 _wdns_rrset_to_ubuf(ubuf *, wdns_rrset_t *rrset, unsigned sec);
109 
110 void
111 _wdns_rrset_array_to_ubuf(ubuf *, wdns_rrset_array_t *a, unsigned sec);
112