Blame SOURCES/memcached-ipv6.patch

77eb1b
commit 4baa578f2b7552ed44482b32fb18dc4bbb53f538
77eb1b
Author: githublvv <ymaillvv@yahoo.com>
77eb1b
Date:   Fri Jan 9 18:15:36 2015 -0500
77eb1b
77eb1b
    Fix for 310: 	memcached unable to bind to an ipv6 address
77eb1b
    
77eb1b
    URL: https://code.google.com/p/memcached/issues/detail?id=310
77eb1b
77eb1b
diff --git a/memcached.c b/memcached.c
77eb1b
index 154b15a..1181966 100644
77eb1b
--- a/memcached.c
77eb1b
+++ b/memcached.c
77eb1b
@@ -4614,15 +4614,40 @@ static int server_sockets(int port, enum network_transport transport,
77eb1b
              p != NULL;
77eb1b
              p = strtok_r(NULL, ";,", &b)) {
77eb1b
             int the_port = port;
77eb1b
+
77eb1b
+            char *h = NULL;
77eb1b
+            if (*p == '[') {
77eb1b
+                // expecting it to be an IPv6 address enclosed in []
77eb1b
+                // i.e. RFC3986 style recommended by RFC5952
77eb1b
+                char *e = strchr(p, ']');
77eb1b
+                if (e == NULL) {
77eb1b
+                    fprintf(stderr, "Invalid IPV6 address: \"%s\"", p);
77eb1b
+                    return 1;
77eb1b
+                }
77eb1b
+                h = ++p; // skip the opening '['
77eb1b
+                *e = '\0';
77eb1b
+                p = ++e; // skip the closing ']'
77eb1b
+            }
77eb1b
+
77eb1b
             char *s = strchr(p, ':');
77eb1b
             if (s != NULL) {
77eb1b
-                *s = '\0';
77eb1b
-                ++s;
77eb1b
-                if (!safe_strtol(s, &the_port)) {
77eb1b
-                    fprintf(stderr, "Invalid port number: \"%s\"", s);
77eb1b
-                    return 1;
77eb1b
+                // If no more semicolons - attempt to treat as port number.
77eb1b
+                // Otherwise the only valid option is an unenclosed IPv6 without port, until
77eb1b
+                // of course there was an RFC3986 IPv6 address previously specified -
77eb1b
+                // in such a case there is no good option, will just send it to fail as port number.
77eb1b
+                if (strchr(s + 1, ':') == NULL || h != NULL) {
77eb1b
+                    *s = '\0';
77eb1b
+                    ++s;
77eb1b
+                    if (!safe_strtol(s, &the_port)) {
77eb1b
+                        fprintf(stderr, "Invalid port number: \"%s\"", s);
77eb1b
+                        return 1;
77eb1b
+                    }
77eb1b
                 }
77eb1b
             }
77eb1b
+
77eb1b
+            if (h != NULL)
77eb1b
+                p = h;
77eb1b
+
77eb1b
             if (strcmp(p, "*") == 0) {
77eb1b
                 p = NULL;
77eb1b
             }