[ovs-dev,03/11] ovn-northd: Use common code for sockaddr_storage.

Message ID 20180413172655.31638-3-blp@ovn.org
State Accepted
Headers show
Series
  • [ovs-dev,01/11] socket-util: Fix error in comment on ss_format_address().
Related show

Commit Message

Ben Pfaff April 13, 2018, 5:26 p.m.
This better reuses existing code.  It does require adding a slight variant
on ss_format_address(), but it still seems like a net win.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/socket-util.c       | 28 ++++++++++++++++++++++------
 lib/socket-util.h       |  1 +
 ovn/northd/ovn-northd.c | 22 ++++++----------------
 3 files changed, 29 insertions(+), 22 deletions(-)

Patch

diff --git a/lib/socket-util.c b/lib/socket-util.c
index 86ac4d433a2d..b36de371baa1 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -1000,10 +1000,10 @@  is_safe_name(const char *name)
     return true;
 }
 
-/* Formats the IPv4 or IPv6 address in 'ss' into 's'.  If 'ss' is an IPv6
- * address, puts square brackets around the address. */
-void
-ss_format_address(const struct sockaddr_storage *ss, struct ds *s)
+static void
+ss_format_address__(const struct sockaddr_storage *ss,
+                    const char *lbrack, const char *rbrack,
+                    struct ds *s)
 {
     if (ss->ss_family == AF_INET) {
         const struct sockaddr_in *sin
@@ -1014,7 +1014,7 @@  ss_format_address(const struct sockaddr_storage *ss, struct ds *s)
         const struct sockaddr_in6 *sin6
             = ALIGNED_CAST(const struct sockaddr_in6 *, ss);
 
-        ds_put_char(s, '[');
+        ds_put_cstr(s, lbrack);
         ds_reserve(s, s->length + INET6_ADDRSTRLEN);
         char *tail = &s->string[s->length];
         inet_ntop(AF_INET6, sin6->sin6_addr.s6_addr, tail, INET6_ADDRSTRLEN);
@@ -1034,12 +1034,28 @@  ss_format_address(const struct sockaddr_storage *ss, struct ds *s)
         }
 #endif
 
-        ds_put_char(s, ']');
+        ds_put_cstr(s, rbrack);
     } else {
         OVS_NOT_REACHED();
     }
 }
 
+/* Formats the IPv4 or IPv6 address in 'ss' into 's'.  If 'ss' is an IPv6
+ * address, puts square brackets around the address. */
+void
+ss_format_address(const struct sockaddr_storage *ss, struct ds *s)
+{
+    ss_format_address__(ss, "[", "]", s);
+}
+
+/* Formats the IPv4 or IPv6 address in 'ss' into 's'.  Does not add square
+ * brackets around IPv6 addresses. */
+void
+ss_format_address_nobracks(const struct sockaddr_storage *ss, struct ds *s)
+{
+    ss_format_address__(ss, "", "", s);
+}
+
 size_t
 ss_length(const struct sockaddr_storage *ss)
 {
diff --git a/lib/socket-util.h b/lib/socket-util.h
index 439f0c285107..d8c6f082c8cf 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -75,6 +75,7 @@  char *describe_fd(int fd);
  * IPv6 address. */
 uint16_t ss_get_port(const struct sockaddr_storage *);
 void ss_format_address(const struct sockaddr_storage *, struct ds *);
+void ss_format_address_nobracks(const struct sockaddr_storage *, struct ds *);
 size_t ss_length(const struct sockaddr_storage *);
 const char *sock_strerror(int error);
 
diff --git a/ovn/northd/ovn-northd.c b/ovn/northd/ovn-northd.c
index 845faba86fe0..b518953f2e63 100644
--- a/ovn/northd/ovn-northd.c
+++ b/ovn/northd/ovn-northd.c
@@ -2942,29 +2942,19 @@  ip_address_and_port_from_lb_key(const char *key, char **ip_address,
                                 uint16_t *port, int *addr_family)
 {
     struct sockaddr_storage ss;
-    char ip_addr_buf[INET6_ADDRSTRLEN];
-    char *error;
-
-    error = ipv46_parse(key, PORT_OPTIONAL, &ss);
-    if (error) {
+    if (!inet_parse_active(key, 0, &ss)) {
         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
         VLOG_WARN_RL(&rl, "bad ip address or port for load balancer key %s",
                      key);
-        free(error);
         return;
     }
 
-    if (ss.ss_family == AF_INET) {
-        struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *, &ss);
-        *port = sin->sin_port == 0 ? 0 : ntohs(sin->sin_port);
-        inet_ntop(AF_INET, &sin->sin_addr, ip_addr_buf, sizeof ip_addr_buf);
-    } else {
-        struct sockaddr_in6 *sin6 = ALIGNED_CAST(struct sockaddr_in6 *, &ss);
-        *port = sin6->sin6_port == 0 ? 0 : ntohs(sin6->sin6_port);
-        inet_ntop(AF_INET6, &sin6->sin6_addr, ip_addr_buf, sizeof ip_addr_buf);
-    }
+    struct ds s = DS_EMPTY_INITIALIZER;
+    ss_format_address_nobracks(&ss, &s);
+    *ip_address = ds_steal_cstr(&s);
+
+    *port = ss_get_port(&ss);
 
-    *ip_address = xstrdup(ip_addr_buf);
     *addr_family = ss.ss_family;
 }