From e2ed1e654be1240f1877664fdcf4fcf2eb234fa5 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Fri, 5 Oct 2018 23:03:24 +0800 Subject: [PATCH] Use sysconf(_SC_HOST_NAME_MAX) to determine maximum hostname length (#70) --- src/util.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/util.c b/src/util.c index bcfe1e1..b3f8eef 100644 --- a/src/util.c +++ b/src/util.c @@ -23,12 +23,24 @@ void hostname(char** out) { struct addrinfo hints; struct addrinfo* info; - char hostname[HOST_NAME_MAX + 1]; + char* hostname; char* dot; + int host_name_max; int result; - hostname[HOST_NAME_MAX] = '\0'; - gethostname(hostname, HOST_NAME_MAX); + if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) == -1) + { + perror("sysconf(_SC_HOST_NAME_MAX)"); + exit(1); + } + + if ((hostname = malloc(host_name_max+1)) == NULL) + { + perror("malloc"); + exit(1); + } + + gethostname(hostname, sizeof(hostname)); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -47,6 +59,7 @@ void hostname(char** out) hostname_backup = *out; freeaddrinfo(info); + free(hostname); } void free_hostname()