From 20690cd266ff724e3ea1c543ea6a5fe47fec27e7 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 26 Jan 2021 14:44:09 +0100 Subject: [PATCH] set_hostinfo --- src/libmemcached/connect.cc | 46 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/libmemcached/connect.cc b/src/libmemcached/connect.cc index e6db6fd6..54e58ec3 100644 --- a/src/libmemcached/connect.cc +++ b/src/libmemcached/connect.cc @@ -18,50 +18,44 @@ #include - static memcached_return_t set_hostinfo(memcached_instance_st *server) { assert(server->type != MEMCACHED_CONNECTION_UNIX_SOCKET); + assert(server->hostname()); + server->clear_addrinfo(); - char str_port[MEMCACHED_NI_MAXSERV] = {0}; + char str_host[MEMCACHED_NI_MAXHOST] = {0}, str_port[MEMCACHED_NI_MAXSERV] = {0}; errno = 0; - int length = snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port())); - if (length >= MEMCACHED_NI_MAXSERV or length <= 0 or errno) { + + auto length = snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port())); + if (length <= 0 or errno) { return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("snprintf(NI_MAXSERV)")); } - struct addrinfo hints; - memset(&hints, 0, sizeof(struct addrinfo)); - + struct addrinfo hints{}; hints.ai_family = AF_UNSPEC; if (memcached_is_udp(server->root)) { hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; } else { - hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; + hints.ai_socktype = SOCK_STREAM; } - assert(server->address_info == NULL); - assert(server->address_info_next == NULL); - int errcode; - char hostname[MEMCACHED_NI_MAXHOST]; - const char *addr; - char *p; - - assert(server->hostname()); - // drop [] from address, commonly used for IPv6 - addr = server->hostname(); - if (*addr == '[') { - strcpy(hostname, addr +1); - p = strchr(hostname, ']'); - if (p) { - *p = 0; - addr = hostname; - } + auto hostname = server->hostname(); + if (*hostname == '[') { + auto closing_bracket = &hostname[strlen(hostname) - 1]; + if (*closing_bracket == ']') { + auto host_len = closing_bracket - hostname - 1; + if (host_len < MEMCACHED_NI_MAXHOST) { + hostname = strncpy(str_host, hostname + 1, host_len); + } + } } - switch (errcode = getaddrinfo(addr, str_port, &hints, &server->address_info)) { + + auto errcode = getaddrinfo(hostname, str_port, &hints, &server->address_info); + switch (errcode) { case 0: server->address_info_next = server->address_info; server->state = MEMCACHED_SERVER_STATE_ADDRINFO; -- 2.30.2