xref: /freebsd/usr.sbin/pkg/dns_utils.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1 /*-
2  * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include <netinet/in.h>
33 #include <resolv.h>
34 
35 #include "dns_utils.h"
36 
37 typedef union {
38 	HEADER hdr;
39 	unsigned char buf[1024];
40 } dns_query;
41 
42 struct dns_srvinfo *
43 dns_getsrvinfo(const char *zone)
44 {
45 	struct dns_srvinfo **res, *first;
46 	unsigned char *end, *p;
47 	char host[MAXHOSTNAMELEN];
48 	dns_query q;
49 	int len, qdcount, ancount, n, i;
50 	unsigned int type, class, ttl, priority, weight, port;
51 
52 	if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
53 	    len < (int)sizeof(HEADER))
54 		return (NULL);
55 
56 	qdcount = ntohs(q.hdr.qdcount);
57 	ancount = ntohs(q.hdr.ancount);
58 
59 	end = q.buf + len;
60 	p = q.buf + sizeof(HEADER);
61 
62 	while(qdcount > 0 && p < end) {
63 		qdcount--;
64 		if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
65 			return (NULL);
66 		p += len + NS_QFIXEDSZ;
67 	}
68 
69 	res = malloc(sizeof(struct dns_srvinfo) * ancount);
70 	if (res == NULL)
71 		return (NULL);
72 	memset(res, 0, sizeof(struct dns_srvinfo) * ancount);
73 
74 	n = 0;
75 	while (ancount > 0 && p < end) {
76 		ancount--;
77 		len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
78 		if (len < 0) {
79 			for (i = 0; i < n; i++)
80 				free(res[i]);
81 			free(res);
82 			return NULL;
83 		}
84 
85 		p += len;
86 
87 		NS_GET16(type, p);
88 		NS_GET16(class, p);
89 		NS_GET32(ttl, p);
90 		NS_GET16(len, p);
91 
92 		if (type != T_SRV) {
93 			p += len;
94 			continue;
95 		}
96 
97 		NS_GET16(priority, p);
98 		NS_GET16(weight, p);
99 		NS_GET16(port, p);
100 
101 		len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
102 		if (len < 0) {
103 			for (i = 0; i < n; i++)
104 				free(res[i]);
105 			free(res);
106 			return (NULL);
107 		}
108 
109 		res[n] = malloc(sizeof(struct dns_srvinfo));
110 		if (res[n] == NULL) {
111 			for (i = 0; i < n; i++)
112 				free(res[i]);
113 			free(res);
114 			return (NULL);
115 		}
116 		res[n]->type = type;
117 		res[n]->class = class;
118 		res[n]->ttl = ttl;
119 		res[n]->priority = priority;
120 		res[n]->weight = weight;
121 		res[n]->port = port;
122 		res[n]->next = NULL;
123 		strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
124 
125 		p += len;
126 		n++;
127 	}
128 
129 	for (i = 0; i < n - 1; i++)
130 		res[i]->next = res[i + 1];
131 
132 	first = res[0];
133 	free(res);
134 
135 	return (first);
136 }
137