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

Ben Pfaff blp at ovn.org
Fri Apr 13 17:26:48 UTC 2018


This will acquire its first user in an upcoming commit.

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

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);
 
-- 
2.16.1



More information about the dev mailing list