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
)
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(1);
90 q
.type
= cpu_to_be16(type
);
92 len
= dn_comp(question
, (void *) name_buffer
, sizeof(name_buffer
), NULL
, NULL
);
98 if (interface_send_packet(iface
, iov
, ARRAY_SIZE(iov
)) < 0)
99 fprintf(stderr
, "failed to send question\n");
101 DBG(1, "Q <- %s %s\n", dns_type_string(type
), 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
)
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(announce_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");
304 if (a
->rdlength
> *rlen
) {
305 fprintf(stderr
, "dropping: bad question\n");
309 *rlen
-= a
->rdlength
;
313 cache_answer(iface
, buffer
, len
, name
, a
, rdata
);
319 parse_question(struct interface
*iface
, char *name
, struct dns_question
*q
)
323 DBG(1, "Q -> %s %s\n", dns_type_string(q
->type
), name
);
327 if (!strcmp(name
, mdns_hostname_local
))
328 service_reply(iface
, NULL
);
332 service_announce_services(iface
, name
);
333 service_reply(iface
, name
);
338 host
= strstr(name
, ".local");
341 if (!strcmp(mdns_hostname
, name
))
342 service_reply_a(iface
, q
->type
);
348 dns_handle_packet(struct interface
*iface
, uint8_t *buffer
, int len
)
350 struct dns_header
*h
;
354 h
= dns_consume_header(&b
, &rlen
);
356 fprintf(stderr
, "dropping: bad header\n");
360 while (h
->questions
-- > 0) {
361 char *name
= dns_consume_name(buffer
, len
, &b
, &rlen
);
362 struct dns_question
*q
;
365 fprintf(stderr
, "dropping: bad name\n");
369 q
= dns_consume_question(&b
, &rlen
);
371 fprintf(stderr
, "dropping: bad question\n");
375 if (!(h
->flags
& FLAG_RESPONSE
))
376 parse_question(iface
, name
, q
);
379 if (!(h
->flags
& FLAG_RESPONSE
))
382 while (h
->answers
-- > 0)
383 if (parse_answer(iface
, buffer
, len
, &b
, &rlen
, 1))
386 while (h
->authority
-- > 0)
387 if (parse_answer(iface
, buffer
, len
, &b
, &rlen
, 1))
390 while (h
->additional
-- > 0)
391 if (parse_answer(iface
, buffer
, len
, &b
, &rlen
, 1))