ares_private.h: use curl_memdebug.h instead of memdebug.h
[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_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     return ARES_ENOMEM;
90
91   /* nsname */
92   status = ares__expand_name_for_response(aptr, abuf, alen, &soa->nsname, &len);
93   if (status != ARES_SUCCESS)
94     goto failed_stat;
95   aptr += len;
96
97   /* hostmaster */
98   status = ares__expand_name_for_response(aptr, abuf, alen, &soa->hostmaster, &len);
99   if (status != ARES_SUCCESS)
100     goto failed_stat;
101   aptr += len;
102
103   /* integer fields */
104   if (aptr + 5 * 4 > abuf + alen)
105     goto failed;
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);
111
112   free(qname);
113   free(rr_name);
114
115   *soa_out = soa;
116
117   return ARES_SUCCESS;
118
119 failed:
120   status = ARES_EBADRESP;
121
122 failed_stat:
123   ares_free_data(soa);
124   if (qname)
125     free(qname);
126   if (rr_name)
127     free(rr_name);
128   return status;
129 }
130