root/apps/iphone/sampleTelApp/trunk/DotTel_SDK/ldns_sources/higher.c
@
913
| Revision 82, 7.4 kB (checked in by henri, 5 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * higher.c |
| 3 | * |
| 4 | * Specify some higher level functions that would |
| 5 | * be usefull to would be developers |
| 6 | * |
| 7 | * a Net::DNS like library for C |
| 8 | * |
| 9 | * (c) NLnet Labs, 2004-2006 |
| 10 | * |
| 11 | * See the file LICENSE for the license |
| 12 | */ |
| 13 | |
| 14 | #include "ldns/config.h" |
| 15 | |
| 16 | #include "ldns.h" |
| 17 | |
| 18 | #ifdef HAVE_SSL |
| 19 | #include <openssl/ssl.h> |
| 20 | #include <openssl/sha.h> |
| 21 | #endif |
| 22 | |
| 23 | ldns_rr_list * |
| 24 | ldns_get_rr_list_addr_by_name(ldns_resolver *res, ldns_rdf *name, ldns_rr_class c, |
| 25 | uint16_t flags) |
| 26 | { |
| 27 | ldns_pkt *pkt; |
| 28 | ldns_rr_list *aaaa; |
| 29 | ldns_rr_list *a; |
| 30 | ldns_rr_list *result = NULL; |
| 31 | ldns_rr_list *hostsfilenames; |
| 32 | size_t i; |
| 33 | uint8_t ip6; |
| 34 | |
| 35 | a = NULL; |
| 36 | aaaa = NULL; |
| 37 | result = NULL; |
| 38 | |
| 39 | if (!res) { |
| 40 | return NULL; |
| 41 | } |
| 42 | if (ldns_rdf_get_type(name) != LDNS_RDF_TYPE_DNAME) { |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | ip6 = ldns_resolver_ip6(res); /* we use INET_ANY here, save |
| 47 | what was there */ |
| 48 | |
| 49 | ldns_resolver_set_ip6(res, LDNS_RESOLV_INETANY); |
| 50 | |
| 51 | hostsfilenames = ldns_get_rr_list_hosts_frm_file(NULL); |
| 52 | for (i = 0; i < ldns_rr_list_rr_count(hostsfilenames); i++) { |
| 53 | if (ldns_rdf_compare(name, |
| 54 | ldns_rr_owner(ldns_rr_list_rr(hostsfilenames, |
| 55 | i))) == 0) { |
| 56 | if (!result) { |
| 57 | result = ldns_rr_list_new(); |
| 58 | } |
| 59 | ldns_rr_list_push_rr(result, |
| 60 | ldns_rr_clone(ldns_rr_list_rr(hostsfilenames, i))); |
| 61 | } |
| 62 | } |
| 63 | ldns_rr_list_deep_free(hostsfilenames); |
| 64 | |
| 65 | if (result) { |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /* add the RD flags, because we want an answer */ |
| 70 | pkt = ldns_resolver_query(res, name, LDNS_RR_TYPE_AAAA, c, flags | LDNS_RD); |
| 71 | if (pkt) { |
| 72 | /* extract the data we need */ |
| 73 | aaaa = ldns_pkt_rr_list_by_type(pkt, LDNS_RR_TYPE_AAAA, |
| 74 | LDNS_SECTION_ANSWER); |
| 75 | ldns_pkt_free(pkt); |
| 76 | } |
| 77 | |
| 78 | pkt = ldns_resolver_query(res, name, LDNS_RR_TYPE_A, c, flags | LDNS_RD); |
| 79 | if (pkt) { |
| 80 | /* extract the data we need */ |
| 81 | a = ldns_pkt_rr_list_by_type(pkt, LDNS_RR_TYPE_A, LDNS_SECTION_ANSWER); |
| 82 | ldns_pkt_free(pkt); |
| 83 | } |
| 84 | ldns_resolver_set_ip6(res, ip6); |
| 85 | |
| 86 | if (aaaa && a) { |
| 87 | result = ldns_rr_list_cat_clone(aaaa, a); |
| 88 | ldns_rr_list_deep_free(aaaa); |
| 89 | ldns_rr_list_deep_free(a); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | if (aaaa) { |
| 94 | result = ldns_rr_list_clone(aaaa); |
| 95 | } |
| 96 | |
| 97 | if (a) { |
| 98 | result = ldns_rr_list_clone(a); |
| 99 | } |
| 100 | |
| 101 | ldns_rr_list_deep_free(aaaa); |
| 102 | ldns_rr_list_deep_free(a); |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | ldns_rr_list * |
| 107 | ldns_get_rr_list_name_by_addr(ldns_resolver *res, ldns_rdf *addr, ldns_rr_class c, |
| 108 | uint16_t flags) |
| 109 | { |
| 110 | ldns_pkt *pkt; |
| 111 | ldns_rr_list *names; |
| 112 | ldns_rdf *name; |
| 113 | size_t i; |
| 114 | |
| 115 | i = 0; |
| 116 | names = NULL; |
| 117 | |
| 118 | if (!res || !addr) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | if (ldns_rdf_get_type(addr) != LDNS_RDF_TYPE_A && |
| 123 | ldns_rdf_get_type(addr) != LDNS_RDF_TYPE_AAAA) { |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | name = ldns_rdf_address_reverse(addr); |
| 128 | |
| 129 | /* add the RD flags, because we want an answer */ |
| 130 | pkt = ldns_resolver_query(res, name, LDNS_RR_TYPE_PTR, c, flags | LDNS_RD); |
| 131 | if (pkt) { |
| 132 | /* extract the data we need */ |
| 133 | names = ldns_pkt_rr_list_by_type(pkt, |
| 134 | LDNS_RR_TYPE_PTR, LDNS_SECTION_ANSWER); |
| 135 | } |
| 136 | return names; |
| 137 | } |
| 138 | |
| 139 | /* read a line, put it in a buffer, parse the buffer */ |
| 140 | ldns_rr_list * |
| 141 | ldns_get_rr_list_hosts_frm_fp(FILE *fp) |
| 142 | { |
| 143 | return ldns_get_rr_list_hosts_frm_fp_l(fp, NULL); |
| 144 | } |
| 145 | |
| 146 | ldns_rr_list * |
| 147 | ldns_get_rr_list_hosts_frm_fp_l(FILE *fp, int *line_nr) |
| 148 | { |
| 149 | ssize_t i, j; |
| 150 | size_t cnt; |
| 151 | char *line; |
| 152 | char *word; |
| 153 | char *addr; |
| 154 | char *rr_str; |
| 155 | ldns_buffer *linebuf; |
| 156 | ldns_rr *rr; |
| 157 | ldns_rr_list *list; |
| 158 | ldns_rdf *tmp; |
| 159 | bool ip6; |
| 160 | ldns_status parse_result; |
| 161 | |
| 162 | line = LDNS_XMALLOC(char, LDNS_MAX_LINELEN + 1); |
| 163 | word = LDNS_XMALLOC(char, LDNS_MAX_LINELEN + 1); |
| 164 | addr = LDNS_XMALLOC(char, LDNS_MAX_LINELEN + 1); |
| 165 | rr_str = LDNS_XMALLOC(char, LDNS_MAX_LINELEN + 1); |
| 166 | ip6 = false; |
| 167 | list = ldns_rr_list_new(); |
| 168 | rr = NULL; |
| 169 | |
| 170 | for(i = ldns_fget_token_l(fp, line, "\n", 0, line_nr); |
| 171 | i > 0; i = ldns_fget_token_l(fp, line, "\n", 0, line_nr)) { |
| 172 | /* # is comment */ |
| 173 | if (line[0] == '#') { |
| 174 | continue; |
| 175 | } |
| 176 | /* put it in a buffer for further processing */ |
| 177 | linebuf = LDNS_MALLOC(ldns_buffer); |
| 178 | |
| 179 | ldns_buffer_new_frm_data(linebuf, line, (size_t) i); |
| 180 | for(cnt = 0, j = ldns_bget_token(linebuf, word, LDNS_PARSE_NO_NL, 0); |
| 181 | j > 0; |
| 182 | j = ldns_bget_token(linebuf, word, LDNS_PARSE_NO_NL, 0), cnt++) { |
| 183 | if (cnt == 0) { |
| 184 | /* the address */ |
| 185 | if ((tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA, |
| 186 | word))) { |
| 187 | /* ip6 */ |
| 188 | ldns_rdf_deep_free(tmp); |
| 189 | ip6 = true; |
| 190 | } else { |
| 191 | if ((tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A, |
| 192 | word))) { |
| 193 | /* ip4 */ |
| 194 | ldns_rdf_deep_free(tmp); |
| 195 | ip6 = false; |
| 196 | } else { |
| 197 | /* kaput */ |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | strncpy(addr, word, LDNS_IP6ADDRLEN); |
| 202 | } else { |
| 203 | /* la al la la */ |
| 204 | if (ip6) { |
| 205 | snprintf(rr_str, LDNS_MAX_LINELEN, |
| 206 | "%s IN AAAA %s", word, addr); |
| 207 | } else { |
| 208 | snprintf(rr_str, LDNS_MAX_LINELEN, |
| 209 | "%s IN A %s", word, addr); |
| 210 | } |
| 211 | parse_result = ldns_rr_new_frm_str(&rr, rr_str, 0, NULL, NULL); |
| 212 | if (parse_result == LDNS_STATUS_OK && ldns_rr_owner(rr) && ldns_rr_rd_count(rr) > 0) { |
| 213 | ldns_rr_list_push_rr(list, ldns_rr_clone(rr)); |
| 214 | } |
| 215 | ldns_rr_free(rr); |
| 216 | } |
| 217 | } |
| 218 | ldns_buffer_free(linebuf); |
| 219 | } |
| 220 | LDNS_FREE(line); |
| 221 | LDNS_FREE(word); |
| 222 | LDNS_FREE(addr); |
| 223 | LDNS_FREE(rr_str); |
| 224 | return list; |
| 225 | } |
| 226 | |
| 227 | ldns_rr_list * |
| 228 | ldns_get_rr_list_hosts_frm_file(char *filename) |
| 229 | { |
| 230 | ldns_rr_list *names; |
| 231 | FILE *fp; |
| 232 | |
| 233 | if (!filename) { |
| 234 | fp = fopen(LDNS_RESOLV_HOSTS, "r"); |
| 235 | |
| 236 | } else { |
| 237 | fp = fopen(filename, "r"); |
| 238 | } |
| 239 | if (!fp) { |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | names = ldns_get_rr_list_hosts_frm_fp(fp); |
| 244 | fclose(fp); |
| 245 | return names; |
| 246 | } |
| 247 | |
| 248 | uint16_t |
| 249 | ldns_getaddrinfo(ldns_resolver *res, ldns_rdf *node, ldns_rr_class c, |
| 250 | ldns_rr_list **ret) |
| 251 | { |
| 252 | ldns_rdf_type t; |
| 253 | uint16_t names_found; |
| 254 | ldns_resolver *r; |
| 255 | ldns_status s; |
| 256 | |
| 257 | t = ldns_rdf_get_type(node); |
| 258 | names_found = 0; |
| 259 | r = res; |
| 260 | |
| 261 | if (res == NULL) { |
| 262 | /* prepare a new resolver, using /etc/resolv.conf as a guide */ |
| 263 | s = ldns_resolver_new_frm_file(&r, NULL); |
| 264 | if (s != LDNS_STATUS_OK) { |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (t == LDNS_RDF_TYPE_DNAME) { |
| 270 | /* we're asked to query for a name */ |
| 271 | *ret = ldns_get_rr_list_addr_by_name(r, node, c, 0); |
| 272 | names_found = ldns_rr_list_rr_count(*ret); |
| 273 | } |
| 274 | |
| 275 | if (t == LDNS_RDF_TYPE_A || t == LDNS_RDF_TYPE_AAAA) { |
| 276 | /* an address */ |
| 277 | *ret = ldns_get_rr_list_name_by_addr(r, node, c, 0); |
| 278 | names_found = ldns_rr_list_rr_count(*ret); |
| 279 | } |
| 280 | |
| 281 | if (res == NULL) { |
| 282 | ldns_resolver_deep_free(r); |
| 283 | } |
| 284 | |
| 285 | return names_found; |
| 286 | } |
| 287 | |
| 288 | bool |
| 289 | ldns_nsec_type_check(ldns_rr *nsec, ldns_rr_type t) |
| 290 | { |
| 291 | /* does the nsec cover the t given? */ |
| 292 | /* copied from host2str.c line 465: ldns_rdf2buffer_str_nsec */ |
| 293 | uint8_t window_block_nr; |
| 294 | uint8_t bitmap_length; |
| 295 | uint16_t type; |
| 296 | uint16_t pos = 0; |
| 297 | uint16_t bit_pos; |
| 298 | ldns_rdf *nsec_type_list = ldns_rr_rdf(nsec, 1); |
| 299 | uint8_t *data = ldns_rdf_data(nsec_type_list); |
| 300 | |
| 301 | while(pos < ldns_rdf_size(nsec_type_list)) { |
| 302 | window_block_nr = data[pos]; |
| 303 | bitmap_length = data[pos + 1]; |
| 304 | pos += 2; |
| 305 | |
| 306 | for (bit_pos = 0; bit_pos < (bitmap_length) * 8; bit_pos++) { |
| 307 | if (ldns_get_bit(&data[pos], bit_pos)) { |
| 308 | type = 256 * (uint16_t) window_block_nr + bit_pos; |
| 309 | |
| 310 | if ((ldns_rr_type)type == t) { |
| 311 | /* we have a winner */ |
| 312 | return true; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | pos += (uint16_t) bitmap_length; |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | ldns_print_rr_rdf(FILE *fp, ldns_rr *r, int rdfnum, ...) |
| 323 | { |
| 324 | int16_t rdf; |
| 325 | ldns_rdf *rd; |
| 326 | va_list va_rdf; |
| 327 | va_start(va_rdf, rdfnum); |
| 328 | |
| 329 | for (rdf = (int16_t)rdfnum; rdf != -1; rdf = (int16_t)va_arg(va_rdf, int)) |
| 330 | { |
| 331 | rd = ldns_rr_rdf(r, rdf); |
| 332 | if (!rd) { |
| 333 | continue; |
| 334 | } else { |
| 335 | ldns_rdf_print(fp, rd); |
| 336 | fprintf(fp, " "); /* not sure if we want to do this */ |
| 337 | } |
| 338 | } |
| 339 | va_end(va_rdf); |
| 340 | } |
Note: See TracBrowser
for help on using the browser.








