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>
40 #include "interface.h"
42 char rdata_buffer[MAX_DATA_LEN + 1];
43 static char name_buffer[MAX_NAME_LEN + 1];
46 dns_type_string(uint16_t type)
72 dns_send_question(struct interface *iface, const char *question, int type)
74 static struct dns_header h = {
75 .questions = cpu_to_be16(1),
77 static struct dns_question q = {
78 .class = cpu_to_be16(1),
80 static struct iovec iov[] = {
86 .iov_base = name_buffer,
95 q.type = __cpu_to_be16(type);
97 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
101 iov[1].iov_len = len;
103 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
104 fprintf(stderr, "failed to send question\n");
106 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
119 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
120 static int dns_answer_cnt;
123 dns_init_answer(void)
129 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
131 struct dns_reply *a = &dns_reply[dns_answer_cnt];
132 if (dns_answer_cnt == MAX_ANSWER)
134 a->rdata = memdup(rdata, rdlength);
136 a->rdlength = rdlength;
141 dns_send_answer(struct interface *iface, const char *answer)
144 struct dns_header h = { 0 };
151 h.answers = __cpu_to_be16(dns_answer_cnt);
152 h.flags = __cpu_to_be16(0x8400);
154 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
155 iov[0].iov_base = &h;
156 iov[0].iov_len = sizeof(struct dns_header);
158 for (i = 0; i < dns_answer_cnt; i++) {
159 struct dns_answer *a = &dns_reply[i].a;
160 int id = (i * 3) + 1;
162 memset(a, 0, sizeof(*a));
163 a->type = __cpu_to_be16(dns_reply[i].type);
164 a->class = __cpu_to_be16(1);
165 a->ttl = __cpu_to_be32(announce_ttl);
166 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
168 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
172 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
173 iov[id].iov_len = len;
175 iov[id + 1].iov_base = a;
176 iov[id + 1].iov_len = sizeof(struct dns_answer);
178 iov[id + 2].iov_base = dns_reply[i].rdata;
179 iov[id + 2].iov_len = dns_reply[i].rdlength;
181 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
184 if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
185 fprintf(stderr, "failed to send question\n");
187 for (i = 0; i < dns_answer_cnt; i++) {
188 free(dns_reply[i].buffer);
189 free(dns_reply[i].rdata);
195 scan_name(const uint8_t *buffer, int len)
199 while (len && (*buffer != '\0')) {
202 if (IS_COMPRESSED(l))
210 if (!len || !offset || (*buffer != '\0'))
216 static struct dns_header*
217 dns_consume_header(uint8_t **data, int *len)
219 struct dns_header *h = (struct dns_header *) *data;
220 uint16_t *swap = (uint16_t *) h;
223 if (*len < sizeof(struct dns_header))
226 while (endianess--) {
227 *swap = __be16_to_cpu(*swap);
231 *len -= sizeof(struct dns_header);
232 *data += sizeof(struct dns_header);
237 static struct dns_question*
238 dns_consume_question(uint8_t **data, int *len)
240 struct dns_question *q = (struct dns_question *) *data;
241 uint16_t *swap = (uint16_t *) q;
244 if (*len < sizeof(struct dns_question))
247 while (endianess--) {
248 *swap = __be16_to_cpu(*swap);
252 *len -= sizeof(struct dns_question);
253 *data += sizeof(struct dns_question);
258 static struct dns_answer*
259 dns_consume_answer(uint8_t **data, int *len)
261 struct dns_answer *a = (struct dns_answer *) *data;
263 if (*len < sizeof(struct dns_answer))
266 a->type = __be16_to_cpu(a->type);
267 a->class = __be16_to_cpu(a->class);
268 a->ttl = __be32_to_cpu(a->ttl);
269 a->rdlength = __be16_to_cpu(a->rdlength);
271 *len -= sizeof(struct dns_answer);
272 *data += sizeof(struct dns_answer);
278 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
280 int nlen = scan_name(*data, *len);
285 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
286 perror("dns_consume_name/dn_expand");
297 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
299 char *name = dns_consume_name(buffer, len, b, rlen);
300 struct dns_answer *a;
304 fprintf(stderr, "dropping: bad question\n");
308 a = dns_consume_answer(b, rlen);
310 fprintf(stderr, "dropping: bad question\n");
315 if (a->rdlength > *rlen) {
316 fprintf(stderr, "dropping: bad question\n");
320 *rlen -= a->rdlength;
324 cache_answer(iface, buffer, len, name, a, rdata);
330 parse_question(struct interface *iface, char *name, struct dns_question *q)
334 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
338 host = service_name("local");
339 if (!strcmp(name, host))
340 service_reply(iface, NULL);
344 service_announce_services(iface, name);
345 service_reply(iface, name);
350 host = strstr(name, ".local");
353 if (!strcmp(hostname, name))
354 service_reply_a(iface, q->type);
360 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
362 struct dns_header *h;
366 h = dns_consume_header(&b, &rlen);
368 fprintf(stderr, "dropping: bad header\n");
372 while (h->questions-- > 0) {
373 char *name = dns_consume_name(buffer, len, &b, &rlen);
374 struct dns_question *q;
377 fprintf(stderr, "dropping: bad name\n");
381 q = dns_consume_question(&b, &rlen);
383 fprintf(stderr, "dropping: bad question\n");
387 if (!(h->flags & FLAG_RESPONSE))
388 parse_question(iface, name, q);
391 if (!(h->flags & FLAG_RESPONSE))
394 while (h->answers-- > 0)
395 parse_answer(iface, buffer, len, &b, &rlen, 1);
397 while (h->authority-- > 0)
398 parse_answer(iface, buffer, len, &b, &rlen, 0);
400 while (h->additional-- > 0)
401 parse_answer(iface, buffer, len, &b, &rlen, 1);