diff mbox series

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

Message ID 20200624161211.1559878-3-mmichels@redhat.com
State Superseded
Headers show
Series [ovs-dev,v3,ovn,1/6] Avoid case-sensitive MAC address comparisons. | expand

Commit Message

Mark Michelson June 24, 2020, 4:12 p.m. UTC
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@redhat.com>
---
 utilities/ovn-nbctl.c | 81 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 69 insertions(+), 12 deletions(-)
diff mbox series

Patch

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