Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 1 | /* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu Exp $ */ |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2001 Jakob Schlyter. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * Portions Copyright (c) 1999-2001 Internet Software Consortium. |
| 31 | * |
| 32 | * Permission to use, copy, modify, and distribute this software for any |
| 33 | * purpose with or without fee is hereby granted, provided that the above |
| 34 | * copyright notice and this permission notice appear in all copies. |
| 35 | * |
| 36 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM |
| 37 | * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL |
| 39 | * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 40 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 41 | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 42 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 43 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 44 | */ |
| 45 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 46 | /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */ |
| 47 | |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 48 | #include "includes.h" |
| 49 | |
Darren Tucker | 5f88d34 | 2003-10-15 16:57:57 +1000 | [diff] [blame] | 50 | #ifndef HAVE_GETRRSETBYNAME |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 51 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame^] | 52 | #include <string.h> |
| 53 | |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 54 | #include "getrrsetbyname.h" |
| 55 | |
Darren Tucker | 991d95f | 2004-05-13 20:24:10 +1000 | [diff] [blame] | 56 | #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO |
Darren Tucker | 5bb1400 | 2004-04-23 18:53:10 +1000 | [diff] [blame] | 57 | extern int h_errno; |
| 58 | #endif |
| 59 | |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 60 | /* We don't need multithread support here */ |
| 61 | #ifdef _THREAD_PRIVATE |
| 62 | # undef _THREAD_PRIVATE |
| 63 | #endif |
| 64 | #define _THREAD_PRIVATE(a,b,c) (c) |
Darren Tucker | 13c539a | 2006-05-15 17:15:56 +1000 | [diff] [blame] | 65 | |
| 66 | /* to avoid conflicts where a platform already has _res */ |
Darren Tucker | 7243f9d | 2006-06-30 11:47:49 +1000 | [diff] [blame] | 67 | #ifdef _res |
| 68 | # undef _res |
| 69 | #endif |
Darren Tucker | 13c539a | 2006-05-15 17:15:56 +1000 | [diff] [blame] | 70 | #define _res _compat_res |
| 71 | |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 72 | struct __res_state _res; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 73 | |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 74 | /* Necessary functions and macros */ |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Inline versions of get/put short/long. Pointer is advanced. |
| 78 | * |
| 79 | * These macros demonstrate the property of C whereby it can be |
| 80 | * portable or it can be elegant but rarely both. |
| 81 | */ |
| 82 | |
| 83 | #ifndef INT32SZ |
| 84 | # define INT32SZ 4 |
| 85 | #endif |
| 86 | #ifndef INT16SZ |
| 87 | # define INT16SZ 2 |
| 88 | #endif |
| 89 | |
| 90 | #ifndef GETSHORT |
| 91 | #define GETSHORT(s, cp) { \ |
| 92 | register u_char *t_cp = (u_char *)(cp); \ |
| 93 | (s) = ((u_int16_t)t_cp[0] << 8) \ |
| 94 | | ((u_int16_t)t_cp[1]) \ |
| 95 | ; \ |
| 96 | (cp) += INT16SZ; \ |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | #ifndef GETLONG |
| 101 | #define GETLONG(l, cp) { \ |
| 102 | register u_char *t_cp = (u_char *)(cp); \ |
| 103 | (l) = ((u_int32_t)t_cp[0] << 24) \ |
| 104 | | ((u_int32_t)t_cp[1] << 16) \ |
| 105 | | ((u_int32_t)t_cp[2] << 8) \ |
| 106 | | ((u_int32_t)t_cp[3]) \ |
| 107 | ; \ |
| 108 | (cp) += INT32SZ; \ |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | /* |
| 113 | * Routines to insert/extract short/long's. |
| 114 | */ |
| 115 | |
Tim Rice | 0ac16a4 | 2003-09-08 06:33:33 -0700 | [diff] [blame] | 116 | #ifndef HAVE__GETSHORT |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 117 | static u_int16_t |
| 118 | _getshort(msgp) |
| 119 | register const u_char *msgp; |
| 120 | { |
| 121 | register u_int16_t u; |
| 122 | |
| 123 | GETSHORT(u, msgp); |
| 124 | return (u); |
| 125 | } |
Darren Tucker | 84ce9b4 | 2005-05-26 20:12:15 +1000 | [diff] [blame] | 126 | #elif defined(HAVE_DECL__GETSHORT) && (HAVE_DECL__GETSHORT == 0) |
| 127 | u_int16_t _getshort(register const u_char *); |
Tim Rice | 0ac16a4 | 2003-09-08 06:33:33 -0700 | [diff] [blame] | 128 | #endif |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 129 | |
Tim Rice | 0ac16a4 | 2003-09-08 06:33:33 -0700 | [diff] [blame] | 130 | #ifndef HAVE__GETLONG |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 131 | static u_int32_t |
| 132 | _getlong(msgp) |
| 133 | register const u_char *msgp; |
| 134 | { |
| 135 | register u_int32_t u; |
| 136 | |
| 137 | GETLONG(u, msgp); |
| 138 | return (u); |
| 139 | } |
Darren Tucker | 84ce9b4 | 2005-05-26 20:12:15 +1000 | [diff] [blame] | 140 | #elif defined(HAVE_DECL__GETLONG) && (HAVE_DECL__GETLONG == 0) |
| 141 | u_int32_t _getlong(register const u_char *); |
Tim Rice | 0ac16a4 | 2003-09-08 06:33:33 -0700 | [diff] [blame] | 142 | #endif |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 143 | |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 144 | /* ************** */ |
| 145 | |
| 146 | #define ANSWER_BUFFER_SIZE 1024*64 |
| 147 | |
| 148 | struct dns_query { |
| 149 | char *name; |
| 150 | u_int16_t type; |
| 151 | u_int16_t class; |
| 152 | struct dns_query *next; |
| 153 | }; |
| 154 | |
| 155 | struct dns_rr { |
| 156 | char *name; |
| 157 | u_int16_t type; |
| 158 | u_int16_t class; |
| 159 | u_int16_t ttl; |
| 160 | u_int16_t size; |
| 161 | void *rdata; |
| 162 | struct dns_rr *next; |
| 163 | }; |
| 164 | |
| 165 | struct dns_response { |
| 166 | HEADER header; |
| 167 | struct dns_query *query; |
| 168 | struct dns_rr *answer; |
| 169 | struct dns_rr *authority; |
| 170 | struct dns_rr *additional; |
| 171 | }; |
| 172 | |
| 173 | static struct dns_response *parse_dns_response(const u_char *, int); |
| 174 | static struct dns_query *parse_dns_qsection(const u_char *, int, |
| 175 | const u_char **, int); |
| 176 | static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **, |
| 177 | int); |
| 178 | |
| 179 | static void free_dns_query(struct dns_query *); |
| 180 | static void free_dns_rr(struct dns_rr *); |
| 181 | static void free_dns_response(struct dns_response *); |
| 182 | |
| 183 | static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t); |
| 184 | |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 185 | int |
| 186 | getrrsetbyname(const char *hostname, unsigned int rdclass, |
| 187 | unsigned int rdtype, unsigned int flags, |
| 188 | struct rrsetinfo **res) |
| 189 | { |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 190 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 191 | int result; |
| 192 | struct rrsetinfo *rrset = NULL; |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 193 | struct dns_response *response = NULL; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 194 | struct dns_rr *rr; |
| 195 | struct rdatainfo *rdata; |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 196 | int length; |
| 197 | unsigned int index_ans, index_sig; |
| 198 | u_char answer[ANSWER_BUFFER_SIZE]; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 199 | |
| 200 | /* check for invalid class and type */ |
| 201 | if (rdclass > 0xffff || rdtype > 0xffff) { |
| 202 | result = ERRSET_INVAL; |
| 203 | goto fail; |
| 204 | } |
| 205 | |
| 206 | /* don't allow queries of class or type ANY */ |
| 207 | if (rdclass == 0xff || rdtype == 0xff) { |
| 208 | result = ERRSET_INVAL; |
| 209 | goto fail; |
| 210 | } |
| 211 | |
| 212 | /* don't allow flags yet, unimplemented */ |
| 213 | if (flags) { |
| 214 | result = ERRSET_INVAL; |
| 215 | goto fail; |
| 216 | } |
| 217 | |
| 218 | /* initialize resolver */ |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 219 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 220 | result = ERRSET_FAIL; |
| 221 | goto fail; |
| 222 | } |
| 223 | |
| 224 | #ifdef DEBUG |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 225 | _resp->options |= RES_DEBUG; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 226 | #endif /* DEBUG */ |
| 227 | |
| 228 | #ifdef RES_USE_DNSSEC |
| 229 | /* turn on DNSSEC if EDNS0 is configured */ |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 230 | if (_resp->options & RES_USE_EDNS0) |
| 231 | _resp->options |= RES_USE_DNSSEC; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 232 | #endif /* RES_USE_DNSEC */ |
| 233 | |
| 234 | /* make query */ |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 235 | length = res_query(hostname, (signed int) rdclass, (signed int) rdtype, |
| 236 | answer, sizeof(answer)); |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 237 | if (length < 0) { |
| 238 | switch(h_errno) { |
| 239 | case HOST_NOT_FOUND: |
| 240 | result = ERRSET_NONAME; |
| 241 | goto fail; |
| 242 | case NO_DATA: |
| 243 | result = ERRSET_NODATA; |
| 244 | goto fail; |
| 245 | default: |
| 246 | result = ERRSET_FAIL; |
| 247 | goto fail; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* parse result */ |
| 252 | response = parse_dns_response(answer, length); |
| 253 | if (response == NULL) { |
| 254 | result = ERRSET_FAIL; |
| 255 | goto fail; |
| 256 | } |
| 257 | |
| 258 | if (response->header.qdcount != 1) { |
| 259 | result = ERRSET_FAIL; |
| 260 | goto fail; |
| 261 | } |
| 262 | |
| 263 | /* initialize rrset */ |
| 264 | rrset = calloc(1, sizeof(struct rrsetinfo)); |
| 265 | if (rrset == NULL) { |
| 266 | result = ERRSET_NOMEMORY; |
| 267 | goto fail; |
| 268 | } |
| 269 | rrset->rri_rdclass = response->query->class; |
| 270 | rrset->rri_rdtype = response->query->type; |
| 271 | rrset->rri_ttl = response->answer->ttl; |
| 272 | rrset->rri_nrdatas = response->header.ancount; |
| 273 | |
Darren Tucker | 16fd99c | 2005-11-12 14:06:29 +1100 | [diff] [blame] | 274 | #ifdef HAVE_HEADER_AD |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 275 | /* check for authenticated data */ |
| 276 | if (response->header.ad == 1) |
| 277 | rrset->rri_flags |= RRSET_VALIDATED; |
Darren Tucker | 16fd99c | 2005-11-12 14:06:29 +1100 | [diff] [blame] | 278 | #endif |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 279 | |
| 280 | /* copy name from answer section */ |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 281 | rrset->rri_name = strdup(response->answer->name); |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 282 | if (rrset->rri_name == NULL) { |
| 283 | result = ERRSET_NOMEMORY; |
| 284 | goto fail; |
| 285 | } |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 286 | |
| 287 | /* count answers */ |
| 288 | rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass, |
| 289 | rrset->rri_rdtype); |
| 290 | rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass, |
| 291 | T_SIG); |
| 292 | |
| 293 | /* allocate memory for answers */ |
| 294 | rrset->rri_rdatas = calloc(rrset->rri_nrdatas, |
| 295 | sizeof(struct rdatainfo)); |
| 296 | if (rrset->rri_rdatas == NULL) { |
| 297 | result = ERRSET_NOMEMORY; |
| 298 | goto fail; |
| 299 | } |
| 300 | |
| 301 | /* allocate memory for signatures */ |
| 302 | rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 303 | if (rrset->rri_sigs == NULL) { |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 304 | result = ERRSET_NOMEMORY; |
| 305 | goto fail; |
| 306 | } |
| 307 | |
| 308 | /* copy answers & signatures */ |
| 309 | for (rr = response->answer, index_ans = 0, index_sig = 0; |
| 310 | rr; rr = rr->next) { |
| 311 | |
| 312 | rdata = NULL; |
| 313 | |
| 314 | if (rr->class == rrset->rri_rdclass && |
| 315 | rr->type == rrset->rri_rdtype) |
| 316 | rdata = &rrset->rri_rdatas[index_ans++]; |
| 317 | |
| 318 | if (rr->class == rrset->rri_rdclass && |
| 319 | rr->type == T_SIG) |
| 320 | rdata = &rrset->rri_sigs[index_sig++]; |
| 321 | |
| 322 | if (rdata) { |
| 323 | rdata->rdi_length = rr->size; |
| 324 | rdata->rdi_data = malloc(rr->size); |
| 325 | |
| 326 | if (rdata->rdi_data == NULL) { |
| 327 | result = ERRSET_NOMEMORY; |
| 328 | goto fail; |
| 329 | } |
| 330 | memcpy(rdata->rdi_data, rr->rdata, rr->size); |
| 331 | } |
| 332 | } |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 333 | free_dns_response(response); |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 334 | |
| 335 | *res = rrset; |
| 336 | return (ERRSET_SUCCESS); |
| 337 | |
| 338 | fail: |
| 339 | if (rrset != NULL) |
| 340 | freerrset(rrset); |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 341 | if (response != NULL) |
| 342 | free_dns_response(response); |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 343 | return (result); |
| 344 | } |
| 345 | |
| 346 | void |
| 347 | freerrset(struct rrsetinfo *rrset) |
| 348 | { |
| 349 | u_int16_t i; |
| 350 | |
| 351 | if (rrset == NULL) |
| 352 | return; |
| 353 | |
| 354 | if (rrset->rri_rdatas) { |
| 355 | for (i = 0; i < rrset->rri_nrdatas; i++) { |
| 356 | if (rrset->rri_rdatas[i].rdi_data == NULL) |
| 357 | break; |
| 358 | free(rrset->rri_rdatas[i].rdi_data); |
| 359 | } |
| 360 | free(rrset->rri_rdatas); |
| 361 | } |
| 362 | |
| 363 | if (rrset->rri_sigs) { |
| 364 | for (i = 0; i < rrset->rri_nsigs; i++) { |
| 365 | if (rrset->rri_sigs[i].rdi_data == NULL) |
| 366 | break; |
| 367 | free(rrset->rri_sigs[i].rdi_data); |
| 368 | } |
| 369 | free(rrset->rri_sigs); |
| 370 | } |
| 371 | |
| 372 | if (rrset->rri_name) |
| 373 | free(rrset->rri_name); |
| 374 | free(rrset); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * DNS response parsing routines |
| 379 | */ |
| 380 | static struct dns_response * |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 381 | parse_dns_response(const u_char *answer, int size) |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 382 | { |
| 383 | struct dns_response *resp; |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 384 | const u_char *cp; |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 385 | |
| 386 | /* allocate memory for the response */ |
| 387 | resp = calloc(1, sizeof(*resp)); |
| 388 | if (resp == NULL) |
| 389 | return (NULL); |
| 390 | |
| 391 | /* initialize current pointer */ |
| 392 | cp = answer; |
| 393 | |
| 394 | /* copy header */ |
| 395 | memcpy(&resp->header, cp, HFIXEDSZ); |
| 396 | cp += HFIXEDSZ; |
| 397 | |
| 398 | /* fix header byte order */ |
| 399 | resp->header.qdcount = ntohs(resp->header.qdcount); |
| 400 | resp->header.ancount = ntohs(resp->header.ancount); |
| 401 | resp->header.nscount = ntohs(resp->header.nscount); |
| 402 | resp->header.arcount = ntohs(resp->header.arcount); |
| 403 | |
| 404 | /* there must be at least one query */ |
| 405 | if (resp->header.qdcount < 1) { |
| 406 | free_dns_response(resp); |
| 407 | return (NULL); |
| 408 | } |
| 409 | |
| 410 | /* parse query section */ |
| 411 | resp->query = parse_dns_qsection(answer, size, &cp, |
| 412 | resp->header.qdcount); |
| 413 | if (resp->header.qdcount && resp->query == NULL) { |
| 414 | free_dns_response(resp); |
| 415 | return (NULL); |
| 416 | } |
| 417 | |
| 418 | /* parse answer section */ |
| 419 | resp->answer = parse_dns_rrsection(answer, size, &cp, |
| 420 | resp->header.ancount); |
| 421 | if (resp->header.ancount && resp->answer == NULL) { |
| 422 | free_dns_response(resp); |
| 423 | return (NULL); |
| 424 | } |
| 425 | |
| 426 | /* parse authority section */ |
| 427 | resp->authority = parse_dns_rrsection(answer, size, &cp, |
| 428 | resp->header.nscount); |
| 429 | if (resp->header.nscount && resp->authority == NULL) { |
| 430 | free_dns_response(resp); |
| 431 | return (NULL); |
| 432 | } |
| 433 | |
| 434 | /* parse additional section */ |
| 435 | resp->additional = parse_dns_rrsection(answer, size, &cp, |
| 436 | resp->header.arcount); |
| 437 | if (resp->header.arcount && resp->additional == NULL) { |
| 438 | free_dns_response(resp); |
| 439 | return (NULL); |
| 440 | } |
| 441 | |
| 442 | return (resp); |
| 443 | } |
| 444 | |
| 445 | static struct dns_query * |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 446 | parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count) |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 447 | { |
| 448 | struct dns_query *head, *curr, *prev; |
| 449 | int i, length; |
| 450 | char name[MAXDNAME]; |
| 451 | |
| 452 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { |
| 453 | |
| 454 | /* allocate and initialize struct */ |
| 455 | curr = calloc(1, sizeof(struct dns_query)); |
| 456 | if (curr == NULL) { |
| 457 | free_dns_query(head); |
| 458 | return (NULL); |
| 459 | } |
| 460 | if (head == NULL) |
| 461 | head = curr; |
| 462 | if (prev != NULL) |
| 463 | prev->next = curr; |
| 464 | |
| 465 | /* name */ |
| 466 | length = dn_expand(answer, answer + size, *cp, name, |
| 467 | sizeof(name)); |
| 468 | if (length < 0) { |
| 469 | free_dns_query(head); |
| 470 | return (NULL); |
| 471 | } |
| 472 | curr->name = strdup(name); |
| 473 | if (curr->name == NULL) { |
| 474 | free_dns_query(head); |
| 475 | return (NULL); |
| 476 | } |
| 477 | *cp += length; |
| 478 | |
| 479 | /* type */ |
| 480 | curr->type = _getshort(*cp); |
| 481 | *cp += INT16SZ; |
| 482 | |
| 483 | /* class */ |
| 484 | curr->class = _getshort(*cp); |
| 485 | *cp += INT16SZ; |
| 486 | } |
| 487 | |
| 488 | return (head); |
| 489 | } |
| 490 | |
| 491 | static struct dns_rr * |
Damien Miller | 9b59ada | 2005-11-05 16:56:52 +1100 | [diff] [blame] | 492 | parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, |
| 493 | int count) |
Damien Miller | d9ec370 | 2003-05-15 12:27:08 +1000 | [diff] [blame] | 494 | { |
| 495 | struct dns_rr *head, *curr, *prev; |
| 496 | int i, length; |
| 497 | char name[MAXDNAME]; |
| 498 | |
| 499 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { |
| 500 | |
| 501 | /* allocate and initialize struct */ |
| 502 | curr = calloc(1, sizeof(struct dns_rr)); |
| 503 | if (curr == NULL) { |
| 504 | free_dns_rr(head); |
| 505 | return (NULL); |
| 506 | } |
| 507 | if (head == NULL) |
| 508 | head = curr; |
| 509 | if (prev != NULL) |
| 510 | prev->next = curr; |
| 511 | |
| 512 | /* name */ |
| 513 | length = dn_expand(answer, answer + size, *cp, name, |
| 514 | sizeof(name)); |
| 515 | if (length < 0) { |
| 516 | free_dns_rr(head); |
| 517 | return (NULL); |
| 518 | } |
| 519 | curr->name = strdup(name); |
| 520 | if (curr->name == NULL) { |
| 521 | free_dns_rr(head); |
| 522 | return (NULL); |
| 523 | } |
| 524 | *cp += length; |
| 525 | |
| 526 | /* type */ |
| 527 | curr->type = _getshort(*cp); |
| 528 | *cp += INT16SZ; |
| 529 | |
| 530 | /* class */ |
| 531 | curr->class = _getshort(*cp); |
| 532 | *cp += INT16SZ; |
| 533 | |
| 534 | /* ttl */ |
| 535 | curr->ttl = _getlong(*cp); |
| 536 | *cp += INT32SZ; |
| 537 | |
| 538 | /* rdata size */ |
| 539 | curr->size = _getshort(*cp); |
| 540 | *cp += INT16SZ; |
| 541 | |
| 542 | /* rdata itself */ |
| 543 | curr->rdata = malloc(curr->size); |
| 544 | if (curr->rdata == NULL) { |
| 545 | free_dns_rr(head); |
| 546 | return (NULL); |
| 547 | } |
| 548 | memcpy(curr->rdata, *cp, curr->size); |
| 549 | *cp += curr->size; |
| 550 | } |
| 551 | |
| 552 | return (head); |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | free_dns_query(struct dns_query *p) |
| 557 | { |
| 558 | if (p == NULL) |
| 559 | return; |
| 560 | |
| 561 | if (p->name) |
| 562 | free(p->name); |
| 563 | free_dns_query(p->next); |
| 564 | free(p); |
| 565 | } |
| 566 | |
| 567 | static void |
| 568 | free_dns_rr(struct dns_rr *p) |
| 569 | { |
| 570 | if (p == NULL) |
| 571 | return; |
| 572 | |
| 573 | if (p->name) |
| 574 | free(p->name); |
| 575 | if (p->rdata) |
| 576 | free(p->rdata); |
| 577 | free_dns_rr(p->next); |
| 578 | free(p); |
| 579 | } |
| 580 | |
| 581 | static void |
| 582 | free_dns_response(struct dns_response *p) |
| 583 | { |
| 584 | if (p == NULL) |
| 585 | return; |
| 586 | |
| 587 | free_dns_query(p->query); |
| 588 | free_dns_rr(p->answer); |
| 589 | free_dns_rr(p->authority); |
| 590 | free_dns_rr(p->additional); |
| 591 | free(p); |
| 592 | } |
| 593 | |
| 594 | static int |
| 595 | count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type) |
| 596 | { |
| 597 | int n = 0; |
| 598 | |
| 599 | while(p) { |
| 600 | if (p->class == class && p->type == type) |
| 601 | n++; |
| 602 | p = p->next; |
| 603 | } |
| 604 | |
| 605 | return (n); |
| 606 | } |
| 607 | |
Darren Tucker | b8b4d0b | 2003-10-15 17:22:37 +1000 | [diff] [blame] | 608 | #endif /* !defined(HAVE_GETRRSETBYNAME) */ |