From c6a3b1f54ccc754be82f0a2ea5b4d60746053a2b Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 2 Sep 2014 09:04:27 +0100 Subject: [PATCH 1/1] Provide text2iaddr. This will be used shortly. Signed-off-by: Ian Jackson --- util.c | 24 ++++++++++++++++++++++++ util.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/util.c b/util.c index 2299f72..b91497e 100644 --- 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 @@ -479,6 +480,29 @@ extern void slilog_part(struct log_if *lf, int priority, const char *message, .. 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) diff --git a/util.h b/util.h index 836b53e..1693d60 100644 --- 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); +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); \ -- 2.30.2