本文整理汇总了C++中IPAddress::isV6方法的典型用法代码示例。如果您正苦于以下问题:C++ IPAddress::isV6方法的具体用法?C++ IPAddress::isV6怎么用?C++ IPAddress::isV6使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPAddress
的用法示例。
在下文中一共展示了IPAddress::isV6方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sizeof
// tests code example
TEST(IPAddress, CodeExample) {
EXPECT_EQ(4, sizeof(IPAddressV4));
EXPECT_EQ(20, sizeof(IPAddressV6));
EXPECT_EQ(24, sizeof(IPAddress));
IPAddress uninitaddr;
IPAddress v4addr("192.0.2.129");
IPAddress v6map("::ffff:192.0.2.129");
ASSERT_TRUE(uninitaddr.empty());
ASSERT_FALSE(v4addr.empty());
ASSERT_FALSE(v6map.empty());
EXPECT_TRUE(v4addr.inSubnet("192.0.2.0/24"));
EXPECT_TRUE(v4addr.inSubnet(IPAddress("192.0.2.0"), 24));
EXPECT_TRUE(v4addr.inSubnet("192.0.2.128/30"));
EXPECT_FALSE(v4addr.inSubnet("192.0.2.128/32"));
EXPECT_EQ(2164392128, v4addr.asV4().toLong());
EXPECT_EQ(3221226113, v4addr.asV4().toLongHBO());
ASSERT_FALSE(uninitaddr.isV4());
ASSERT_FALSE(uninitaddr.isV6());
ASSERT_TRUE(v4addr.isV4());
ASSERT_TRUE(v6map.isV6());
EXPECT_TRUE(v4addr == v6map);
ASSERT_TRUE(v6map.isIPv4Mapped());
EXPECT_TRUE(v4addr.asV4() == IPAddress::createIPv4(v6map));
EXPECT_TRUE(IPAddress::createIPv6(v4addr) == v6map.asV6());
}
示例2: bytes
TEST(IPAddress, fromBinaryV6) {
for (auto& tc : provideBinary16Bytes) {
SCOPED_TRACE(tc.first);
ByteRange bytes(&tc.second.front(), tc.second.size());
auto fromBin = IPAddressV6::fromBinary(bytes);
IPAddressV6 fromStr(tc.first);
EXPECT_EQ(fromStr, fromBin);
IPAddressV6 addr2("::0");
addr2 = IPAddressV6::fromBinary(bytes);
EXPECT_EQ(fromStr, addr2);
IPAddress genericAddr = IPAddress::fromBinary(bytes);
ASSERT_TRUE(genericAddr.isV6());
EXPECT_EQ(fromStr, genericAddr.asV6());
EXPECT_EQ(ByteRange(genericAddr.bytes(), genericAddr.byteCount()), bytes);
}
uint8_t data[20];
EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 3)),
IPAddressFormatException);
EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 4)),
IPAddressFormatException);
EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 20)),
IPAddressFormatException);
}
示例3:
// public static
IPAddressV6 IPAddress::createIPv6(const IPAddress& addr) {
if (addr.isV6()) {
return addr.asV6();
} else {
return addr.asV4().createIPv6();
}
}
示例4: return
// public
bool operator==(const IPAddress& addr1, const IPAddress& addr2) {
if (addr1.family() == addr2.family()) {
if (addr1.isV6()) {
return (addr1.asV6() == addr2.asV6());
} else if (addr1.isV4()) {
return (addr1.asV4() == addr2.asV4());
} else {
CHECK_EQ(addr1.family(), AF_UNSPEC);
// Two default initialized AF_UNSPEC addresses should be considered equal.
// AF_UNSPEC is the only other value for which an IPAddress can be
// created, in the default constructor case.
return true;
}
}
// addr1 is v4 mapped v6 address, addr2 is v4
if (addr1.isIPv4Mapped() && addr2.isV4()) {
if (IPAddress::createIPv4(addr1) == addr2.asV4()) {
return true;
}
}
// addr2 is v4 mapped v6 address, addr1 is v4
if (addr2.isIPv4Mapped() && addr1.isV4()) {
if (IPAddress::createIPv4(addr2) == addr1.asV4()) {
return true;
}
}
// we only compare IPv4 and IPv6 addresses
return false;
}
示例5: inSubnetWithMask
// 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;
}
示例6: inSubnet
// 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;
}