2 * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <netinet/in.h>
35 #include "dns_utils.h"
39 unsigned char buf[1024];
43 dns_getsrvinfo(const char *zone)
45 struct dns_srvinfo **res, *first;
46 unsigned char *end, *p;
47 char host[MAXHOSTNAMELEN];
49 int len, qdcount, ancount, n, i;
50 unsigned int type, class, ttl, priority, weight, port;
52 if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
53 len < (int)sizeof(HEADER))
56 qdcount = ntohs(q.hdr.qdcount);
57 ancount = ntohs(q.hdr.ancount);
60 p = q.buf + sizeof(HEADER);
62 while(qdcount > 0 && p < end) {
64 if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
66 p += len + NS_QFIXEDSZ;
69 res = calloc(ancount, sizeof(struct dns_srvinfo *));
74 while (ancount > 0 && p < end) {
76 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
78 for (i = 0; i < n; i++)
96 NS_GET16(priority, p);
100 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
102 for (i = 0; i < n; i++)
108 res[n] = malloc(sizeof(struct dns_srvinfo));
109 if (res[n] == NULL) {
110 for (i = 0; i < n; i++)
116 res[n]->class = class;
118 res[n]->priority = priority;
119 res[n]->weight = weight;
122 strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
128 for (i = 0; i < n - 1; i++)
129 res[i]->next = res[i + 1];