ecfaa6353131f646f42abd9220654147ed6e22fa
[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
34 #include "announce.h"
35 #include "util.h"
36 #include "dns.h"
37
38 char *rdata_buffer;
39 static char *name_buffer;
40
41 const char*
42 dns_type_string(uint16_t type)
43 {
44         switch (type) {
45         case TYPE_A:
46                 return "A";
47
48         case TYPE_AAAA:
49                 return "AAAA";
50
51         case TYPE_PTR:
52                 return "PTR";
53
54         case TYPE_TXT:
55                 return "TXT";
56
57         case TYPE_SRV:
58                 return "SRV";
59
60         case TYPE_ANY:
61                 return "ANY";
62         }
63
64         return "N/A";
65 }
66
67 void
68 dns_send_question(struct uloop_fd *u, char *question, int type)
69 {
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;
78         struct cmsghdr *cmsg;
79         struct in_addr in;
80         int len;
81
82         a.sin_family = AF_INET;
83         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
84         a.sin_port = htons(MCAST_PORT);
85
86         h.questions = __cpu_to_be16(1);
87         q.type = __cpu_to_be16(type);
88         q.class = __cpu_to_be16(1);
89
90         len = dn_comp(question, buffer, MAX_NAME_LEN, NULL, NULL);
91         if (len < 1)
92                 return;
93
94         m.msg_name = &a;
95         m.msg_namelen = sizeof(struct sockaddr_in);
96         m.msg_iov = iov;
97         m.msg_iovlen = 3;
98
99         memset(cmsg_data, 0, sizeof(cmsg_data));
100         m.msg_control = cmsg_data;
101         m.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
102
103         cmsg = CMSG_FIRSTHDR(&m);
104         cmsg->cmsg_len = m.msg_controllen;
105         cmsg->cmsg_level = IPPROTO_IP;
106         cmsg->cmsg_type = IP_PKTINFO;
107
108         inet_aton(iface_ip, &in);
109
110         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
111         pkti->ipi_ifindex = iface_index;
112
113         iov[0].iov_base = &h;
114         iov[0].iov_len = sizeof(struct dns_header);
115
116         iov[1].iov_base = buffer;
117         iov[1].iov_len = len;
118
119         iov[2].iov_base = &q;
120         iov[2].iov_len = sizeof(struct dns_question);
121
122         if (sendmsg(u->fd, &m, 0) < 0)
123                 fprintf(stderr, "failed to send question\n");
124         else
125                 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
126 }
127
128
129 struct dns_reply {
130         int type;
131         struct dns_answer a;
132         uint16_t rdlength;
133         uint8_t *rdata;
134         char *buffer;
135 };
136
137 #define MAX_ANSWER      8
138 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
139 static int dns_answer_cnt;
140
141 void
142 dns_init_answer(void)
143 {
144         dns_answer_cnt = 0;
145 }
146
147 void
148 dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
149 {
150         struct dns_reply *a = &dns_reply[dns_answer_cnt];
151         if (dns_answer_cnt == MAX_ANSWER)
152                 return;
153         a->rdata = memdup(rdata, rdlength);
154         a->type = type;
155         a->rdlength = rdlength;
156         dns_answer_cnt++;
157 }
158
159 void
160 dns_send_answer(struct uloop_fd *u, char *answer)
161 {
162         uint8_t buffer[256];
163         struct dns_header h = { 0 };
164         struct msghdr m = { 0 };
165         struct iovec *iov;
166         struct sockaddr_in in = { 0 };
167         int len, i;
168
169         if (!dns_answer_cnt)
170                 return;
171
172         in.sin_family = AF_INET;
173         in.sin_addr.s_addr = inet_addr(MCAST_ADDR);
174         in.sin_port = htons(MCAST_PORT);
175
176         h.answers = __cpu_to_be16(dns_answer_cnt);
177         h.flags = __cpu_to_be16(0x8400);
178
179         iov = malloc(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
180         if (!iov)
181                 return;
182
183         m.msg_name = &in;
184         m.msg_namelen = sizeof(struct sockaddr_in);
185         m.msg_iov = iov;
186         m.msg_iovlen = (dns_answer_cnt * 3) + 1;
187
188         iov[0].iov_base = &h;
189         iov[0].iov_len = sizeof(struct dns_header);
190
191         for (i = 0; i < dns_answer_cnt; i++) {
192                 struct dns_answer *a = &dns_reply[i].a;
193                 int id = (i * 3) + 1;
194
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);
200
201                 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
202                 if (len < 1)
203                         return;
204
205                 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
206                 iov[id].iov_len = len;
207
208                 iov[id + 1].iov_base = a;
209                 iov[id + 1].iov_len = sizeof(struct dns_answer);
210
211                 iov[id + 2].iov_base = dns_reply[i].rdata;
212                 iov[id + 2].iov_len = dns_reply[i].rdlength;
213
214                 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
215         }
216
217         if (sendmsg(u->fd, &m, 0) < 0)
218                 fprintf(stderr, "failed to send question\n");
219
220         for (i = 0; i < dns_answer_cnt; i++) {
221                 free(dns_reply[i].buffer);
222                 free(dns_reply[i].rdata);
223         }
224         dns_answer_cnt = 0;
225 }
226
227 static int
228 scan_name(uint8_t *buffer, int len)
229 {
230         int offset = 0;
231
232         while (len && (*buffer != '\0')) {
233                 int l = *buffer;
234
235                 if (IS_COMPRESSED(l))
236                         return offset + 2;
237
238                 len -= l + 1;
239                 offset += l + 1;
240                 buffer += l + 1;
241         }
242
243         if (!len || !offset || (*buffer != '\0'))
244                 return -1;
245
246         return offset + 1;
247 }
248
249 struct dns_header*
250 dns_consume_header(uint8_t **data, int *len)
251 {
252         struct dns_header *h = (struct dns_header *) *data;
253         uint16_t *swap = (uint16_t *) h;
254         int endianess = 6;
255
256         if (*len < sizeof(struct dns_header))
257                 return NULL;
258
259         while (endianess--) {
260                 *swap = __be16_to_cpu(*swap);
261                 swap++;
262         }
263
264         *len -= sizeof(struct dns_header);
265         *data += sizeof(struct dns_header);
266
267         return h;
268 }
269
270 struct dns_question*
271 dns_consume_question(uint8_t **data, int *len)
272 {
273         struct dns_question *q = (struct dns_question *) *data;
274         uint16_t *swap = (uint16_t *) q;
275         int endianess = 2;
276
277         if (*len < sizeof(struct dns_question))
278                 return NULL;
279
280         while (endianess--) {
281                 *swap = __be16_to_cpu(*swap);
282                 swap++;
283         }
284
285         *len -= sizeof(struct dns_question);
286         *data += sizeof(struct dns_question);
287
288         return q;
289 }
290
291 struct dns_answer*
292 dns_consume_answer(uint8_t **data, int *len)
293 {
294         struct dns_answer *a = (struct dns_answer *) *data;
295
296         if (*len < sizeof(struct dns_answer))
297                 return NULL;
298
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);
303
304         *len -= sizeof(struct dns_answer);
305         *data += sizeof(struct dns_answer);
306
307         return a;
308 }
309
310 char*
311 dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
312 {
313         int nlen = scan_name(*data, *len);
314
315         if (nlen < 1)
316                 return NULL;
317
318         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
319                 perror("dns_consume_name/dn_expand");
320                 return NULL;
321         }
322
323         *len -= nlen;
324         *data += nlen;
325
326         return name_buffer;
327 }
328
329 int
330 dns_init(void)
331 {
332         name_buffer = malloc(MAX_NAME_LEN + 1);
333         rdata_buffer = malloc(MAX_DATA_LEN + 1);
334
335         if (!name_buffer || !rdata_buffer)
336                 return -1;
337
338         memset(name_buffer, 0, MAX_NAME_LEN + 1);
339         memset(rdata_buffer, 0, MAX_NAME_LEN + 1);
340
341         return 0;
342 }
343
344 void
345 dns_cleanup(void)
346 {
347         free(name_buffer);
348         free(rdata_buffer);
349 }