Ejemplo n.º 1
0
// public
bool IPAddress::inSubnet(const IPAddress& subnet, uint8_t cidr) const {
  if (bitCount() == subnet.bitCount()) {
    if (isV4()) {
      return asV4().inSubnet(subnet.asV4(), cidr);
    } else {
      return asV6().inSubnet(subnet.asV6(), cidr);
    }
  }
  // an IPv4 address can never belong in a IPv6 subnet unless the IPv6 is a 6to4
  // address and vice-versa
  if (isV6()) {
    const IPAddressV6& v6addr = asV6();
    const IPAddressV4& v4subnet = subnet.asV4();
    if (v6addr.is6To4()) {
      return v6addr.getIPv4For6To4().inSubnet(v4subnet, cidr);
    }
  } else if (subnet.isV6()) {
    const IPAddressV6& v6subnet = subnet.asV6();
    const IPAddressV4& v4addr = asV4();
    if (v6subnet.is6To4()) {
      return v4addr.inSubnet(v6subnet.getIPv4For6To4(), cidr);
    }
  }
  return false;
}
Ejemplo n.º 2
0
// public
bool IPAddress::inSubnetWithMask(const IPAddress& subnet, ByteRange mask)
    const {
  auto mkByteArray4 = [&]() -> ByteArray4 {
    ByteArray4 ba{{0}};
    std::memcpy(ba.data(), mask.begin(), std::min<size_t>(mask.size(), 4));
    return ba;
  };

  if (bitCount() == subnet.bitCount()) {
    if (isV4()) {
      return asV4().inSubnetWithMask(subnet.asV4(), mkByteArray4());
    } else {
      ByteArray16 ba{{0}};
      std::memcpy(ba.data(), mask.begin(), std::min<size_t>(mask.size(), 16));
      return asV6().inSubnetWithMask(subnet.asV6(), ba);
    }
  }

  // an IPv4 address can never belong in a IPv6 subnet unless the IPv6 is a 6to4
  // address and vice-versa
  if (isV6()) {
    const IPAddressV6& v6addr = asV6();
    const IPAddressV4& v4subnet = subnet.asV4();
    if (v6addr.is6To4()) {
      return v6addr.getIPv4For6To4().inSubnetWithMask(v4subnet, mkByteArray4());
    }
  } else if (subnet.isV6()) {
    const IPAddressV6& v6subnet = subnet.asV6();
    const IPAddressV4& v4addr = asV4();
    if (v6subnet.is6To4()) {
      return v4addr.inSubnetWithMask(v6subnet.getIPv4For6To4(), mkByteArray4());
    }
  }
  return false;
}
Ejemplo n.º 3
0
QHostAddress HostAddress::toAddress() const {
	if (isV6())
		return QHostAddress(qip6);
	else {
		return QHostAddress(ntohl(hash[3]));
	}
}
Ejemplo n.º 4
0
size_t IPAddress::printTo(Print& p) const {
    size_t n = 0;

    if (!isSet())
        return p.print(F("(IP unset)"));

#if LWIP_IPV6
    if (isV6()) {
        int count0 = 0;
        for (int i = 0; i < 8; i++) {
            uint16_t bit = PP_NTOHS(raw6()[i]);
            if (bit || count0 < 0) {
                n += p.printf("%x", bit);
                if (count0 > 0)
                    // no more hiding 0
                    count0 = -8;
            } else
                count0++;
            if ((i != 7 && count0 < 2) || count0 == 7)
                n += p.print(':');
        }
        return n;
    }
#endif

    for(int i = 0; i < 4; i++) {
        n += p.print((*this)[i], DEC);
        if (i != 3)
            n += p.print('.');
    }
    return n;
}
Ejemplo n.º 5
0
std::string SocketAddress::toString(void) const
{
	char buffer[mxos::MAX_ADDR_V6_LEN + 1] =
	{ 0 };

	UShort port = getPort();

	if (isV6())
	{
		if (NULL != mxos::inetNtoP(AF_INET6, (void*) &data_.in6_.sin6_addr,
				buffer, mxos::MAX_ADDR_V6_LEN, 0))
		{
			std::string ret("[");
			ret += buffer;
			ret += "]:";
			ret += port.toString();
			return buffer;
		}
	}
	else
	{
		if (NULL != mxos::inetNtoP(AF_INET, (void*) &data_.in4_.sin_addr,
				buffer, mxos::MAX_ADDR_V6_LEN, 0))
		{

			std::string ret(buffer);
			ret += ":";
			ret += port.toString();
			return ret;
		}

	}
	return "";
}
Ejemplo n.º 6
0
String IPAddress::toString() const
{
    StreamString sstr;
#if LWIP_IPV6
    if (isV6())
        sstr.reserve(40); // 8 shorts x 4 chars each + 7 colons + nullterm
    else
#endif
        sstr.reserve(16); // 4 bytes with 3 chars max + 3 dots + nullterm, or '(IP unset)'
    printTo(sstr);
    return sstr;
}
Ejemplo n.º 7
0
void HostAddress::toSockaddr(sockaddr_storage *dst) const {
	memset(dst, 0, sizeof(*dst));
	if (isV6()) {
		struct sockaddr_in6 *in6 = reinterpret_cast<struct sockaddr_in6 *>(dst);
		dst->ss_family = AF_INET6;
		memcpy(in6->sin6_addr.s6_addr, qip6.c, 16);
	} else {
		struct sockaddr_in *in = reinterpret_cast<struct sockaddr_in *>(dst);
		dst->ss_family = AF_INET;
		in->sin_addr.s_addr = hash[3];
	}
}
Ejemplo n.º 8
0
QString HostAddress::toString() const {
	if (isV6()) {
		if (isValid()) {
			QString qs;
			qs.sprintf("[%x:%x:%x:%x:%x:%x:%x:%x]", ntohs(shorts[0]), ntohs(shorts[1]), ntohs(shorts[2]), ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]), ntohs(shorts[7]));
			return qs.replace(QRegExp(QLatin1String("(:0)+")),QLatin1String(":"));
		} else {
			return QLatin1String("[::]");
		}
	} else {
		return QHostAddress(ntohl(hash[3])).toString();
	}
}
Ejemplo n.º 9
0
std::string SocketAddress::toAddressString(void) const
{
	char buffer[mxos::MAX_ADDR_V6_LEN + 1] =
	{ 0 };

	if (isV6())
	{
		if (NULL != mxos::inetNtoP(AF_INET6, (void*) &data_.in6_.sin6_addr,
				buffer, mxos::MAX_ADDR_V6_LEN, 0))
		{
			return buffer;
		}
	}
	else
	{
		if (NULL != mxos::inetNtoP(AF_INET, (void*) &data_.in4_.sin_addr,
				buffer, mxos::MAX_ADDR_V6_LEN, 0))
		{
			return buffer;
		}
	}
	return "";
}
Ejemplo n.º 10
0
size_t SocketAddress::addressLength(void) const
{
	return isV6() ? sizeof(struct in6_addr) : sizeof(struct in_addr);
}
Ejemplo n.º 11
0
const void* SocketAddress::address(void) const
{
	return isV6() ? (const void*) &data_.in6_.sin6_addr
			: (const void*) &data_.in4_.sin_addr;
}