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
ares_parse_soa_reply(const unsigned char * abuf,int alen,struct ares_soa_reply ** soa_out)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, qclass;
52 int status, i, rr_type, rr_class, rr_len;
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
61 if (qdcount != 1)
62 return ARES_EBADRESP;
63 if (ancount == 0)
64 return ARES_EBADRESP;
65
66 aptr = abuf + HFIXEDSZ;
67
68 /* query name */
69 status = ares__expand_name_for_response(aptr, abuf, alen, &qname, &len);
70 if (status != ARES_SUCCESS)
71 goto failed_stat;
72 aptr += len;
73
74 qclass = DNS_QUESTION_TYPE(aptr);
75
76 /* skip qtype & qclass */
77 if (aptr + QFIXEDSZ > abuf + alen)
78 goto failed;
79 aptr += QFIXEDSZ;
80
81 /* qclass of SOA with multiple answers */
82 if (qclass == T_SOA && ancount > 1)
83 goto failed;
84
85 /* examine all the records, break and return if found soa */
86 for (i = 0; i < ancount; i++)
87 {
88 rr_name = NULL;
89 status = ares__expand_name_for_response (aptr, abuf, alen, &rr_name, &len);
90 if (status != ARES_SUCCESS)
91 {
92 ares_free(rr_name);
93 goto failed_stat;
94 }
95
96 aptr += len;
97 if ( aptr + RRFIXEDSZ > abuf + alen )
98 {
99 ares_free(rr_name);
100 status = ARES_EBADRESP;
101 goto failed_stat;
102 }
103 rr_type = DNS_RR_TYPE( aptr );
104 rr_class = DNS_RR_CLASS( aptr );
105 rr_len = DNS_RR_LEN( aptr );
106 aptr += RRFIXEDSZ;
107 if (aptr + rr_len > abuf + alen)
108 {
109 ares_free(rr_name);
110 status = ARES_EBADRESP;
111 goto failed_stat;
112 }
113 if ( rr_class == C_IN && rr_type == T_SOA )
114 {
115 /* allocate result struct */
116 soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY);
117 if (!soa)
118 {
119 ares_free(rr_name);
120 status = ARES_ENOMEM;
121 goto failed_stat;
122 }
123
124 /* nsname */
125 status = ares__expand_name_for_response(aptr, abuf, alen, &soa->nsname,
126 &len);
127 if (status != ARES_SUCCESS)
128 {
129 ares_free(rr_name);
130 goto failed_stat;
131 }
132 aptr += len;
133
134 /* hostmaster */
135 status = ares__expand_name_for_response(aptr, abuf, alen,
136 &soa->hostmaster, &len);
137 if (status != ARES_SUCCESS)
138 {
139 ares_free(rr_name);
140 goto failed_stat;
141 }
142 aptr += len;
143
144 /* integer fields */
145 if (aptr + 5 * 4 > abuf + alen)
146 {
147 ares_free(rr_name);
148 goto failed;
149 }
150 soa->serial = DNS__32BIT(aptr + 0 * 4);
151 soa->refresh = DNS__32BIT(aptr + 1 * 4);
152 soa->retry = DNS__32BIT(aptr + 2 * 4);
153 soa->expire = DNS__32BIT(aptr + 3 * 4);
154 soa->minttl = DNS__32BIT(aptr + 4 * 4);
155
156 ares_free(qname);
157 ares_free(rr_name);
158
159 *soa_out = soa;
160
161 return ARES_SUCCESS;
162 }
163 aptr += rr_len;
164
165 ares_free(rr_name);
166
167 if (aptr > abuf + alen)
168 goto failed_stat;
169 }
170 /* no SOA record found */
171 status = ARES_EBADRESP;
172 goto failed_stat;
173 failed:
174 status = ARES_EBADRESP;
175
176 failed_stat:
177 if (soa)
178 ares_free_data(soa);
179 if (qname)
180 ares_free(qname);
181 return status;
182 }
183