Example #1
0
File: DNS.cpp Project: 12307/poco
HostEntry DNS::hostByAddress(const IPAddress& address, unsigned 
#ifdef POCO_HAVE_ADDRINFO
							 hintFlags
#endif
							)
{
#if defined(POCO_HAVE_LIBRESOLV)
	Poco::ScopedReadRWLock readLock(resolverLock);
#endif

#if defined(POCO_HAVE_ADDRINFO)
	SocketAddress sa(address, 0);
	static char fqname[1024];
	int rc = getnameinfo(sa.addr(), sa.length(), fqname, sizeof(fqname), NULL, 0, NI_NAMEREQD); 
	if (rc == 0)
	{
		struct addrinfo* pAI;
		struct addrinfo hints;
		std::memset(&hints, 0, sizeof(hints));
		hints.ai_flags = hintFlags;
		rc = getaddrinfo(fqname, NULL, &hints, &pAI);
		if (rc == 0)
		{
			HostEntry result(pAI);
			freeaddrinfo(pAI);
			return result;
		}
		else
		{
			aierror(rc, address.toString());
		}
	}
	else
	{
		aierror(rc, address.toString());
	}
#elif defined(POCO_VXWORKS)
	char name[MAXHOSTNAMELEN + 1];
	if (hostGetByAddr(*reinterpret_cast<const int*>(address.addr()), name) == OK)
	{
		return HostEntry(std::string(name), address);
	}
#else
	struct hostent* he = gethostbyaddr(reinterpret_cast<const char*>(address.addr()), address.length(), address.af());
	if (he)
	{
		return HostEntry(he);
	}
#endif
	int err = lastError();
	error(err, address.toString()); // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #2
0
HostEntry DNS::hostByAddress(const IPAddress& address)
{
	NetworkInitializer networkInitializer;

#if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO)
	SocketAddress sa(address, 0);
	static char fqname[1024];
	int rc = getnameinfo(sa.addr(), sa.length(), fqname, sizeof(fqname), NULL, 0, NI_NAMEREQD); 
	if (rc == 0)
	{
		struct addrinfo* pAI;
		struct addrinfo hints;
		std::memset(&hints, 0, sizeof(hints));
		hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
		rc = getaddrinfo(fqname, NULL, &hints, &pAI);
		if (rc == 0)
		{
			HostEntry result(pAI);
			freeaddrinfo(pAI);
			return result;
		}
		else
		{
			aierror(rc, address.toString());
		}
	}
	else
	{
		aierror(rc, address.toString());
	}
#elif defined(POCO_VXWORKS)
	char name[MAXHOSTNAMELEN + 1];
	if (hostGetByAddr(*reinterpret_cast<const int*>(address.addr()), name) == OK)
	{
		return HostEntry(std::string(name), address);
	}
#else
	struct hostent* he = gethostbyaddr(reinterpret_cast<const char*>(address.addr()), address.length(), address.af());
	if (he)
	{
		return HostEntry(he);
	}
#endif
	int err = lastError();
	error(err, address.toString());      // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #3
0
File: DNS.cpp Project: 12307/poco
HostEntry DNS::hostByName(const std::string& hostname, unsigned 
#ifdef POCO_HAVE_ADDRINFO
						  hintFlags
#endif
						 )
{
#if defined(POCO_HAVE_LIBRESOLV)
	Poco::ScopedReadRWLock readLock(resolverLock);
#endif

#if defined(POCO_HAVE_ADDRINFO)
	struct addrinfo* pAI;
	struct addrinfo hints;
	std::memset(&hints, 0, sizeof(hints));
	hints.ai_flags = hintFlags;
	int rc = getaddrinfo(hostname.c_str(), NULL, &hints, &pAI); 
	if (rc == 0)
	{
		HostEntry result(pAI);
		freeaddrinfo(pAI);
		return result;
	}
	else
	{
		aierror(rc, hostname);
	}
#elif defined(POCO_VXWORKS)
	int addr = hostGetByName(const_cast<char*>(hostname.c_str()));
	if (addr != ERROR)
	{
		return HostEntry(hostname, IPAddress(&addr, sizeof(addr)));
	}
#else
	struct hostent* he = gethostbyname(hostname.c_str());
	if (he)
	{
		return HostEntry(he);
	}
#endif
	error(lastError(), hostname); // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #4
0
HostEntry DNS::hostByName(const std::string& hostname)
{
	NetworkInitializer networkInitializer;
	
#if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO)
	struct addrinfo* pAI;
	struct addrinfo hints;
	std::memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
	int rc = getaddrinfo(hostname.c_str(), NULL, &hints, &pAI); 
	if (rc == 0)
	{
		HostEntry result(pAI);
		freeaddrinfo(pAI);
		return result;
	}
	else
	{
		aierror(rc, hostname);
	}
#elif defined(POCO_VXWORKS)
	int addr = hostGetByName(const_cast<char*>(hostname.c_str()));
	if (addr != ERROR)
	{
		return HostEntry(hostname, IPAddress(&addr, sizeof(addr)));
	}
#else
	struct hostent* he = gethostbyname(hostname.c_str());
	if (he)
	{
		return HostEntry(he);
	}
#endif
	error(lastError(), hostname);      // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #5
0
const HostEntry& DNS::hostByName(const std::string& hostname)
{
	FastMutex::ScopedLock lock(_mutex);
	
	DNSCache::const_iterator it = _cache.find(hostname);
	if (it != _cache.end())
	{
		return it->second;
	}
	else
	{
#if defined(_WIN32) && defined(POCO_HAVE_IPv6)
		struct addrinfo* pAI;
		struct addrinfo hints;
		memset(&hints, 0, sizeof(hints));
		hints.ai_flags = AI_CANONNAME;
		if (getaddrinfo(hostname.c_str(), NULL, &hints, &pAI) == 0)
		{
			std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(hostname, HostEntry(pAI)));
			freeaddrinfo(pAI);
			return res.first->second;
		}
#elif defined(POCO_VXWORKS)
		static char buffer[2048];
		struct hostent* he = resolvGetHostByName((char*) hostname.c_str(), buffer, sizeof(buffer));
		if (he)
		{
			std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(hostname, HostEntry(he)));
			return res.first->second;
		}
#else
		struct hostent* he = gethostbyname(hostname.c_str());
		if (he)
		{
			std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(hostname, HostEntry(he)));
			return res.first->second;
		}
#endif
	}
	error(lastError(), hostname);      // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #6
0
const HostEntry& DNS::hostByAddress(const IPAddress& address)
{
	FastMutex::ScopedLock lock(_mutex);

#if defined(_WIN32) && defined(POCO_HAVE_IPv6)
	SocketAddress sa(address, 0);
	static char fqname[1024];
	if (getnameinfo(sa.addr(), sa.length(), fqname, sizeof(fqname), NULL, 0, 0) == 0)
	{
		DNSCache::const_iterator it = _cache.find(std::string(fqname));
		if (it != _cache.end())
		{
			return it->second;
		}
		else
		{
			struct addrinfo* pAI;
			struct addrinfo hints;
			memset(&hints, 0, sizeof(hints));
			hints.ai_flags = AI_CANONNAME;
			if (getaddrinfo(fqname, NULL, &hints, &pAI) == 0)
			{
				std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(std::string(fqname), HostEntry(pAI)));
				freeaddrinfo(pAI);
				return res.first->second;
			}
		}
	}
#elif defined(POCO_VXWORKS)
	char buffer[2048];
	struct hostent* he = resolvGetHostByAddr(reinterpret_cast<const char*>(address.addr()), buffer, sizeof(buffer));
	if (he)
	{
		std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(std::string(he->h_name), HostEntry(he)));
		return res.first->second;
	}
#else
	struct hostent* he = gethostbyaddr(reinterpret_cast<const char*>(address.addr()), address.length(), address.af());
	if (he)
	{
		std::pair<DNSCache::iterator, bool> res = _cache.insert(std::pair<std::string, HostEntry>(std::string(he->h_name), HostEntry(he)));
		return res.first->second;
	}
#endif
	int err = lastError();
	error(err, address.toString());      // will throw an appropriate exception
	throw NetException(); // to silence compiler
}
Example #7
0
//	////////////////////////////////////////////////////////////////////////////
HostEntry HostNameToEntry(const char *host_name)
{
	if ((host_name == NULL) || (!(*host_name)))
		MLB::Utility::ThrowInvalidArgument("The host name specified for "
			"resolution is " + std::string((host_name == NULL) ? "NULL." :
			"an empty string."));

	HostEntry out_host_entry;

	try {
		hostent                 *host_entry_ptr = NULL;
#ifdef MLB_SOCKET_IO_RESOLVE_HOST_CPP_HAS__gethostbyname_r
		hostent                  host_entry;
		char                     tmp_buffer[0xFFFF + 1];
		std::size_t              buffer_length = sizeof(tmp_buffer);
		boost::shared_ptr<char>  buffer_sptr(tmp_buffer, ResolveHostNullDeleter());
		for ( ; ; ) {
			int local_errno;
# if defined(__SVR4)
			if ((host_entry_ptr = ::gethostbyname_r(host_name, &host_entry,
				buffer_sptr.get(), static_cast<int>(buffer_length),
				&local_errno)) == NULL)
				MLB::Utility::ThrowSystemError("Unable to locate host with "
					"'gethostbyname_r()'.");
			else if (!local_errno)
				break;
			else if (local_errno != ERANGE)
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyname_r()' failed");
			else if (buffer_length >= (1 << 20))
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyname_r()' failed with 'ERANGE' and the "
					"maximum provisional buffer size was reached");
# elif __linux__
			if (::gethostbyname_r(host_name, &host_entry, buffer_sptr.get(),
				static_cast<int>(buffer_length), &host_entry_ptr,
				&local_errno) == 0) {
				if (host_entry_ptr == NULL)
					MLB::Utility::ThrowSystemError("Unable to locate host with "
						"'gethostbyname_r()'.");
				break;
			}
			else if (local_errno != ERANGE)
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyname_r()' failed");
			else if (buffer_length >= (1 << 20))
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyname_r()' failed with 'ERANGE' and the "
					"maximum provisional buffer size was reached");
# endif // #if defined(__SVR4)
			buffer_length += 0xFFFF + 1;
			buffer_sptr.reset(new char[buffer_length]);
		}
