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 static char name_buffer[MAX_NAME_LEN + 1];
45 dns_type_string(uint16_t type)
52 { TYPE_AAAA, "AAAA" },
60 for (i = 0; i < ARRAY_SIZE(type_str); i++) {
61 if (type == type_str[i].type)
62 return type_str[i].str;
69 dns_send_question(struct interface *iface, const char *question, int type)
71 static struct dns_header h = {
72 .questions = cpu_to_be16(1),
74 static struct dns_question q = {
75 .class = cpu_to_be16(1),
77 static struct iovec iov[] = {
83 .iov_base = name_buffer,
92 q.type = __cpu_to_be16(type);
94 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
100 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
101 fprintf(stderr, "failed to send question\n");
103 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
116 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
117 static int dns_answer_cnt;
120 dns_init_answer(void)
126 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
128 struct dns_reply *a = &dns_reply[dns_answer_cnt];
129 if (dns_answer_cnt == MAX_ANSWER)
131 a->rdata = memdup(rdata, rdlength);
133 a->rdlength = rdlength;
138 dns_send_answer(struct interface *iface, const char *answer)
141 struct dns_header h = { 0 };
148 h.answers = __cpu_to_be16(dns_answer_cnt);
149 h.flags = __cpu_to_be16(0x8400);
151 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
152 iov[0].iov_base = &h;
153 iov[0].iov_len = sizeof(struct dns_header);
155 for (i = 0; i < dns_answer_cnt; i++) {
156 struct dns_answer *a = &dns_reply[i].a;
157 int id = (i * 3) + 1;
159 memset(a, 0, sizeof(*a));
160 a->type = __cpu_to_be16(dns_reply[i].type);
161 a->class = __cpu_to_be16(1);
162 a->ttl = __cpu_to_be32(announce_ttl);
163 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
165 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
169 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
170 iov[id].iov_len = len;
172 iov[id + 1].iov_base = a;
173 iov[id + 1].iov_len = sizeof(struct dns_answer);
175 iov[id + 2].iov_base = dns_reply[i].rdata;
176 iov[id + 2].iov_len = dns_reply[i].rdlength;
178 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
181 if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
182 fprintf(stderr, "failed to send question\n");
184 for (i = 0; i < dns_answer_cnt; i++) {
185 free(dns_reply[i].buffer);
186 free(dns_reply[i].rdata);
192 scan_name(const uint8_t *buffer, int len)
196 while (len && (*buffer != '\0')) {
199 if (IS_COMPRESSED(l))
207 if (!len || !offset || (*buffer != '\0'))
213 static struct dns_header*
214 dns_consume_header(uint8_t **data, int *len)
216 struct dns_header *h = (struct dns_header *) *data;
217 uint16_t *swap = (uint16_t *) h;
220 if (*len < sizeof(struct dns_header))
223 while (endianess--) {
224 *swap = __be16_to_cpu(*swap);
228 *len -= sizeof(struct dns_header);
229 *data += sizeof(struct dns_header);
234 static struct dns_question*
235 dns_consume_question(uint8_t **data, int *len)
237 struct dns_question *q = (struct dns_question *) *data;
238 uint16_t *swap = (uint16_t *) q;
241 if (*len < sizeof(struct dns_question))
244 while (endianess--) {
245 *swap = __be16_to_cpu(*swap);
249 *len -= sizeof(struct dns_question);
250 *data += sizeof(struct dns_question);
255 static struct dns_answer*
256 dns_consume_answer(uint8_t **data, int *len)
258 struct dns_answer *a = (struct dns_answer *) *data;
260 if (*len < sizeof(struct dns_answer))
263 a->type = __be16_to_cpu(a->type);
264 a->class = __be16_to_cpu(a->class);
265 a->ttl = __be32_to_cpu(a->ttl);
266 a->rdlength = __be16_to_cpu(a->rdlength);
268 *len -= sizeof(struct dns_answer);
269 *data += sizeof(struct dns_answer);
275 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
277 int nlen = scan_name(*data, *len);
282 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
283 perror("dns_consume_name/dn_expand");
294 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
296 char *name = dns_consume_name(buffer, len, b, rlen);
297 struct dns_answer *a;
301 fprintf(stderr, "dropping: bad question\n");
305 a = dns_consume_answer(b, rlen);
307 fprintf(stderr, "dropping: bad question\n");
312 if (a->rdlength > *rlen) {
313 fprintf(stderr, "dropping: bad question\n");
317 *rlen -= a->rdlength;
321 cache_answer(iface, buffer, len, name, a, rdata);
327 parse_question(struct interface *iface, char *name, struct dns_question *q)
331 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
335 if (!strcmp(name, mdns_hostname_local))
336 service_reply(iface, NULL);
340 service_announce_services(iface, name);
341 service_reply(iface, name);
346 host = strstr(name, ".local");
349 if (!strcmp(mdns_hostname, name))
350 service_reply_a(iface, q->type);
356 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
358 struct dns_header *h;
362 h = dns_consume_header(&b, &rlen);
364 fprintf(stderr, "dropping: bad header\n");
368 while (h->questions-- > 0) {
369 char *name = dns_consume_name(buffer, len, &b, &rlen);
370 struct dns_question *q;
373 fprintf(stderr, "dropping: bad name\n");
377 q = dns_consume_question(&b, &rlen);
379 fprintf(stderr, "dropping: bad question\n");
383 if (!(h->flags & FLAG_RESPONSE))
384 parse_question(iface, name, q);
387 if (!(h->flags & FLAG_RESPONSE))
390 while (h->answers-- > 0)
391 parse_answer(iface, buffer, len, &b, &rlen, 1);
393 while (h->authority-- > 0)
394 parse_answer(iface, buffer, len, &b, &rlen, 0);
396 while (h->additional-- > 0)
397 parse_answer(iface, buffer, len, &b, &rlen, 1);