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