#else
		if ((host_entry_ptr = ::gethostbyname(host_name)) == NULL)
			MLB::Utility::ThrowSystemError("Unable to locate host with "
				"'gethostbyname()'.");
#endif // #ifdef MLB_SOCKET_IO_RESOLVE_HOST_CPP_HAS__gethostbyname_r
		HostEntry(host_entry_ptr).swap(out_host_entry);
	}
	catch (const std::exception &except) {
		MLB::Utility::Rethrow(except, "Unable to resolve host name '" +
			std::string(host_name) + "': " + std::string(except.what()));
	}

	return(out_host_entry);
}
Example #8
0
//	////////////////////////////////////////////////////////////////////////////
HostEntry HostAddressToEntry(const void *host_address,
	int host_address_length, int host_address_type)
{
	if (host_address == NULL)
		MLB::Utility::ThrowInvalidArgument("The host address specified for "
			"resolution is a NULL pointer.");
	else if (host_address_length < 1)
		MLB::Utility::ThrowInvalidArgument("The length of the host address "
			"specified for resolution (" +
			MLB::Utility::AnyToString(host_address_length) + ") is less than 1.");

	HostEntry out_host_entry;

	try {
		hostent                 *host_entry_ptr = NULL;
#ifdef MLB_SOCKET_IO_RESOLVE_HOST_CPP_HAS__gethostbyaddr_r
		hostent                  host_entry;
		char                     tmp_buffer[0xFFFF + 1];
		std::size_t              buffer_length = sizeof(tmp_buffer);
		boost::shared_ptr<char>  buffer_sptr(tmp_buffer, ResolveHostNullDeleter());
		for ( ; ; ) {
			int local_errno;
# if defined(__SVR4)
			if ((host_entry_ptr = ::gethostbyaddr_r(host_address,
				host_address_length, host_address_type, &host_entry,
				buffer_sptr.get(), static_cast<int>(buffer_length),
				&local_errno)) == NULL)
				MLB::Utility::ThrowSystemError("Unable to locate host with "
					"'gethostbyaddr_r()'.");
			else if (!local_errno)
				break;
			else if (local_errno != ERANGE)
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyaddr_r()' failed");
			else if (buffer_length >= (1 << 20))
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyaddr_r()' failed with 'ERANGE' and the "
					"maximum provisional buffer size was reached");
# elif __linux__
			if (::gethostbyaddr_r(host_address, host_address_length,
				host_address_type, &host_entry, buffer_sptr.get(),
				static_cast<int>(buffer_length), &host_entry_ptr,
				&local_errno) == 0) {
				if (host_entry_ptr == NULL)
					MLB::Utility::ThrowSystemError("Unable to locate host with "
						"'gethostbyaddr_r()'.");
				break;
			}
			else if (local_errno != ERANGE)
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyaddr_r()' failed");
			else if (buffer_length >= (1 << 20))
				MLB::Utility::ThrowSystemError(local_errno,
					"Call to 'gethostbyaddr_r()' failed with 'ERANGE' and the "
					"maximum provisional buffer size was reached");
# endif // #if defined(__SVR4)
			buffer_length += 0xFFFF + 1;
			buffer_sptr.reset(new char[buffer_length]);
		}
#else
# if _MSC_VER
		if ((host_entry_ptr = ::gethostbyaddr(
			static_cast<const char *>(host_address), host_address_length,
			host_address_type)) == NULL)
# else
		if ((host_entry_ptr = ::gethostbyaddr(host_address,
			host_address_length, host_address_type)) == NULL)
# endif // # if _MSC_VER
			MLB::Utility::ThrowSystemError("Unable to locate host with "
				"'gethostbyaddr()'.");
#endif // #ifdef MLB_SOCKET_IO_RESOLVE_HOST_CPP_HAS__gethostbyaddr_r
		HostEntry(host_entry_ptr).swap(out_host_entry);
	}
	catch (const std::exception &except) {
		MLB::Utility::Rethrow(except, "Unable to resolve host address at '" +
			MLB::Utility::ValueToStringHex(host_address) + " (" +
			MLB::Utility::ToHexString(
			static_cast<std::size_t>(host_address_length),
			static_cast<const char *>(host_address)) + ") with an address length "
			"of " + MLB::Utility::AnyToString(host_address_length) + " and an "
			"address type of " + MLB::Utility::AnyToString(host_address_type) +
			": " + std::string(except.what()));
	}

	return(out_host_entry);
}