shared_ptr<Vlan> ThriftConfigApplier::updateVlan(const shared_ptr<Vlan>& orig, const cfg::Vlan* config) { CHECK_EQ(orig->getID(), config->id); const auto& ports = vlanPorts_[orig->getID()]; auto newVlan = orig->clone(); bool changed_neighbor_table = updateNeighborResponseTables(newVlan.get(), config); bool changed_dhcp_overrides = updateDhcpOverrides(newVlan.get(), config); auto oldDhcpV4Relay = orig->getDhcpV4Relay(); auto newDhcpV4Relay = config->__isset.dhcpRelayAddressV4 ? IPAddressV4(config->dhcpRelayAddressV4) : IPAddressV4(); auto oldDhcpV6Relay = orig->getDhcpV6Relay(); auto newDhcpV6Relay = config->__isset.dhcpRelayAddressV6 ? IPAddressV6(config->dhcpRelayAddressV6) : IPAddressV6("::"); if (orig->getName() == config->name && orig->getPorts() == ports && oldDhcpV4Relay == newDhcpV4Relay && oldDhcpV6Relay == newDhcpV6Relay && !changed_neighbor_table && !changed_dhcp_overrides) { return nullptr; } newVlan->setName(config->name); newVlan->setPorts(ports); newVlan->setDhcpV4Relay(newDhcpV4Relay); newVlan->setDhcpV6Relay(newDhcpV6Relay); return newVlan; }
bool ThriftConfigApplier::updateDhcpOverrides(Vlan* vlan, const cfg::Vlan* config) { DhcpV4OverrideMap newDhcpV4OverrideMap; for (const auto& pair : config->dhcpRelayOverridesV4) { try { newDhcpV4OverrideMap[MacAddress(pair.first)] = IPAddressV4(pair.second); } catch (const IPAddressFormatException& ex) { throw FbossError("Invalid IPv4 address in DHCPv4 relay override map: ", ex.what()); } } DhcpV6OverrideMap newDhcpV6OverrideMap; for (const auto& pair : config->dhcpRelayOverridesV6) { try { newDhcpV6OverrideMap[MacAddress(pair.first)] = IPAddressV6(pair.second); } catch (const IPAddressFormatException& ex) { throw FbossError("Invalid IPv4 address in DHCPv4 relay override map: ", ex.what()); } } bool changed = false; auto oldDhcpV4OverrideMap = vlan->getDhcpV4RelayOverrides(); if (oldDhcpV4OverrideMap != newDhcpV4OverrideMap) { vlan->setDhcpV4RelayOverrides(newDhcpV4OverrideMap); changed = true; } auto oldDhcpV6OverrideMap = vlan->getDhcpV6RelayOverrides(); if (oldDhcpV6OverrideMap != newDhcpV6OverrideMap) { vlan->setDhcpV6RelayOverrides(newDhcpV6OverrideMap); changed = true; } return changed; }
// public IPAddressV4 IPAddressV4::mask(size_t numBits) const { static const auto bits = bitCount(); if (numBits > bits) { throw IPAddressFormatException("numBits(", numBits, ") > bitsCount(", bits, ")"); } ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_); return IPAddressV4(ba); }
Vlan::Vlan(const cfg::Vlan* config, uint32_t mtu, MemberPorts ports) : NodeBaseT(VlanID(config->id), config->name, mtu, (config->__isset.dhcpRelayAddressV4 ? IPAddressV4(config->dhcpRelayAddressV4) : IPAddressV4()), (config->__isset.dhcpRelayAddressV6 ? IPAddressV6(config->dhcpRelayAddressV6) : IPAddressV6()), std::move(ports)) { DhcpV4OverrideMap map4; for (const auto& o: config->dhcpRelayOverridesV4) { map4[MacAddress(o.first)] = folly::IPAddressV4(o.second); } setDhcpV4RelayOverrides(map4); DhcpV6OverrideMap map6; for (const auto& o: config->dhcpRelayOverridesV6) { map6[MacAddress(o.first)] = folly::IPAddressV6(o.second); } setDhcpV6RelayOverrides(map6); }
// public string constructor IPAddress::IPAddress(StringPiece addr) : addr_() , family_(AF_UNSPEC) { string ip = addr.str(); // inet_pton() needs NUL-terminated string auto throwFormatException = [&](const string& msg) { throw IPAddressFormatException( to<std::string>("Invalid IP '", ip, "': ", msg)); }; if (ip.size() < 2) { throwFormatException("address too short"); } if (ip.front() == '[' && ip.back() == ']') { ip = ip.substr(1, ip.size() - 2); } // need to check for V4 address second, since IPv4-mapped IPv6 addresses may // contain a period if (ip.find(':') != string::npos) { struct addrinfo* result; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST; if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) { struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr; addr_ = IPAddressV46(IPAddressV6(*ipAddr)); family_ = AF_INET6; freeaddrinfo(result); } else { throwFormatException("getsockaddr failed for V6 address"); } } else if (ip.find('.') != string::npos) { in_addr ipAddr; if (inet_pton(AF_INET, ip.c_str(), &ipAddr) != 1) { throwFormatException("inet_pton failed for V4 address"); } addr_ = IPAddressV46(IPAddressV4(ipAddr)); family_ = AF_INET; } else { throwFormatException("invalid address format"); } }
// public sockaddr constructor IPAddress::IPAddress(const sockaddr* addr) : addr_(), family_(AF_UNSPEC) { if (addr == nullptr) { throw IPAddressFormatException("sockaddr == nullptr"); } family_ = addr->sa_family; switch (addr->sa_family) { case AF_INET: { const sockaddr_in* v4addr = reinterpret_cast<const sockaddr_in*>(addr); addr_.ipV4Addr = IPAddressV4(v4addr->sin_addr); break; } case AF_INET6: { const sockaddr_in6* v6addr = reinterpret_cast<const sockaddr_in6*>(addr); addr_.ipV6Addr = IPAddressV6(*v6addr); break; } default: throw InvalidAddressFamilyException(addr->sa_family); } }
// public ipv4 constructor IPAddress::IPAddress(const in_addr ipV4Addr) noexcept : addr_(IPAddressV4(ipV4Addr)), family_(AF_INET) {}
IPAddressV4 IPAddressV4::fromLongHBO(uint32_t src) { in_addr addr; addr.s_addr = htonl(src); return IPAddressV4(addr); }
// public static IPAddressV4 IPAddressV4::fromLong(uint32_t src) { in_addr addr; addr.s_addr = src; return IPAddressV4(addr); }