[ovs-dev,04/11] socket-util: New function inet_parse_address().

Message ID 20180413172655.31638-4-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 will acquire its first user in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/socket-util.c | 26 ++++++++++++++++++++++++++
 lib/socket-util.h |  2 ++
 2 files changed, 28 insertions(+)

Patch

diff --git a/lib/socket-util.c b/lib/socket-util.c
index b36de371baa1..2d893bc9feb6 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -698,6 +698,32 @@  error:
     return -error;
 }
 
+/* Parses 'target', which may be an IPv4 address or an IPv6 address
+ * enclosed in square brackets.
+ *
+ * On success, returns true and stores the parsed remote address into '*ss'.
+ * On failure, logs an error, stores zeros into '*ss', and returns false. */
+bool
+inet_parse_address(const char *target_, struct sockaddr_storage *ss)
+{
+    char *target = xstrdup(target_);
+    char *p = target;
+    char *host = inet_parse_token(&p);
+    bool ok = false;
+    if (!host) {
+        VLOG_ERR("%s: host must be specified", target_);
+    } else if (p && p[strspn(p, " \t\r\n")] != '\0') {
+        VLOG_ERR("%s: unexpected characters follow host", target_);
+    } else {
+        ok = parse_sockaddr_components(ss, host, NULL, 0, target_);
+    }
+    if (!ok) {
+        memset(ss, 0, sizeof *ss);
+    }
+    free(target);
+    return ok;
+}
+
 int
 read_fully(int fd, void *p_, size_t size, size_t *bytes_read)
 {
diff --git a/lib/socket-util.h b/lib/socket-util.h
index d8c6f082c8cf..b1eca88eb131 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -58,6 +58,8 @@  int inet_open_passive(int style, const char *target, int default_port,
                       struct sockaddr_storage *ssp, uint8_t dscp,
                       bool kernel_print_port);
 
+bool inet_parse_address(const char *target, struct sockaddr_storage *);
+
 int read_fully(int fd, void *, size_t, size_t *bytes_read);
 int write_fully(int fd, const void *, size_t, size_t *bytes_written);