move announce state to struct interface
[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 "interface.h"
39
40 char rdata_buffer[MAX_DATA_LEN + 1];
41 static char name_buffer[MAX_NAME_LEN + 1];
42
43 const char*
44 dns_type_string(uint16_t type)
45 {
46         switch (type) {
47         case TYPE_A:
48                 return "A";
49
50         case TYPE_AAAA:
51                 return "AAAA";
52
53         case TYPE_PTR:
54                 return "PTR";
55
56         case TYPE_TXT:
57                 return "TXT";
58
59         case TYPE_SRV:
60                 return "SRV";
61
62         case TYPE_ANY:
63                 return "ANY";
64         }
65
66         return "N/A";
67 }
68
69 void
70 dns_send_question(struct interface *iface, const char *question, int type)
71 {
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         int len;
92
93         q.type = __cpu_to_be16(type);
94
95         len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
96         if (len < 1)
97                 return;
98
99         iov[1].iov_len = len;
100
101         if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
102                 fprintf(stderr, "failed to send question\n");
103         else
104                 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
105 }
106
107
108 struct dns_reply {
109         int type;
110         struct dns_answer a;
111         uint16_t rdlength;
112         uint8_t *rdata;
113         char *buffer;
114 };
115
116 #define MAX_ANSWER      8
117 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
118 static int dns_answer_cnt;
119
120 void
121 dns_init_answer(void)
122 {
123         dns_answer_cnt = 0;
124 }
125
126 void
127 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
128 {
129         struct dns_reply *a = &dns_reply[dns_answer_cnt];
130         if (dns_answer_cnt == MAX_ANSWER)
131                 return;
132         a->rdata = memdup(rdata, rdlength);
133         a->type = type;
134         a->rdlength = rdlength;
135         dns_answer_cnt++;
136 }
137
138 void
139 dns_send_answer(struct interface *iface, const char *answer)
140 {
141         uint8_t buffer[256];
142         struct dns_header h = { 0 };
143         struct iovec *iov;
144         int len, i;
145
146         if (!dns_answer_cnt)
147                 return;
148
149         h.answers = __cpu_to_be16(dns_answer_cnt);
150         h.flags = __cpu_to_be16(0x8400);
151
152         iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
153         iov[0].iov_base = &h;
154         iov[0].iov_len = sizeof(struct dns_header);
155
156         for (i = 0; i < dns_answer_cnt; i++) {
157                 struct dns_answer *a = &dns_reply[i].a;
158                 int id = (i * 3) + 1;
159
160                 memset(a, 0, sizeof(*a));
161                 a->type = __cpu_to_be16(dns_reply[i].type);
162                 a->class = __cpu_to_be16(1);
163                 a->ttl = __cpu_to_be32(announce_ttl);
164                 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
165
166                 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
167                 if (len < 1)
168                         return;
169
170                 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
171                 iov[id].iov_len = len;
172
173                 iov[id + 1].iov_base = a;
174                 iov[id + 1].iov_len = sizeof(struct dns_answer);
175
176                 iov[id + 2].iov_base = dns_reply[i].rdata;
177                 iov[id + 2].iov_len = dns_reply[i].rdlength;
178
179                 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
180         }
181
182         if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
183                 fprintf(stderr, "failed to send question\n");
184
185         for (i = 0; i < dns_answer_cnt; i++) {
186                 free(dns_reply[i].buffer);
187                 free(dns_reply[i].rdata);
188         }
189         dns_answer_cnt = 0;
190 }
191
192 static int
193 scan_name(const uint8_t *buffer, int len)
194 {
195         int offset = 0;
196
197         while (len && (*buffer != '\0')) {
198                 int l = *buffer;
199
200                 if (IS_COMPRESSED(l))
201                         return offset + 2;
202
203                 len -= l + 1;
204                 offset += l + 1;
205                 buffer += l + 1;
206         }
207
208         if (!len || !offset || (*buffer != '\0'))
209                 return -1;
210
211         return offset + 1;
212 }
213
214 struct dns_header*
215 dns_consume_header(uint8_t **data, int *len)
216 {
217         struct dns_header *h = (struct dns_header *) *data;
218         uint16_t *swap = (uint16_t *) h;
219         int endianess = 6;
220
221         if (*len < sizeof(struct dns_header))
222                 return NULL;
223
224         while (endianess--) {
225                 *swap = __be16_to_cpu(*swap);
226                 swap++;
227         }
228
229         *len -= sizeof(struct dns_header);
230         *data += sizeof(struct dns_header);
231
232         return h;
233 }
234
235 struct dns_question*
236 dns_consume_question(uint8_t **data, int *len)
237 {
238         struct dns_question *q = (struct dns_question *) *data;
239         uint16_t *swap = (uint16_t *) q;
240         int endianess = 2;
241
242         if (*len < sizeof(struct dns_question))
243                 return NULL;
244
245         while (endianess--) {
246                 *swap = __be16_to_cpu(*swap);
247                 swap++;
248         }
249
250         *len -= sizeof(struct dns_question);
251         *data += sizeof(struct dns_question);
252
253         return q;
254 }
255
256 struct dns_answer*
257 dns_consume_answer(uint8_t **data, int *len)
258 {
259         struct dns_answer *a = (struct dns_answer *) *data;
260
261         if (*len < sizeof(struct dns_answer))
262                 return NULL;
263
264         a->type = __be16_to_cpu(a->type);
265         a->class = __be16_to_cpu(a->class);
266         a->ttl = __be32_to_cpu(a->ttl);
267         a->rdlength = __be16_to_cpu(a->rdlength);
268
269         *len -= sizeof(struct dns_answer);
270         *data += sizeof(struct dns_answer);
271
272         return a;
273 }
274
275 char*
276 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
277 {
278         int nlen = scan_name(*data, *len);
279
280         if (nlen < 1)
281                 return NULL;
282
283         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
284                 perror("dns_consume_name/dn_expand");
285                 return NULL;
286         }
287
288         *len -= nlen;
289         *data += nlen;
290
291         return name_buffer;
292 }