2 * Copyright (c) 2012-2013 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 srv_priority_cmp(const void *a, const void *b)
45 const struct dns_srvinfo *da, *db;
48 da = *(struct dns_srvinfo * const *)a;
49 db = *(struct dns_srvinfo * const *)b;
54 return ((l > r) - (l < r));
58 srv_final_cmp(const void *a, const void *b)
60 const struct dns_srvinfo *da, *db;
61 unsigned int r, l, wr, wl;
64 da = *(struct dns_srvinfo * const *)a;
65 db = *(struct dns_srvinfo * const *)b;
70 res = ((l > r) - (l < r));
75 res = ((wr > wl) - (wr < wl));
82 compute_weight(struct dns_srvinfo **d, int first, int last)
84 int i, j, totalweight;
89 for (i = 0; i <= last; i++)
90 totalweight += d[i]->weight;
95 chosen = malloc(sizeof(int) * (last - first + 1));
97 for (i = 0; i <= last; i++) {
99 chosen[i] = random() % (d[i]->weight * 100 / totalweight);
100 for (j = 0; j < i; j++) {
101 if (chosen[i] == chosen[j])
105 d[i]->finalweight = chosen[i];
115 dns_getsrvinfo(const char *zone)
117 struct dns_srvinfo **res, *first;
118 unsigned char *end, *p;
119 char host[MAXHOSTNAMELEN];
121 int len, qdcount, ancount, n, i, f, l;
122 unsigned int type, class, ttl, priority, weight, port;
124 if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
125 len < (int)sizeof(HEADER))
128 qdcount = ntohs(q.hdr.qdcount);
129 ancount = ntohs(q.hdr.ancount);
132 p = q.buf + sizeof(HEADER);
134 while(qdcount > 0 && p < end) {
136 if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
138 p += len + NS_QFIXEDSZ;
141 res = calloc(ancount, sizeof(struct dns_srvinfo *));
146 while (ancount > 0 && p < end) {
148 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
150 for (i = 0; i < n; i++)
168 NS_GET16(priority, p);
172 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
174 for (i = 0; i < n; i++)
180 res[n] = malloc(sizeof(struct dns_srvinfo));
181 if (res[n] == NULL) {
182 for (i = 0; i < n; i++)
188 res[n]->class = class;
190 res[n]->priority = priority;
191 res[n]->weight = weight;
194 strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
200 qsort(res, n, sizeof(res[0]), srv_priority_cmp);
202 priority = f = l = 0;
203 for (i = 0; i < n; i++) {
204 if (res[i]->priority != priority) {
206 compute_weight(res, f, l);
208 priority = res[i]->priority;
213 qsort(res, n, sizeof(res[0]), srv_final_cmp);
215 for (i = 0; i < n - 1; i++)
216 res[i]->next = res[i + 1];