5595384a544b0f86ba326629defd27a47c2a3b27
[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 void
69 dns_send_question(struct uloop_fd *u, char *question, int type)
70 {
71         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
72         static struct dns_header h = {
73                 .questions = cpu_to_be16(1),
74         };
75         static struct dns_question q = {
76                 .class = cpu_to_be16(1),
77         };
78         static struct iovec iov[] = {
79                 {
80                         .iov_base = &h,
81                         .iov_len = sizeof(h),
82                 },
83                 {
84                         .iov_base = name_buffer,
85                 },
86                 {
87                         .iov_base = &q,
88                         .iov_len = sizeof(q),
89                 }
90         };
91         static struct sockaddr_in a = {
92                 .sin_family = AF_INET,
93                 .sin_port = htons(MCAST_PORT),
94         };
95         static struct msghdr m = {
96                 .msg_name = (struct sockaddr *) &a,
97                 .msg_namelen = sizeof(a),
98                 .msg_iov = iov,
99                 .msg_iovlen = ARRAY_SIZE(iov),
100                 .msg_control = cmsg_data,
101                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
102         };
103         struct in_pktinfo *pkti;
104         struct cmsghdr *cmsg;
105         int len;
106
107         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
108         q.type = __cpu_to_be16(type);
109
110         len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
111         if (len < 1)
112                 return;
113
114         iov[1].iov_len = len;
115
116         memset(cmsg_data, 0, sizeof(cmsg_data));
117
118         cmsg = CMSG_FIRSTHDR(&m);
119         cmsg->cmsg_len = m.msg_controllen;
120         cmsg->cmsg_level = IPPROTO_IP;
121         cmsg->cmsg_type = IP_PKTINFO;
122
123         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
124         pkti->ipi_ifindex = iface_index;
125
126         if (sendmsg(u->fd, &m, 0) < 0)
127                 fprintf(stderr, "failed to send question\n");
128         else
129                 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
130 }
131
132
133 struct dns_reply {
134         int type;
135         struct dns_answer a;
136         uint16_t rdlength;
137         uint8_t *rdata;
138         char *buffer;
139 };
140
141 #define MAX_ANSWER      8
142 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
143 static int dns_answer_cnt;
144
145 void
146 dns_init_answer(void)
147 {
148         dns_answer_cnt = 0;
149 }
150
151 void
152 dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
153 {
154         struct dns_reply *a = &dns_reply[dns_answer_cnt];
155         if (dns_answer_cnt == MAX_ANSWER)
156                 return;
157         a->rdata = memdup(rdata, rdlength);
158         a->type = type;
159         a->rdlength = rdlength;
160         dns_answer_cnt++;
161 }
162
163 void
164 dns_send_answer(struct uloop_fd *u, char *answer)
165 {
166         uint8_t buffer[256];
167         struct dns_header h = { 0 };
168         struct msghdr m = { 0 };
169         struct iovec *iov;
170         struct sockaddr_in in = { 0 };
171         int len, i;
172
173         if (!dns_answer_cnt)
174                 return;
175
176         in.sin_family = AF_INET;
177         in.sin_addr.s_addr = inet_addr(MCAST_ADDR);
178         in.sin_port = htons(MCAST_PORT);
179
180         h.answers = __cpu_to_be16(dns_answer_cnt);
181         h.flags = __cpu_to_be16(0x8400);
182
183         iov = malloc(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
184         if (!iov)
185                 return;
186
187         m.msg_name = &in;
188         m.msg_namelen = sizeof(struct sockaddr_in);
189         m.msg_iov = iov;
190         m.msg_iovlen = (dns_answer_cnt * 3) + 1;
191
192         iov[0].iov_base = &h;
193         iov[0].iov_len = sizeof(struct dns_header);
194
195         for (i = 0; i < dns_answer_cnt; i++) {
196                 struct dns_answer *a = &dns_reply[i].a;
197                 int id = (i * 3) + 1;
198
199                 memset(a, 0, sizeof(*a));
200                 a->type = __cpu_to_be16(dns_reply[i].type);
201                 a->class = __cpu_to_be16(1);
202                 a->ttl = __cpu_to_be32(announce_ttl);
203                 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
204
205                 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
206                 if (len < 1)
207                         return;
208
209                 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
210                 iov[id].iov_len = len;
211
212                 iov[id + 1].iov_base = a;
213                 iov[id + 1].iov_len = sizeof(struct dns_answer);
214
215                 iov[id + 2].iov_base = dns_reply[i].rdata;
216                 iov[id + 2].iov_len = dns_reply[i].rdlength;
217
218                 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
219         }
220
221         if (sendmsg(u->fd, &m, 0) < 0)
222                 fprintf(stderr, "failed to send question\n");
223
224         for (i = 0; i < dns_answer_cnt; i++) {
225                 free(dns_reply[i].buffer);
226                 free(dns_reply[i].rdata);
227         }
228         dns_answer_cnt = 0;
229 }
230
231 static int
232 scan_name(uint8_t *buffer, int len)
233 {
234         int offset = 0;
235
236         while (len && (*buffer != '\0')) {
237                 int l = *buffer;
238
239                 if (IS_COMPRESSED(l))
240                         return offset + 2;
241
242                 len -= l + 1;
243                 offset += l + 1;
244                 buffer += l + 1;
245         }
246
247         if (!len || !offset || (*buffer != '\0'))
248                 return -1;
249
250         return offset + 1;
251 }
252
253 struct dns_header*
254 dns_consume_header(uint8_t **data, int *len)
255 {
256         struct dns_header *h = (struct dns_header *) *data;
257         uint16_t *swap = (uint16_t *) h;
258         int endianess = 6;
259
260         if (*len < sizeof(struct dns_header))
261                 return NULL;
262
263         while (endianess--) {
264                 *swap = __be16_to_cpu(*swap);
265                 swap++;
266         }
267
268         *len -= sizeof(struct dns_header);
269         *data += sizeof(struct dns_header);
270
271         return h;
272 }
273
274 struct dns_question*
275 dns_consume_question(uint8_t **data, int *len)
276 {
277         struct dns_question *q = (struct dns_question *) *data;
278         uint16_t *swap = (uint16_t *) q;
279         int endianess = 2;
280
281         if (*len < sizeof(struct dns_question))
282                 return NULL;
283
284         while (endianess--) {
285                 *swap = __be16_to_cpu(*swap);
286                 swap++;
287         }
288
289         *len -= sizeof(struct dns_question);
290         *data += sizeof(struct dns_question);
291
292         return q;
293 }
294
295 struct dns_answer*
296 dns_consume_answer(uint8_t **data, int *len)
297 {
298         struct dns_answer *a = (struct dns_answer *) *data;
299
300         if (*len < sizeof(struct dns_answer))
301                 return NULL;
302
303         a->type = __be16_to_cpu(a->type);
304         a->class = __be16_to_cpu(a->class);
305         a->ttl = __be32_to_cpu(a->ttl);
306         a->rdlength = __be16_to_cpu(a->rdlength);
307
308         *len -= sizeof(struct dns_answer);
309         *data += sizeof(struct dns_answer);
310
311         return a;
312 }
313
314 char*
315 dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
316 {
317         int nlen = scan_name(*data, *len);
318
319         if (nlen < 1)
320                 return NULL;
321
322         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
323                 perror("dns_consume_name/dn_expand");
324                 return NULL;
325         }
326
327         *len -= nlen;
328         *data += nlen;
329
330         return name_buffer;
331 }