Added support for parsing NAPTR records
[platform/upstream/c-ares.git] / ares_parse_naptr_reply.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.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 <stdlib.h>
42 #include <string.h>
43 #include "ares.h"
44 #include "ares_dns.h"
45 #include "ares_data.h"
46 #include "ares_private.h"
47
48 /* AIX portability check */
49 #ifndef T_NAPTR
50         #define T_NAPTR 35 /* naming authority pointer */
51 #endif
52
53 int
54 ares_parse_naptr_reply (const unsigned char *abuf, int alen,
55                         struct ares_naptr_reply **naptr_out)
56 {
57   unsigned int qdcount, ancount, i;
58   const unsigned char *aptr, *vptr;
59   int status, rr_type, rr_class, rr_len;
60   long len;
61   char *hostname = NULL, *rr_name = NULL;
62   struct ares_naptr_reply *naptr_head = NULL;
63   struct ares_naptr_reply *naptr_last = NULL;
64   struct ares_naptr_reply *naptr_curr;
65
66   /* Set *naptr_out to NULL for all failure cases. */
67   *naptr_out = NULL;
68
69   /* Give up if abuf doesn't have room for a header. */
70   if (alen < HFIXEDSZ)
71     return ARES_EBADRESP;
72
73   /* Fetch the question and answer count from the header. */
74   qdcount = DNS_HEADER_QDCOUNT (abuf);
75   ancount = DNS_HEADER_ANCOUNT (abuf);
76   if (qdcount != 1)
77     return ARES_EBADRESP;
78   if (ancount == 0)
79     return ARES_ENODATA;
80
81   /* Expand the name from the question, and skip past the question. */
82   aptr = abuf + HFIXEDSZ;
83   status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
84   if (status != ARES_SUCCESS)
85     return status;
86
87   if (aptr + len + QFIXEDSZ > abuf + alen)
88     {
89       free (hostname);
90       return ARES_EBADRESP;
91     }
92   aptr += len + QFIXEDSZ;
93
94   /* Examine each answer resource record (RR) in turn. */
95   for (i = 0; i < ancount; i++)
96     {
97       /* Decode the RR up to the data field. */
98       status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
99       if (status != ARES_SUCCESS)
100         {
101           break;
102         }
103       aptr += len;
104       if (aptr + RRFIXEDSZ > abuf + alen)
105         {
106           status = ARES_EBADRESP;
107           break;
108         }
109       rr_type = DNS_RR_TYPE (aptr);
110       rr_class = DNS_RR_CLASS (aptr);
111       rr_len = DNS_RR_LEN (aptr);
112       aptr += RRFIXEDSZ;
113
114       /* Check if we are really looking at a NAPTR record */
115       if (rr_class == C_IN && rr_type == T_NAPTR)
116         {
117           /* parse the NAPTR record itself */
118
119           /* Allocate storage for this NAPTR answer appending it to the list */
120           naptr_curr = ares_malloc_data(ARES_DATATYPE_NAPTR_REPLY);
121           if (!naptr_curr)
122             {
123               status = ARES_ENOMEM;
124               break;
125             }
126           if (naptr_last)
127             {
128               naptr_last->next = naptr_curr;
129             }
130           else
131             {
132               naptr_head = naptr_curr;
133             }
134           naptr_last = naptr_curr;
135
136           vptr = aptr;
137           naptr_curr->order = DNS__16BIT(vptr);
138           vptr += sizeof(unsigned short);
139           naptr_curr->preference = DNS__16BIT(vptr);
140           vptr += sizeof(unsigned short);
141
142           status = ares_expand_string(vptr, abuf, alen, &naptr_curr->flags, &len);
143           if (status != ARES_SUCCESS)
144             break;
145           vptr += len;
146
147           status = ares_expand_string(vptr, abuf, alen, &naptr_curr->service, &len);
148           if (status != ARES_SUCCESS)
149             break;
150           vptr += len;
151
152           status = ares_expand_string(vptr, abuf, alen, &naptr_curr->regexp, &len);
153           if (status != ARES_SUCCESS)
154             break;
155           vptr += len;
156
157           status = ares_expand_name(vptr, abuf, alen, &naptr_curr->replacement, &len);
158           if (status != ARES_SUCCESS)
159             break;
160         }
161
162       /* Don't lose memory in the next iteration */
163       free (rr_name);
164       rr_name = NULL;
165
166       /* Move on to the next record */
167       aptr += rr_len;
168     }
169
170   if (hostname)
171     free (hostname);
172   if (rr_name)
173     free (rr_name);
174
175   /* clean up on error */
176   if (status != ARES_SUCCESS)
177     {
178       if (naptr_head)
179         ares_free_data (naptr_head);
180       return status;
181     }
182
183   /* everything looks fine, return the data */
184   *naptr_out = naptr_head;
185
186   return ARES_SUCCESS;
187 }
188