@@ -513,6 +513,37 @@ static inline uint8_t _mdns_append_string(uint8_t * packet, uint16_t * index, co
513
513
return len + 1 ;
514
514
}
515
515
516
+ /**
517
+ * @brief appends one TXT record ("key=value" or "key")
518
+ *
519
+ * @param packet MDNS packet
520
+ * @param index offset in the packet
521
+ * @param txt one txt record
522
+ *
523
+ * @return length of added data: length of the added txt value + 1 on success
524
+ * 0 if data won't fit the packet
525
+ * -1 if invalid TXT entry
526
+ */
527
+ static inline int append_one_txt_record_entry (uint8_t * packet , uint16_t * index , mdns_txt_linked_item_t * txt )
528
+ {
529
+ if (txt == NULL || txt -> key == NULL ) {
530
+ return -1 ;
531
+ }
532
+ size_t key_len = strlen (txt -> key );
533
+ size_t len = key_len + txt -> value_len + (txt -> value ? 1 : 0 );
534
+ if ((* index + len + 1 ) >= MDNS_MAX_PACKET_SIZE ) {
535
+ return 0 ;
536
+ }
537
+ _mdns_append_u8 (packet , index , len );
538
+ memcpy (packet + * index , txt -> key , key_len );
539
+ if (txt -> value ) {
540
+ packet [* index + key_len ] = '=' ;
541
+ memcpy (packet + * index + key_len + 1 , txt -> value , txt -> value_len );
542
+ }
543
+ * index += len ;
544
+ return len + 1 ;
545
+ }
546
+
516
547
/**
517
548
* @brief appends FQDN to a packet, incrementing the index and
518
549
* compressing the output if previous occurrence of the string (or part of it) has been found
@@ -775,19 +806,13 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns
775
806
uint16_t data_len_location = * index - 2 ;
776
807
uint16_t data_len = 0 ;
777
808
778
- char * tmp ;
779
809
mdns_txt_linked_item_t * txt = service -> txt ;
780
810
while (txt ) {
781
- if (asprintf (& tmp , "%s%s%.*s" , txt -> key , txt -> value ? "=" : "" , txt -> value_len , txt -> value ) > 0 ) {
782
- uint8_t l = _mdns_append_string (packet , index , tmp );
783
- free (tmp );
784
- if (!l ) {
785
- return 0 ;
786
- }
811
+ int l = append_one_txt_record_entry (packet , index , txt );
812
+ if (l > 0 ) {
787
813
data_len += l ;
788
- } else {
789
- HOOK_MALLOC_FAILED ;
790
- // continue
814
+ } else if (l == 0 ) { // TXT entry won't fit into the mdns packet
815
+ return 0 ;
791
816
}
792
817
txt = txt -> next ;
793
818
}
@@ -2574,17 +2599,10 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d
2574
2599
2575
2600
uint8_t ours [len ];
2576
2601
uint16_t index = 0 ;
2577
- char * tmp ;
2578
2602
2579
2603
txt = service -> txt ;
2580
2604
while (txt ) {
2581
- if (asprintf (& tmp , "%s%s%.*s" , txt -> key , txt -> value ? "=" : "" , txt -> value_len , txt -> value ) > 0 ) {
2582
- _mdns_append_string (ours , & index , tmp );
2583
- free (tmp );
2584
- } else {
2585
- HOOK_MALLOC_FAILED ;
2586
- // continue
2587
- }
2605
+ append_one_txt_record_entry (ours , & index , txt );
2588
2606
txt = txt -> next ;
2589
2607
}
2590
2608
0 commit comments