]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: global: export function and permits to not resolve DNS names
authorThierry FOURNIER <tfournier@haproxy.com>
Tue, 25 Nov 2014 11:02:25 +0000 (12:02 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 28 Feb 2015 22:12:32 +0000 (23:12 +0100)
exports the commonly used function str2ip. The function str2ip2 is
created and permits to not resolve DNS names.

include/common/standard.h
src/standard.c

index 427d0d7250100f9b1e58e7cac90c11246a24be4a..d0d5065dfad4afe54d6d7daa62232caea7e24d8f 100644 (file)
@@ -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 <str> to a struct sockaddr_storage* provided by the caller. The
+ * caller must have zeroed <sa> 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 <resolve> 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 <str> to two struct in6_addr* which must be pre-allocated.
  * The format is "addr[/mask]", where "addr" cannot be empty, and mask
index f28825f827acba11f2e392189c88cfd770deecf4..79158ff8baba93eeae3372befbf0b2ca0c5937ca 100644 (file)
@@ -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 <resolve> 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;