chiark / gitweb /
Provide text2iaddr.
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>
Sat, 27 Sep 2014 17:48:12 +0000 (18:48 +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 2299f724e7cdf66bc4483f8f88f7cd735aecf041..b91497edc8b3bc8606054008898d8bc98a6da230 100644 (file)
--- a/util.c
+++ b/util.c
@@ -40,6 +40,7 @@
 #include "util.h"
 #include "unaligned.h"
 #include "magic.h"
 #include "util.h"
 #include "unaligned.h"
 #include "magic.h"
+#include "ipaddr.h"
 
 #define MIN_BUFFER_SIZE 64
 #define DEFAULT_BUFFER_SIZE 4096
 
 #define MIN_BUFFER_SIZE 64
 #define DEFAULT_BUFFER_SIZE 4096
@@ -479,6 +480,29 @@ extern void slilog_part(struct log_if *lf, int priority, const char *message, ..
     va_end(ap);
 }
 
     va_end(ap);
 }
 
+void text2iaddr(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)
 
 #define IADDR_NBUFS_SHIFT 3
 #define IADDR_NBUFS (1 << IADDR_NBUFS_SHIFT)
 
diff --git a/util.h b/util.h
index 836b53efe55c77fea0cfd5ac7f0ab5331c7690d3..1693d6097c72a93f84e130d3a3d32c824015cdc1 100644 (file)
--- a/util.h
+++ b/util.h
@@ -60,6 +60,9 @@ const char *iaddr_to_string(const union iaddr *ia);
 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib);
 int iaddr_socklen(const union iaddr *ia);
 
 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib);
 int iaddr_socklen(const union iaddr *ia);
 
+void text2iaddr(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);                    \
 #define MINMAX(ae,be,op) ({                    \
        typeof((ae)) a=(ae);                    \
        typeof((be)) b=(be);                    \