/* * Copyright (c) 2020 Friedt Professional Engineering Services, Inc * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include #include #include #include #include #include #include #include #include "dns_pack.h" #include "dns_sd.h" #include LOG_MODULE_REGISTER(net_dns_sd, CONFIG_DNS_SD_LOG_LEVEL); const char dns_sd_empty_txt[1]; #ifndef CONFIG_NET_TEST static size_t service_proto_size(const struct dns_sd_rec *inst); static bool label_is_valid(const char *label, size_t label_size); static int add_a_record(const struct dns_sd_rec *inst, uint32_t ttl, uint16_t host_offset, uint32_t addr, uint8_t *buf, uint16_t buf_offset, uint16_t buf_size); static int add_ptr_record(const struct dns_sd_rec *inst, uint32_t ttl, uint8_t *buf, uint16_t buf_offset, uint16_t buf_size, uint16_t *service_offset, uint16_t *instance_offset, uint16_t *domain_offset); static int add_txt_record(const struct dns_sd_rec *inst, uint32_t ttl, uint16_t instance_offset, uint8_t *buf, uint16_t buf_offset, uint16_t buf_size); static int add_aaaa_record(const struct dns_sd_rec *inst, uint32_t ttl, uint16_t host_offset, const uint8_t addr[16], uint8_t *buf, uint16_t buf_offset, uint16_t buf_size); static int add_srv_record(const struct dns_sd_rec *inst, uint32_t ttl, uint16_t instance_offset, uint16_t domain_offset, uint8_t *buf, uint16_t buf_offset, uint16_t buf_size, uint16_t *host_offset); static bool rec_is_valid(const struct dns_sd_rec *inst); #endif /* CONFIG_NET_TEST */ /** * Calculate the size of a DNS-SD service * * This macro calculates the size of the DNS-SD service for a DNS * Resource Record (RR). * * For example, if there is a service called 'My Foo'._http._tcp.local., * then the returned size is 18. That is broken down as shown below. * * - 1 byte for the size of "_http" * - 5 bytes for the value of "_http" * - 1 byte for the size of "_tcp" * - 4 bytes for the value of "_tcp" * - 1 byte for the size of "local" * - 5 bytes for the value of "local" * - 1 byte for the trailing NUL terminator '\0' * * @param ref the DNS-SD record * @return the size of the DNS-SD service for a DNS Resource Record */ size_t service_proto_size(const struct dns_sd_rec *ref) { return 0 + DNS_LABEL_LEN_SIZE + strlen(ref->service) + DNS_LABEL_LEN_SIZE + strlen(ref->proto) + DNS_LABEL_LEN_SIZE + strlen(ref->domain) + DNS_LABEL_LEN_SIZE ; } /** * Check Label Validity according to RFC 1035, Section 3.5 * *