-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathmdns.c
7816 lines (7170 loc) · 265 KB
/
mdns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_event.h"
#include "mdns.h"
#include "mdns_private.h"
#include "mdns_networking.h"
#include "esp_log.h"
#include "esp_random.h"
#include "esp_check.h"
static void _mdns_browse_item_free(mdns_browse_t *browse);
static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse);
static esp_err_t _mdns_sync_browse_action(mdns_action_type_t type, mdns_browse_sync_t *browse_sync);
static void _mdns_browse_sync(mdns_browse_sync_t *browse_sync);
static void _mdns_browse_finish(mdns_browse_t *browse);
static void _mdns_browse_add(mdns_browse_t *browse);
static void _mdns_browse_send(mdns_browse_t *browse, mdns_if_t interface);
#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
#include "esp_eth.h"
#endif
#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0)
#define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED
#else
#define MDNS_ESP_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED
#endif
#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP)
#include "esp_wifi.h"
#endif
#ifdef MDNS_ENABLE_DEBUG
void mdns_debug_packet(const uint8_t *data, size_t len);
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#endif
// Internal size of IPv6 address is defined here as size of AAAA record in mdns packet
// since the ip6_addr_t is defined in lwip and depends on using IPv6 zones
#define _MDNS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE)
static const char *MDNS_DEFAULT_DOMAIN = "local";
static const char *MDNS_SUB_STR = "_sub";
mdns_server_t *_mdns_server = NULL;
static mdns_host_item_t *_mdns_host_list = NULL;
static mdns_host_item_t _mdns_self_host;
static const char *TAG = "mdns";
static volatile TaskHandle_t _mdns_service_task_handle = NULL;
static SemaphoreHandle_t _mdns_service_semaphore = NULL;
static void _mdns_search_finish_done(void);
static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *search, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static void _mdns_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto,
uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
static void _mdns_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto,
mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
#ifdef MDNS_ENABLE_DEBUG
static void debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t);
static void debug_printf_browse_result_all(mdns_result_t *r_t);
#endif // MDNS_ENABLE_DEBUG
static void _mdns_search_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
uint32_t ttl);
static mdns_result_t *_mdns_search_result_add_ptr(mdns_search_once_t *search, const char *instance,
const char *service_type, const char *proto, mdns_if_t tcpip_if,
mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static bool _mdns_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], size_t services_len, bool flush, bool bye);
static bool _mdns_append_host_list(mdns_out_answer_t **destination, bool flush, bool bye);
static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname);
static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action);
static void _mdns_query_results_free(mdns_result_t *results);
typedef enum {
MDNS_IF_STA = 0,
MDNS_IF_AP = 1,
MDNS_IF_ETH = 2,
} mdns_predef_if_t;
typedef struct mdns_interfaces mdns_interfaces_t;
struct mdns_interfaces {
const bool predefined;
esp_netif_t *netif;
const mdns_predef_if_t predef_if;
mdns_if_t duplicate;
};
/*
* @brief Internal collection of mdns supported interfaces
*
*/
static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = {
#if CONFIG_MDNS_PREDEF_NETIF_STA
{ .predefined = true, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_MAX_INTERFACES },
#endif
#if CONFIG_MDNS_PREDEF_NETIF_AP
{ .predefined = true, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_MAX_INTERFACES },
#endif
#if CONFIG_MDNS_PREDEF_NETIF_ETH
{ .predefined = true, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_MAX_INTERFACES },
#endif
};
/**
* @brief Convert Predefined interface to the netif id from the internal netif list
* @param predef_if Predefined interface enum
* @return Ordinal number of internal list of mdns network interface.
* Returns MDNS_MAX_INTERFACES if the predefined interface wasn't found in the list
*/
static mdns_if_t mdns_if_from_preset_if(mdns_predef_if_t predef_if)
{
for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) {
return i;
}
}
return MDNS_MAX_INTERFACES;
}
/**
* @brief Convert Predefined interface to esp-netif handle
* @param predef_if Predefined interface enum
* @return esp_netif pointer from system list of network interfaces
*/
static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t predef_if)
{
switch (predef_if) {
case MDNS_IF_STA:
return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
case MDNS_IF_AP:
return esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
case MDNS_IF_ETH:
return esp_netif_get_handle_from_ifkey("ETH_DEF");
#endif
default:
return NULL;
}
}
/**
* @brief Gets the actual esp_netif pointer from the internal network interface list
*
* The supplied ordinal number could
* - point to a predef netif -> "STA", "AP", "ETH"
* - if no entry in the list (NULL) -> check if the system added this netif
* - point to a custom netif -> just return the entry in the list
* - users is responsible for the lifetime of this netif (to be valid between mdns-init -> deinit)
*
* @param tcpip_if Ordinal number of the interface
* @return Pointer ot the esp_netif object if the interface is available, NULL otherwise
*/
esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if)
{
if (tcpip_if < MDNS_MAX_INTERFACES) {
if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) {
// If the local copy is NULL and this netif is predefined -> we can find it in the global netif list
s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if);
// failing to find it means that the netif is *not* available -> return NULL
}
return s_esp_netifs[tcpip_if].netif;
}
return NULL;
}
/*
* @brief Clean internal mdns interface's pointer
*/
static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if)
{
if (tcpip_if < MDNS_MAX_INTERFACES) {
s_esp_netifs[tcpip_if].netif = NULL;
}
}
/*
* @brief Convert esp-netif handle to mdns if
*/
static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif)
{
for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
// The predefined netifs in the static array are NULL when firstly calling this function
// if IPv4 is disabled. Set these netifs here.
if (s_esp_netifs[i].netif == NULL && s_esp_netifs[i].predefined) {
s_esp_netifs[i].netif = esp_netif_from_preset_if(s_esp_netifs[i].predef_if);
}
if (esp_netif == s_esp_netifs[i].netif) {
return i;
}
}
return MDNS_MAX_INTERFACES;
}
static inline bool _str_null_or_empty(const char *str)
{
return (str == NULL || *str == 0);
}
/*
* @brief Appends/increments a number to name/instance in case of collision
* */
static char *_mdns_mangle_name(char *in)
{
char *p = strrchr(in, '-');
int suffix = 0;
if (p == NULL) {
//No - in ``in``
suffix = 2;
} else {
char *endp = NULL;
suffix = strtol(p + 1, &endp, 10);
if (*endp != 0) {
//suffix is not numerical
suffix = 2;
p = NULL; //so we append -suffix to the entire string
}
}
char *ret;
if (p == NULL) {
//need to add -2 to string
ret = malloc(strlen(in) + 3);
if (ret == NULL) {
HOOK_MALLOC_FAILED;
return NULL;
}
sprintf(ret, "%s-2", in);
} else {
ret = malloc(strlen(in) + 2); //one extra byte in case 9-10 or 99-100 etc
if (ret == NULL) {
HOOK_MALLOC_FAILED;
return NULL;
}
strcpy(ret, in);
int baseLen = p - in; //length of 'bla' in 'bla-123'
//overwrite suffix with new suffix
sprintf(ret + baseLen, "-%d", suffix + 1);
}
return ret;
}
static bool _mdns_service_match(const mdns_service_t *srv, const char *service, const char *proto,
const char *hostname)
{
if (!service || !proto || !srv->hostname) {
return false;
}
return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
(_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
}
/**
* @brief finds service from given service type
* @param server the server
* @param service service type to match
* @param proto proto to match
* @param hostname hostname of the service (if non-null)
*
* @return the service item if found or NULL on error
*/
static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname)
{
mdns_srv_item_t *s = _mdns_server->services;
while (s) {
if (_mdns_service_match(s->service, service, proto, hostname)) {
return s;
}
s = s->next;
}
return NULL;
}
static mdns_srv_item_t *_mdns_get_service_item_subtype(const char *subtype, const char *service, const char *proto)
{
mdns_srv_item_t *s = _mdns_server->services;
while (s) {
if (_mdns_service_match(s->service, service, proto, NULL)) {
mdns_subtype_t *subtype_item = s->service->subtype;
while (subtype_item) {
if (!strcasecmp(subtype_item->subtype, subtype)) {
return s;
}
subtype_item = subtype_item->next;
}
}
s = s->next;
}
return NULL;
}
static mdns_host_item_t *mdns_get_host_item(const char *hostname)
{
if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
return &_mdns_self_host;
}
mdns_host_item_t *host = _mdns_host_list;
while (host != NULL) {
if (strcasecmp(host->hostname, hostname) == 0) {
return host;
}
host = host->next;
}
return NULL;
}
static bool _mdns_can_add_more_services(void)
{
mdns_srv_item_t *s = _mdns_server->services;
uint16_t service_num = 0;
while (s) {
service_num ++;
s = s->next;
if (service_num >= MDNS_MAX_SERVICES) {
return false;
}
}
return true;
}
esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet)
{
mdns_action_t *action = NULL;
action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
if (!action) {
HOOK_MALLOC_FAILED;
return ESP_ERR_NO_MEM;
}
action->type = ACTION_RX_HANDLE;
action->data.rx_handle.packet = packet;
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
free(action);
return ESP_ERR_NO_MEM;
}
return ESP_OK;
}
static const char *_mdns_get_default_instance_name(void)
{
if (_mdns_server && !_str_null_or_empty(_mdns_server->instance)) {
return _mdns_server->instance;
}
if (_mdns_server && !_str_null_or_empty(_mdns_server->hostname)) {
return _mdns_server->hostname;
}
return NULL;
}
/**
* @brief Get the service name of a service
*/
static const char *_mdns_get_service_instance_name(const mdns_service_t *service)
{
if (service && !_str_null_or_empty(service->instance)) {
return service->instance;
}
return _mdns_get_default_instance_name();
}
static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
{
if (lhs == NULL) {
lhs = _mdns_get_default_instance_name();
}
if (rhs == NULL) {
rhs = _mdns_get_default_instance_name();
}
return !strcasecmp(lhs, rhs);
}
static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
const char *proto, const char *hostname)
{
// service and proto must be supplied, if not this instance won't match
if (!service || !proto) {
return false;
}
// instance==NULL -> _mdns_instance_name_match() will check the default instance
// hostname==NULL -> matches if instance, service and proto matches
return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
!strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
}
static mdns_srv_item_t *_mdns_get_service_item_instance(const char *instance, const char *service, const char *proto,
const char *hostname)
{
mdns_srv_item_t *s = _mdns_server->services;
while (s) {
if (instance) {
if (_mdns_service_match_instance(s->service, instance, service, proto, hostname)) {
return s;
}
} else {
if (_mdns_service_match(s->service, service, proto, hostname)) {
return s;
}
}
s = s->next;
}
return NULL;
}
/**
* @brief reads MDNS FQDN into mdns_name_t structure
* FQDN is in format: [hostname.|[instance.]_service._proto.]local.
*
* @param packet MDNS packet
* @param start Starting point of FQDN
* @param name mdns_name_t structure to populate
* @param buf temporary char buffer
*
* @return the address after the parsed FQDN in the packet or NULL on error
*/
static const uint8_t *_mdns_read_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, char *buf, size_t packet_len)
{
size_t index = 0;
const uint8_t *packet_end = packet + packet_len;
while (start + index < packet_end && start[index]) {
if (name->parts == 4) {
name->invalid = true;
}
uint8_t len = start[index++];
if (len < 0xC0) {
if (len > 63) {
//length can not be more than 63
return NULL;
}
uint8_t i;
for (i = 0; i < len; i++) {
if (start + index >= packet_end) {
return NULL;
}
buf[i] = start[index++];
}
buf[len] = '\0';
if (name->parts == 1 && buf[0] != '_'
&& (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0)
&& (strcasecmp(buf, "arpa") != 0)
#ifndef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
&& (strcasecmp(buf, "ip6") != 0)
&& (strcasecmp(buf, "in-addr") != 0)
#endif
) {
strlcat(name->host, ".", sizeof(name->host));
strlcat(name->host, buf, sizeof(name->host));
} else if (strcasecmp(buf, MDNS_SUB_STR) == 0) {
name->sub = 1;
} else if (!name->invalid) {
char *mdns_name_ptrs[] = {name->host, name->service, name->proto, name->domain};
memcpy(mdns_name_ptrs[name->parts++], buf, len + 1);
}
} else {
size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++];
if ((packet + address) >= start) {
//reference address can not be after where we are
return NULL;
}
if (_mdns_read_fqdn(packet, packet + address, name, buf, packet_len)) {
return start + index;
}
return NULL;
}
}
return start + index + 1;
}
/**
* @brief sets uint16_t value in a packet
*
* @param packet MDNS packet
* @param index offset of uint16_t value
* @param value the value to set
*/
static inline void _mdns_set_u16(uint8_t *packet, uint16_t index, uint16_t value)
{
if ((index + 1) >= MDNS_MAX_PACKET_SIZE) {
return;
}
packet[index] = (value >> 8) & 0xFF;
packet[index + 1] = value & 0xFF;
}
/**
* @brief appends byte in a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param value the value to set
*
* @return length of added data: 0 on error or 1 on success
*/
static inline uint8_t _mdns_append_u8(uint8_t *packet, uint16_t *index, uint8_t value)
{
if (*index >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
packet[*index] = value;
*index += 1;
return 1;
}
/**
* @brief appends uint16_t in a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param value the value to set
*
* @return length of added data: 0 on error or 2 on success
*/
static inline uint8_t _mdns_append_u16(uint8_t *packet, uint16_t *index, uint16_t value)
{
if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
_mdns_append_u8(packet, index, (value >> 8) & 0xFF);
_mdns_append_u8(packet, index, value & 0xFF);
return 2;
}
/**
* @brief appends uint32_t in a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param value the value to set
*
* @return length of added data: 0 on error or 4 on success
*/
static inline uint8_t _mdns_append_u32(uint8_t *packet, uint16_t *index, uint32_t value)
{
if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
_mdns_append_u8(packet, index, (value >> 24) & 0xFF);
_mdns_append_u8(packet, index, (value >> 16) & 0xFF);
_mdns_append_u8(packet, index, (value >> 8) & 0xFF);
_mdns_append_u8(packet, index, value & 0xFF);
return 4;
}
/**
* @brief appends answer type, class, ttl and data length to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param type answer type
* @param ttl answer ttl
*
* @return length of added data: 0 on error or 10 on success
*/
static inline uint8_t _mdns_append_type(uint8_t *packet, uint16_t *index, uint8_t type, bool flush, uint32_t ttl)
{
if ((*index + 10) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
uint16_t mdns_class = MDNS_CLASS_IN;
if (flush) {
mdns_class = MDNS_CLASS_IN_FLUSH_CACHE;
}
if (type == MDNS_ANSWER_PTR) {
_mdns_append_u16(packet, index, MDNS_TYPE_PTR);
_mdns_append_u16(packet, index, mdns_class);
} else if (type == MDNS_ANSWER_TXT) {
_mdns_append_u16(packet, index, MDNS_TYPE_TXT);
_mdns_append_u16(packet, index, mdns_class);
} else if (type == MDNS_ANSWER_SRV) {
_mdns_append_u16(packet, index, MDNS_TYPE_SRV);
_mdns_append_u16(packet, index, mdns_class);
} else if (type == MDNS_ANSWER_A) {
_mdns_append_u16(packet, index, MDNS_TYPE_A);
_mdns_append_u16(packet, index, mdns_class);
} else if (type == MDNS_ANSWER_AAAA) {
_mdns_append_u16(packet, index, MDNS_TYPE_AAAA);
_mdns_append_u16(packet, index, mdns_class);
} else {
return 0;
}
_mdns_append_u32(packet, index, ttl);
_mdns_append_u16(packet, index, 0);
return 10;
}
static inline uint8_t _mdns_append_string_with_len(uint8_t *packet, uint16_t *index, const char *string, uint8_t len)
{
if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
_mdns_append_u8(packet, index, len);
memcpy(packet + *index, string, len);
*index += len;
return len + 1;
}
/**
* @brief appends single string to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param string the string to append
*
* @return length of added data: 0 on error or length of the string + 1 on success
*/
static inline uint8_t _mdns_append_string(uint8_t *packet, uint16_t *index, const char *string)
{
uint8_t len = strlen(string);
if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
_mdns_append_u8(packet, index, len);
memcpy(packet + *index, string, len);
*index += len;
return len + 1;
}
/**
* @brief appends one TXT record ("key=value" or "key")
*
* @param packet MDNS packet
* @param index offset in the packet
* @param txt one txt record
*
* @return length of added data: length of the added txt value + 1 on success
* 0 if data won't fit the packet
* -1 if invalid TXT entry
*/
static inline int append_one_txt_record_entry(uint8_t *packet, uint16_t *index, mdns_txt_linked_item_t *txt)
{
if (txt == NULL || txt->key == NULL) {
return -1;
}
size_t key_len = strlen(txt->key);
size_t len = key_len + txt->value_len + (txt->value ? 1 : 0);
if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
_mdns_append_u8(packet, index, len);
memcpy(packet + *index, txt->key, key_len);
if (txt->value) {
packet[*index + key_len] = '=';
memcpy(packet + *index + key_len + 1, txt->value, txt->value_len);
}
*index += len;
return len + 1;
}
#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
static inline int append_single_str(uint8_t *packet, uint16_t *index, const char *str, int len)
{
if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
return 0;
}
if (!_mdns_append_u8(packet, index, len)) {
return 0;
}
memcpy(packet + *index, str, len);
*index += len;
return *index;
}
/**
* @brief appends FQDN to a packet from hostname separated by dots. This API works the same way as
* _mdns_append_fqdn(), but refrains from DNS compression (as it's mainly used for IP addresses (many short items),
* where we gain very little (or compression even gets counter-productive mainly for IPv6 addresses)
*
* @param packet MDNS packet
* @param index offset in the packet
* @param name name representing FQDN in '.' separated parts
* @param last true if appending the last part (domain, typically "arpa")
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t append_fqdn_dots(uint8_t *packet, uint16_t *index, const char *name, bool last)
{
int len = strlen(name);
char *host = (char *)name;
char *end = host;
char *start = host;
do {
end = memchr(start, '.', host + len - start);
end = end ? end : host + len;
int part_len = end - start;
if (!append_single_str(packet, index, start, part_len)) {
return 0;
}
start = ++end;
} while (end < name + len);
if (!append_single_str(packet, index, "arpa", sizeof("arpa") - 1)) {
return 0;
}
//empty string so terminate
if (!_mdns_append_u8(packet, index, 0)) {
return 0;
}
return *index;
}
#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
/**
* @brief appends FQDN to a packet, incrementing the index and
* compressing the output if previous occurrence of the string (or part of it) has been found
*
* @param packet MDNS packet
* @param index offset in the packet
* @param strings string array containing the parts of the FQDN
* @param count number of strings in the array
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t _mdns_append_fqdn(uint8_t *packet, uint16_t *index, const char *strings[], uint8_t count, size_t packet_len)
{
if (!count) {
//empty string so terminate
return _mdns_append_u8(packet, index, 0);
}
mdns_name_t name;
static char buf[MDNS_NAME_BUF_LEN];
uint8_t len = strlen(strings[0]);
//try to find first the string length in the packet (if it exists)
uint8_t *len_location = (uint8_t *)memchr(packet, (char)len, *index);
while (len_location) {
//check if the string after len_location is the string that we are looking for
if (memcmp(len_location + 1, strings[0], len)) { //not continuing with our string
search_next:
//try and find the length byte further in the packet
len_location = (uint8_t *)memchr(len_location + 1, (char)len, *index - (len_location + 1 - packet));
continue;
}
//seems that we might have found the string that we are looking for
//read the destination into name and compare
name.parts = 0;
name.sub = 0;
name.invalid = false;
name.host[0] = 0;
name.service[0] = 0;
name.proto[0] = 0;
name.domain[0] = 0;
const uint8_t *content = _mdns_read_fqdn(packet, len_location, &name, buf, packet_len);
if (!content) {
//not a readable fqdn?
goto search_next; // could be our unfinished fqdn, continue searching
}
if (name.parts == count) {
uint8_t i;
for (i = 0; i < count; i++) {
if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) {
//not our string! let's search more
goto search_next;
}
}
//we actually have found the string
break;
} else {
goto search_next;
}
}
//string is not yet in the packet, so let's add it
if (!len_location) {
uint8_t written = _mdns_append_string(packet, index, strings[0]);
if (!written) {
return 0;
}
//run the same for the other strings in the name
return written + _mdns_append_fqdn(packet, index, &strings[1], count - 1, packet_len);
}
//we have found the string so let's insert a pointer to it instead
uint16_t offset = len_location - packet;
offset |= MDNS_NAME_REF;
return _mdns_append_u16(packet, index, offset);
}
/**
* @brief appends PTR record for service to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param server the server that is hosting the service
* @param service the service to add record for
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t _mdns_append_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, const char *service, const char *proto, bool flush, bool bye)
{
const char *str[4];
uint16_t record_length = 0;
uint8_t part_length;
if (service == NULL) {
return 0;
}
str[0] = instance;
str[1] = service;
str[2] = proto;
str[3] = MDNS_DEFAULT_DOMAIN;
part_length = _mdns_append_fqdn(packet, index, str + 1, 3, MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
record_length += part_length;
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL);
if (!part_length) {
return 0;
}
record_length += part_length;
uint16_t data_len_location = *index - 2;
part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
_mdns_set_u16(packet, data_len_location, part_length);
record_length += part_length;
return record_length;
}
/**
* @brief appends PTR record for a subtype to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param instance the service instance name
* @param subtype the service subtype
* @param proto the service protocol
* @param flush whether to set the flush flag
* @param bye whether to set the bye flag
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t _mdns_append_subtype_ptr_record(uint8_t *packet, uint16_t *index, const char *instance,
const char *subtype, const char *service, const char *proto, bool flush,
bool bye)
{
const char *subtype_str[5] = {subtype, MDNS_SUB_STR, service, proto, MDNS_DEFAULT_DOMAIN};
const char *instance_str[4] = {instance, service, proto, MDNS_DEFAULT_DOMAIN};
uint16_t record_length = 0;
uint8_t part_length;
if (service == NULL) {
return 0;
}
part_length = _mdns_append_fqdn(packet, index, subtype_str, ARRAY_SIZE(subtype_str), MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
record_length += part_length;
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL);
if (!part_length) {
return 0;
}
record_length += part_length;
uint16_t data_len_location = *index - 2;
part_length = _mdns_append_fqdn(packet, index, instance_str, ARRAY_SIZE(instance_str), MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
_mdns_set_u16(packet, data_len_location, part_length);
record_length += part_length;
return record_length;
}
/**
* @brief appends DNS-SD PTR record for service to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param server the server that is hosting the service
* @param service the service to add record for
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t _mdns_append_sdptr_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye)
{
const char *str[3];
const char *sd_str[4];
uint16_t record_length = 0;
uint8_t part_length;
if (service == NULL) {
return 0;
}
sd_str[0] = (char *)"_services";
sd_str[1] = (char *)"_dns-sd";
sd_str[2] = (char *)"_udp";
sd_str[3] = MDNS_DEFAULT_DOMAIN;
str[0] = service->service;
str[1] = service->proto;
str[2] = MDNS_DEFAULT_DOMAIN;
part_length = _mdns_append_fqdn(packet, index, sd_str, 4, MDNS_MAX_PACKET_SIZE);
record_length += part_length;
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL);
if (!part_length) {
return 0;
}
record_length += part_length;
uint16_t data_len_location = *index - 2;
part_length = _mdns_append_fqdn(packet, index, str, 3, MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
_mdns_set_u16(packet, data_len_location, part_length);
record_length += part_length;
return record_length;
}
/**
* @brief appends TXT record for service to a packet, incrementing the index
*
* @param packet MDNS packet
* @param index offset in the packet
* @param server the server that is hosting the service
* @param service the service to add record for
*
* @return length of added data: 0 on error or length on success
*/
static uint16_t _mdns_append_txt_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye)
{
const char *str[4];
uint16_t record_length = 0;
uint8_t part_length;
if (service == NULL) {
return 0;
}
str[0] = _mdns_get_service_instance_name(service);
str[1] = service->service;
str[2] = service->proto;
str[3] = MDNS_DEFAULT_DOMAIN;
if (!str[0]) {
return 0;
}
part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE);
if (!part_length) {
return 0;
}
record_length += part_length;
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_TXT, flush, bye ? 0 : MDNS_ANSWER_TXT_TTL);
if (!part_length) {
return 0;
}
record_length += part_length;
uint16_t data_len_location = *index - 2;
uint16_t data_len = 0;
mdns_txt_linked_item_t *txt = service->txt;
while (txt) {
int l = append_one_txt_record_entry(packet, index, txt);