[ovs-dev] [PATCH v3 ovn 3/6] Add more IP address normalization functions.

Mark Michelson mmichels at redhat.com
Wed Jun 24 16:12:08 UTC 2020


Normalization of IP addresses helps to fix common pitfalls when
comparing addresses:
* Case-sensitivity in IPv6 addresses
* Different valid represntations of netmasks

This commit adds new normalization functions and cleans up some of the
existing ones.

The new functions are not used in this commit but will be used in later
commits in this series.

Signed-off-by: Mark Michelson <mmichels at redhat.com>
---
 utilities/ovn-nbctl.c | 81 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 69 insertions(+), 12 deletions(-)

diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 708ff2e5b..2e5253d4e 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -3510,28 +3510,85 @@ normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen)
     }
 }
 
-/* The caller must free the returned string. */
 static char *
-normalize_prefix_str(const char *orig_prefix)
+normalize_ipv4_prefix_str(const char *orig_prefix)
 {
     unsigned int plen;
     ovs_be32 ipv4;
     char *error;
 
     error = ip_parse_cidr(orig_prefix, &ipv4, &plen);
-    if (!error) {
-        return normalize_ipv4_prefix(ipv4, plen);
-    } else {
-        struct in6_addr ipv6;
+    if (error) {
         free(error);
+        return NULL;
+    }
+    return normalize_ipv4_prefix(ipv4, plen);
+}
 
-        error = ipv6_parse_cidr(orig_prefix, &ipv6, &plen);
-        if (error) {
-            free(error);
-            return NULL;
-        }
-        return normalize_ipv6_prefix(ipv6, plen);
+static char *
+normalize_ipv6_prefix_str(const char *orig_prefix)
+{
+    unsigned int plen;
+    struct in6_addr ipv6;
+    char *error;
+
+    error = ipv6_parse_cidr(orig_prefix, &ipv6, &plen);
+    if (error) {
+        free(error);
+        return NULL;
+    }
+    return normalize_ipv6_prefix(ipv6, plen);
+}
+
+/* The caller must free the returned string. */
+static char *
+normalize_prefix_str(const char *orig_prefix)
+{
+    char *ret;
+
+    ret = normalize_ipv4_prefix_str(orig_prefix);
+    if (!ret) {
+        ret = normalize_ipv6_prefix_str(orig_prefix);
+    }
+    return ret;
+}
+
+static char *
+normalize_ipv4_addr_str(const char *orig_addr)
+{
+    ovs_be32 ipv4;
+
+    if (!ip_parse(orig_addr, &ipv4)) {
+        return NULL;
     }
+
+    return normalize_ipv4_prefix(ipv4, 32);
+}
+
+static char *
+normalize_ipv6_addr_str(const char *orig_addr)
+{
+    struct in6_addr ipv6;
+
+    if (!ipv6_parse(orig_addr, &ipv6)) {
+        return NULL;
+    }
+
+    return normalize_ipv6_prefix(ipv6, 128);
+}
+
+/* Similar to normalize_prefix_str but must be an un-masked address.
+ * The caller must free the returned string. */
+OVS_UNUSED static char *
+normalize_addr_str(const char *orig_addr)
+{
+    char *ret;
+
+    ret = normalize_ipv4_addr_str(orig_addr);
+    if (!ret) {
+        ret = normalize_ipv6_addr_str(orig_addr);
+    }
+    return ret;
 }
 
 static void
-- 
2.25.4



More information about the dev mailing list