#include <arpa/inet.h>
#include <netdb.h>
+#include <string.h>
#include <ipfunc.hxx>
return target;
}
-string IP_RANGE::resolve_ip(const string &iporname)
+string IP_RANGE::resolve_ip(const string &iporname, const bool enable_ipv6)
{
struct in_addr ip_adr;
- struct hostent *dnsdata;
-
+
if (inet_aton(iporname.c_str(),&ip_adr) != 0)
{
// is already a ip
}
// we need dns
- dnsdata=gethostbyname(iporname.c_str());
+ struct addrinfo hints, *result = NULL;
+ memset(&hints, 0, sizeof(struct addrinfo));
+ if (enable_ipv6)
+ hints.ai_family = AF_UNSPEC;
+ else
+ hints.ai_family = AF_INET;
- if (dnsdata==NULL)
- throw dns_exception(hstrerror(h_errno));
+ if (getaddrinfo(iporname.c_str(), NULL, &hints, &result) != 0)
+ throw dns_exception("no corresponding ip found for: "+iporname);
- if (dnsdata->h_addr_list == NULL || *(dnsdata->h_addr_list) == NULL)
+ if (result == NULL || result->ai_addrlen == 0 || result->ai_addr == NULL)
throw dns_exception("no corresponding ip found for: "+iporname);
- return inet_ntoa(*(struct in_addr*)(*dnsdata->h_addr_list));
+ // Convert addr to ip string
+ char str_ip[NI_MAXHOST], dummy_serv[NI_MAXSERV];
+ if (getnameinfo(result->ai_addr, result->ai_addrlen, str_ip, NI_MAXHOST, dummy_serv, NI_MAXSERV, NI_NUMERICHOST) != 0)
+ throw dns_exception("can't convert ip for: "+iporname);
+
+ freeaddrinfo(result);
+
+ return string(str_ip);
}
- /*
+/*
The software in this package is distributed under the GNU General
Public License version 2 (with a special exception described below).
static unsigned int calc_netmask_from_cidr(unsigned int cidr);
static std::string ip_string(unsigned int ip);
static std::string ip_num_string(unsigned int ip);
- static std::string resolve_ip(const std::string &iporname);
+ static std::string resolve_ip(const std::string &iporname, const bool enable_ipv6=false);
};
class dns_exception : public std::runtime_error
BOOST_CHECK_EQUAL(IP_RANGE("192.168.2.0/255.255.255.0"),myip);
}
+BOOST_AUTO_TEST_CASE(ResolveDNS)
+{
+ IP_RANGE test;
+ BOOST_CHECK_EQUAL("127.0.0.1", test.resolve_ip("localhost"));
+}
+
+/*
+// Note: Disabled on the Intranator
+BOOST_AUTO_TEST_CASE(ResolveDNSIPv6)
+{
+ IP_RANGE test;
+ BOOST_CHECK_EQUAL("::1", test.resolve_ip("localhost", true));
+}
+*/
+
+BOOST_AUTO_TEST_CASE(ResolveExistingIP)
+{
+ IP_RANGE test;
+ BOOST_CHECK_EQUAL("192.168.1.254", test.resolve_ip("192.168.1.254"));
+}
+
+
BOOST_AUTO_TEST_SUITE_END()