diff mbox

[ovs-dev,v6,1/3] ovn-util: Add a new util function extract_ip_addresses

Message ID 20170425142916.30735-1-nusiddiq@redhat.com
State Accepted
Headers show

Commit Message

Numan Siddique April 25, 2017, 2:29 p.m. UTC
From: Numan Siddique <nusiddiq@redhat.com>

An upcoming commit will use this function to extract the IP (v4 and
v6) addresses from a string without a preceding eth address.

Signed-off-by: Numan Siddique <nusiddiq@redhat.com>
Acked-by: Ben Pfaff <blp@ovn.org>
---
 ovn/lib/ovn-util.c | 72 ++++++++++++++++++++++++++++++++++++++----------------
 ovn/lib/ovn-util.h |  1 +
 2 files changed, 52 insertions(+), 21 deletions(-)
diff mbox

Patch

diff --git a/ovn/lib/ovn-util.c b/ovn/lib/ovn-util.c
index 237e989..037d079 100644
--- a/ovn/lib/ovn-util.c
+++ b/ovn/lib/ovn-util.c
@@ -82,19 +82,9 @@  is_dynamic_lsp_address(const char *address)
                          ETH_ADDR_SCAN_ARGS(ea), &n) && address[n] == '\0'));
 }
 
-/* Extracts the mac, IPv4 and IPv6 addresses from * 'address' which
- * should be of the format "MAC [IP1 IP2 ..] .." where IPn should be a
- * valid IPv4 or IPv6 address and stores them in the 'ipv4_addrs' and
- * 'ipv6_addrs' fields of 'laddrs'.  There may be additional content in
- * 'address' after "MAC [IP1 IP2 .. ]".  The value of 'ofs' that is
- * returned indicates the offset where that additional content begins.
- *
- * Returns true if at least 'MAC' is found in 'address', false otherwise.
- *
- * The caller must call destroy_lport_addresses(). */
-bool
-extract_addresses(const char *address, struct lport_addresses *laddrs,
-                  int *ofs)
+static bool
+parse_and_store_addresses(const char *address, struct lport_addresses *laddrs,
+                          int *ofs, bool extract_eth_addr)
 {
     memset(laddrs, 0, sizeof *laddrs);
 
@@ -102,15 +92,18 @@  extract_addresses(const char *address, struct lport_addresses *laddrs,
     const char *const start = buf;
     int buf_index = 0;
     const char *buf_end = buf + strlen(address);
-    if (!ovs_scan_len(buf, &buf_index, ETH_ADDR_SCAN_FMT,
-                      ETH_ADDR_SCAN_ARGS(laddrs->ea))) {
-        laddrs->ea = eth_addr_zero;
-        *ofs = 0;
-        return false;
-    }
 
-    snprintf(laddrs->ea_s, sizeof laddrs->ea_s, ETH_ADDR_FMT,
-             ETH_ADDR_ARGS(laddrs->ea));
+    if (extract_eth_addr) {
+        if (!ovs_scan_len(buf, &buf_index, ETH_ADDR_SCAN_FMT,
+                          ETH_ADDR_SCAN_ARGS(laddrs->ea))) {
+            laddrs->ea = eth_addr_zero;
+            *ofs = 0;
+            return false;
+        }
+
+        snprintf(laddrs->ea_s, sizeof laddrs->ea_s, ETH_ADDR_FMT,
+                 ETH_ADDR_ARGS(laddrs->ea));
+    }
 
     ovs_be32 ip4;
     struct in6_addr ip6;
@@ -145,6 +138,23 @@  extract_addresses(const char *address, struct lport_addresses *laddrs,
 }
 
 /* Extracts the mac, IPv4 and IPv6 addresses from * 'address' which
+ * should be of the format "MAC [IP1 IP2 ..] .." where IPn should be a
+ * valid IPv4 or IPv6 address and stores them in the 'ipv4_addrs' and
+ * 'ipv6_addrs' fields of 'laddrs'.  There may be additional content in
+ * 'address' after "MAC [IP1 IP2 .. ]".  The value of 'ofs' that is
+ * returned indicates the offset where that additional content begins.
+ *
+ * Returns true if at least 'MAC' is found in 'address', false otherwise.
+ *
+ * The caller must call destroy_lport_addresses(). */
+bool
+extract_addresses(const char *address, struct lport_addresses *laddrs,
+                  int *ofs)
+{
+    return parse_and_store_addresses(address, laddrs, ofs, true);
+}
+
+/* Extracts the mac, IPv4 and IPv6 addresses from * 'address' which
  * should be of the format 'MAC [IP1 IP2 ..]" where IPn should be a
  * valid IPv4 or IPv6 address and stores them in the 'ipv4_addrs' and
  * 'ipv6_addrs' fields of 'laddrs'.
@@ -166,6 +176,26 @@  extract_lsp_addresses(const char *address, struct lport_addresses *laddrs)
     return success;
 }
 
+/* Extracts the IPv4 and IPv6 addresses from * 'address' which
+ * should be of the format 'IP1 IP2 .." where IPn should be a
+ * valid IPv4 or IPv6 address and stores them in the 'ipv4_addrs' and
+ * 'ipv6_addrs' fields of 'laddrs'.
+ *
+ * Return true if at least one IP address is found in 'address',
+ * false otherwise.
+ *
+ * The caller must call destroy_lport_addresses(). */
+bool
+extract_ip_addresses(const char *address, struct lport_addresses *laddrs)
+{
+    int ofs;
+    if (parse_and_store_addresses(address, laddrs, &ofs, false)) {
+        return (laddrs->n_ipv4_addrs || laddrs->n_ipv6_addrs);
+    }
+
+    return false;
+}
+
 /* Extracts the mac, IPv4 and IPv6 addresses from the
  * "nbrec_logical_router_port" parameter 'lrp'.  Stores the IPv4 and
  * IPv6 addresses in the 'ipv4_addrs' and 'ipv6_addrs' fields of
diff --git a/ovn/lib/ovn-util.h b/ovn/lib/ovn-util.h
index 8252283..b3d2125 100644
--- a/ovn/lib/ovn-util.h
+++ b/ovn/lib/ovn-util.h
@@ -57,6 +57,7 @@  bool is_dynamic_lsp_address(const char *address);
 bool extract_addresses(const char *address, struct lport_addresses *,
                        int *ofs);
 bool extract_lsp_addresses(const char *address, struct lport_addresses *);
+bool extract_ip_addresses(const char *address, struct lport_addresses *);
 bool extract_lrp_networks(const struct nbrec_logical_router_port *,
                           struct lport_addresses *);
 void destroy_lport_addresses(struct lport_addresses *);