2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 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.5 2004/03/09 06:11:07 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 case dns_rdatatype_a6
:
111 result
= in_rootns(rootns
, name
);
112 if (result
!= ISC_R_SUCCESS
)
115 case dns_rdatatype_ns
:
116 if (dns_name_compare(name
, dns_rootname
) == 0)
120 result
= ISC_R_FAILURE
;
123 dns_rdataset_disassociate(&rdataset
);
124 result
= dns_rdatasetiter_next(rdsiter
);
126 if (result
== ISC_R_NOMORE
)
127 result
= ISC_R_SUCCESS
;
129 if (dns_rdataset_isassociated(&rdataset
))
130 dns_rdataset_disassociate(&rdataset
);
135 check_hints(dns_db_t
*db
) {
137 dns_rdataset_t rootns
;
138 dns_dbiterator_t
*dbiter
= NULL
;
139 dns_dbnode_t
*node
= NULL
;
141 dns_fixedname_t fixname
;
143 dns_rdatasetiter_t
*rdsiter
= NULL
;
145 isc_stdtime_get(&now
);
147 dns_fixedname_init(&fixname
);
148 name
= dns_fixedname_name(&fixname
);
150 dns_rdataset_init(&rootns
);
151 (void)dns_db_find(db
, dns_rootname
, NULL
, dns_rdatatype_ns
, 0,
152 now
, NULL
, name
, &rootns
, NULL
);
153 result
= dns_db_createiterator(db
, ISC_FALSE
, &dbiter
);
154 if (result
!= ISC_R_SUCCESS
)
156 result
= dns_dbiterator_first(dbiter
);
157 while (result
== ISC_R_SUCCESS
) {
158 result
= dns_dbiterator_current(dbiter
, &node
, name
);
159 if (result
!= ISC_R_SUCCESS
)
161 result
= dns_db_allrdatasets(db
, node
, NULL
, now
, &rdsiter
);
162 if (result
!= ISC_R_SUCCESS
)
164 result
= check_node(&rootns
, name
, rdsiter
);
165 if (result
!= ISC_R_SUCCESS
)
167 dns_rdatasetiter_destroy(&rdsiter
);
168 dns_db_detachnode(db
, &node
);
169 result
= dns_dbiterator_next(dbiter
);
171 if (result
== ISC_R_NOMORE
)
172 result
= ISC_R_SUCCESS
;
175 if (dns_rdataset_isassociated(&rootns
))
176 dns_rdataset_disassociate(&rootns
);
178 dns_rdatasetiter_destroy(&rdsiter
);
180 dns_db_detachnode(db
, &node
);
182 dns_dbiterator_destroy(&dbiter
);
187 dns_rootns_create(isc_mem_t
*mctx
, dns_rdataclass_t rdclass
,
188 const char *filename
, dns_db_t
**target
)
190 isc_result_t result
, eresult
;
193 dns_rdatacallbacks_t callbacks
;
196 REQUIRE(target
!= NULL
&& *target
== NULL
);
198 result
= dns_db_create(mctx
, "rbt", dns_rootname
, dns_dbtype_zone
,
199 rdclass
, 0, NULL
, &db
);
200 if (result
!= ISC_R_SUCCESS
)
203 dns_rdatacallbacks_init(&callbacks
);
205 len
= strlen(root_ns
);
206 isc_buffer_init(&source
, root_ns
, len
);
207 isc_buffer_add(&source
, len
);
209 result
= dns_db_beginload(db
, &callbacks
.add
,
210 &callbacks
.add_private
);
211 if (result
!= ISC_R_SUCCESS
)
213 if (filename
!= NULL
) {
215 * Load the hints from the specified filename.
217 result
= dns_master_loadfile(filename
, &db
->origin
,
218 &db
->origin
, db
->rdclass
, 0,
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
, 0,
226 &callbacks
, db
->mctx
);
228 result
= ISC_R_NOTFOUND
;
229 eresult
= dns_db_endload(db
, &callbacks
.add_private
);
230 if (result
== ISC_R_SUCCESS
|| result
== DNS_R_SEENINCLUDE
)
232 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_SEENINCLUDE
)
234 if (check_hints(db
) != ISC_R_SUCCESS
)
235 isc_log_write(dns_lctx
, DNS_LOGCATEGORY_GENERAL
,
236 DNS_LOGMODULE_HINTS
, ISC_LOG_WARNING
,
237 "extra data in root hints '%s'",
238 (filename
!= NULL
) ? filename
: "<BUILT-IN>");
240 return (ISC_R_SUCCESS
);