ares_inet_ntop.3: s/socklen_t/ares_socklen_t
[platform/upstream/c-ares.git] / ares_parse_mx_reply.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2010 Jeremy Lal <kapouer@melix.org>
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_mx_reply (const unsigned char *abuf, int alen,
45                      struct ares_mx_reply **mx_out)
46 {
47   unsigned int qdcount, ancount, i;
48   const unsigned char *aptr, *vptr;
49   int status, rr_type, rr_class, rr_len;
50   long len;
51   char *hostname = NULL, *rr_name = NULL;
52   struct ares_mx_reply *mx_head = NULL;
53   struct ares_mx_reply *mx_last = NULL;
54   struct ares_mx_reply *mx_curr;
55
56   /* Set *mx_out to NULL for all failure cases. */
57   *mx_out = NULL;
58
59   /* Give up if abuf doesn't have room for a header. */
60   if (alen < HFIXEDSZ)
61     return ARES_EBADRESP;
62
63   /* Fetch the question and answer count from the header. */
64   qdcount = DNS_HEADER_QDCOUNT (abuf);
65   ancount = DNS_HEADER_ANCOUNT (abuf);
66   if (qdcount != 1)
67     return ARES_EBADRESP;
68   if (ancount == 0)
69     return ARES_ENODATA;
70
71   /* Expand the name from the question, and skip past the question. */
72   aptr = abuf + HFIXEDSZ;
73   status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
74   if (status != ARES_SUCCESS)
75     return status;
76
77   if (aptr + len + QFIXEDSZ > abuf + alen)
78     {
79       free (hostname);
80       return ARES_EBADRESP;
81     }
82   aptr += len + QFIXEDSZ;
83
84   /* Examine each answer resource record (RR) in turn. */
85   for (i = 0; i < ancount; i++)
86     {
87       /* Decode the RR up to the data field. */
88       status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
89       if (status != ARES_SUCCESS)
90         {
91           break;
92         }
93       aptr += len;
94       if (aptr + RRFIXEDSZ > abuf + alen)
95         {
96           status = ARES_EBADRESP;
97           break;
98         }
99       rr_type = DNS_RR_TYPE (aptr);
100       rr_class = DNS_RR_CLASS (aptr);
101       rr_len = DNS_RR_LEN (aptr);
102       aptr += RRFIXEDSZ;
103
104       /* Check if we are really looking at a MX record */
105       if (rr_class == C_IN && rr_type == T_MX)
106         {
107           /* parse the MX record itself */
108           if (rr_len < 2)
109             {
110               status = ARES_EBADRESP;
111               break;
112             }
113
114           /* Allocate storage for this MX answer appending it to the list */
115           mx_curr = ares_malloc_data(ARES_DATATYPE_MX_REPLY);
116           if (!mx_curr)
117             {
118               status = ARES_ENOMEM;
119               break;
120             }
121           if (mx_last)
122             {
123               mx_last->next = mx_curr;
124             }
125           else
126             {
127               mx_head = mx_curr;
128             }
129           mx_last = mx_curr;
130
131           vptr = aptr;
132           mx_curr->priority = DNS__16BIT(vptr);
133           vptr += sizeof(unsigned short);
134
135           status = ares_expand_name (vptr, abuf, alen, &mx_curr->host, &len);
136           if (status != ARES_SUCCESS)
137             break;
138         }
139
140       /* Don't lose memory in the next iteration */
141       free (rr_name);
142       rr_name = NULL;
143
144       /* Move on to the next record */
145       aptr += rr_len;
146     }
147
148   if (hostname)
149     free (hostname);
150   if (rr_name)
151     free (rr_name);
152
153   /* clean up on error */
154   if (status != ARES_SUCCESS)
155     {
156       if (mx_head)
157         ares_free_data (mx_head);
158       return status;
159     }
160
161   /* everything looks fine, return the data */
162   *mx_out = mx_head;
163
164   return ARES_SUCCESS;
165 }