chiark / gitweb /
Provide string_item_to_iaddr
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 2 Sep 2014 08:04:27 +0000 (09:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Oct 2014 21:28:39 +0000 (22:28 +0100)
This will be used shortly.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
util.c
util.h

diff --git a/util.c b/util.c
index 426fc69c1a9e922931c52c23d88377dbcf743f1d..9c8abb3393353cf08322d4292f9e0e4d0c71ccfd 100644 (file)
--- a/util.c
+++ b/util.c
@@ -40,6 +40,7 @@
 #include "util.h"
 #include "unaligned.h"
 #include "magic.h"
+#include "ipaddr.h"
 
 #define MIN_BUFFER_SIZE 64
 #define DEFAULT_BUFFER_SIZE 4096
@@ -478,6 +479,29 @@ extern void slilog_part(struct log_if *lf, int priority, const char *message, ..
     va_end(ap);
 }
 
+void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
+                         const char *desc)
+{
+#ifndef CONFIG_IPV6
+
+    ia->sin.sin_family=AF_INET;
+    ia->sin.sin_addr.s_addr=string_item_to_ipaddr(item,desc);
+
+#else /* CONFIG_IPV6 => we have adns_text2addr */
+
+    if (item->type!=t_string)
+       cfgfatal(item->loc,desc,"expecting a string IP (v4 or v6) address\n");
+    socklen_t salen=sizeof(*ia);
+    int r=adns_text2addr(item->data.string, port,
+                        adns_qf_addrlit_ipv4_quadonly,
+                        &ia->sa, &salen);
+    assert(r!=ENOSPC);
+    if (r) cfgfatal(item->loc,desc,"invalid IP (v4 or v6) address: %s\n",
+                   strerror(r));
+
+#endif /* CONFIG_IPV6 */
+}
+
 #define IADDR_NBUFS_SHIFT 3
 #define IADDR_NBUFS (1 << IADDR_NBUFS_SHIFT)
 
diff --git a/util.h b/util.h
index e3218142cbf27a3c98517300185d3d359d9c3749..33b2b2bd865e0cb81c3e2d2d5c4a2a4706da7296 100644 (file)
--- a/util.h
+++ b/util.h
@@ -84,6 +84,9 @@ extern int consttime_memeq(const void *s1, const void *s2, size_t n);
 const char *iaddr_to_string(const union iaddr *ia);
 int iaddr_socklen(const union iaddr *ia);
 
+void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
+                         const char *desc);
+
 #define MINMAX(ae,be,op) ({                    \
        typeof((ae)) a=(ae);                    \
        typeof((be)) b=(be);                    \