diff mbox

[ovs-dev,21/23] packets: New function ip_parse_masked().

Message ID 1444450902-12236-2-git-send-email-blp@nicira.com
State Accepted
Headers show

Commit Message

Ben Pfaff Oct. 10, 2015, 4:21 a.m. UTC
Signed-off-by: Ben Pfaff <blp@nicira.com>
---
 lib/meta-flow.c | 19 +------------------
 lib/packets.c   | 21 +++++++++++++++++++++
 lib/packets.h   |  2 ++
 3 files changed, 24 insertions(+), 18 deletions(-)

Comments

Justin Pettit Oct. 16, 2015, 1:31 a.m. UTC | #1
> On Oct 9, 2015, at 9:21 PM, Ben Pfaff <blp@nicira.com> wrote:
> 
> Signed-off-by: Ben Pfaff <blp@nicira.com>
> 
> +char * OVS_WARN_UNUSED_RESULT
> +ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)

It might be nice to provide a comment describing this function.

Acked-by: Justin Pettit <jpettit@nicira.com>

--Justin
Ben Pfaff Oct. 16, 2015, 8:54 p.m. UTC | #2
On Thu, Oct 15, 2015 at 06:31:51PM -0700, Justin Pettit wrote:
> 
> > On Oct 9, 2015, at 9:21 PM, Ben Pfaff <blp@nicira.com> wrote:
> > 
> > Signed-off-by: Ben Pfaff <blp@nicira.com>
> > 
> > +char * OVS_WARN_UNUSED_RESULT
> > +ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
> 
> It might be nice to provide a comment describing this function.

OK, done:

/* Parses string 's', which must be an IP address with an optional netmask or
 * CIDR prefix length.  Stores the IP address into '*ip' and the netmask into
 * '*mask'.  (If 's' does not contain a netmask, 255.255.255.255 is
 * assumed.)
 *
 * Returns NULL if successful, otherwise an error message that the caller must
 * free(). */

> Acked-by: Justin Pettit <jpettit@nicira.com>

Thanks!
diff mbox

Patch

diff --git a/lib/meta-flow.c b/lib/meta-flow.c
index 224ba53..fba628c 100644
--- a/lib/meta-flow.c
+++ b/lib/meta-flow.c
@@ -1930,25 +1930,8 @@  static char *
 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
                     ovs_be32 *ip, ovs_be32 *mask)
 {
-    int prefix;
-
     ovs_assert(mf->n_bytes == sizeof *ip);
-
-    if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
-                 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
-        /* OK. */
-    } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
-        if (prefix <= 0 || prefix > 32) {
-            return xasprintf("%s: network prefix bits not between 0 and "
-                             "32", s);
-        }
-        *mask = be32_prefix_mask(prefix);
-    } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
-        *mask = OVS_BE32_MAX;
-    } else {
-        return xasprintf("%s: invalid IP address", s);
-    }
-    return NULL;
+    return ip_parse_masked(s, ip, mask);
 }
 
 static char *
diff --git a/lib/packets.c b/lib/packets.c
index a4d7854..778fc77 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -404,6 +404,27 @@  ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
     }
 }
 
+char * OVS_WARN_UNUSED_RESULT
+ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
+{
+    int prefix;
+
+    if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
+                 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
+        /* OK. */
+    } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
+        if (prefix <= 0 || prefix > 32) {
+            return xasprintf("%s: network prefix bits not between 0 and "
+                             "32", s);
+        }
+        *mask = be32_prefix_mask(prefix);
+    } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
+        *mask = OVS_BE32_MAX;
+    } else {
+        return xasprintf("%s: invalid IP address", s);
+    }
+    return NULL;
+}
 
 /* Stores the string representation of the IPv6 address 'addr' into the
  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
diff --git a/lib/packets.h b/lib/packets.h
index e841fb1..ea74646 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -561,6 +561,8 @@  ip_is_local_multicast(ovs_be32 ip)
 }
 int ip_count_cidr_bits(ovs_be32 netmask);
 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
+char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
+    OVS_WARN_UNUSED_RESULT;
 
 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)