From: Ian Jackson Date: Tue, 2 Sep 2014 08:04:27 +0000 (+0100) Subject: Provide text2iaddr. X-Git-Tag: base.polypath.v3~14 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=7f47a4c64dd0355cf9c000c5a137261780d970fc Provide text2iaddr. This will be used shortly. Signed-off-by: Ian Jackson --- diff --git a/util.c b/util.c index 426fc69..0914732 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 @@ -478,6 +479,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 c4e34a8..6b0b4a3 100644 --- a/util.h +++ b/util.h @@ -85,6 +85,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); \