2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.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"
43 /* AIX portability check */
45 # define T_SRV 33 /* server selection */
49 ares_parse_srv_reply (const unsigned char *abuf, int alen,
50 struct ares_srv_reply **srv_out)
52 unsigned int qdcount, ancount, i;
53 const unsigned char *aptr, *vptr;
54 int status, rr_type, rr_class, rr_len;
56 char *hostname = NULL, *rr_name = NULL;
57 struct ares_srv_reply *srv_head = NULL;
58 struct ares_srv_reply *srv_last = NULL;
59 struct ares_srv_reply *srv_curr;
61 /* Set *srv_out to NULL for all failure cases. */
64 /* Give up if abuf doesn't have room for a header. */
68 /* Fetch the question and answer count from the header. */
69 qdcount = DNS_HEADER_QDCOUNT (abuf);
70 ancount = DNS_HEADER_ANCOUNT (abuf);
76 /* Expand the name from the question, and skip past the question. */
77 aptr = abuf + HFIXEDSZ;
78 status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
79 if (status != ARES_SUCCESS)
82 if (aptr + len + QFIXEDSZ > abuf + alen)
87 aptr += len + QFIXEDSZ;
89 /* Examine each answer resource record (RR) in turn. */
90 for (i = 0; i < ancount; i++)
92 /* Decode the RR up to the data field. */
93 status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
94 if (status != ARES_SUCCESS)
99 if (aptr + RRFIXEDSZ > abuf + alen)
101 status = ARES_EBADRESP;
104 rr_type = DNS_RR_TYPE (aptr);
105 rr_class = DNS_RR_CLASS (aptr);
106 rr_len = DNS_RR_LEN (aptr);
109 /* Check if we are really looking at a SRV record */
110 if (rr_class == C_IN && rr_type == T_SRV)
112 /* parse the SRV record itself */
115 status = ARES_EBADRESP;
119 /* Allocate storage for this SRV answer appending it to the list */
120 srv_curr = ares_malloc_data(ARES_DATATYPE_SRV_REPLY);
123 status = ARES_ENOMEM;
128 srv_last->next = srv_curr;
137 srv_curr->priority = DNS__16BIT(vptr);
138 vptr += sizeof(unsigned short);
139 srv_curr->weight = DNS__16BIT(vptr);
140 vptr += sizeof(unsigned short);
141 srv_curr->port = DNS__16BIT(vptr);
142 vptr += sizeof(unsigned short);
144 status = ares_expand_name (vptr, abuf, alen, &srv_curr->host, &len);
145 if (status != ARES_SUCCESS)
149 /* Don't lose memory in the next iteration */
153 /* Move on to the next record */
162 /* clean up on error */
163 if (status != ARES_SUCCESS)
166 ares_free_data (srv_head);
170 /* everything looks fine, return the data */