1 // SPDX-FileCopyrightText: 2020 Guus Sliepen <guus@meshlink.io>
2 // SPDX-License-Identifier: GPL-2.0-or-later
18 static void buf_add(buf_t *buf, const void *data, uint32_t len) {
20 memcpy(buf->ptr, data, len);
28 static void buf_add_uint8(buf_t *buf, uint8_t val) {
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));
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));
48 static void buf_add_label(buf_t *buf, const char *str) {
49 size_t len = strlen(str);
52 buf_add_uint8(buf, len);
53 buf_add(buf, str, len);
59 static void buf_add_ulabel(buf_t *buf, const char *str) {
60 size_t len = strlen(str);
63 buf_add_uint8(buf, len + 1);
64 buf_add_uint8(buf, '_');
65 buf_add(buf, str, len);
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);
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);
85 static uint8_t *buf_len_start(buf_t *buf) {
90 uint8_t *ptr = buf->ptr;
97 static void buf_len_end(buf_t *buf, uint8_t *ptr) {
102 uint16_t len = htons(buf->ptr - ptr - 2);
103 memcpy(ptr, &len, sizeof(len));
106 // Functions reading a buffer
113 static void buf_check(cbuf_t *buf, const void *data, uint32_t len) {
114 if(buf->len >= len && !memcmp(buf->ptr, data, len)) {
122 static void buf_check_uint8(cbuf_t *buf, uint8_t val) {
123 if(buf->len >= 1 && buf->ptr[0] == val) {
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));
136 static uint16_t buf_get_uint16(cbuf_t *buf) {
140 memcpy(&nval, buf->ptr, 2);
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));
155 static void buf_check_label(cbuf_t *buf, const char *str) {
156 size_t len = strlen(str);
159 buf_check_uint8(buf, len);
160 buf_check(buf, str, len);
166 static char *buf_get_label(cbuf_t *buf) {
172 uint8_t len = buf->ptr[0];
181 char *label = xmalloc(len + 1);
182 memcpy(label, buf->ptr, len);
189 static void buf_check_ulabel(cbuf_t *buf, const char *str) {
190 size_t len = strlen(str);
193 buf_check_uint8(buf, len + 1);
194 buf_check_uint8(buf, '_');
195 buf_check(buf, str, len);
201 static void buf_get_kvp(cbuf_t *buf, const char *key, char **val) {
202 char *kvp = buf_get_label(buf);
208 char *split = strchr(kvp, '=');
217 if(strcmp(kvp, key)) {
222 memmove(kvp, split, strlen(split) + 1);
226 static const uint8_t *buf_check_len_start(cbuf_t *buf) {
231 const uint8_t *ptr = buf->ptr;
238 static void buf_check_len_end(cbuf_t *buf, const uint8_t *ptr) {
243 uint16_t len = htons(buf->ptr - ptr - 2);
245 if(memcmp(ptr, &len, sizeof(len))) {
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, bool response) {
251 // Create the request/response packet right now
252 uint8_t *data = vdata;
253 buf_t buf = {data, size};
256 buf_add_uint16(&buf, 0); // TX ID
257 buf_add_uint16(&buf, response ? ntohs(1 << 15) : 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
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
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);
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);
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);
303 for(int i = 0; i < nkeys; i++) {
304 buf_add_kvp(&buf, keys[i], values[i]);
307 buf_len_end(&buf, len_ptr);
313 return buf.ptr - data;
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, bool *response) {
318 const uint8_t *data = vdata;
319 cbuf_t buf = {data, size};
322 buf_check_uint16(&buf, 0); // TX ID
323 uint16_t flags = buf_get_uint16(&buf); // 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
329 if(buf.len == -1 || flags & ~ntohs(1 << 15)) {
333 *response = flags & ntohs(1 << 15);
335 // Question section: _protocol._transport.local PTR IN
336 buf_check_ulabel(&buf, protocol);
337 buf_check_ulabel(&buf, transport);
338 uint16_t local = buf.ptr - data;
339 buf_check_label(&buf, "local");
340 buf_check_uint8(&buf, 0);
341 buf_check_uint16(&buf, 0xc); // PTR
342 buf_check_uint16(&buf, 0x1); // IN
348 // Answer section: _protocol._transport local PTR IN 3600 name._protocol._transport
349 buf_check_uint16(&buf, 0xc00c); // _protocol._transport.local
350 buf_check_uint16(&buf, 0xc); // PTR
351 buf_check_uint16(&buf, 0x1); // IN
352 buf_check_uint32(&buf, 3600); // TTL
353 const uint8_t *len_ptr = buf_check_len_start(&buf);
354 uint16_t full_name = buf.ptr - data; // remember start of full name
355 *name = buf_get_label(&buf);
356 buf_check_uint16(&buf, 0xc00c); // _protocol._transport.local
357 buf_check_len_end(&buf, len_ptr);
363 // Check the SRV record: name._protocol._transport.local SRV IN 120 0 0 port name.local
364 buf_check_uint16(&buf, 0xc000 | full_name);
365 buf_check_uint16(&buf, 0x21); // SRV
366 buf_check_uint16(&buf, 0x1); // IN
367 buf_check_uint32(&buf, 120); // TTL
368 len_ptr = buf_check_len_start(&buf);
369 buf_check_uint16(&buf, 0); // priority
370 buf_check_uint16(&buf, 0); // weight
371 *port = buf_get_uint16(&buf); // port
372 buf_check_label(&buf, *name);
373 buf_check_uint16(&buf, 0xc000 | local);
374 buf_check_len_end(&buf, len_ptr);
380 // Check the TXT records: name._protocol._transport.local TXT IN 3600 key=value...
381 buf_check_uint16(&buf, 0xc000 | full_name);
382 buf_check_uint16(&buf, 0x10); // TXT
383 buf_check_uint16(&buf, 0x1); // IN
384 buf_check_uint32(&buf, 3600); // TTL
385 len_ptr = buf_check_len_start(&buf);
387 for(int i = 0; i < nkeys; i++) {
388 buf_get_kvp(&buf, keys[i], &values[i]);
391 buf_check_len_end(&buf, len_ptr);