495177412fdbd2a38a1043ca2d61c29e4c16b145
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
)
71 dns_send_question(struct interface
*iface
, const char *question
, int type
)
73 static struct dns_header h
= {
74 .questions
= cpu_to_be16(1),
76 static struct dns_question q
= {
77 .class = cpu_to_be16(1),
79 static struct iovec iov
[] = {
85 .iov_base
= name_buffer
,
94 q
.type
= __cpu_to_be16(type
);
96 len
= dn_comp(question
, (void *) name_buffer
, sizeof(name_buffer
), NULL
, NULL
);
100 iov
[1].iov_len
= len
;
102 if (interface_send_packet(iface
, iov
, ARRAY_SIZE(iov
)) < 0)
103 fprintf(stderr
, "failed to send question\n");
105 DBG(1, "Q <- %s %s\n", dns_type_string(type
), question
);
118 static struct dns_reply dns_reply
[1 + (MAX_ANSWER
* 3)];
119 static int dns_answer_cnt
;
122 dns_init_answer(void)
128 dns_add_answer(int type
, const uint8_t *rdata
, uint16_t rdlength
)
130 struct dns_reply
*a
= &dns_reply
[dns_answer_cnt
];
131 if (dns_answer_cnt
== MAX_ANSWER
)
133 a
->rdata
= memdup(rdata
, rdlength
);
135 a
->rdlength
= rdlength
;
140 dns_send_answer(struct interface
*iface
, const char *answer
)
143 struct dns_header h
= { 0 };
150 h
.answers
= __cpu_to_be16(dns_answer_cnt
);
151 h
.flags
= __cpu_to_be16(0x8400);
153 iov
= alloca(sizeof(struct iovec
) * ((dns_answer_cnt
* 3) + 1));
154 iov
[0].iov_base
= &h
;
155 iov
[0].iov_len
= sizeof(struct dns_header
);
157 for (i
= 0; i
< dns_answer_cnt
; i
++) {
158 struct dns_answer
*a
= &dns_reply
[i
].a
;
159 int id
= (i
* 3) + 1;
161 memset(a
, 0, sizeof(*a
));
162 a
->type
= __cpu_to_be16(dns_reply
[i
].type
);
163 a
->class = __cpu_to_be16(1);
164 a
->ttl
= __cpu_to_be32(announce_ttl
);
165 a
->rdlength
= __cpu_to_be16(dns_reply
[i
].rdlength
);
167 len
= dn_comp(answer
, buffer
, sizeof(buffer
), NULL
, NULL
);
171 dns_reply
[i
].buffer
= iov
[id
].iov_base
= memdup(buffer
, len
);
172 iov
[id
].iov_len
= len
;
174 iov
[id
+ 1].iov_base
= a
;
175 iov
[id
+ 1].iov_len
= sizeof(struct dns_answer
);
177 iov
[id
+ 2].iov_base
= dns_reply
[i
].rdata
;
178 iov
[id
+ 2].iov_len
= dns_reply
[i
].rdlength
;
180 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply
[i
].type
), answer
);
183 if (interface_send_packet(iface
, iov
, (dns_answer_cnt
* 3) + 1) < 0)
184 fprintf(stderr
, "failed to send question\n");
186 for (i
= 0; i
< dns_answer_cnt
; i
++) {
187 free(dns_reply
[i
].buffer
);
188 free(dns_reply
[i
].rdata
);
194 scan_name(const uint8_t *buffer
, int len
)
198 while (len
&& (*buffer
!= '\0')) {
201 if (IS_COMPRESSED(l
))
209 if (!len
|| !offset
|| (*buffer
!= '\0'))
215 static struct dns_header
*
216 dns_consume_header(uint8_t **data
, int *len
)
218 struct dns_header
*h
= (struct dns_header
*) *data
;
219 uint16_t *swap
= (uint16_t *) h
;
222 if (*len
< sizeof(struct dns_header
))
225 while (endianess
--) {
226 *swap
= __be16_to_cpu(*swap
);
230 *len
-= sizeof(struct dns_header
);
231 *data
+= sizeof(struct dns_header
);
236 static struct dns_question
*
237 dns_consume_question(uint8_t **data
, int *len
)
239 struct dns_question
*q
= (struct dns_question
*) *data
;
240 uint16_t *swap
= (uint16_t *) q
;
243 if (*len
< sizeof(struct dns_question
))
246 while (endianess
--) {
247 *swap
= __be16_to_cpu(*swap
);
251 *len
-= sizeof(struct dns_question
);
252 *data
+= sizeof(struct dns_question
);
257 static struct dns_answer
*
258 dns_consume_answer(uint8_t **data
, int *len
)
260 struct dns_answer
*a
= (struct dns_answer
*) *data
;
262 if (*len
< sizeof(struct dns_answer
))
265 a
->type
= __be16_to_cpu(a
->type
);
266 a
->class = __be16_to_cpu(a
->class);
267 a
->ttl
= __be32_to_cpu(a
->ttl
);
268 a
->rdlength
= __be16_to_cpu(a
->rdlength
);
270 *len
-= sizeof(struct dns_answer
);
271 *data
+= sizeof(struct dns_answer
);
277 dns_consume_name(const uint8_t *base
, int blen
, uint8_t **data
, int *len
)
279 int nlen
= scan_name(*data
, *len
);
284 if (dn_expand(base
, base
+ blen
, *data
, name_buffer
, MAX_NAME_LEN
) < 0) {
285 perror("dns_consume_name/dn_expand");
296 parse_answer(struct interface
*iface
, uint8_t *buffer
, int len
, uint8_t **b
, int *rlen
, int cache
)
298 char *name
= dns_consume_name(buffer
, len
, b
, rlen
);
299 struct dns_answer
*a
;
303 fprintf(stderr
, "dropping: bad question\n");
307 a
= dns_consume_answer(b
, rlen
);
309 fprintf(stderr
, "dropping: bad question\n");
314 if (a
->rdlength
> *rlen
) {
315 fprintf(stderr
, "dropping: bad question\n");
319 *rlen
-= a
->rdlength
;
323 cache_answer(iface
, buffer
, len
, name
, a
, rdata
);
329 parse_question(struct interface
*iface
, char *name
, struct dns_question
*q
)
333 DBG(1, "Q -> %s %s\n", dns_type_string(q
->type
), name
);
337 host
= service_name("local");
338 if (!strcmp(name
, host
))
339 service_reply(iface
, NULL
);
343 service_announce_services(iface
, name
);
344 service_reply(iface
, name
);
349 host
= strstr(name
, ".local");
352 if (!strcmp(hostname
, name
))
353 service_reply_a(iface
, q
->type
);
359 dns_handle_packet(struct interface
*iface
, uint8_t *buffer
, int len
)
361 struct dns_header
*h
;
365 h
= dns_consume_header(&b
, &rlen
);
367 fprintf(stderr
, "dropping: bad header\n");
371 while (h
->questions
-- > 0) {
372 char *name
= dns_consume_name(buffer
, len
, &b
, &rlen
);
373 struct dns_question
*q
;
376 fprintf(stderr
, "dropping: bad name\n");
380 q
= dns_consume_question(&b
, &rlen
);
382 fprintf(stderr
, "dropping: bad question\n");
386 if (!(h
->flags
& FLAG_RESPONSE
))
387 parse_question(iface
, name
, q
);
390 if (!(h
->flags
& FLAG_RESPONSE
))
393 while (h
->answers
-- > 0)
394 parse_answer(iface
, buffer
, len
, &b
, &rlen
, 1);
396 while (h
->authority
-- > 0)
397 parse_answer(iface
, buffer
, len
, &b
, &rlen
, 0);
399 while (h
->additional
-- > 0)
400 parse_answer(iface
, buffer
, len
, &b
, &rlen
, 1);