cache: reduce allocation of cache entries to one chunk of memory, using calloc_a
[project/mdnsd.git] / dns.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include <fcntl.h>
18 #include <time.h>
19 #include <stdio.h>
20 #include <unistd.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>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <libubox/uloop.h>
32 #include <libubox/usock.h>
33 #include <libubox/utils.h>
34
35 #include "announce.h"
36 #include "util.h"
37 #include "dns.h"
38
39 char rdata_buffer[MAX_DATA_LEN + 1];
40 static char name_buffer[MAX_NAME_LEN + 1];
41
42 const char*
43 dns_type_string(uint16_t type)
44 {
45 switch (type) {
46 case TYPE_A:
47 return "A";
48
49 case TYPE_AAAA:
50 return "AAAA";
51
52 case TYPE_PTR:
53 return "PTR";
54
55 case TYPE_TXT:
56 return "TXT";
57
58 case TYPE_SRV:
59 return "SRV";
60
61 case TYPE_ANY:
62 return "ANY";
63 }
64
65 return "N/A";
66 }
67
68 static int
69 dns_send_packet(int fd, struct iovec *iov, int iov_len)
70 {
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),
75 };
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)),
81 };
82 struct in_pktinfo *pkti;
83 struct cmsghdr *cmsg;
84
85 m.msg_iov = iov;
86 m.msg_iovlen = iov_len;
87
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;
93
94 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
95 pkti->ipi_ifindex = iface_index;
96
97 a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
98
99 return sendmsg(fd, &m, 0);
100 }
101
102 void
103 dns_send_question(struct uloop_fd *u, char *question, int type)
104 {
105 static struct dns_header h = {
106 .questions = cpu_to_be16(1),
107 };
108 static struct dns_question q = {
109 .class = cpu_to_be16(1),
110 };
111 static struct iovec iov[] = {
112 {
113 .iov_base = &h,
114 .iov_len = sizeof(h),
115 },
116 {
117 .iov_base = name_buffer,
118 },
119 {
120 .iov_base = &q,
121 .iov_len = sizeof(q),
122 }
123 };
124 int len;
125
126 q.type = __cpu_to_be16(type);
127
128 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
129 if (len < 1)
130 return;
131
132 iov[1].iov_len = len;
133
134 if (dns_send_packet(u->fd, iov, ARRAY_SIZE(iov)) < 0)
135 fprintf(stderr, "failed to send question\n");
136 else
137 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
138 }
139
140
141 struct dns_reply {
142 int type;
143 struct dns_answer a;
144 uint16_t rdlength;
145 uint8_t *rdata;
146 char *buffer;
147 };
148
149 #define MAX_ANSWER 8
150 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
151 static int dns_answer_cnt;
152
153 void
154 dns_init_answer(void)
155 {
156 dns_answer_cnt = 0;
157 }
158
159 void
160 dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
161 {
162 struct dns_reply *a = &dns_reply[dns_answer_cnt];
163 if (dns_answer_cnt == MAX_ANSWER)
164 return;
165 a->rdata = memdup(rdata, rdlength);
166 a->type = type;
167 a->rdlength = rdlength;
168 dns_answer_cnt++;
169 }
170
171 void
172 dns_send_answer(struct uloop_fd *u, char *answer)
173 {
174 uint8_t buffer[256];
175 struct dns_header h = { 0 };
176 struct iovec *iov;
177 int len, i;
178
179 if (!dns_answer_cnt)
180 return;
181
182 h.answers = __cpu_to_be16(dns_answer_cnt);
183 h.flags = __cpu_to_be16(0x8400);
184
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);
188
189 for (i = 0; i < dns_answer_cnt; i++) {
190 struct dns_answer *a = &dns_reply[i].a;
191 int id = (i * 3) + 1;
192
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);
198
199 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
200 if (len < 1)
201 return;
202
203 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
204 iov[id].iov_len = len;
205
206 iov[id + 1].iov_base = a;
207 iov[id + 1].iov_len = sizeof(struct dns_answer);
208
209 iov[id + 2].iov_base = dns_reply[i].rdata;
210 iov[id + 2].iov_len = dns_reply[i].rdlength;
211
212 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
213 }
214
215 if (dns_send_packet(u->fd, iov, (dns_answer_cnt * 3) + 1) < 0)
216 fprintf(stderr, "failed to send question\n");
217
218 for (i = 0; i < dns_answer_cnt; i++) {
219 free(dns_reply[i].buffer);
220 free(dns_reply[i].rdata);
221 }
222 dns_answer_cnt = 0;
223 }
224
225 static int
226 scan_name(uint8_t *buffer, int len)
227 {
228 int offset = 0;
229
230 while (len && (*buffer != '\0')) {
231 int l = *buffer;
232
233 if (IS_COMPRESSED(l))
234 return offset + 2;
235
236 len -= l + 1;
237 offset += l + 1;
238 buffer += l + 1;
239 }
240
241 if (!len || !offset || (*buffer != '\0'))
242 return -1;
243
244 return offset + 1;
245 }
246
247 struct dns_header*
248 dns_consume_header(uint8_t **data, int *len)
249 {
250 struct dns_header *h = (struct dns_header *) *data;
251 uint16_t *swap = (uint16_t *) h;
252 int endianess = 6;
253
254 if (*len < sizeof(struct dns_header))
255 return NULL;
256
257 while (endianess--) {
258 *swap = __be16_to_cpu(*swap);
259 swap++;
260 }
261
262 *len -= sizeof(struct dns_header);
263 *data += sizeof(struct dns_header);
264
265 return h;
266 }
267
268 struct dns_question*
269 dns_consume_question(uint8_t **data, int *len)
270 {
271 struct dns_question *q = (struct dns_question *) *data;
272 uint16_t *swap = (uint16_t *) q;
273 int endianess = 2;
274
275 if (*len < sizeof(struct dns_question))
276 return NULL;
277
278 while (endianess--) {
279 *swap = __be16_to_cpu(*swap);
280 swap++;
281 }
282
283 *len -= sizeof(struct dns_question);
284 *data += sizeof(struct dns_question);
285
286 return q;
287 }
288
289 struct dns_answer*
290 dns_consume_answer(uint8_t **data, int *len)
291 {
292 struct dns_answer *a = (struct dns_answer *) *data;
293
294 if (*len < sizeof(struct dns_answer))
295 return NULL;
296
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);
301
302 *len -= sizeof(struct dns_answer);
303 *data += sizeof(struct dns_answer);
304
305 return a;
306 }
307
308 char*
309 dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
310 {
311 int nlen = scan_name(*data, *len);
312
313 if (nlen < 1)
314 return NULL;
315
316 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
317 perror("dns_consume_name/dn_expand");
318 return NULL;
319 }
320
321 *len -= nlen;
322 *data += nlen;
323
324 return name_buffer;
325 }