]> git.meshlink.io Git - meshlink/blob - src/mdns.c
Send and receive multicast mDNS packets.
[meshlink] / src / mdns.c
1 // SPDX-FileCopyrightText: 2020 Guus Sliepen <guus@meshlink.io>
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "system.h"
5
6 #include <stddef.h>
7
8 #include "mdns.h"
9 #include "xalloc.h"
10
11 // Creating a buffer
12
13 typedef struct {
14         uint8_t *ptr;
15         ptrdiff_t len;
16 } buf_t;
17
18 static void buf_add(buf_t *buf, const void *data, uint32_t len) {
19         if(buf->len >= len) {
20                 memcpy(buf->ptr, data, len);
21                 buf->ptr += len;
22                 buf->len -= len;
23         } else {
24                 buf->len = -1;
25         }
26 }
27
28 static void buf_add_uint8(buf_t *buf, uint8_t val) {
29         if(buf->len >= 1) {
30                 buf->ptr[0] = val;
31                 buf->ptr++;
32                 buf->len--;
33         } else {
34                 buf->len = -1;
35         }
36 }
37
38 static void buf_add_uint16(buf_t *buf, uint16_t val) {
39         uint16_t nval = htons(val);
40         buf_add(buf, &nval, sizeof(nval));
41 }
42
43 static void buf_add_uint32(buf_t *buf, uint32_t val) {
44         uint32_t nval = htonl(val);
45         buf_add(buf, &nval, sizeof(nval));
46 }
47
48 static void buf_add_label(buf_t *buf, const char *str) {
49         size_t len = strlen(str);
50
51         if(len < 256) {
52                 buf_add_uint8(buf, len);
53                 buf_add(buf, str, len);
54         } else {
55                 buf->len = -1;
56         }
57 }
58
59 static void buf_add_ulabel(buf_t *buf, const char *str) {
60         size_t len = strlen(str);
61
62         if(len + 1 < 256) {
63                 buf_add_uint8(buf, len + 1);
64                 buf_add_uint8(buf, '_');
65                 buf_add(buf, str, len);
66         } else {
67                 buf->len = -1;
68         }
69 }
70
71 static void buf_add_kvp(buf_t *buf, const char *key, const char *val) {
72         size_t key_len = strlen(key);
73         size_t val_len = strlen(val);
74
75         if(key_len + val_len + 1 < 256) {
76                 buf_add_uint8(buf, key_len + val_len + 1);
77                 buf_add(buf, key, key_len);
78                 buf_add_uint8(buf, '=');
79                 buf_add(buf, val, val_len);
80         } else {
81                 buf->len = -1;
82         }
83 }
84
85 static uint8_t *buf_len_start(buf_t *buf) {
86         if(buf->len < 2) {
87                 buf->len = -1;
88                 return NULL;
89         } else {
90                 uint8_t *ptr = buf->ptr;
91                 buf->ptr += 2;
92                 buf->len -= 2;
93                 return ptr;
94         }
95 }
96
97 static void buf_len_end(buf_t *buf, uint8_t *ptr) {
98         if(buf->len < 0) {
99                 return;
100         }
101
102         uint16_t len = htons(buf->ptr - ptr - 2);
103         memcpy(ptr, &len, sizeof(len));
104 }
105
106 // Functions reading a buffer
107
108 typedef struct {
109         const uint8_t *ptr;
110         ptrdiff_t len;
111 } cbuf_t;
112
113 static void buf_check(cbuf_t *buf, const void *data, uint32_t len) {
114         if(buf->len >= len && !memcmp(buf->ptr, data, len)) {
115                 buf->ptr += len;
116                 buf->len -= len;
117         } else {
118                 buf->len = -1;
119         }
120 }
121
122 static void buf_check_uint8(cbuf_t *buf, uint8_t val) {
123         if(buf->len >= 1 && buf->ptr[0] == val) {
124                 buf->ptr++;
125                 buf->len--;
126         } else {
127                 buf->len = -1;
128         }
129 }
130
131 static void buf_check_uint16(cbuf_t *buf, uint16_t val) {
132         uint16_t nval = htons(val);
133         buf_check(buf, &nval, sizeof(nval));
134 }
135
136 static uint16_t buf_get_uint16(cbuf_t *buf) {
137         uint16_t nval;
138
139         if(buf->len >= 2) {
140                 memcpy(&nval, buf->ptr, 2);
141                 buf->ptr += 2;
142                 buf->len -= 2;
143                 return ntohs(nval);
144         } else {
145                 buf->len = -1;
146                 return 0;
147         }
148 }
149
150 static void buf_check_uint32(cbuf_t *buf, uint32_t val) {
151         uint32_t nval = htonl(val);
152         buf_check(buf, &nval, sizeof(nval));
153 }
154
155 static void buf_check_label(cbuf_t *buf, const char *str) {
156         size_t len = strlen(str);
157
158         if(len < 256) {
159                 buf_check_uint8(buf, len);
160                 buf_check(buf, str, len);
161         } else {
162                 buf->len = -1;
163         }
164 }
165
166 static char *buf_get_label(cbuf_t *buf) {
167         if(buf->len < 1) {
168                 buf->len = -1;
169                 return NULL;
170         }
171
172         uint8_t len = buf->ptr[0];
173         buf->ptr++;
174         buf->len--;
175
176         if(buf->len < len) {
177                 buf->len = -1;
178                 return NULL;
179         }
180
181         char *label = xmalloc(len + 1);
182         memcpy(label, buf->ptr, len);
183         label[len] = 0;
184         buf->ptr += len;
185         buf->len -= len;
186         return label;
187 }
188
189 static void buf_check_ulabel(cbuf_t *buf, const char *str) {
190         size_t len = strlen(str);
191
192         if(len + 1 < 256) {
193                 buf_check_uint8(buf, len + 1);
194                 buf_check_uint8(buf, '_');
195                 buf_check(buf, str, len);
196         } else {
197                 buf->len = -1;
198         }
199 }
200
201 static void buf_get_kvp(cbuf_t *buf, const char *key, char **val) {
202         char *kvp = buf_get_label(buf);
203
204         if(buf->len == -1) {
205                 return;
206         }
207
208         char *split = strchr(kvp, '=');
209
210         if(!split) {
211                 buf->len = -1;
212                 return;
213         }
214
215         *split++ = 0;
216
217         if(strcmp(kvp, key)) {
218                 buf->len = -1;
219                 return;
220         }
221
222         memmove(kvp, split, strlen(split) + 1);
223         *val = kvp;
224 }
225
226 static const uint8_t *buf_check_len_start(cbuf_t *buf) {
227         if(buf->len < 2) {
228                 buf->len = -1;
229                 return NULL;
230         } else {
231                 const uint8_t *ptr = buf->ptr;
232                 buf->ptr += 2;
233                 buf->len -= 2;
234                 return ptr;
235         }
236 }
237
238 static void buf_check_len_end(cbuf_t *buf, const uint8_t *ptr) {
239         if(buf->len < 0) {
240                 return;
241         }
242
243         uint16_t len = htons(buf->ptr - ptr - 2);
244
245         if(memcmp(ptr, &len, sizeof(len))) {
246                 buf->len = -1;
247         }
248 }
249
250 size_t prepare_packet(void *vdata, size_t size, const char *name, const char *protocol, const char *transport, uint16_t port, int nkeys, const char **keys, const char **values) {
251         // Create the request/response packet right now
252         uint8_t *data = vdata;
253         buf_t buf = {data, size};
254
255         // Header
256         buf_add_uint16(&buf, 0); // TX ID
257         buf_add_uint16(&buf, 0); // flags
258         buf_add_uint16(&buf, 1); // 1 question
259         buf_add_uint16(&buf, 1); // 1 answer RR
260         buf_add_uint16(&buf, 0); // 0 authority RRs
261         buf_add_uint16(&buf, 2); // 1 additional RR
262
263         // Question section: _protocol._transport.local PTR IN
264         buf_add_ulabel(&buf, protocol);
265         buf_add_ulabel(&buf, transport);
266         uint16_t local = buf.ptr - data;
267         buf_add_label(&buf, "local");
268         buf_add_uint8(&buf, 0);
269         buf_add_uint16(&buf, 0xc); // PTR
270         buf_add_uint16(&buf, 0x1); // IN
271
272         // Answer section: _protocol._transport local PTR IN 3600 name._protocol._transport
273         buf_add_uint16(&buf, 0xc00c); // _protocol._transport.local
274         buf_add_uint16(&buf, 0xc); // PTR
275         buf_add_uint16(&buf, 0x1); // IN
276         buf_add_uint32(&buf, 3600); // TTL
277         uint8_t *len_ptr = buf_len_start(&buf);
278         uint16_t full_name = buf.ptr - data; // remember start of full name
279         buf_add_label(&buf, name);
280         buf_add_uint16(&buf, 0xc00c); // _protocol._transport.local
281         buf_len_end(&buf, len_ptr);
282
283         // Add the SRV record: name._protocol._transport.local SRV IN 120 0 0 port name.local
284         buf_add_uint16(&buf, 0xc000 | full_name);
285         buf_add_uint16(&buf, 0x21); // SRV
286         buf_add_uint16(&buf, 0x1); // IN
287         buf_add_uint32(&buf, 120); // TTL
288         len_ptr = buf_len_start(&buf);
289         buf_add_uint16(&buf, 0); // priority
290         buf_add_uint16(&buf, 0); // weight
291         buf_add_uint16(&buf, port); // port
292         buf_add_label(&buf, name);
293         buf_add_uint16(&buf, 0xc000 | local);
294         buf_len_end(&buf, len_ptr);
295
296         // Add the TXT records: name._protocol._transport.local TXT IN 3600 key=value...
297         buf_add_uint16(&buf, 0xc000 | full_name);
298         buf_add_uint16(&buf, 0x10); // TXT
299         buf_add_uint16(&buf, 0x1); // IN
300         buf_add_uint32(&buf, 3600); // TTL
301         len_ptr = buf_len_start(&buf);
302
303         for(int i = 0; i < nkeys; i++) {
304                 buf_add_kvp(&buf, keys[i], values[i]);
305         }
306
307         buf_len_end(&buf, len_ptr);
308
309         // Done.
310         if(buf.len < 0) {
311                 return 0;
312         } else {
313                 return buf.ptr - data;
314         }
315 }
316
317 bool parse_packet(const void *vdata, size_t size, char **name, const char *protocol, const char *transport, uint16_t *port, int nkeys, const char **keys, char **values) {
318         const uint8_t *data = vdata;
319         cbuf_t buf = {data, size};
320
321         // Header
322         buf_check_uint16(&buf, 0); // TX ID
323         buf_check_uint16(&buf, 0); // flags
324         buf_check_uint16(&buf, 1); // 1 question
325         buf_check_uint16(&buf, 1); // 1 answer RR
326         buf_check_uint16(&buf, 0); // 0 authority RRs
327         buf_check_uint16(&buf, 2); // 1 checkitional RR
328
329         if(buf.len == -1) {
330                 return false;
331         }
332
333         // Question section: _protocol._transport.local PTR IN
334         buf_check_ulabel(&buf, protocol);
335         buf_check_ulabel(&buf, transport);
336         uint16_t local = buf.ptr - data;
337         buf_check_label(&buf, "local");
338         buf_check_uint8(&buf, 0);
339         buf_check_uint16(&buf, 0xc); // PTR
340         buf_check_uint16(&buf, 0x1); // IN
341
342         if(buf.len == -1) {
343                 return false;
344         }
345
346         // Answer section: _protocol._transport local PTR IN 3600 name._protocol._transport
347         buf_check_uint16(&buf, 0xc00c); // _protocol._transport.local
348         buf_check_uint16(&buf, 0xc); // PTR
349         buf_check_uint16(&buf, 0x1); // IN
350         buf_check_uint32(&buf, 3600); // TTL
351         const uint8_t *len_ptr = buf_check_len_start(&buf);
352         uint16_t full_name = buf.ptr - data; // remember start of full name
353         *name = buf_get_label(&buf);
354         buf_check_uint16(&buf, 0xc00c); // _protocol._transport.local
355         buf_check_len_end(&buf, len_ptr);
356
357         if(buf.len == -1) {
358                 return false;
359         }
360
361         // Check the SRV record: name._protocol._transport.local SRV IN 120 0 0 port name.local
362         buf_check_uint16(&buf, 0xc000 | full_name);
363         buf_check_uint16(&buf, 0x21); // SRV
364         buf_check_uint16(&buf, 0x1); // IN
365         buf_check_uint32(&buf, 120); // TTL
366         len_ptr = buf_check_len_start(&buf);
367         buf_check_uint16(&buf, 0); // priority
368         buf_check_uint16(&buf, 0); // weight
369         *port = buf_get_uint16(&buf); // port
370         buf_check_label(&buf, *name);
371         buf_check_uint16(&buf, 0xc000 | local);
372         buf_check_len_end(&buf, len_ptr);
373
374         if(buf.len == -1) {
375                 return false;
376         }
377
378         // Check the TXT records: name._protocol._transport.local TXT IN 3600 key=value...
379         buf_check_uint16(&buf, 0xc000 | full_name);
380         buf_check_uint16(&buf, 0x10); // TXT
381         buf_check_uint16(&buf, 0x1); // IN
382         buf_check_uint32(&buf, 3600); // TTL
383         len_ptr = buf_check_len_start(&buf);
384
385         for(int i = 0; i < nkeys; i++) {
386                 buf_get_kvp(&buf, keys[i], &values[i]);
387         }
388
389         buf_check_len_end(&buf, len_ptr);
390
391         // Done.
392         return buf.len == 0;
393 }