From b1099c96c9ac16d468995a65b79474ba03de305c Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Wed, 6 Apr 2022 18:18:46 -0400 Subject: [PATCH] Use FQDN_LEN instead of MAXHOSTNAMELEN for DNS names As defined in nbase.h (or in some system header file on Linux), MAXHOSTNAMELEN is 64. This is the maximum length of a hostname, but not of a DNS name, which may be longer. RFC 1035 spells out that DNS names are formed of labels, each of which must be 63 bytes or less, and that labels are combined to form a DNS name (a.k.a. FQDN) which may not exceed 255 bytes. So add and use that, which fixed a long name issue for me. Signed-off-by: Robin Getz --- dns_sd.c | 2 +- dns_sd.h | 4 +--- dns_sd_bonjour.c | 4 ++-- network-windows.c | 3 --- network.c | 2 +- utilities.c | 2 +- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/dns_sd.c b/dns_sd.c index cc807944a..1d26375d5 100644 --- a/dns_sd.c +++ b/dns_sd.c @@ -81,7 +81,7 @@ static int dnssd_fill_context_info(struct iio_context_info *info, char *hostname, char *addr_str, uint16_t port) { struct iio_context *ctx; - char uri[sizeof("ip:") + MAXHOSTNAMELEN + sizeof (":65535") + 1]; + char uri[sizeof("ip:") + FQDN_LEN + sizeof (":65535") + 1]; char description[255], *p; const char *hw_model, *serial; unsigned int i; diff --git a/dns_sd.h b/dns_sd.h index 3fb370b34..7906837e2 100644 --- a/dns_sd.h +++ b/dns_sd.h @@ -15,14 +15,12 @@ #ifdef _WIN32 #include -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN (MAX_COMPUTERNAME_LENGTH+1) -#endif /* MAXHOSTNAMELEN */ #else #include #endif #define DNS_SD_ADDRESS_STR_MAX (40) /* IPv6 Max = 4*8 + 7 + 1 for NUL */ +#define FQDN_LEN (255) /* RFC 1035 */ /* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */ #ifndef ENOMEDIUM diff --git a/dns_sd_bonjour.c b/dns_sd_bonjour.c index 18779836f..2d7ed5e11 100644 --- a/dns_sd_bonjour.c +++ b/dns_sd_bonjour.c @@ -30,8 +30,8 @@ static void __cfnet_browser_cb(CFNetServiceBrowserRef browser, struct dns_sd_discovery_data *dd = info; char address_v4[DNS_SD_ADDRESS_STR_MAX+1] = ""; char address_v6[DNS_SD_ADDRESS_STR_MAX+1] = ""; - char hostname[MAXHOSTNAMELEN]; - char name[MAXHOSTNAMELEN]; + char hostname[FQDN_LEN]; + char name[FQDN_LEN]; bool have_v4 = false; bool have_v6 = false; struct sockaddr_in *sa; diff --git a/network-windows.c b/network-windows.c index 5cfb22e39..b3f689544 100644 --- a/network-windows.c +++ b/network-windows.c @@ -11,9 +11,6 @@ #include #include #define close(s) closesocket(s) -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN (MAX_COMPUTERNAME_LENGTH+1) -#endif /* MAXHOSTNAMELEN */ int set_blocking_mode(int s, bool blocking) { diff --git a/network.c b/network.c index cdedf5e9b..03cb09f62 100644 --- a/network.c +++ b/network.c @@ -1218,7 +1218,7 @@ struct iio_context * network_create_context(const char *host) uri_len = strlen(description); if (host && host[0]) - uri_len = strnlen(host, MAXHOSTNAMELEN); + uri_len = strnlen(host, FQDN_LEN); uri_len += sizeof ("ip:"); uri = malloc(uri_len); diff --git a/utilities.c b/utilities.c index 5ada6ad34..71787395d 100644 --- a/utilities.c +++ b/utilities.c @@ -321,7 +321,7 @@ char * iio_getenv (char * envvar) if (!hostname) return NULL; - tmp = MAXHOSTNAMELEN + sizeof("serial:") + sizeof(":65535") - 2; + tmp = FQDN_LEN + sizeof("serial:") + sizeof(":65535") - 2; len = strnlen(hostname, tmp); /* Should be smaller than max length */