5595384a544b0f86ba326629defd27a47c2a3b27
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>
39 char rdata_buffer
[MAX_DATA_LEN
+ 1];
40 static char name_buffer
[MAX_NAME_LEN
+ 1];
43 dns_type_string(uint16_t type
)
69 dns_send_question(struct uloop_fd
*u
, char *question
, int type
)
71 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
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
,
91 static struct sockaddr_in a
= {
92 .sin_family
= AF_INET
,
93 .sin_port
= htons(MCAST_PORT
),
95 static struct msghdr m
= {
96 .msg_name
= (struct sockaddr
*) &a
,
97 .msg_namelen
= sizeof(a
),
99 .msg_iovlen
= ARRAY_SIZE(iov
),
100 .msg_control
= cmsg_data
,
101 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
103 struct in_pktinfo
*pkti
;
104 struct cmsghdr
*cmsg
;
107 a
.sin_addr
.s_addr
= inet_addr(MCAST_ADDR
);
108 q
.type
= __cpu_to_be16(type
);
110 len
= dn_comp(question
, (void *) name_buffer
, sizeof(name_buffer
), NULL
, NULL
);
114 iov
[1].iov_len
= len
;
116 memset(cmsg_data
, 0, sizeof(cmsg_data
));
118 cmsg
= CMSG_FIRSTHDR(&m
);
119 cmsg
->cmsg_len
= m
.msg_controllen
;
120 cmsg
->cmsg_level
= IPPROTO_IP
;
121 cmsg
->cmsg_type
= IP_PKTINFO
;
123 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
124 pkti
->ipi_ifindex
= iface_index
;
126 if (sendmsg(u
->fd
, &m
, 0) < 0)
127 fprintf(stderr
, "failed to send question\n");
129 DBG(1, "Q <- %s %s\n", dns_type_string(type
), question
);
142 static struct dns_reply dns_reply
[1 + (MAX_ANSWER
* 3)];
143 static int dns_answer_cnt
;
146 dns_init_answer(void)
152 dns_add_answer(int type
, uint8_t *rdata
, uint16_t rdlength
)
154 struct dns_reply
*a
= &dns_reply
[dns_answer_cnt
];
155 if (dns_answer_cnt
== MAX_ANSWER
)
157 a
->rdata
= memdup(rdata
, rdlength
);
159 a
->rdlength
= rdlength
;
164 dns_send_answer(struct uloop_fd
*u
, char *answer
)
167 struct dns_header h
= { 0 };
168 struct msghdr m
= { 0 };
170 struct sockaddr_in in
= { 0 };
176 in
.sin_family
= AF_INET
;
177 in
.sin_addr
.s_addr
= inet_addr(MCAST_ADDR
);
178 in
.sin_port
= htons(MCAST_PORT
);
180 h
.answers
= __cpu_to_be16(dns_answer_cnt
);
181 h
.flags
= __cpu_to_be16(0x8400);
183 iov
= malloc(sizeof(struct iovec
) * ((dns_answer_cnt
* 3) + 1));
188 m
.msg_namelen
= sizeof(struct sockaddr_in
);
190 m
.msg_iovlen
= (dns_answer_cnt
* 3) + 1;
192 iov
[0].iov_base
= &h
;
193 iov
[0].iov_len
= sizeof(struct dns_header
);
195 for (i
= 0; i
< dns_answer_cnt
; i
++) {
196 struct dns_answer
*a
= &dns_reply
[i
].a
;
197 int id
= (i
* 3) + 1;
199 memset(a
, 0, sizeof(*a
));
200 a
->type
= __cpu_to_be16(dns_reply
[i
].type
);
201 a
->class = __cpu_to_be16(1);
202 a
->ttl
= __cpu_to_be32(announce_ttl
);
203 a
->rdlength
= __cpu_to_be16(dns_reply
[i
].rdlength
);
205 len
= dn_comp(answer
, buffer
, sizeof(buffer
), NULL
, NULL
);
209 dns_reply
[i
].buffer
= iov
[id
].iov_base
= memdup(buffer
, len
);
210 iov
[id
].iov_len
= len
;
212 iov
[id
+ 1].iov_base
= a
;
213 iov
[id
+ 1].iov_len
= sizeof(struct dns_answer
);
215 iov
[id
+ 2].iov_base
= dns_reply
[i
].rdata
;
216 iov
[id
+ 2].iov_len
= dns_reply
[i
].rdlength
;
218 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply
[i
].type
), answer
);
221 if (sendmsg(u
->fd
, &m
, 0) < 0)
222 fprintf(stderr
, "failed to send question\n");
224 for (i
= 0; i
< dns_answer_cnt
; i
++) {
225 free(dns_reply
[i
].buffer
);
226 free(dns_reply
[i
].rdata
);
232 scan_name(uint8_t *buffer
, int len
)
236 while (len
&& (*buffer
!= '\0')) {
239 if (IS_COMPRESSED(l
))
247 if (!len
|| !offset
|| (*buffer
!= '\0'))
254 dns_consume_header(uint8_t **data
, int *len
)
256 struct dns_header
*h
= (struct dns_header
*) *data
;
257 uint16_t *swap
= (uint16_t *) h
;
260 if (*len
< sizeof(struct dns_header
))
263 while (endianess
--) {
264 *swap
= __be16_to_cpu(*swap
);
268 *len
-= sizeof(struct dns_header
);
269 *data
+= sizeof(struct dns_header
);
275 dns_consume_question(uint8_t **data
, int *len
)
277 struct dns_question
*q
= (struct dns_question
*) *data
;
278 uint16_t *swap
= (uint16_t *) q
;
281 if (*len
< sizeof(struct dns_question
))
284 while (endianess
--) {
285 *swap
= __be16_to_cpu(*swap
);
289 *len
-= sizeof(struct dns_question
);
290 *data
+= sizeof(struct dns_question
);
296 dns_consume_answer(uint8_t **data
, int *len
)
298 struct dns_answer
*a
= (struct dns_answer
*) *data
;
300 if (*len
< sizeof(struct dns_answer
))
303 a
->type
= __be16_to_cpu(a
->type
);
304 a
->class = __be16_to_cpu(a
->class);
305 a
->ttl
= __be32_to_cpu(a
->ttl
);
306 a
->rdlength
= __be16_to_cpu(a
->rdlength
);
308 *len
-= sizeof(struct dns_answer
);
309 *data
+= sizeof(struct dns_answer
);
315 dns_consume_name(uint8_t *base
, int blen
, uint8_t **data
, int *len
)
317 int nlen
= scan_name(*data
, *len
);
322 if (dn_expand(base
, base
+ blen
, *data
, name_buffer
, MAX_NAME_LEN
) < 0) {
323 perror("dns_consume_name/dn_expand");