4f82849ef6f64e44c17a3ae5df7a6bc8f9818a19
[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 <arpa/nameser.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/utils.h>
33
34 #include "announce.h"
35 #include "util.h"
36 #include "dns.h"
37 #include "cache.h"
38 #include "service.h"
39 #include "interface.h"
40
41 static char name_buffer[MAX_NAME_LEN + 1];
42 static struct blob_buf ans_buf;
43
44 const char*
45 dns_type_string(uint16_t type)
46 {
47 static const struct {
48 uint16_t type;
49 char str[5];
50 } type_str[] = {
51 { TYPE_A, "A" },
52 { TYPE_AAAA, "AAAA" },
53 { TYPE_PTR, "PTR" },
54 { TYPE_TXT, "TXT" },
55 { TYPE_SRV, "SRV" },
56 { TYPE_ANY, "ANY" },
57 };
58 int i;
59
60 for (i = 0; i < ARRAY_SIZE(type_str); i++) {
61 if (type == type_str[i].type)
62 return type_str[i].str;
63 }
64
65 return "N/A";
66 }
67
68 void
69 dns_send_question(struct interface *iface, const char *question, int type, int unicast)
70 {
71 static struct dns_header h;
72 static struct dns_question q;
73 static struct iovec iov[] = {
74 {
75 .iov_base = &h,
76 .iov_len = sizeof(h),
77 },
78 {
79 .iov_base = name_buffer,
80 },
81 {
82 .iov_base = &q,
83 .iov_len = sizeof(q),
84 }
85 };
86 int len;
87
88 h.questions = cpu_to_be16(1);
89 q.class = cpu_to_be16(((unicast) ? (CLASS_UNICAST) : (0)) | 1);
90 q.type = cpu_to_be16(type);
91
92 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
93 if (len < 1)
94 return;
95
96 iov[1].iov_len = len;
97
98 if (unicast == iface->multicast)
99 iface = iface->peer;
100
101 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
102 fprintf(stderr, "failed to send question\n");
103 else
104 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
105 }
106
107
108 struct dns_reply {
109 int type;
110 struct dns_answer a;
111 uint16_t rdlength;
112 uint8_t *rdata;
113 char *buffer;
114 };
115
116 static int dns_answer_cnt;
117
118 void
119 dns_init_answer(void)
120 {
121 dns_answer_cnt = 0;
122 blob_buf_init(&ans_buf, 0);
123 }
124
125 void
126 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
127 {
128 struct blob_attr *attr;
129 struct dns_answer *a;
130
131 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
132 a = blob_data(attr);
133 a->type = cpu_to_be16(type);
134 a->class = cpu_to_be16(1);
135 a->ttl = cpu_to_be32(ttl);
136 a->rdlength = cpu_to_be16(rdlength);
137 memcpy(a + 1, rdata, rdlength);
138
139 dns_answer_cnt++;
140 }
141
142 void
143 dns_send_answer(struct interface *iface, const char *answer)
144 {
145 uint8_t buffer[256];
146 struct blob_attr *attr;
147 struct dns_header h = { 0 };
148 struct iovec *iov;
149 int answer_len, rem;
150 int n_iov = 0;
151
152 if (!dns_answer_cnt)
153 return;
154
155 h.answers = cpu_to_be16(dns_answer_cnt);
156 h.flags = cpu_to_be16(0x8400);
157
158 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
159
160 iov[n_iov].iov_base = &h;
161 iov[n_iov].iov_len = sizeof(struct dns_header);
162 n_iov++;
163
164 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
165 if (answer_len < 1)
166 return;
167
168 blob_for_each_attr(attr, ans_buf.head, rem) {
169 struct dns_answer *a = blob_data(attr);
170
171 iov[n_iov].iov_base = buffer;
172 iov[n_iov].iov_len = answer_len;
173 n_iov++;
174
175 iov[n_iov].iov_base = blob_data(attr);
176 iov[n_iov].iov_len = blob_len(attr);
177 n_iov++;
178
179 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
180 }
181
182 if (interface_send_packet(iface, iov, n_iov) < 0)
183 fprintf(stderr, "failed to send question\n");
184 }
185
186 static int
187 scan_name(const uint8_t *buffer, int len)
188 {
189 int offset = 0;
190
191 while (len && (*buffer != '\0')) {
192 int l = *buffer;
193
194 if (IS_COMPRESSED(l))
195 return offset + 2;
196
197 len -= l + 1;
198 offset += l + 1;
199 buffer += l + 1;
200 }
201
202 if (!len || !offset || (*buffer != '\0'))
203 return -1;
204
205 return offset + 1;
206 }
207
208 static struct dns_header*
209 dns_consume_header(uint8_t **data, int *len)
210 {
211 struct dns_header *h = (struct dns_header *) *data;
212 uint16_t *swap = (uint16_t *) h;
213 int endianess = 6;
214
215 if (*len < sizeof(struct dns_header))
216 return NULL;
217
218 while (endianess--) {
219 *swap = be16_to_cpu(*swap);
220 swap++;
221 }
222
223 *len -= sizeof(struct dns_header);
224 *data += sizeof(struct dns_header);
225
226 return h;
227 }
228
229 static struct dns_question*
230 dns_consume_question(uint8_t **data, int *len)
231 {
232 struct dns_question *q = (struct dns_question *) *data;
233 uint16_t *swap = (uint16_t *) q;
234 int endianess = 2;
235
236 if (*len < sizeof(struct dns_question))
237 return NULL;
238
239 while (endianess--) {
240 *swap = be16_to_cpu(*swap);
241 swap++;
242 }
243
244 *len -= sizeof(struct dns_question);
245 *data += sizeof(struct dns_question);
246
247 return q;
248 }
249
250 static struct dns_answer*
251 dns_consume_answer(uint8_t **data, int *len)
252 {
253 struct dns_answer *a = (struct dns_answer *) *data;
254
255 if (*len < sizeof(struct dns_answer))
256 return NULL;
257
258 a->type = be16_to_cpu(a->type);
259 a->class = be16_to_cpu(a->class);
260 a->ttl = be32_to_cpu(a->ttl);
261 a->rdlength = be16_to_cpu(a->rdlength);
262
263 *len -= sizeof(struct dns_answer);
264 *data += sizeof(struct dns_answer);
265
266 return a;
267 }
268
269 static char *
270 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
271 {
272 int nlen = scan_name(*data, *len);
273
274 if (nlen < 1)
275 return NULL;
276
277 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
278 perror("dns_consume_name/dn_expand");
279 return NULL;
280 }
281
282 *len -= nlen;
283 *data += nlen;
284
285 return name_buffer;
286 }
287
288 static int
289 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
290 {
291 char *name = dns_consume_name(buffer, len, b, rlen);
292 struct dns_answer *a;
293 uint8_t *rdata;
294
295 if (!name) {
296 fprintf(stderr, "dropping: bad question\n");
297 return -1;
298 }
299
300 a = dns_consume_answer(b, rlen);
301 if (!a) {
302 fprintf(stderr, "dropping: bad question\n");
303 return -1;
304 }
305
306 rdata = *b;
307 if (a->rdlength > *rlen) {
308 fprintf(stderr, "dropping: bad question\n");
309 return -1;
310 }
311
312 *rlen -= a->rdlength;
313 *b += a->rdlength;
314
315 if (cache)
316 cache_answer(iface, buffer, len, name, a, rdata);
317
318 return 0;
319 }
320
321 static void
322 parse_question(struct interface *iface, char *name, struct dns_question *q)
323 {
324 char *host;
325
326 if ((q->class & CLASS_UNICAST) && iface->multicast)
327 iface = iface->peer;
328
329 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
330
331 switch (q->type) {
332 case TYPE_ANY:
333 if (!strcmp(name, mdns_hostname_local))
334 service_reply(iface, NULL, announce_ttl);
335 break;
336
337 case TYPE_PTR:
338 service_announce_services(iface, name);
339 service_reply(iface, name, announce_ttl);
340 break;
341
342 case TYPE_AAAA:
343 case TYPE_A:
344 host = strstr(name, ".local");
345 if (host)
346 *host = '\0';
347 if (!strcmp(mdns_hostname, name))
348 service_reply_a(iface, q->type, announce_ttl);
349 break;
350 };
351 }
352
353 void
354 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len, int unicast)
355 {
356 struct dns_header *h;
357 uint8_t *b = buffer;
358 int rlen = len;
359
360 h = dns_consume_header(&b, &rlen);
361 if (!h) {
362 fprintf(stderr, "dropping: bad header\n");
363 return;
364 }
365
366 while (h->questions-- > 0) {
367 char *name = dns_consume_name(buffer, len, &b, &rlen);
368 struct dns_question *q;
369
370 if (!name) {
371 fprintf(stderr, "dropping: bad name\n");
372 return;
373 }
374
375 q = dns_consume_question(&b, &rlen);
376 if (!q) {
377 fprintf(stderr, "dropping: bad question\n");
378 return;
379 }
380
381 if (!(h->flags & FLAG_RESPONSE))
382 parse_question(iface, name, q);
383 }
384
385 if (!(h->flags & FLAG_RESPONSE))
386 return;
387
388 while (h->answers-- > 0)
389 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
390 return;
391
392 while (h->authority-- > 0)
393 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
394 return;
395
396 while (h->additional-- > 0)
397 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
398 return;
399
400 }