1 /*
2 * root.c
3 * Function to handle to the rootservers
4 * and to update and prime them
5 * (c) 2005 NLnet Labs
6 *
7 * See the file LICENSE for the license
8 *
9 */
10
11 #include "drill.h"
12 #include <ldns/ldns.h>
13 #include <errno.h>
14
15 /* a global list of the root-servers */
16 ldns_rr_list *global_dns_root = NULL;
17
18 /* put a hardcoded list in the root and
19 * init the root rrlist structure */
20 void
init_root(void)21 init_root(void)
22 {
23 ldns_rr *r;
24
25 global_dns_root = ldns_rr_list_new();
26
27 (void)ldns_rr_new_frm_str(&r, "A.ROOT-SERVERS.NET. 3600000 A 198.41.0.4", 0, NULL, NULL);
28 ldns_rr_list_push_rr(global_dns_root, r);
29 (void)ldns_rr_new_frm_str(&r, "A.ROOT-SERVERS.NET. 3600000 AAAA 2001:503:BA3E::2:30", 0, NULL, NULL);
30 ldns_rr_list_push_rr(global_dns_root, r);
31 (void)ldns_rr_new_frm_str(&r, "B.ROOT-SERVERS.NET. 3600000 A 170.247.170.2", 0, NULL, NULL);
32 ldns_rr_list_push_rr(global_dns_root, r);
33 (void)ldns_rr_new_frm_str(&r, "B.ROOT-SERVERS.NET. 3600000 AAAA 2801:1b8:10::B", 0, NULL, NULL);
34 ldns_rr_list_push_rr(global_dns_root, r);
35 (void)ldns_rr_new_frm_str(&r, "C.ROOT-SERVERS.NET. 3600000 A 192.33.4.12", 0, NULL, NULL);
36 ldns_rr_list_push_rr(global_dns_root, r);
37 (void)ldns_rr_new_frm_str(&r, "C.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2::C", 0, NULL, NULL);
38 ldns_rr_list_push_rr(global_dns_root, r);
39 (void)ldns_rr_new_frm_str(&r, "D.ROOT-SERVERS.NET. 3600000 A 199.7.91.13", 0, NULL, NULL);
40 ldns_rr_list_push_rr(global_dns_root, r);
41 (void)ldns_rr_new_frm_str(&r, "D.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2D::D", 0, NULL, NULL);
42 ldns_rr_list_push_rr(global_dns_root, r);
43 (void)ldns_rr_new_frm_str(&r, "E.ROOT-SERVERS.NET. 3600000 A 192.203.230.10", 0, NULL, NULL);
44 ldns_rr_list_push_rr(global_dns_root, r);
45 (void)ldns_rr_new_frm_str(&r, "E.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:A8::E", 0, NULL, NULL);
46 ldns_rr_list_push_rr(global_dns_root, r);
47 (void)ldns_rr_new_frm_str(&r, "F.ROOT-SERVERS.NET. 3600000 A 192.5.5.241", 0, NULL, NULL);
48 ldns_rr_list_push_rr(global_dns_root, r);
49 (void)ldns_rr_new_frm_str(&r, "F.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2F::F", 0, NULL, NULL);
50 ldns_rr_list_push_rr(global_dns_root, r);
51 (void)ldns_rr_new_frm_str(&r, "G.ROOT-SERVERS.NET. 3600000 A 192.112.36.4", 0, NULL, NULL);
52 ldns_rr_list_push_rr(global_dns_root, r);
53 (void)ldns_rr_new_frm_str(&r, "G.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:12::D0D", 0, NULL, NULL);
54 ldns_rr_list_push_rr(global_dns_root, r);
55 (void)ldns_rr_new_frm_str(&r, "H.ROOT-SERVERS.NET. 3600000 A 198.97.190.53", 0, NULL, NULL);
56 ldns_rr_list_push_rr(global_dns_root, r);
57 (void)ldns_rr_new_frm_str(&r, "H.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:1::53", 0, NULL, NULL);
58 ldns_rr_list_push_rr(global_dns_root, r);
59 (void)ldns_rr_new_frm_str(&r, "I.ROOT-SERVERS.NET. 3600000 A 192.36.148.17", 0, NULL, NULL);
60 ldns_rr_list_push_rr(global_dns_root, r);
61 (void)ldns_rr_new_frm_str(&r, "I.ROOT-SERVERS.NET. 3600000 AAAA 2001:7FE::53", 0, NULL, NULL);
62 ldns_rr_list_push_rr(global_dns_root, r);
63 (void)ldns_rr_new_frm_str(&r, "J.ROOT-SERVERS.NET. 3600000 A 192.58.128.30", 0, NULL, NULL);
64 ldns_rr_list_push_rr(global_dns_root, r);
65 (void)ldns_rr_new_frm_str(&r, "J.ROOT-SERVERS.NET. 3600000 AAAA 2001:503:C27::2:30", 0, NULL, NULL);
66 ldns_rr_list_push_rr(global_dns_root, r);
67 (void)ldns_rr_new_frm_str(&r, "K.ROOT-SERVERS.NET. 3600000 A 193.0.14.129 ", 0, NULL, NULL);
68 ldns_rr_list_push_rr(global_dns_root, r);
69 (void)ldns_rr_new_frm_str(&r, "K.ROOT-SERVERS.NET. 3600000 AAAA 2001:7FD::1", 0, NULL, NULL);
70 ldns_rr_list_push_rr(global_dns_root, r);
71 (void)ldns_rr_new_frm_str(&r, "L.ROOT-SERVERS.NET. 3600000 A 199.7.83.42", 0, NULL, NULL);
72 ldns_rr_list_push_rr(global_dns_root, r);
73 (void)ldns_rr_new_frm_str(&r, "L.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:9F::42", 0, NULL, NULL);
74 ldns_rr_list_push_rr(global_dns_root, r);
75 (void)ldns_rr_new_frm_str(&r, "M.ROOT-SERVERS.NET. 3600000 A 202.12.27.33", 0, NULL, NULL);
76 ldns_rr_list_push_rr(global_dns_root, r);
77 (void)ldns_rr_new_frm_str(&r, "M.ROOT-SERVERS.NET. 3600000 AAAA 2001:DC3::35", 0, NULL, NULL);
78 ldns_rr_list_push_rr(global_dns_root, r);
79 }
80
81 /*
82 * Read a hints file as root
83 *
84 * The file with the given path should contain a list of NS RRs
85 * for the root zone and A records for those NS RRs.
86 * Read them, check them, and append the a records to the rr list given.
87 */
88 ldns_rr_list *
read_root_hints(const char * filename)89 read_root_hints(const char *filename)
90 {
91 FILE *fp = NULL;
92 int line_nr = 0;
93 ldns_zone *z;
94 ldns_status status;
95 ldns_rr_list *addresses = NULL;
96 ldns_rr *rr;
97 size_t i;
98
99 fp = fopen(filename, "r");
100 if (!fp) {
101 fprintf(stderr, "Unable to open %s for reading: %s\n", filename, strerror(errno));
102 return NULL;
103 }
104
105 status = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, 0, &line_nr);
106 fclose(fp);
107 if (status != LDNS_STATUS_OK) {
108 fprintf(stderr, "Error reading root hints file: %s\n", ldns_get_errorstr_by_id(status));
109 return NULL;
110 } else {
111 addresses = ldns_rr_list_new();
112 for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
113 rr = ldns_rr_list_rr(ldns_zone_rrs(z), i);
114 /*if ((address_family == 0 || address_family == 1) &&
115 */
116 if ( ldns_rr_get_type(rr) == LDNS_RR_TYPE_A ) {
117 ldns_rr_list_push_rr(addresses, ldns_rr_clone(rr));
118 }
119 /*if ((address_family == 0 || address_family == 2) &&*/
120 if ( ldns_rr_get_type(rr) == LDNS_RR_TYPE_AAAA) {
121 ldns_rr_list_push_rr(addresses, ldns_rr_clone(rr));
122 }
123 }
124 ldns_zone_deep_free(z);
125 return addresses;
126 }
127 }
128
129
130 void
clear_root(void)131 clear_root(void)
132 {
133 ldns_rr_list_deep_free(global_dns_root);
134 }
135