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_packet(int fd
, struct iovec
*iov
, int iov_len
)
71 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
72 static struct sockaddr_in a
= {
73 .sin_family
= AF_INET
,
74 .sin_port
= htons(MCAST_PORT
),
76 static struct msghdr m
= {
77 .msg_name
= (struct sockaddr
*) &a
,
78 .msg_namelen
= sizeof(a
),
79 .msg_control
= cmsg_data
,
80 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
82 struct in_pktinfo
*pkti
;
86 m
.msg_iovlen
= iov_len
;
88 memset(cmsg_data
, 0, sizeof(cmsg_data
));
89 cmsg
= CMSG_FIRSTHDR(&m
);
90 cmsg
->cmsg_len
= m
.msg_controllen
;
91 cmsg
->cmsg_level
= IPPROTO_IP
;
92 cmsg
->cmsg_type
= IP_PKTINFO
;
94 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
95 pkti
->ipi_ifindex
= iface_index
;
97 a
.sin_addr
.s_addr
= inet_addr(MCAST_ADDR
);
99 return sendmsg(fd
, &m
, 0);
103 dns_send_question(struct uloop_fd
*u
, const char *question
, int type
)
105 static struct dns_header h
= {
106 .questions
= cpu_to_be16(1),
108 static struct dns_question q
= {
109 .class = cpu_to_be16(1),
111 static struct iovec iov
[] = {
114 .iov_len
= sizeof(h
),
117 .iov_base
= name_buffer
,
121 .iov_len
= sizeof(q
),
126 q
.type
= __cpu_to_be16(type
);
128 len
= dn_comp(question
, (void *) name_buffer
, sizeof(name_buffer
), NULL
, NULL
);
132 iov
[1].iov_len
= len
;
134 if (dns_send_packet(u
->fd
, iov
, ARRAY_SIZE(iov
)) < 0)
135 fprintf(stderr
, "failed to send question\n");
137 DBG(1, "Q <- %s %s\n", dns_type_string(type
), question
);
150 static struct dns_reply dns_reply
[1 + (MAX_ANSWER
* 3)];
151 static int dns_answer_cnt
;
154 dns_init_answer(void)
160 dns_add_answer(int type
, const uint8_t *rdata
, uint16_t rdlength
)
162 struct dns_reply
*a
= &dns_reply
[dns_answer_cnt
];
163 if (dns_answer_cnt
== MAX_ANSWER
)
165 a
->rdata
= memdup(rdata
, rdlength
);
167 a
->rdlength
= rdlength
;
172 dns_send_answer(struct uloop_fd
*u
, const char *answer
)
175 struct dns_header h
= { 0 };
182 h
.answers
= __cpu_to_be16(dns_answer_cnt
);
183 h
.flags
= __cpu_to_be16(0x8400);
185 iov
= alloca(sizeof(struct iovec
) * ((dns_answer_cnt
* 3) + 1));
186 iov
[0].iov_base
= &h
;
187 iov
[0].iov_len
= sizeof(struct dns_header
);
189 for (i
= 0; i
< dns_answer_cnt
; i
++) {
190 struct dns_answer
*a
= &dns_reply
[i
].a
;
191 int id
= (i
* 3) + 1;
193 memset(a
, 0, sizeof(*a
));
194 a
->type
= __cpu_to_be16(dns_reply
[i
].type
);
195 a
->class = __cpu_to_be16(1);
196 a
->ttl
= __cpu_to_be32(announce_ttl
);
197 a
->rdlength
= __cpu_to_be16(dns_reply
[i
].rdlength
);
199 len
= dn_comp(answer
, buffer
, sizeof(buffer
), NULL
, NULL
);
203 dns_reply
[i
].buffer
= iov
[id
].iov_base
= memdup(buffer
, len
);
204 iov
[id
].iov_len
= len
;
206 iov
[id
+ 1].iov_base
= a
;
207 iov
[id
+ 1].iov_len
= sizeof(struct dns_answer
);
209 iov
[id
+ 2].iov_base
= dns_reply
[i
].rdata
;
210 iov
[id
+ 2].iov_len
= dns_reply
[i
].rdlength
;
212 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply
[i
].type
), answer
);
215 if (dns_send_packet(u
->fd
, iov
, (dns_answer_cnt
* 3) + 1) < 0)
216 fprintf(stderr
, "failed to send question\n");
218 for (i
= 0; i
< dns_answer_cnt
; i
++) {
219 free(dns_reply
[i
].buffer
);
220 free(dns_reply
[i
].rdata
);
226 scan_name(const uint8_t *buffer
, int len
)
230 while (len
&& (*buffer
!= '\0')) {
233 if (IS_COMPRESSED(l
))
241 if (!len
|| !offset
|| (*buffer
!= '\0'))
248 dns_consume_header(uint8_t **data
, int *len
)
250 struct dns_header
*h
= (struct dns_header
*) *data
;
251 uint16_t *swap
= (uint16_t *) h
;
254 if (*len
< sizeof(struct dns_header
))
257 while (endianess
--) {
258 *swap
= __be16_to_cpu(*swap
);
262 *len
-= sizeof(struct dns_header
);
263 *data
+= sizeof(struct dns_header
);
269 dns_consume_question(uint8_t **data
, int *len
)
271 struct dns_question
*q
= (struct dns_question
*) *data
;
272 uint16_t *swap
= (uint16_t *) q
;
275 if (*len
< sizeof(struct dns_question
))
278 while (endianess
--) {
279 *swap
= __be16_to_cpu(*swap
);
283 *len
-= sizeof(struct dns_question
);
284 *data
+= sizeof(struct dns_question
);
290 dns_consume_answer(uint8_t **data
, int *len
)
292 struct dns_answer
*a
= (struct dns_answer
*) *data
;
294 if (*len
< sizeof(struct dns_answer
))
297 a
->type
= __be16_to_cpu(a
->type
);
298 a
->class = __be16_to_cpu(a
->class);
299 a
->ttl
= __be32_to_cpu(a
->ttl
);
300 a
->rdlength
= __be16_to_cpu(a
->rdlength
);
302 *len
-= sizeof(struct dns_answer
);
303 *data
+= sizeof(struct dns_answer
);
309 dns_consume_name(const uint8_t *base
, int blen
, uint8_t **data
, int *len
)
311 int nlen
= scan_name(*data
, *len
);
316 if (dn_expand(base
, base
+ blen
, *data
, name_buffer
, MAX_NAME_LEN
) < 0) {
317 perror("dns_consume_name/dn_expand");