2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2012 Marko Kreen <markokr@gmail.com>
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.
18 #include "ares_setup.h"
20 #ifdef HAVE_NETINET_IN_H
21 # include <netinet/in.h>
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
29 #ifdef HAVE_ARPA_NAMESER_H
30 # include <arpa/nameser.h>
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 # include <arpa/nameser_compat.h>
40 #include "ares_data.h"
41 #include "ares_private.h"
44 ares_parse_soa_reply(const unsigned char *abuf, int alen,
45 struct ares_soa_reply **soa_out)
47 const unsigned char *aptr;
49 char *qname = NULL, *rr_name = NULL;
50 struct ares_soa_reply *soa = NULL;
57 /* parse message header */
58 qdcount = DNS_HEADER_QDCOUNT(abuf);
59 ancount = DNS_HEADER_ANCOUNT(abuf);
60 if (qdcount != 1 || ancount != 1)
62 aptr = abuf + HFIXEDSZ;
65 status = ares__expand_name_for_response(aptr, abuf, alen, &qname, &len);
66 if (status != ARES_SUCCESS)
70 /* skip qtype & qclass */
71 if (aptr + QFIXEDSZ > abuf + alen)
76 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
77 if (status != ARES_SUCCESS)
81 /* skip rr_type, rr_class, rr_ttl, rr_rdlen */
82 if (aptr + RRFIXEDSZ > abuf + alen)
86 /* allocate result struct */
87 soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY);
92 status = ares__expand_name_for_response(aptr, abuf, alen, &soa->nsname, &len);
93 if (status != ARES_SUCCESS)
98 status = ares__expand_name_for_response(aptr, abuf, alen, &soa->hostmaster, &len);
99 if (status != ARES_SUCCESS)
104 if (aptr + 5 * 4 > abuf + alen)
106 soa->serial = DNS__32BIT(aptr + 0 * 4);
107 soa->refresh = DNS__32BIT(aptr + 1 * 4);
108 soa->retry = DNS__32BIT(aptr + 2 * 4);
109 soa->expire = DNS__32BIT(aptr + 3 * 4);
110 soa->minttl = DNS__32BIT(aptr + 4 * 4);
120 status = ARES_EBADRESP;