From 58639a0ef3e07a5a7bbd4792a82f4ab382d8de53 Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Tue, 25 Nov 2014 12:02:25 +0100 Subject: [PATCH] MINOR: global: export function and permits to not resolve DNS names exports the commonly used function str2ip. The function str2ip2 is created and permits to not resolve DNS names. --- include/common/standard.h | 25 +++++++++++++++++++++++++ src/standard.c | 9 +++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/common/standard.h b/include/common/standard.h index 427d0d7250..d0d5065dfa 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -294,6 +294,31 @@ int cidr2dotted(int cidr, struct in_addr *mask); */ int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask); +/* str2ip and str2ip2: + * + * converts to a struct sockaddr_storage* provided by the caller. The + * caller must have zeroed first, and may have set sa->ss_family to force + * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then + * the function tries to guess the address family from the syntax. If the + * family is forced and the format doesn't match, an error is returned. The + * string is assumed to contain only an address, no port. The address can be a + * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to + * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. + * The return address will only have the address family and the address set, + * all other fields remain zero. The string is not supposed to be modified. + * The IPv6 '::' address is IN6ADDR_ANY. + * + * str2ip2: + * + * If is set, this function try to resolve DNS, otherwise, it returns + * NULL result. + */ +struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve); +static inline struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa) +{ + return str2ip2(str, sa, 1); +} + /* * converts to two struct in6_addr* which must be pre-allocated. * The format is "addr[/mask]", where "addr" cannot be empty, and mask diff --git a/src/standard.c b/src/standard.c index f28825f827..79158ff8ba 100644 --- a/src/standard.c +++ b/src/standard.c @@ -567,9 +567,11 @@ const char *invalid_domainchar(const char *name) { * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. * The return address will only have the address family and the address set, * all other fields remain zero. The string is not supposed to be modified. - * The IPv6 '::' address is IN6ADDR_ANY. + * The IPv6 '::' address is IN6ADDR_ANY. If is non-zero, the hostname + * is resolved, otherwise only IP addresses are resolved, and anything else + * returns NULL. */ -static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa) +struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve) { struct hostent *he; @@ -603,6 +605,9 @@ static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage return sa; } + if (!resolve) + return NULL; + #ifdef USE_GETADDRINFO if (global.tune.options & GTUNE_USE_GAI) { struct addrinfo hints, *result;