drop [] from address, commonly used for IPv6
authorRemi Collet <remi@remirepo.net>
Tue, 26 Jan 2021 10:35:46 +0000 (11:35 +0100)
committerRemi Collet <remi@remirepo.net>
Tue, 26 Jan 2021 10:35:46 +0000 (11:35 +0100)
src/libmemcached/connect.cc

index 90f1b21756a5930299ba405a446af4d79c0d7498..e6db6fd64da5271376b47a4c0d5581450f98041a 100644 (file)
@@ -46,8 +46,22 @@ static memcached_return_t set_hostinfo(memcached_instance_st *server) {
   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());
-  switch (errcode = getaddrinfo(server->hostname(), str_port, &hints, &server->address_info)) {
+  // 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;
+     }
+  }
+  switch (errcode = getaddrinfo(addr, str_port, &hints, &server->address_info)) {
   case 0:
     server->address_info_next = server->address_info;
     server->state = MEMCACHED_SERVER_STATE_ADDRINFO;