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