2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/types.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <asm/byteorder.h>
26 #include <arpa/nameser.h>
31 #include <libubox/uloop.h>
32 #include <libubox/usock.h>
33 #include <libubox/utils.h>
38 #include "interface.h"
40 char rdata_buffer[MAX_DATA_LEN + 1];
41 static char name_buffer[MAX_NAME_LEN + 1];
44 dns_type_string(uint16_t type)
70 dns_send_question(struct interface *iface, const char *question, int type)
72 static struct dns_header h = {
73 .questions = cpu_to_be16(1),
75 static struct dns_question q = {
76 .class = cpu_to_be16(1),
78 static struct iovec iov[] = {
84 .iov_base = name_buffer,
93 q.type = __cpu_to_be16(type);
95 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
101 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
102 fprintf(stderr, "failed to send question\n");
104 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
117 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
118 static int dns_answer_cnt;
121 dns_init_answer(void)
127 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
129 struct dns_reply *a = &dns_reply[dns_answer_cnt];
130 if (dns_answer_cnt == MAX_ANSWER)
132 a->rdata = memdup(rdata, rdlength);
134 a->rdlength = rdlength;
139 dns_send_answer(struct uloop_fd *u, const char *answer)
142 struct dns_header h = { 0 };
149 h.answers = __cpu_to_be16(dns_answer_cnt);
150 h.flags = __cpu_to_be16(0x8400);
152 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
153 iov[0].iov_base = &h;
154 iov[0].iov_len = sizeof(struct dns_header);
156 for (i = 0; i < dns_answer_cnt; i++) {
157 struct dns_answer *a = &dns_reply[i].a;
158 int id = (i * 3) + 1;
160 memset(a, 0, sizeof(*a));
161 a->type = __cpu_to_be16(dns_reply[i].type);
162 a->class = __cpu_to_be16(1);
163 a->ttl = __cpu_to_be32(announce_ttl);
164 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
166 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
170 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
171 iov[id].iov_len = len;
173 iov[id + 1].iov_base = a;
174 iov[id + 1].iov_len = sizeof(struct dns_answer);
176 iov[id + 2].iov_base = dns_reply[i].rdata;
177 iov[id + 2].iov_len = dns_reply[i].rdlength;
179 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
182 if (interface_send_packet(cur_iface, iov, (dns_answer_cnt * 3) + 1) < 0)
183 fprintf(stderr, "failed to send question\n");
185 for (i = 0; i < dns_answer_cnt; i++) {
186 free(dns_reply[i].buffer);
187 free(dns_reply[i].rdata);
193 scan_name(const uint8_t *buffer, int len)
197 while (len && (*buffer != '\0')) {
200 if (IS_COMPRESSED(l))
208 if (!len || !offset || (*buffer != '\0'))
215 dns_consume_header(uint8_t **data, int *len)
217 struct dns_header *h = (struct dns_header *) *data;
218 uint16_t *swap = (uint16_t *) h;
221 if (*len < sizeof(struct dns_header))
224 while (endianess--) {
225 *swap = __be16_to_cpu(*swap);
229 *len -= sizeof(struct dns_header);
230 *data += sizeof(struct dns_header);
236 dns_consume_question(uint8_t **data, int *len)
238 struct dns_question *q = (struct dns_question *) *data;
239 uint16_t *swap = (uint16_t *) q;
242 if (*len < sizeof(struct dns_question))
245 while (endianess--) {
246 *swap = __be16_to_cpu(*swap);
250 *len -= sizeof(struct dns_question);
251 *data += sizeof(struct dns_question);
257 dns_consume_answer(uint8_t **data, int *len)
259 struct dns_answer *a = (struct dns_answer *) *data;
261 if (*len < sizeof(struct dns_answer))
264 a->type = __be16_to_cpu(a->type);
265 a->class = __be16_to_cpu(a->class);
266 a->ttl = __be32_to_cpu(a->ttl);
267 a->rdlength = __be16_to_cpu(a->rdlength);
269 *len -= sizeof(struct dns_answer);
270 *data += sizeof(struct dns_answer);
276 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
278 int nlen = scan_name(*data, *len);
283 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
284 perror("dns_consume_name/dn_expand");