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 struct blob_buf ans_buf;
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, int unicast)
71 static struct dns_header h;
72 static struct dns_question q;
73 static struct iovec iov[] = {
79 .iov_base = name_buffer,
88 h.questions = cpu_to_be16(1);
89 q.class = cpu_to_be16(((unicast) ? (CLASS_UNICAST) : (0)) | 1);
90 q.type = cpu_to_be16(type);
92 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
98 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
99 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
100 perror("failed to send question :");
112 static int dns_answer_cnt;
115 dns_init_answer(void)
118 blob_buf_init(&ans_buf, 0);
122 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
124 struct blob_attr *attr;
125 struct dns_answer *a;
127 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
129 a->type = cpu_to_be16(type);
130 a->class = cpu_to_be16(1);
131 a->ttl = cpu_to_be32(ttl);
132 a->rdlength = cpu_to_be16(rdlength);
133 memcpy(a + 1, rdata, rdlength);
139 dns_send_answer(struct interface *iface, const char *answer)
142 struct blob_attr *attr;
143 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 * 2) + 1));
156 iov[n_iov].iov_base = &h;
157 iov[n_iov].iov_len = sizeof(struct dns_header);
160 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
164 blob_for_each_attr(attr, ans_buf.head, rem) {
165 struct dns_answer *a = blob_data(attr);
167 iov[n_iov].iov_base = buffer;
168 iov[n_iov].iov_len = answer_len;
171 iov[n_iov].iov_base = blob_data(attr);
172 iov[n_iov].iov_len = blob_len(attr);
175 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
178 if (interface_send_packet(iface, iov, n_iov) < 0)
179 fprintf(stderr, "failed to send question\n");
183 scan_name(const uint8_t *buffer, int len)
187 while (len && (*buffer != '\0')) {
190 if (IS_COMPRESSED(l))
198 if (!len || !offset || (*buffer != '\0'))
204 static struct dns_header*
205 dns_consume_header(uint8_t **data, int *len)
207 struct dns_header *h = (struct dns_header *) *data;
208 uint16_t *swap = (uint16_t *) h;
211 if (*len < sizeof(struct dns_header))
214 while (endianess--) {
215 *swap = be16_to_cpu(*swap);
219 *len -= sizeof(struct dns_header);
220 *data += sizeof(struct dns_header);
225 static struct dns_question*
226 dns_consume_question(uint8_t **data, int *len)
228 struct dns_question *q = (struct dns_question *) *data;
229 uint16_t *swap = (uint16_t *) q;
232 if (*len < sizeof(struct dns_question))
235 while (endianess--) {
236 *swap = be16_to_cpu(*swap);
240 *len -= sizeof(struct dns_question);
241 *data += sizeof(struct dns_question);
246 static struct dns_answer*
247 dns_consume_answer(uint8_t **data, int *len)
249 struct dns_answer *a = (struct dns_answer *) *data;
251 if (*len < sizeof(struct dns_answer))
254 a->type = be16_to_cpu(a->type);
255 a->class = be16_to_cpu(a->class);
256 a->ttl = be32_to_cpu(a->ttl);
257 a->rdlength = be16_to_cpu(a->rdlength);
259 *len -= sizeof(struct dns_answer);
260 *data += sizeof(struct dns_answer);
266 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
268 int nlen = scan_name(*data, *len);
273 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
274 perror("dns_consume_name/dn_expand");
285 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
287 char *name = dns_consume_name(buffer, len, b, rlen);
288 struct dns_answer *a;
292 fprintf(stderr, "dropping: bad question\n");
296 a = dns_consume_answer(b, rlen);
298 fprintf(stderr, "dropping: bad question\n");
302 if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
306 if (a->rdlength > *rlen) {
307 fprintf(stderr, "dropping: bad question\n");
311 *rlen -= a->rdlength;
315 cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
321 parse_question(struct interface *iface, char *name, struct dns_question *q)
325 if ((q->class & CLASS_UNICAST) && iface->multicast)
328 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
332 if (!strcmp(name, mdns_hostname_local))
333 service_reply(iface, NULL, announce_ttl);
337 service_announce_services(iface, name, announce_ttl);
338 service_reply(iface, name, announce_ttl);
343 host = strstr(name, ".local");
346 if (!strcmp(mdns_hostname, name))
347 service_reply_a(iface, announce_ttl);
353 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
355 struct dns_header *h;
359 h = dns_consume_header(&b, &rlen);
361 fprintf(stderr, "dropping: bad header\n");
365 if (h->questions && !iface->multicast && port != 5353)
366 // silently drop unicast questions that dont originate from port 5353
369 while (h->questions-- > 0) {
370 char *name = dns_consume_name(buffer, len, &b, &rlen);
371 struct dns_question *q;
374 fprintf(stderr, "dropping: bad name\n");
378 q = dns_consume_question(&b, &rlen);
380 fprintf(stderr, "dropping: bad question\n");
384 if (!(h->flags & FLAG_RESPONSE))
385 parse_question(iface, name, q);
388 if (!(h->flags & FLAG_RESPONSE))
391 while (h->answers-- > 0)
392 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
395 while (h->authority-- > 0)
396 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
399 while (h->additional-- > 0)
400 if (parse_answer(iface, buffer, len, &b, &rlen, 1))