Example #1
0
IPAddress::IPAddress(const IPAddress& addr)
{
	if (addr.family() == IPv4)
		newIPv4(addr.addr());
	else
		newIPv6(addr.addr(), addr.scope());
}
Example #2
0
void SocketAddress::init(const IPAddress& host, Poco::UInt16 port)
{
	if (host.family() == IPAddress::IPv4)
		_pImpl = new IPv4SocketAddressImpl(host.addr(), htons(port));
#if defined(POCO_HAVE_IPv6)
	else if (host.family() == IPAddress::IPv6)
		_pImpl = new IPv6SocketAddressImpl(host.addr(), htons(port));
#endif
	else throw Poco::NotImplementedException("unsupported IP address family");
}
Example #3
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 #4
0
	IPv4SocketAddress(const IPAddress& host, UInt16 port) : _host(host) {
		memset(&_addr, 0, sizeof(_addr));
		_addr.sin_family = AF_INET;
		NET_SOCKLEN size(0);
		memcpy(&_addr.sin_addr, host.addr(size), sizeof(_addr.sin_addr));
		_addr.sin_port = port;
	}
Example #5
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 #6
0
	IPv6SocketAddress(const IPAddress& host, UInt16 port, UInt32 scope = 0) : _host(host) {
		memset(&_addr, 0, sizeof(_addr));
		_addr.sin6_family = AF_INET6;
		set_sin6_len(&_addr);
		memcpy(&_addr.sin6_addr, host.addr(), sizeof(_addr.sin6_addr));
		_addr.sin6_port = port;
		_addr.sin6_scope_id = scope;
	}
Example #7
0
bool IPAddress::operator >= (const IPAddress& a) const
{
	poco_socklen_t l1 = length();
	poco_socklen_t l2 = a.length();
	if (l1 == l2)
		return std::memcmp(addr(), a.addr(), l1) >= 0;
	else
		return l1 > l2;
}
Example #8
0
void MulticastSocket::leaveGroup(const IPAddress& groupAddress, const NetworkInterface& interfc)
{
	if (groupAddress.af() == AF_INET)
	{
		struct ip_mreq mr;
		std::memcpy(&mr.imr_multiaddr, groupAddress.addr(), groupAddress.length());
		std::memcpy(&mr.imr_interface, interfc.firstAddress(IPAddress::IPv4).addr(), interfc.firstAddress(IPAddress::IPv4).length());
		impl()->setRawOption(IPPROTO_IP, IP_DROP_MEMBERSHIP, &mr, sizeof(mr));
	}
	else
	{
#if defined(POCO_HAVE_IPv6)
		struct ipv6_mreq mr;
		std::memcpy(&mr.ipv6mr_multiaddr, groupAddress.addr(), groupAddress.length());
		mr.ipv6mr_interface = interfc.index();
		impl()->setRawOption(IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mr, sizeof(mr));
#endif
	}
}
Example #9
0
bool IPAddress::operator < (const IPAddress& a) const
{
	poco_socklen_t l1 = length();
	poco_socklen_t l2 = a.length();
	if (l1 == l2)
    {
#if defined(POCO_HAVE_IPv6)
        if ( scope() != a.scope() )
            return scope() < a.scope();
#endif
		return std::memcmp(addr(), a.addr(), l1) < 0;
    }
	else return l1 < l2;
}
Example #10
0
bool Address::operator==(const SocketAddress& address) const {
	if(port != address.port())
		return false;
	IPAddress tmp = address.host();
	const UInt8* bytes = reinterpret_cast<const UInt8*>(tmp.addr());
	if(tmp.family() == IPAddress::IPv6) {
		if(host.size()!=16)
			return false;
	} else {
		if(host.size()!=4)
			return false;
	}
	for(int i=0;i<host.size();++i) {
		if(bytes[i]!=host[i])
			return false;
	}
	return true;
}
Example #11
0
void SocketImpl::setOption(int level, int option, const IPAddress& value)
{
	setRawOption(level, option, value.addr(), value.length());
}