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>
39 static char *name_buffer;
42 dns_type_string(uint16_t type)
68 dns_send_question(struct uloop_fd *u, char *question, int type)
70 size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
71 unsigned char buffer[MAX_NAME_LEN];
72 struct dns_header h = { 0 };
73 struct dns_question q = { 0 };
74 struct msghdr m = { 0 };
75 struct iovec iov[3] = { {0}, {0}, {0} };
76 struct sockaddr_in a = { 0 };
77 struct in_pktinfo *pkti;
82 a.sin_family = AF_INET;
83 a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
84 a.sin_port = htons(MCAST_PORT);
86 h.questions = __cpu_to_be16(1);
87 q.type = __cpu_to_be16(type);
88 q.class = __cpu_to_be16(1);
90 len = dn_comp(question, buffer, MAX_NAME_LEN, NULL, NULL);
95 m.msg_namelen = sizeof(struct sockaddr_in);
99 memset(cmsg_data, 0, sizeof(cmsg_data));
100 m.msg_control = cmsg_data;
101 m.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
103 cmsg = CMSG_FIRSTHDR(&m);
104 cmsg->cmsg_len = m.msg_controllen;
105 cmsg->cmsg_level = IPPROTO_IP;
106 cmsg->cmsg_type = IP_PKTINFO;
108 inet_aton(iface_ip, &in);
110 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
111 pkti->ipi_ifindex = iface_index;
113 iov[0].iov_base = &h;
114 iov[0].iov_len = sizeof(struct dns_header);
116 iov[1].iov_base = buffer;
117 iov[1].iov_len = len;
119 iov[2].iov_base = &q;
120 iov[2].iov_len = sizeof(struct dns_question);
122 if (sendmsg(u->fd, &m, 0) < 0)
123 fprintf(stderr, "failed to send question\n");
125 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
138 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
139 static int dns_answer_cnt;
142 dns_init_answer(void)
148 dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
150 struct dns_reply *a = &dns_reply[dns_answer_cnt];
151 if (dns_answer_cnt == MAX_ANSWER)
153 a->rdata = memdup(rdata, rdlength);
155 a->rdlength = rdlength;
160 dns_send_answer(struct uloop_fd *u, char *answer)
163 struct dns_header h = { 0 };
164 struct msghdr m = { 0 };
166 struct sockaddr_in in = { 0 };
172 in.sin_family = AF_INET;
173 in.sin_addr.s_addr = inet_addr(MCAST_ADDR);
174 in.sin_port = htons(MCAST_PORT);
176 h.answers = __cpu_to_be16(dns_answer_cnt);
177 h.flags = __cpu_to_be16(0x8400);
179 iov = malloc(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
184 m.msg_namelen = sizeof(struct sockaddr_in);
186 m.msg_iovlen = (dns_answer_cnt * 3) + 1;
188 iov[0].iov_base = &h;
189 iov[0].iov_len = sizeof(struct dns_header);
191 for (i = 0; i < dns_answer_cnt; i++) {
192 struct dns_answer *a = &dns_reply[i].a;
193 int id = (i * 3) + 1;
195 memset(a, 0, sizeof(*a));
196 a->type = __cpu_to_be16(dns_reply[i].type);
197 a->class = __cpu_to_be16(1);
198 a->ttl = __cpu_to_be32(announce_ttl);
199 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
201 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
205 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
206 iov[id].iov_len = len;
208 iov[id + 1].iov_base = a;
209 iov[id + 1].iov_len = sizeof(struct dns_answer);
211 iov[id + 2].iov_base = dns_reply[i].rdata;
212 iov[id + 2].iov_len = dns_reply[i].rdlength;
214 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
217 if (sendmsg(u->fd, &m, 0) < 0)
218 fprintf(stderr, "failed to send question\n");
220 for (i = 0; i < dns_answer_cnt; i++) {
221 free(dns_reply[i].buffer);
222 free(dns_reply[i].rdata);
228 scan_name(uint8_t *buffer, int len)
232 while (len && (*buffer != '\0')) {
235 if (IS_COMPRESSED(l))
243 if (!len || !offset || (*buffer != '\0'))
250 dns_consume_header(uint8_t **data, int *len)
252 struct dns_header *h = (struct dns_header *) *data;
253 uint16_t *swap = (uint16_t *) h;
256 if (*len < sizeof(struct dns_header))
259 while (endianess--) {
260 *swap = __be16_to_cpu(*swap);
264 *len -= sizeof(struct dns_header);
265 *data += sizeof(struct dns_header);
271 dns_consume_question(uint8_t **data, int *len)
273 struct dns_question *q = (struct dns_question *) *data;
274 uint16_t *swap = (uint16_t *) q;
277 if (*len < sizeof(struct dns_question))
280 while (endianess--) {
281 *swap = __be16_to_cpu(*swap);
285 *len -= sizeof(struct dns_question);
286 *data += sizeof(struct dns_question);
292 dns_consume_answer(uint8_t **data, int *len)
294 struct dns_answer *a = (struct dns_answer *) *data;
296 if (*len < sizeof(struct dns_answer))
299 a->type = __be16_to_cpu(a->type);
300 a->class = __be16_to_cpu(a->class);
301 a->ttl = __be32_to_cpu(a->ttl);
302 a->rdlength = __be16_to_cpu(a->rdlength);
304 *len -= sizeof(struct dns_answer);
305 *data += sizeof(struct dns_answer);
311 dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
313 int nlen = scan_name(*data, *len);
318 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
319 perror("dns_consume_name/dn_expand");
332 name_buffer = malloc(MAX_NAME_LEN + 1);
333 rdata_buffer = malloc(MAX_DATA_LEN + 1);
335 if (!name_buffer || !rdata_buffer)
338 memset(name_buffer, 0, MAX_NAME_LEN + 1);
339 memset(rdata_buffer, 0, MAX_NAME_LEN + 1);