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