From: Timo Sirainen Date: Fri, 28 Jun 2013 16:48:15 +0000 (+0300) Subject: Added net_gethostbyaddr() for DNS PTR lookups. X-Git-Tag: 2.2.5~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=684b65874c28386cc14e4d90eecf002ffefa15a4;p=thirdparty%2Fdovecot%2Fcore.git Added net_gethostbyaddr() for DNS PTR lookups. This code assumes we have IPv6 capability. Perhaps we should just require it everywhere already.. --- diff --git a/src/lib/net.c b/src/lib/net.c index 4162bf1f1d..b78657b34f 100644 --- a/src/lib/net.c +++ b/src/lib/net.c @@ -663,6 +663,24 @@ int net_gethostbyname(const char *addr, struct ip_addr **ips, return 0; } +int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r) +{ + union sockaddr_union so; + socklen_t addrlen = sizeof(so); + char hbuf[NI_MAXHOST]; + int ret; + + memset(&so, 0, sizeof(so)); + sin_set_ip(&so, ip); + ret = getnameinfo(&so.sa, addrlen, hbuf, sizeof(hbuf), NULL, 0, + NI_NAMEREQD); + if (ret != 0) + return ret; + + *name_r = t_strdup(hbuf); + return 0; +} + int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) { union sockaddr_union so; diff --git a/src/lib/net.h b/src/lib/net.h index ed084e0fee..6248733576 100644 --- a/src/lib/net.h +++ b/src/lib/net.h @@ -105,6 +105,9 @@ ssize_t net_transmit(int fd, const void *data, size_t len); to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ int net_gethostbyname(const char *addr, struct ip_addr **ips, unsigned int *ips_count); +/* Return host for the IP address. Returns 0 = ok, others = error code for + net_gethosterror(). */ +int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r); /* get error of net_gethostname() */ const char *net_gethosterror(int error) ATTR_CONST; /* return TRUE if host lookup failed because it didn't exist (ie. not