本文整理汇总了C++中IPAddress::asV4方法的典型用法代码示例。如果您正苦于以下问题:C++ IPAddress::asV4方法的具体用法?C++ IPAddress::asV4怎么用?C++ IPAddress::asV4使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPAddress
的用法示例。
在下文中一共展示了IPAddress::asV4方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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;
}
示例4: bytes
TEST(IPAddress, fromBinaryV4) {
for (auto& tc : provideToLong) {
SCOPED_TRACE(tc.first);
union {
uint8_t u8[4];
uint32_t u32;
} data;
data.u32 = Endian::big(tc.second);
ByteRange bytes(data.u8, 4);
auto fromBin = IPAddressV4::fromBinary(bytes);
IPAddressV4 fromStr(tc.first);
EXPECT_EQ(fromStr, fromBin);
IPAddressV4 addr2("0.0.0.0");
addr2 = IPAddressV4::fromBinary(bytes);
EXPECT_EQ(fromStr, addr2);
IPAddress genericAddr = IPAddress::fromBinary(bytes);
ASSERT_TRUE(genericAddr.isV4());
EXPECT_EQ(fromStr, genericAddr.asV4());
EXPECT_EQ(ByteRange(genericAddr.bytes(), genericAddr.byteCount()), bytes);
}
uint8_t data[20];
EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 3)),
IPAddressFormatException);
EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 16)),
IPAddressFormatException);
EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 20)),
IPAddressFormatException);
}
示例5:
// public static
IPAddressV6 IPAddress::createIPv6(const IPAddress& addr) {
if (addr.isV6()) {
return addr.asV6();
} else {
return addr.asV4().createIPv6();
}
}
示例6: flushEntryImpl
// expects the cachesMutex_ to be held
bool NeighborUpdater::flushEntryImpl(VlanID vlan, IPAddress ip) {
if (ip.isV4()) {
auto cache = getArpCacheInternal(vlan);
return cache->flushEntryBlocking(ip.asV4());
}
auto cache = getNdpCacheInternal(vlan);
return cache->flushEntryBlocking(ip.asV6());
}
示例7:
TEST(IPAddress, InvalidAddressFamilyExceptions) {
// asV4
{
IPAddress addr;
EXPECT_THROW(addr.asV4(), InvalidAddressFamilyException);
}
// asV6
{
IPAddress addr;
EXPECT_THROW(addr.asV6(), InvalidAddressFamilyException);
}
// sockaddr ctor
{
// setup
sockaddr_in addr;
addr.sin_family = AF_UNSPEC;
EXPECT_THROW(IPAddress((sockaddr *)&addr), InvalidAddressFamilyException);
}
}