Header inclusion clean-up
[platform/upstream/c-ares.git] / ares_parse_soa_reply.c
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_SYS_SOCKET_H
21 #  include <sys/socket.h>
22 #endif
23 #ifdef HAVE_NETINET_IN_H
24 #  include <netinet/in.h>
25 #endif
26 #ifdef HAVE_NETDB_H
27 #  include <netdb.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 #  include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 #  include <arpa/nameser.h>
34 #else
35 #  include "nameser.h"
36 #endif
37 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
38 #  include <arpa/nameser_compat.h>
39 #endif
40
41 #include "ares.h"
42 #include "ares_dns.h"
43 #include "ares_data.h"
44 #include "ares_private.h"
45
46 int
47 ares_parse_soa_reply(const unsigned char *abuf, int alen,
48                      struct ares_soa_reply **soa_out)
49 {
50   const unsigned char *aptr;
51   long len;
52   char *qname = NULL, *rr_name = NULL;
53   struct ares_soa_reply *soa = NULL;
54   int qdcount, ancount;
55   int status;
56
57   if (alen < HFIXEDSZ)
58     return ARES_EBADRESP;
59
60   /* parse message header */
61   qdcount = DNS_HEADER_QDCOUNT(abuf);
62   ancount = DNS_HEADER_ANCOUNT(abuf);
63   if (qdcount != 1 || ancount != 1)
64     return ARES_EBADRESP;
65   aptr = abuf + HFIXEDSZ;
66
67   /* query name */
68   status = ares__expand_name_for_response(aptr, abuf, alen, &qname, &len);
69   if (status != ARES_SUCCESS)
70     goto failed_stat;
71   aptr += len;
72
73   /* skip qtype & qclass */
74   if (aptr + QFIXEDSZ > abuf + alen)
75     goto failed;
76   aptr += QFIXEDSZ;
77
78   /* rr_name */
79   status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
80   if (status != ARES_SUCCESS)
81     goto failed_stat;
82   aptr += len;
83
84   /* skip rr_type, rr_class, rr_ttl, rr_rdlen */
85   if (aptr + RRFIXEDSZ > abuf + alen)
86     goto failed;
87   aptr += RRFIXEDSZ;
88
89   /* allocate result struct */
90   soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY);
91   if (!soa)
92     return ARES_ENOMEM;
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   free(qname);
116   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     free(qname);
129   if (rr_name)
130     free(rr_name);
131   return status;
132 }
133