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;
87 chosen = malloc(sizeof(int) * (last - first + 1));
90 for (i = 0; i <= last; i++)
91 totalweight += d[i]->weight;
96 for (i = 0; i <= last; i++) {
98 chosen[i] = random() % (d[i]->weight * 100 / totalweight);
99 for (j = 0; j < i; j++) {
100 if (chosen[i] == chosen[j])
104 d[i]->finalweight = chosen[i];
114 dns_getsrvinfo(const char *zone)
116 struct dns_srvinfo **res, *first;
117 unsigned char *end, *p;
118 char host[MAXHOSTNAMELEN];
120 int len, qdcount, ancount, n, i, f, l;
121 unsigned int type, class, ttl, priority, weight, port;
123 if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
124 len < (int)sizeof(HEADER))
127 qdcount = ntohs(q.hdr.qdcount);
128 ancount = ntohs(q.hdr.ancount);
131 p = q.buf + sizeof(HEADER);
133 while(qdcount > 0 && p < end) {
135 if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
137 p += len + NS_QFIXEDSZ;
140 res = calloc(ancount, sizeof(struct dns_srvinfo *));
145 while (ancount > 0 && p < end) {
147 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
149 for (i = 0; i < n; i++)
167 NS_GET16(priority, p);
171 len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
173 for (i = 0; i < n; i++)
179 res[n] = malloc(sizeof(struct dns_srvinfo));
180 if (res[n] == NULL) {
181 for (i = 0; i < n; i++)
187 res[n]->class = class;
189 res[n]->priority = priority;
190 res[n]->weight = weight;
193 strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
199 qsort(res, n, sizeof(res[0]), srv_priority_cmp);
201 priority = f = l = 0;
202 for (i = 0; i < n; i++) {
203 if (res[i]->priority != priority) {
205 compute_weight(res, f, l);
207 priority = res[i]->priority;
212 qsort(res, n, sizeof(res[0]), srv_final_cmp);
214 for (i = 0; i < n - 1; i++)
215 res[i]->next = res[i + 1];