Exemple #1
0
ADD_TEST(DNSTest, HostByName) {
	Exception ex;
	HostEntry hostEntry;

	DNS::HostByName(ex, "aliastest.appinf.com", hostEntry);
	CHECK(!ex)
	// different systems report different canonical names, unfortunately.
	CHECK(hostEntry.name() == "dnstest.appinf.com" || hostEntry.name() == "aliastest.appinf.com");

	CHECK(hostEntry.addresses().size() >= 1);
	CHECK(hostEntry.addresses().front().toString() == "1.2.3.4");

	DNS::HostByName(ex, "nohost.appinf.com", hostEntry);
	CHECK(ex); // must not to find the host
}
bool SocketAddress::setIntern(Exception& ex,const string& host, UInt16 port,bool resolveHost) {
	IPAddress ip;
	Exception ignore;
	if (ip.set(ignore, host)) {
		set(ip, port);
		return true;
	}
	HostEntry entry;
	if (!resolveHost) {
		if (ignore)
			ex.set(ignore);
		return false;
	}
	if (!DNS::HostByName(ex, host, entry))
		return false;
	auto& addresses = entry.addresses();
	if (addresses.size() > 0) {
		// if we get both IPv4 and IPv6 addresses, prefer IPv4
		for (const IPAddress& address : addresses) {
			if (address.family() == IPAddress::IPv4) {
				set(address, port);
				return true;
			}
		}
		set(addresses.front(), port);
		return true;
	}
	ex.set(Exception::NETADDRESS, "No address found for the host ", host);
	return false;
}
Exemple #3
0
ADD_TEST(DNSTest, HostByAddress) {
	Exception ex;
	HostEntry hostEntry;

	IPAddress ip;
	ip.set(ex, "80.122.195.86");
	CHECK(!ex)
	DNS::HostByAddress(ex, ip, hostEntry);
	CHECK(!ex)
	CHECK(hostEntry.name() == "mailhost.appinf.com");
	CHECK(hostEntry.aliases().empty());
	CHECK(hostEntry.addresses().size() >= 1);
	CHECK(hostEntry.addresses().front().toString() == "80.122.195.86");

	ip.set(ex, "10.0.244.253");
	CHECK(!ex)
	DNS::HostByAddress(ex, ip, hostEntry);
	CHECK(ex)
}
void SocketAddress::init(const std::string& host, Poco::UInt16 port)
{
	IPAddress ip;
	if (IPAddress::tryParse(host, ip))
	{
		init(ip, port);
	}
	else
	{
		HostEntry he = DNS::hostByName(host);
		HostEntry::AddressList addresses = he.addresses();
		if (addresses.size() > 0)
		{
#if defined(POCO_HAVE_IPv6)
			// if we get both IPv4 and IPv6 addresses, prefer IPv4
			std::sort(addresses.begin(), addresses.end(), AFLT());
#endif
			init(addresses[0], port);
		}
		else throw HostNotFoundException("No address found for host", host);
	}
}