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_SYS_SOCKET_H
21 # include <sys/socket.h>
23 #ifdef HAVE_NETINET_IN_H
24 # include <netinet/in.h>
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
37 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
38 # include <arpa/nameser_compat.h>
45 #include "ares_data.h"
46 #include "ares_private.h"
48 /* AIX portability check */
50 # define T_SRV 33 /* server selection */
54 ares_parse_srv_reply (const unsigned char *abuf, int alen,
55 struct ares_srv_reply **srv_out)
57 unsigned int qdcount, ancount, i;
58 const unsigned char *aptr, *vptr;
59 int status, rr_type, rr_class, rr_len;
61 char *hostname = NULL, *rr_name = NULL;
62 struct ares_srv_reply *srv_head = NULL;
63 struct ares_srv_reply *srv_last = NULL;
64 struct ares_srv_reply *srv_curr;
66 /* Set *srv_out to NULL for all failure cases. */
69 /* Give up if abuf doesn't have room for a header. */
73 /* Fetch the question and answer count from the header. */
74 qdcount = DNS_HEADER_QDCOUNT (abuf);
75 ancount = DNS_HEADER_ANCOUNT (abuf);
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)
87 if (aptr + len + QFIXEDSZ > abuf + alen)
92 aptr += len + QFIXEDSZ;
94 /* Examine each answer resource record (RR) in turn. */
95 for (i = 0; i < ancount; i++)
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)
104 if (aptr + RRFIXEDSZ > abuf + alen)
106 status = ARES_EBADRESP;
109 rr_type = DNS_RR_TYPE (aptr);
110 rr_class = DNS_RR_CLASS (aptr);
111 rr_len = DNS_RR_LEN (aptr);
114 /* Check if we are really looking at a SRV record */
115 if (rr_class == C_IN && rr_type == T_SRV)
117 /* parse the SRV record itself */
120 status = ARES_EBADRESP;
124 /* Allocate storage for this SRV answer appending it to the list */
125 srv_curr = ares_malloc_data(ARES_DATATYPE_SRV_REPLY);
128 status = ARES_ENOMEM;
133 srv_last->next = srv_curr;
142 srv_curr->priority = DNS__16BIT(vptr);
143 vptr += sizeof(unsigned short);
144 srv_curr->weight = DNS__16BIT(vptr);
145 vptr += sizeof(unsigned short);
146 srv_curr->port = DNS__16BIT(vptr);
147 vptr += sizeof(unsigned short);
149 status = ares_expand_name (vptr, abuf, alen, &srv_curr->host, &len);
150 if (status != ARES_SUCCESS)
154 /* Don't lose memory in the next iteration */
158 /* Move on to the next record */
167 /* clean up on error */
168 if (status != ARES_SUCCESS)
171 ares_free_data (srv_head);
175 /* everything looks fine, return the data */