961c2d0a2fded8459f23adde7432ca05b14c4202
[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)
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(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 (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
99 fprintf(stderr, "failed to send question\n");
100 else
101 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
102 }
103
104
105 struct dns_reply {
106 int type;
107 struct dns_answer a;
108 uint16_t rdlength;
109 uint8_t *rdata;
110 char *buffer;
111 };
112
113 static int dns_answer_cnt;
114
115 void
116 dns_init_answer(void)
117 {
118 dns_answer_cnt = 0;
119 blob_buf_init(&ans_buf, 0);
120 }
121
122 void
123 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
124 {
125 struct blob_attr *attr;
126 struct dns_answer *a;
127
128 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
129 a = blob_data(attr);
130 a->type = cpu_to_be16(type);
131 a->class = cpu_to_be16(1);
132 a->ttl = cpu_to_be32(ttl);
133 a->rdlength = cpu_to_be16(rdlength);
134 memcpy(a + 1, rdata, rdlength);
135
136 dns_answer_cnt++;
137 }
138
139 void
140 dns_send_answer(struct interface *iface, const char *answer)
141 {
142 uint8_t buffer[256];
143 struct blob_attr *attr;
144 struct dns_header h = { 0 };
145 struct iovec *iov;
146 int answer_len, rem;
147 int n_iov = 0;
148
149 if (!dns_answer_cnt)
150 return;
151
152 h.answers = cpu_to_be16(dns_answer_cnt);
153 h.flags = cpu_to_be16(0x8400);
154
155 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
156
157 iov[n_iov].iov_base = &h;
158 iov[n_iov].iov_len = sizeof(struct dns_header);
159 n_iov++;
160
161 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
162 if (answer_len < 1)
163 return;
164
165 blob_for_each_attr(attr, ans_buf.head, rem) {
166 struct dns_answer *a = blob_data(attr);
167
168 iov[n_iov].iov_base = buffer;
169 iov[n_iov].iov_len = answer_len;
170 n_iov++;
171
172 iov[n_iov].iov_base = blob_data(attr);
173 iov[n_iov].iov_len = blob_len(attr);
174 n_iov++;
175
176 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
177 }
178
179 if (interface_send_packet(iface, iov, n_iov) < 0)
180 fprintf(stderr, "failed to send question\n");
181 }
182
183 static int
184 scan_name(const uint8_t *buffer, int len)
185 {
186 int offset = 0;
187
188 while (len && (*buffer != '\0')) {
189 int l = *buffer;
190
191 if (IS_COMPRESSED(l))
192 return offset + 2;
193
194 len -= l + 1;
195 offset += l + 1;
196 buffer += l + 1;
197 }
198
199 if (!len || !offset || (*buffer != '\0'))
200 return -1;
201
202 return offset + 1;
203 }
204
205 static struct dns_header*
206 dns_consume_header(uint8_t **data, int *len)
207 {
208 struct dns_header *h = (struct dns_header *) *data;
209 uint16_t *swap = (uint16_t *) h;
210 int endianess = 6;
211
212 if (*len < sizeof(struct dns_header))
213 return NULL;
214
215 while (endianess--) {
216 *swap = be16_to_cpu(*swap);
217 swap++;
218 }
219
220 *len -= sizeof(struct dns_header);
221 *data += sizeof(struct dns_header);
222
223 return h;
224 }
225
226 static struct dns_question*
227 dns_consume_question(uint8_t **data, int *len)
228 {
229 struct dns_question *q = (struct dns_question *) *data;
230 uint16_t *swap = (uint16_t *) q;
231 int endianess = 2;
232
233 if (*len < sizeof(struct dns_question))
234 return NULL;
235
236 while (endianess--) {
237 *swap = be16_to_cpu(*swap);
238 swap++;
239 }
240
241 *len -= sizeof(struct dns_question);
242 *data += sizeof(struct dns_question);
243
244 return q;
245 }
246
247 static struct dns_answer*
248 dns_consume_answer(uint8_t **data, int *len)
249 {
250 struct dns_answer *a = (struct dns_answer *) *data;
251
252 if (*len < sizeof(struct dns_answer))
253 return NULL;
254
255 a->type = be16_to_cpu(a->type);
256 a->class = be16_to_cpu(a->class);
257 a->ttl = be32_to_cpu(a->ttl);
258 a->rdlength = be16_to_cpu(a->rdlength);
259
260 *len -= sizeof(struct dns_answer);
261 *data += sizeof(struct dns_answer);
262
263 return a;
264 }
265
266 static char *
267 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
268 {
269 int nlen = scan_name(*data, *len);
270
271 if (nlen < 1)
272 return NULL;
273
274 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
275 perror("dns_consume_name/dn_expand");
276 return NULL;
277 }
278
279 *len -= nlen;
280 *data += nlen;
281
282 return name_buffer;
283 }
284
285 static int
286 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
287 {
288 char *name = dns_consume_name(buffer, len, b, rlen);
289 struct dns_answer *a;
290 uint8_t *rdata;
291
292 if (!name) {
293 fprintf(stderr, "dropping: bad question\n");
294 return -1;
295 }
296
297 a = dns_consume_answer(b, rlen);
298 if (!a) {
299 fprintf(stderr, "dropping: bad question\n");
300 return -1;
301 }
302
303 rdata = *b;
304 if (a->rdlength > *rlen) {
305 fprintf(stderr, "dropping: bad question\n");
306 return -1;
307 }
308
309 *rlen -= a->rdlength;
310 *b += a->rdlength;
311
312 if (cache)
313 cache_answer(iface, buffer, len, name, a, rdata);
314
315 return 0;
316 }
317
318 static void
319 parse_question(struct interface *iface, char *name, struct dns_question *q)
320 {
321 char *host;
322
323 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
324
325 switch (q->type) {
326 case TYPE_ANY:
327 if (!strcmp(name, mdns_hostname_local))
328 service_reply(iface, NULL, announce_ttl);
329 break;
330
331 case TYPE_PTR:
332 service_announce_services(iface, name);
333 service_reply(iface, name, announce_ttl);
334 break;
335
336 case TYPE_AAAA:
337 case TYPE_A:
338 host = strstr(name, ".local");
339 if (host)
340 *host = '\0';
341 if (!strcmp(mdns_hostname, name))
342 service_reply_a(iface, q->type, announce_ttl);
343 break;
344 };
345 }
346
347 void
348 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len, int unicast)
349 {
350 struct dns_header *h;
351 uint8_t *b = buffer;
352 int rlen = len;
353
354 h = dns_consume_header(&b, &rlen);
355 if (!h) {
356 fprintf(stderr, "dropping: bad header\n");
357 return;
358 }
359
360 while (h->questions-- > 0) {
361 char *name = dns_consume_name(buffer, len, &b, &rlen);
362 struct dns_question *q;
363
364 if (!name) {
365 fprintf(stderr, "dropping: bad name\n");
366 return;
367 }
368
369 q = dns_consume_question(&b, &rlen);
370 if (!q) {
371 fprintf(stderr, "dropping: bad question\n");
372 return;
373 }
374
375 if (!(h->flags & FLAG_RESPONSE))
376 parse_question(iface, name, q);
377 }
378
379 if (!(h->flags & FLAG_RESPONSE))
380 return;
381
382 while (h->answers-- > 0)
383 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
384 return;
385
386 while (h->authority-- > 0)
387 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
388 return;
389
390 while (h->additional-- > 0)
391 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
392 return;
393
394 }