// 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; }
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); }
// 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()); }
// public static IPAddressV4 IPAddress::createIPv4(const IPAddress& addr) { if (addr.isV4()) { return addr.asV4(); } else { return addr.asV6().createIPv4(); } }
// 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()); }
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 can not be less than each // other. AF_UNSPEC is the only other value for which an IPAddress can be // created, in the default constructor case. return false; } } if (addr1.isV6()) { // means addr2 is v4, convert it to a mapped v6 address and compare return addr1.asV6() < addr2.asV4().createIPv6(); } if (addr2.isV6()) { // means addr2 is v6, convert addr1 to v4 mapped and compare return addr1.asV4().createIPv6() < addr2.asV6(); } return false; }
/** * Change IP configuration settings disabling the dhcp client * @param local_ip Static ip configuration * @param gateway Static gateway configuration * @param subnet Static Subnet mask * @param dns1 Static DNS server 1 * @param dns2 Static DNS server 2 */ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress arg2, IPAddress arg3, IPAddress dns2) { if(!WiFi.enableSTA(true)) { return false; } //ESP argument order is: ip, gateway, subnet, dns1 //Arduino arg order is: ip, dns, gateway, subnet. //first, check whether dhcp should be used, which is when ip == 0 && gateway == 0 && subnet == 0. bool espOrderUseDHCP = (local_ip == 0U && arg1 == 0U && arg2 == 0U); bool arduinoOrderUseDHCP = (local_ip == 0U && arg2 == 0U && arg3 == 0U); if (espOrderUseDHCP || arduinoOrderUseDHCP) { _useStaticIp = false; wifi_station_dhcpc_start(); return true; } //To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order. IPAddress gateway = arg1; IPAddress subnet = arg2; IPAddress dns1 = arg3; if(subnet[0] != 255) { //octet is not 255 => interpret as Arduino order gateway = arg2; subnet = arg3[0] == 0 ? IPAddress(255,255,255,0) : arg3; //arg order is arduino and 4th arg not given => assign it arduino default dns1 = arg1; } // check whether all is IPv4 (or gateway not set) if (!(local_ip.isV4() && subnet.isV4() && (!gateway.isSet() || gateway.isV4()))) { return false; } //ip and gateway must be in the same subnet if((local_ip.v4() & subnet.v4()) != (gateway.v4() & subnet.v4())) { return false; } struct ip_info info; info.ip.addr = local_ip.v4(); info.gw.addr = gateway.v4(); info.netmask.addr = subnet.v4(); wifi_station_dhcpc_stop(); if(wifi_set_ip_info(STATION_IF, &info)) { _useStaticIp = true; } else { return false; } if(dns1.isSet()) { // Set DNS1-Server dns_setserver(0, dns1); } if(dns2.isSet()) { // Set DNS2-Server dns_setserver(1, dns2); } return true; }