yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | |
| 2 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
| 3 | * Copyright (C) 2012 Marko Kreen <markokr@gmail.com> |
| 4 | * |
| 5 | * Permission to use, copy, modify, and distribute this |
| 6 | * software and its documentation for any purpose and without |
| 7 | * fee is hereby granted, provided that the above copyright |
| 8 | * notice appear in all copies and that both that copyright |
| 9 | * notice and this permission notice appear in supporting |
| 10 | * documentation, and that the name of M.I.T. not be used in |
| 11 | * advertising or publicity pertaining to distribution of the |
| 12 | * software without specific, written prior permission. |
| 13 | * M.I.T. makes no representations about the suitability of |
| 14 | * this software for any purpose. It is provided "as is" |
| 15 | * without express or implied warranty. |
| 16 | */ |
| 17 | |
| 18 | #include "ares_setup.h" |
| 19 | |
| 20 | #ifdef HAVE_NETINET_IN_H |
| 21 | # include <netinet/in.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_NETDB_H |
| 24 | # include <netdb.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_ARPA_INET_H |
| 27 | # include <arpa/inet.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_ARPA_NAMESER_H |
| 30 | # include <arpa/nameser.h> |
| 31 | #else |
| 32 | # include "nameser.h" |
| 33 | #endif |
| 34 | #ifdef HAVE_ARPA_NAMESER_COMPAT_H |
| 35 | # include <arpa/nameser_compat.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "ares.h" |
| 39 | #include "ares_dns.h" |
| 40 | #include "ares_data.h" |
| 41 | #include "ares_private.h" |
| 42 | |
| 43 | int |
| 44 | ares_parse_soa_reply(const unsigned char *abuf, int alen, |
| 45 | struct ares_soa_reply **soa_out) |
| 46 | { |
| 47 | const unsigned char *aptr; |
| 48 | long len; |
| 49 | char *qname = NULL, *rr_name = NULL; |
| 50 | struct ares_soa_reply *soa = NULL; |
| 51 | int qdcount, ancount; |
| 52 | int status; |
| 53 | |
| 54 | if (alen < HFIXEDSZ) |
| 55 | return ARES_EBADRESP; |
| 56 | |
| 57 | /* parse message header */ |
| 58 | qdcount = DNS_HEADER_QDCOUNT(abuf); |
| 59 | ancount = DNS_HEADER_ANCOUNT(abuf); |
| 60 | if (qdcount != 1 || ancount != 1) |
| 61 | return ARES_EBADRESP; |
| 62 | aptr = abuf + HFIXEDSZ; |
| 63 | |
| 64 | /* query name */ |
| 65 | status = ares__expand_name_for_response(aptr, abuf, alen, &qname, &len); |
| 66 | if (status != ARES_SUCCESS) |
| 67 | goto failed_stat; |
| 68 | aptr += len; |
| 69 | |
| 70 | /* skip qtype & qclass */ |
| 71 | if (aptr + QFIXEDSZ > abuf + alen) |
| 72 | goto failed; |
| 73 | aptr += QFIXEDSZ; |
| 74 | |
| 75 | /* rr_name */ |
| 76 | status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len); |
| 77 | if (status != ARES_SUCCESS) |
| 78 | goto failed_stat; |
| 79 | aptr += len; |
| 80 | |
| 81 | /* skip rr_type, rr_class, rr_ttl, rr_rdlen */ |
| 82 | if (aptr + RRFIXEDSZ > abuf + alen) |
| 83 | goto failed; |
| 84 | aptr += RRFIXEDSZ; |
| 85 | |
| 86 | /* allocate result struct */ |
| 87 | soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY); |
| 88 | if (!soa) |
| 89 | { |
| 90 | status = ARES_ENOMEM; |
| 91 | goto failed_stat; |
| 92 | } |
| 93 | |
| 94 | /* nsname */ |
| 95 | status = ares__expand_name_for_response(aptr, abuf, alen, &soa->nsname, &len); |
| 96 | if (status != ARES_SUCCESS) |
| 97 | goto failed_stat; |
| 98 | aptr += len; |
| 99 | |
| 100 | /* hostmaster */ |
| 101 | status = ares__expand_name_for_response(aptr, abuf, alen, &soa->hostmaster, &len); |
| 102 | if (status != ARES_SUCCESS) |
| 103 | goto failed_stat; |
| 104 | aptr += len; |
| 105 | |
| 106 | /* integer fields */ |
| 107 | if (aptr + 5 * 4 > abuf + alen) |
| 108 | goto failed; |
| 109 | soa->serial = DNS__32BIT(aptr + 0 * 4); |
| 110 | soa->refresh = DNS__32BIT(aptr + 1 * 4); |
| 111 | soa->retry = DNS__32BIT(aptr + 2 * 4); |
| 112 | soa->expire = DNS__32BIT(aptr + 3 * 4); |
| 113 | soa->minttl = DNS__32BIT(aptr + 4 * 4); |
| 114 | |
| 115 | ares_free(qname); |
| 116 | ares_free(rr_name); |
| 117 | |
| 118 | *soa_out = soa; |
| 119 | |
| 120 | return ARES_SUCCESS; |
| 121 | |
| 122 | failed: |
| 123 | status = ARES_EBADRESP; |
| 124 | |
| 125 | failed_stat: |
| 126 | ares_free_data(soa); |
| 127 | if (qname) |
| 128 | ares_free(qname); |
| 129 | if (rr_name) |
| 130 | ares_free(rr_name); |
| 131 | return status; |
| 132 | } |
| 133 | |