2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2013 Baptiste Daroussin <bapt@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <netinet/in.h>
37 #include "dns_utils.h"
41 unsigned char buf[1024];
45 srv_priority_cmp(const void *a, const void *b)
47 const struct dns_srvinfo *da, *db;
50 da = *(struct dns_srvinfo * const *)a;
51 db = *(struct dns_srvinfo * const *)b;
56 return ((l > r) - (l < r));
60 srv_final_cmp(const void *a, const void *b)
62 const struct dns_srvinfo *da, *db;
63 unsigned int r, l, wr, wl;
66 da = *(struct dns_srvinfo * const *)a;
67 db = *(struct dns_srvinfo * const *)b;
72 res = ((l > r) - (l < r));
77 res = ((wr > wl) - (wr < wl));
84 compute_weight(struct dns_srvinfo **d, int first, int last)
86 int i, j, totalweight;
91 for (i = 0; i <= last; i++)
92 totalweight += d[i]->weight;
97 chosen = malloc(sizeof(int) * (last - first + 1));
99 for (i = 0; i <= last; i++) {
101 chosen[i] = random() % (d[i]->weight * 100 / totalweight);
102 for (j = 0; j < i; j++) {
103 if (chosen[i] == chosen[j])
107 d[i]->finalweight = chosen[i];
117 dns_getsrvinfo(const char *zone)
119 struct dns_srvinfo **res, *first;
120 unsigned char *end, *p;
121 char host[MAXHOSTNAMELEN];
123 int len, qdcount, ancount, n, i, f, l;
124 unsigned int type, class, ttl, priority, weight, port;
126 if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
127 len < (int)sizeof(HEADER))
130 qdcount = ntohs(q.hdr.qdcount);
131 ancount = ntohs(q.hdr.ancount);
134 p = q.buf + sizeof(HEADER);
136 while(qdcount > 0 && p < end) {
138 if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
140 p += len + NS_QFIXEDSZ;
143 res = calloc(ancount, sizeof(struct dns_srvinfo *));
148 while (ancount > 0 && p < end) {
150 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
152 for (i = 0; i < n; i++)
170 NS_GET16(priority, p);
174 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
176 for (i = 0; i < n; i++)
182 res[n] = malloc(sizeof(struct dns_srvinfo));
183 if (res[n] == NULL) {
184 for (i = 0; i < n; i++)
190 res[n]->class = class;
192 res[n]->priority = priority;
193 res[n]->weight = weight;
196 strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
202 qsort(res, n, sizeof(res[0]), srv_priority_cmp);
204 priority = f = l = 0;
205 for (i = 0; i < n; i++) {
206 if (res[i]->priority != priority) {
208 compute_weight(res, f, l);
210 priority = res[i]->priority;
215 qsort(res, n, sizeof(res[0]), srv_final_cmp);
217 for (i = 0; i < n - 1; i++)
218 res[i]->next = res[i + 1];