From 05e80bad2881b0f1f4cd048641aedb66f64d0b00 Mon Sep 17 00:00:00 2001 From: Alecs King Date: Tue, 11 Nov 2014 13:53:09 -0800 Subject: [PATCH] Use folly::IPAddress::hash instead of gethostid Summary: find a non-loopback ipv4 or ipv6 address and feed it to folly::IPAddress::hash Test Plan: 1) fbconfig -r mcrouter fbmake runtests 2) compare hostids on different hosts Reviewed By: pavlo@fb.com Subscribers: njormrod, folly-diffs@, trunkagent, ps, bmatheny, alikhtarov FB internal diff: D1668944 Tasks: 5557721 Signature: t1:1668944:1415736928:fb4b042a575c0b00f52780f3abf54bf7630b3a97 --- folly/IPAddress.h | 6 ++++++ folly/IPAddressV4.cpp | 7 +++++++ folly/IPAddressV4.h | 3 +++ folly/IPAddressV6.cpp | 1 + folly/test/IPAddressTest.cpp | 10 +++++----- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/folly/IPAddress.h b/folly/IPAddress.h index b1c474a6..e1b46137 100644 --- a/folly/IPAddress.h +++ b/folly/IPAddress.h @@ -318,6 +318,12 @@ class IPAddress : boost::totally_ordered { : asV6().isLoopback(); } + // Return true if the address qualifies as link local + bool isLinkLocal() const { + return isV4() ? asV4().isLinkLocal() + : asV6().isLinkLocal(); + } + // Return true if the address qualifies as broadcast. bool isLinkLocalBroadcast() const { return isV4() ? asV4().isLinkLocalBroadcast() diff --git a/folly/IPAddressV4.cpp b/folly/IPAddressV4.cpp index d66993c4..41862045 100644 --- a/folly/IPAddressV4.cpp +++ b/folly/IPAddressV4.cpp @@ -144,11 +144,18 @@ bool IPAddressV4::inSubnetWithMask(const IPAddressV4& subnet, return (mask == subMask); } +// public bool IPAddressV4::isLoopback() const { static IPAddressV4 loopback_addr("127.0.0.0"); return inSubnetWithMask(loopback_addr, fetchMask(8)); } +// public +bool IPAddressV4::isLinkLocal() const { + static IPAddressV4 linklocal_addr("169.254.0.0"); + return inSubnetWithMask(linklocal_addr, fetchMask(16)); +} + // public bool IPAddressV4::isNonroutable() const { auto ip = toLongHBO(); diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 9c66afb6..a225e8bf 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -139,6 +139,9 @@ class IPAddressV4 : boost::totally_ordered { // @see IPAddress#isLoopback bool isLoopback() const; + // @see IPAddress#isLinkLocal + bool isLinkLocal() const; + // @see IPAddress#isNonroutable bool isNonroutable() const; diff --git a/folly/IPAddressV6.cpp b/folly/IPAddressV6.cpp index 45e9e3a1..e7a5b9df 100644 --- a/folly/IPAddressV6.cpp +++ b/folly/IPAddressV6.cpp @@ -286,6 +286,7 @@ bool IPAddressV6::isPrivate() const { return isLoopback() || inBinarySubnet({{0xfc, 0x00}}, 7); } +// public bool IPAddressV6::isLinkLocal() const { return inBinarySubnet({{0xfe, 0x80}}, 10); } diff --git a/folly/test/IPAddressTest.cpp b/folly/test/IPAddressTest.cpp index 202661a6..e51452dc 100644 --- a/folly/test/IPAddressTest.cpp +++ b/folly/test/IPAddressTest.cpp @@ -690,10 +690,7 @@ TEST_P(IPAddressFlagTest, IsZero) { TEST_P(IPAddressFlagTest, IsLinkLocal) { AddressFlags param = GetParam(); - if (param.version != 6) { - return; - } - IPAddressV6 addr(param.address); + IPAddress addr(param.address); EXPECT_EQ(param.isLinkLocal(), addr.isLinkLocal()) << addr; } @@ -954,13 +951,16 @@ static vector flagProvider = { AddressFlags("127.0.0.1", 4, IS_LOCAL | IS_PVT_NONROUTE), AddressFlags("::1", 6, IS_LOCAL | IS_PVT_NONROUTE), + // link-local v4 + AddressFlags("169.254.0.1", 4, IS_LINK_LOCAL | IS_PVT_NONROUTE), + // private v4 AddressFlags("10.0.0.0", 4, IS_PVT_NONROUTE), AddressFlags("10.11.12.13", 4, IS_PVT_NONROUTE), AddressFlags("10.255.255.255", 4, IS_PVT_NONROUTE), AddressFlags("127.128.129.200", 4, IS_LOCAL | IS_PVT_NONROUTE), AddressFlags("127.255.255.255", 4, IS_LOCAL | IS_PVT_NONROUTE), - AddressFlags("169.254.0.0", 4, IS_PVT_NONROUTE), + AddressFlags("169.254.0.0", 4, IS_LINK_LOCAL | IS_PVT_NONROUTE), AddressFlags("192.168.0.0", 4, IS_PVT_NONROUTE), AddressFlags("192.168.200.255", 4, IS_PVT_NONROUTE), AddressFlags("192.168.255.255", 4, IS_PVT_NONROUTE), -- 2.34.1