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