2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: rootns.c,v 1.20.2.3.2.5 2004/03/08 09:04:32 marka Exp $ */
22 #include <isc/buffer.h>
23 #include <isc/string.h> /* Required for HP/UX (and others?) */
26 #include <dns/callbacks.h>
28 #include <dns/dbiterator.h>
30 #include <dns/fixedname.h>
31 #include <dns/master.h>
32 #include <dns/rdata.h>
33 #include <dns/rdatasetiter.h>
34 #include <dns/rdataset.h>
35 #include <dns/rdatastruct.h>
36 #include <dns/result.h>
37 #include <dns/rootns.h>
39 static char root_ns[] =
41 "; Internet Root Nameservers\n"
43 "; Thu Sep 23 17:57:37 PDT 1999\n"
46 ". 518400 IN NS A.ROOT-SERVERS.NET.\n"
47 ". 518400 IN NS B.ROOT-SERVERS.NET.\n"
48 ". 518400 IN NS C.ROOT-SERVERS.NET.\n"
49 ". 518400 IN NS D.ROOT-SERVERS.NET.\n"
50 ". 518400 IN NS E.ROOT-SERVERS.NET.\n"
51 ". 518400 IN NS F.ROOT-SERVERS.NET.\n"
52 ". 518400 IN NS G.ROOT-SERVERS.NET.\n"
53 ". 518400 IN NS H.ROOT-SERVERS.NET.\n"
54 ". 518400 IN NS I.ROOT-SERVERS.NET.\n"
55 ". 518400 IN NS J.ROOT-SERVERS.NET.\n"
56 ". 518400 IN NS K.ROOT-SERVERS.NET.\n"
57 ". 518400 IN NS L.ROOT-SERVERS.NET.\n"
58 ". 518400 IN NS M.ROOT-SERVERS.NET.\n"
59 "A.ROOT-SERVERS.NET. 3600000 IN A 198.41.0.4\n"
60 "B.ROOT-SERVERS.NET. 3600000 IN A 192.228.79.201\n"
61 "C.ROOT-SERVERS.NET. 3600000 IN A 192.33.4.12\n"
62 "D.ROOT-SERVERS.NET. 3600000 IN A 128.8.10.90\n"
63 "E.ROOT-SERVERS.NET. 3600000 IN A 192.203.230.10\n"
64 "F.ROOT-SERVERS.NET. 3600000 IN A 192.5.5.241\n"
65 "G.ROOT-SERVERS.NET. 3600000 IN A 192.112.36.4\n"
66 "H.ROOT-SERVERS.NET. 3600000 IN A 128.63.2.53\n"
67 "I.ROOT-SERVERS.NET. 3600000 IN A 192.36.148.17\n"
68 "J.ROOT-SERVERS.NET. 3600000 IN A 192.58.128.30\n"
69 "K.ROOT-SERVERS.NET. 3600000 IN A 193.0.14.129\n"
70 "L.ROOT-SERVERS.NET. 3600000 IN A 198.32.64.12\n"
71 "M.ROOT-SERVERS.NET. 3600000 IN A 202.12.27.33\n";
74 in_rootns(dns_rdataset_t *rootns, dns_name_t *name) {
76 dns_rdata_t rdata = DNS_RDATA_INIT;
79 if (!dns_rdataset_isassociated(rootns))
80 return (ISC_R_NOTFOUND);
82 result = dns_rdataset_first(rootns);
83 while (result == ISC_R_SUCCESS) {
84 dns_rdataset_current(rootns, &rdata);
85 result = dns_rdata_tostruct(&rdata, &ns, NULL);
86 if (result != ISC_R_SUCCESS)
88 if (dns_name_compare(name, &ns.name) == 0)
89 return (ISC_R_SUCCESS);
90 result = dns_rdataset_next(rootns);
92 if (result == ISC_R_NOMORE)
93 result = ISC_R_NOTFOUND;
98 check_node(dns_rdataset_t *rootns, dns_name_t *name,
99 dns_rdatasetiter_t *rdsiter) {
101 dns_rdataset_t rdataset;
103 dns_rdataset_init(&rdataset);
104 result = dns_rdatasetiter_first(rdsiter);
105 while (result == ISC_R_SUCCESS) {
106 dns_rdatasetiter_current(rdsiter, &rdataset);
107 switch (rdataset.type) {
108 case dns_rdatatype_a:
109 case dns_rdatatype_aaaa:
110 result = in_rootns(rootns, name);
111 if (result != ISC_R_SUCCESS)
114 case dns_rdatatype_ns:
115 if (dns_name_compare(name, dns_rootname) == 0)
119 result = ISC_R_FAILURE;
122 dns_rdataset_disassociate(&rdataset);
123 result = dns_rdatasetiter_next(rdsiter);
125 if (result == ISC_R_NOMORE)
126 result = ISC_R_SUCCESS;
128 if (dns_rdataset_isassociated(&rdataset))
129 dns_rdataset_disassociate(&rdataset);
134 check_hints(dns_db_t *db) {
136 dns_rdataset_t rootns;
137 dns_dbiterator_t *dbiter = NULL;
138 dns_dbnode_t *node = NULL;
140 dns_fixedname_t fixname;
142 dns_rdatasetiter_t *rdsiter = NULL;
144 isc_stdtime_get(&now);
146 dns_fixedname_init(&fixname);
147 name = dns_fixedname_name(&fixname);
149 dns_rdataset_init(&rootns);
150 (void)dns_db_find(db, dns_rootname, NULL, dns_rdatatype_ns, 0,
151 now, NULL, name, &rootns, NULL);
152 result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
153 if (result != ISC_R_SUCCESS)
155 result = dns_dbiterator_first(dbiter);
156 while (result == ISC_R_SUCCESS) {
157 result = dns_dbiterator_current(dbiter, &node, name);
158 if (result != ISC_R_SUCCESS)
160 result = dns_db_allrdatasets(db, node, NULL, now, &rdsiter);
161 if (result != ISC_R_SUCCESS)
163 result = check_node(&rootns, name, rdsiter);
164 if (result != ISC_R_SUCCESS)
166 dns_rdatasetiter_destroy(&rdsiter);
167 dns_db_detachnode(db, &node);
168 result = dns_dbiterator_next(dbiter);
170 if (result == ISC_R_NOMORE)
171 result = ISC_R_SUCCESS;
174 if (dns_rdataset_isassociated(&rootns))
175 dns_rdataset_disassociate(&rootns);
177 dns_rdatasetiter_destroy(&rdsiter);
179 dns_db_detachnode(db, &node);
181 dns_dbiterator_destroy(&dbiter);
186 dns_rootns_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
187 const char *filename, dns_db_t **target)
189 isc_result_t result, eresult;
192 dns_rdatacallbacks_t callbacks;
195 REQUIRE(target != NULL && *target == NULL);
197 result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
198 rdclass, 0, NULL, &db);
199 if (result != ISC_R_SUCCESS)
202 dns_rdatacallbacks_init(&callbacks);
204 len = strlen(root_ns);
205 isc_buffer_init(&source, root_ns, len);
206 isc_buffer_add(&source, len);
208 result = dns_db_beginload(db, &callbacks.add,
209 &callbacks.add_private);
210 if (result != ISC_R_SUCCESS)
212 if (filename != NULL) {
214 * Load the hints from the specified filename.
216 result = dns_master_loadfile(filename, &db->origin,
217 &db->origin, db->rdclass,
219 &callbacks, db->mctx);
220 } else if (rdclass == dns_rdataclass_in) {
222 * Default to using the Internet root servers.
224 result = dns_master_loadbuffer(&source, &db->origin,
225 &db->origin, db->rdclass,
227 &callbacks, db->mctx);
229 result = ISC_R_NOTFOUND;
230 eresult = dns_db_endload(db, &callbacks.add_private);
231 if (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE)
233 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
235 if (check_hints(db) != ISC_R_SUCCESS)
236 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
237 DNS_LOGMODULE_HINTS, ISC_LOG_WARNING,
238 "extra data in root hints '%s'",
239 (filename != NULL) ? filename : "<BUILT-IN>");
241 return (ISC_R_SUCCESS);