]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Introduce virStringStripIPv6Brackets
authorJán Tomko <jtomko@redhat.com>
Tue, 7 Oct 2014 15:27:40 +0000 (17:27 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 15 Oct 2014 07:25:33 +0000 (09:25 +0200)
Helper function to strip the brackets from an IPv6 address.
Tested by viruritest.

src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h
src/util/viruri.c
tests/virstringtest.c

index d6265acd66e7329ca94939433bef414bd5b3aa09..c5397ddfbd1f1ad56916d18bc8d1e3e555ec7a14 100644 (file)
@@ -2004,6 +2004,7 @@ virStringSortCompare;
 virStringSortRevCompare;
 virStringSplit;
 virStringSplitCount;
+virStringStripIPv6Brackets;
 virStrncpy;
 virStrndup;
 virStrToDouble;
index 54c0b6fb9324028e940998e439e2f7417e4cebce..df6464a3bfa1d18836bde4542f7932f57df9d112 100644 (file)
@@ -929,3 +929,26 @@ virStringReplace(const char *haystack,
 
     return virBufferContentAndReset(&buf);
 }
+
+
+/**
+ * virStringStripIPv6Brackets:
+ * @str: the string to strip
+ *
+ * Modify the string in-place to remove the leading and closing brackets
+ * from an IPv6 address.
+ */
+void
+virStringStripIPv6Brackets(char *str)
+{
+    size_t len;
+
+    if (!str)
+        return;
+
+    len = strlen(str);
+    if (str[0] == '[' && str[len - 1] == ']' && strchr(str, ':')) {
+        memmove(&str[0], &str[1], len - 2);
+        str[len - 2] = '\0';
+    }
+}
index b82ef2af36a25a010e557ccac362bec29d4ca819..40ebaebb790de2723f8c85686c3227543b45347d 100644 (file)
@@ -268,4 +268,6 @@ char *virStringReplace(const char *haystack,
                        const char *newneedle)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
 
+void virStringStripIPv6Brackets(char *str);
+
 #endif /* __VIR_STRING_H__ */
index 23d86c52e7fcf6fb1998611f0931c793f8aaed73..6166c372b048063eebf11a4ba21712784be348d0 100644 (file)
@@ -182,22 +182,10 @@ virURIParse(const char *uri)
     if (VIR_STRDUP(ret->user, xmluri->user) < 0)
         goto error;
 
-    /* First check: does it even make sense to jump inside */
-    if (ret->server != NULL &&
-        ret->server[0] == '[') {
-        size_t length = strlen(ret->server);
-
-        /* We want to modify the server string only if there are
-         * square brackets on both ends and inside there is IPv6
-         * address. Otherwise we could make a mistake by modifying
-         * something other than an IPv6 address. */
-        if (ret->server[length - 1] == ']' && strchr(ret->server, ':')) {
-            memmove(&ret->server[0], &ret->server[1], length - 2);
-            ret->server[length - 2] = '\0';
-        }
-        /* Even after such modification, it is completely ok to free
-         * the uri with xmlFreeURI() */
-    }
+    /* Strip square bracket from an IPv6 address.
+     * The function modifies the string in-place. Even after such
+     * modification, it is OK to free the URI with xmlFreeURI. */
+    virStringStripIPv6Brackets(ret->server);
 
     if (virURIParseParams(ret) < 0)
         goto error;
index 10fad2c88013cd1ef067c5338b5cb136619c55b6..841531f0bd2fbde6b50e700512a44efb2c85223d 100644 (file)
@@ -522,6 +522,36 @@ testVirStringFreeListCount(const void *opaque ATTRIBUTE_UNUSED)
 }
 
 
+struct testStripIPv6BracketsData {
+    const char *string;
+    const char *result;
+};
+
+static int testStripIPv6Brackets(const void *args)
+{
+    const struct testStripIPv6BracketsData *data = args;
+    int ret = -1;
+    char *res = NULL;
+
+    if (VIR_STRDUP(res, data->string) < 0)
+        goto cleanup;
+
+    virStringStripIPv6Brackets(res);
+
+    if (STRNEQ_NULLABLE(res, data->result)) {
+        fprintf(stderr, "Returned '%s', expected '%s'\n",
+                NULLSTR(res), NULLSTR(data->result));
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(res);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -731,6 +761,25 @@ mymain(void)
                     NULL) < 0)
         ret = -1;
 
+#define TEST_STRIP_IPV6_BRACKETS(str, res)                              \
+    do {                                                                \
+        struct testStripIPv6BracketsData stripData = {                  \
+            .string = str,                                              \
+            .result = res,                                              \
+        };                                                              \
+        if (virtTestRun("Strip brackets from IPv6 " #str,               \
+                        testStripIPv6Brackets, &stripData) < 0)         \
+            ret = -1;                                                   \
+    } while (0)
+
+    TEST_STRIP_IPV6_BRACKETS(NULL, NULL);
+    TEST_STRIP_IPV6_BRACKETS("[]", "[]");
+    TEST_STRIP_IPV6_BRACKETS("[:]", ":");
+    TEST_STRIP_IPV6_BRACKETS("[::1]", "::1");
+    TEST_STRIP_IPV6_BRACKETS("[hello:", "[hello:");
+    TEST_STRIP_IPV6_BRACKETS(":hello]", ":hello]");
+    TEST_STRIP_IPV6_BRACKETS(":[]:", ":[]:");
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }