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];
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)
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);
116 static int dns_answer_cnt;
119 dns_init_answer(void)
122 blob_buf_init(&ans_buf, 0);
126 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
128 struct blob_attr *attr;
129 struct dns_answer *a;
131 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
133 a->type = cpu_to_be16(type);
134 a->class = cpu_to_be16(1);
135 a->ttl = cpu_to_be32(announce_ttl);
136 a->rdlength = cpu_to_be16(rdlength);
137 memcpy(a + 1, rdata, rdlength);
143 dns_send_answer(struct interface *iface, const char *answer)
146 struct blob_attr *attr;
147 struct dns_header h = { 0 };
155 h.answers = __cpu_to_be16(dns_answer_cnt);
156 h.flags = __cpu_to_be16(0x8400);
158 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
160 iov[n_iov].iov_base = &h;
161 iov[n_iov].iov_len = sizeof(struct dns_header);
164 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
168 blob_for_each_attr(attr, ans_buf.head, rem) {
169 struct dns_answer *a = blob_data(attr);
171 iov[n_iov].iov_base = buffer;
172 iov[n_iov].iov_len = answer_len;
175 iov[n_iov].iov_base = blob_data(attr);
176 iov[n_iov].iov_len = blob_len(attr);
179 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
182 if (interface_send_packet(iface, iov, n_iov) < 0)
183 fprintf(stderr, "failed to send question\n");
187 scan_name(const uint8_t *buffer, int len)
191 while (len && (*buffer != '\0')) {
194 if (IS_COMPRESSED(l))
202 if (!len || !offset || (*buffer != '\0'))
208 static struct dns_header*
209 dns_consume_header(uint8_t **data, int *len)
211 struct dns_header *h = (struct dns_header *) *data;
212 uint16_t *swap = (uint16_t *) h;
215 if (*len < sizeof(struct dns_header))
218 while (endianess--) {
219 *swap = __be16_to_cpu(*swap);
223 *len -= sizeof(struct dns_header);
224 *data += sizeof(struct dns_header);
229 static struct dns_question*
230 dns_consume_question(uint8_t **data, int *len)
232 struct dns_question *q = (struct dns_question *) *data;
233 uint16_t *swap = (uint16_t *) q;
236 if (*len < sizeof(struct dns_question))
239 while (endianess--) {
240 *swap = __be16_to_cpu(*swap);
244 *len -= sizeof(struct dns_question);
245 *data += sizeof(struct dns_question);
250 static struct dns_answer*
251 dns_consume_answer(uint8_t **data, int *len)
253 struct dns_answer *a = (struct dns_answer *) *data;
255 if (*len < sizeof(struct dns_answer))
258 a->type = __be16_to_cpu(a->type);
259 a->class = __be16_to_cpu(a->class);
260 a->ttl = __be32_to_cpu(a->ttl);
261 a->rdlength = __be16_to_cpu(a->rdlength);
263 *len -= sizeof(struct dns_answer);
264 *data += sizeof(struct dns_answer);
270 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
272 int nlen = scan_name(*data, *len);
277 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
278 perror("dns_consume_name/dn_expand");
289 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
291 char *name = dns_consume_name(buffer, len, b, rlen);
292 struct dns_answer *a;
296 fprintf(stderr, "dropping: bad question\n");
300 a = dns_consume_answer(b, rlen);
302 fprintf(stderr, "dropping: bad question\n");
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);
322 parse_question(struct interface *iface, char *name, struct dns_question *q)
326 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
330 if (!strcmp(name, mdns_hostname_local))
331 service_reply(iface, NULL);
335 service_announce_services(iface, name);
336 service_reply(iface, name);
341 host = strstr(name, ".local");
344 if (!strcmp(mdns_hostname, name))
345 service_reply_a(iface, q->type);
351 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
353 struct dns_header *h;
357 h = dns_consume_header(&b, &rlen);
359 fprintf(stderr, "dropping: bad header\n");
363 while (h->questions-- > 0) {
364 char *name = dns_consume_name(buffer, len, &b, &rlen);
365 struct dns_question *q;
368 fprintf(stderr, "dropping: bad name\n");
372 q = dns_consume_question(&b, &rlen);
374 fprintf(stderr, "dropping: bad question\n");
378 if (!(h->flags & FLAG_RESPONSE))
379 parse_question(iface, name, q);
382 if (!(h->flags & FLAG_RESPONSE))
385 while (h->answers-- > 0)
386 parse_answer(iface, buffer, len, &b, &rlen, 1);
388 while (h->authority-- > 0)
389 parse_answer(iface, buffer, len, &b, &rlen, 0);
391 while (h->additional-- > 0)
392 parse_answer(iface, buffer, len, &b, &rlen, 1);