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 <arpa/nameser.h>
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/utils.h>
39 #include "interface.h"
41 static char name_buffer[MAX_NAME_LEN + 1];
42 static char dns_buffer[MAX_NAME_LEN];
43 static struct blob_buf ans_buf;
46 dns_type_string(uint16_t type)
53 { TYPE_AAAA, "AAAA" },
61 for (i = 0; i < ARRAY_SIZE(type_str); i++) {
62 if (type == type_str[i].type)
63 return type_str[i].str;
70 dns_send_question(struct interface *iface, const char *question, int type, int multicast)
72 static struct dns_header h;
73 static struct dns_question q;
74 static struct iovec iov[] = {
80 .iov_base = dns_buffer,
89 h.questions = cpu_to_be16(1);
90 q.class = cpu_to_be16((multicast ? 0 : CLASS_UNICAST) | 1);
91 q.type = cpu_to_be16(type);
93 len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
99 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
100 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
101 perror("failed to send question :");
113 static int dns_answer_cnt;
116 dns_init_answer(void)
119 blob_buf_init(&ans_buf, 0);
123 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
125 struct blob_attr *attr;
126 struct dns_answer *a;
128 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
130 a->type = cpu_to_be16(type);
131 a->class = cpu_to_be16(1);
132 a->ttl = cpu_to_be32(ttl);
133 a->rdlength = cpu_to_be16(rdlength);
134 memcpy(a + 1, rdata, rdlength);
140 dns_send_answer(struct interface *iface, const char *answer)
143 struct blob_attr *attr;
144 struct dns_header h = { 0 };
152 h.answers = cpu_to_be16(dns_answer_cnt);
153 h.flags = cpu_to_be16(0x8400);
155 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
157 iov[n_iov].iov_base = &h;
158 iov[n_iov].iov_len = sizeof(struct dns_header);
161 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
165 blob_for_each_attr(attr, ans_buf.head, rem) {
166 struct dns_answer *a = blob_data(attr);
168 iov[n_iov].iov_base = buffer;
169 iov[n_iov].iov_len = answer_len;
172 iov[n_iov].iov_base = blob_data(attr);
173 iov[n_iov].iov_len = blob_len(attr);
176 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
179 if (interface_send_packet(iface, iov, n_iov) < 0)
180 fprintf(stderr, "failed to send question\n");
184 scan_name(const uint8_t *buffer, int len)
188 while (len && (*buffer != '\0')) {
191 if (IS_COMPRESSED(l))
199 if (!len || !offset || (*buffer != '\0'))
205 static struct dns_header*
206 dns_consume_header(uint8_t **data, int *len)
208 struct dns_header *h = (struct dns_header *) *data;
209 uint16_t *swap = (uint16_t *) h;
212 if (*len < sizeof(struct dns_header))
215 while (endianess--) {
216 *swap = be16_to_cpu(*swap);
220 *len -= sizeof(struct dns_header);
221 *data += sizeof(struct dns_header);
226 static struct dns_question*
227 dns_consume_question(uint8_t **data, int *len)
229 struct dns_question *q = (struct dns_question *) *data;
230 uint16_t *swap = (uint16_t *) q;
233 if (*len < sizeof(struct dns_question))
236 while (endianess--) {
237 *swap = be16_to_cpu(*swap);
241 *len -= sizeof(struct dns_question);
242 *data += sizeof(struct dns_question);
247 static struct dns_answer*
248 dns_consume_answer(uint8_t **data, int *len)
250 struct dns_answer *a = (struct dns_answer *) *data;
252 if (*len < sizeof(struct dns_answer))
255 a->type = be16_to_cpu(a->type);
256 a->class = be16_to_cpu(a->class);
257 a->ttl = be32_to_cpu(a->ttl);
258 a->rdlength = be16_to_cpu(a->rdlength);
260 *len -= sizeof(struct dns_answer);
261 *data += sizeof(struct dns_answer);
267 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
269 int nlen = scan_name(*data, *len);
274 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
275 perror("dns_consume_name/dn_expand");
286 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
288 char *name = dns_consume_name(buffer, len, b, rlen);
289 struct dns_answer *a;
293 fprintf(stderr, "dropping: bad question\n");
297 a = dns_consume_answer(b, rlen);
299 fprintf(stderr, "dropping: bad question\n");
303 if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
307 if (a->rdlength > *rlen) {
308 fprintf(stderr, "dropping: bad question\n");
312 *rlen -= a->rdlength;
316 cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
322 parse_question(struct interface *iface, char *name, struct dns_question *q)
326 if ((q->class & CLASS_UNICAST) && iface->multicast)
329 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
333 if (!strcmp(name, mdns_hostname_local))
334 service_reply(iface, NULL, announce_ttl);
338 service_announce_services(iface, name, announce_ttl);
339 service_reply(iface, name, announce_ttl);
344 host = strstr(name, ".local");
347 if (!strcmp(mdns_hostname, name))
348 service_reply_a(iface, announce_ttl);
354 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
356 struct dns_header *h;
360 h = dns_consume_header(&b, &rlen);
362 fprintf(stderr, "dropping: bad header\n");
366 if (h->questions && !iface->multicast && port != 5353)
367 // silently drop unicast questions that dont originate from port 5353
370 while (h->questions-- > 0) {
371 char *name = dns_consume_name(buffer, len, &b, &rlen);
372 struct dns_question *q;
375 fprintf(stderr, "dropping: bad name\n");
379 q = dns_consume_question(&b, &rlen);
381 fprintf(stderr, "dropping: bad question\n");
385 if (!(h->flags & FLAG_RESPONSE))
386 parse_question(iface, name, q);
389 if (!(h->flags & FLAG_RESPONSE))
392 while (h->answers-- > 0)
393 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
396 while (h->authority-- > 0)
397 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
400 while (h->additional-- > 0)
401 if (parse_answer(iface, buffer, len, &b, &rlen, 1))