1 /*
2  * udbzone -- store zone and rrset information in udb file.
3  *
4  * Copyright (c) 2011, NLnet Labs.  See LICENSE for license.
5  */
6 #ifndef UDB_ZONE_H
7 #define UDB_ZONE_H
8 #include "udb.h"
9 #include "dns.h"
10 #include "udbradtree.h"
11 
12 /**
13  * Store the DNS information in udb file on disk.
14  * udb_global
15  *     |
16  *     v
17  * zonetree -> zone -- zone_name
18  * radtree       |--> nsec3param
19  *               |--> log_str
20  *               |--> file_str
21  *               |
22  *               v
23  *            domain --> rrset -> rr
24  *            radtree    list     list
25  *            |-- name
26  */
27 
28 /** zone information in the nsd.udb.  Name allocated after it. */
29 struct zone_d {
30 	/** radtree node in the zonetree for this zone */
31 	udb_rel_ptr node;
32 	/** the radtree for the domain names in the zone */
33 	udb_rel_ptr domains;
34 	/** the NSEC3PARAM rr used for hashing (or 0), rr_d pointer */
35 	udb_rel_ptr nsec3param;
36 	/** the log_str for the AXFR change, or 0 */
37 	udb_rel_ptr log_str;
38 	/** the file name when read from a file, or 0 */
39 	udb_rel_ptr file_str;
40 	/** modification time, time when the zone data was changed */
41 	uint64_t mtime;
42 	/** modification time, nsecs */
43 	uint64_t mtime_nsec;
44 	/** number of RRsets in the zone */
45 	uint64_t rrset_count;
46 	/** number of RRs in the zone */
47 	uint64_t rr_count;
48 	/** the length of the zone name */
49 	udb_radstrlen_type namelen;
50 	/** if the zone is expired */
51 	uint8_t expired;
52 	/** if the zone has been changed by AXFR */
53 	uint8_t is_changed;
54 	/** the zone (wire uncompressed) name in DNS format */
55 	uint8_t name[0];
56 };
57 
58 /** domain name in the nametree. name allocated after it */
59 struct domain_d {
60 	/** radtree node in the nametree for this domain */
61 	udb_rel_ptr node;
62 	/** the list of rrsets for this name, single linked */
63 	udb_rel_ptr rrsets;
64 	/** length of the domain name */
65 	udb_radstrlen_type namelen;
66 	/** the domain (wire uncompressed) name in DNS format */
67 	uint8_t name[0];
68 };
69 
70 /** rrset information. */
71 struct rrset_d {
72 	/** next in rrset list */
73 	udb_rel_ptr next;
74 	/** the singly linked list of rrs for this rrset */
75 	udb_rel_ptr rrs;
76 	/** type of the RRs in this rrset (host order) */
77 	uint16_t type;
78 };
79 
80 /** rr information; wireformat data allocated after it */
81 struct rr_d {
82 	/** next in rr list */
83 	udb_rel_ptr next;
84 	/** type (host order) */
85 	uint16_t type;
86 	/** class (host order) */
87 	uint16_t klass;
88 	/** ttl (host order) */
89 	uint32_t ttl;
90 	/** length of wireformat */
91 	uint16_t len;
92 	/** wireformat of rdata (without rdatalen) */
93 	uint8_t wire[0];
94 };
95 
96 /** init an udb for use as DNS store */
97 int udb_dns_init_file(udb_base* udb);
98 /** de-init an udb for use as DNS store */
99 void udb_dns_deinit_file(udb_base* udb);
100 
101 /** create a zone */
102 int udb_zone_create(udb_base* udb, udb_ptr* result, const uint8_t* dname,
103 	size_t dlen);
104 /** clear all RRsets from a zone */
105 void udb_zone_clear(udb_base* udb, udb_ptr* zone);
106 /** delete a zone */
107 void udb_zone_delete(udb_base* udb, udb_ptr* zone);
108 /** find a zone by name (exact match) */
109 int udb_zone_search(udb_base* udb, udb_ptr* result, const uint8_t* dname,
110 	size_t dlen);
111 /** get modification time for zone or 0 */
112 void udb_zone_get_mtime(udb_base* udb, const uint8_t* dname, size_t dlen,
113 	struct timespec* mtime);
114 /** set log str in udb, or remove it */
115 void udb_zone_set_log_str(udb_base* udb, udb_ptr* zone, const char* str);
116 /** set file str in udb, or remove it */
117 void udb_zone_set_file_str(udb_base* udb, udb_ptr* zone, const char* str);
118 /** get file string for zone or NULL */
119 const char* udb_zone_get_file_str(udb_base* udb, const uint8_t* dname,
120 	size_t dlen);
121 /** find a domain name in the zone domain tree */
122 int udb_domain_find(udb_base* udb, udb_ptr* zone, const uint8_t* nm,
123 	size_t nmlen, udb_ptr* result);
124 /** find rrset in domain */
125 int udb_rrset_find(udb_base* udb, udb_ptr* domain, uint16_t t, udb_ptr* res);
126 
127 /** add an RR to a zone */
128 int udb_zone_add_rr(udb_base* udb, udb_ptr* zone, const uint8_t* nm,
129 	size_t nmlen, uint16_t t, uint16_t k, uint32_t ttl, uint8_t* rdata,
130 	size_t rdatalen);
131 /** del an RR from a zone */
132 void udb_zone_del_rr(udb_base* udb, udb_ptr* zone, const uint8_t* nm,
133 	size_t nmlen, uint16_t t, uint16_t k, uint8_t* rdata, size_t rdatalen);
134 
135 /** get pretty string for nsec3parameters (static buffer returned) */
136 const char* udb_nsec3param_string(udb_ptr* rr);
137 
138 /** for use in udb-walkfunc, walks relptrs in udb_chunk_type_zone */
139 void udb_zone_walk_chunk(void* base, void* d, uint64_t s,
140 	udb_walk_relptr_cb* cb, void* arg);
141 /** for use in udb-walkfunc, walks relptrs in udb_chunk_type_domain */
142 void udb_domain_walk_chunk(void* base, void* d, uint64_t s,
143 	udb_walk_relptr_cb* cb, void* arg);
144 /** for use in udb-walkfunc, walks relptrs in udb_chunk_type_rrset */
145 void udb_rrset_walk_chunk(void* base, void* d, uint64_t s,
146 	udb_walk_relptr_cb* cb, void* arg);
147 /** for use in udb-walkfunc, walks relptrs in udb_chunk_type_rr */
148 void udb_rr_walk_chunk(void* base, void* d, uint64_t s,
149 	udb_walk_relptr_cb* cb, void* arg);
150 
151 /** walk through relptrs in registered types */
152 void namedb_walkfunc(void* base, void* warg, uint8_t t, void* d, uint64_t s,
153         udb_walk_relptr_cb* cb, void* arg);
154 
155 #define ZONE(ptr) ((struct zone_d*)UDB_PTR(ptr))
156 #define DOMAIN(ptr) ((struct domain_d*)UDB_PTR(ptr))
157 #define RRSET(ptr) ((struct rrset_d*)UDB_PTR(ptr))
158 #define RR(ptr) ((struct rr_d*)UDB_PTR(ptr))
159 
160 #endif /* UDB_ZONE_H */
161