Commit 9b6eade1 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Compute masks in IPAddressV6

Summary:
[Folly] Compute masks in `IPAddressV6`.

At runtime, instead of storing a 2Kb masks array in the binary.

Reviewed By: WillerZ, ot

Differential Revision: D5369901

fbshipit-source-id: 031a4eeda1670021236d8ab97f6e85e753675ae5
parent d6f902fb
......@@ -469,8 +469,18 @@ const ByteArray16 IPAddressV6::fetchMask(size_t numBits) {
if (numBits > bits) {
throw IPAddressFormatException("IPv6 addresses are 128 bits.");
}
// masks_ is backed by an array so is zero indexed
return masks_[numBits];
if (numBits == 0) {
return {{0}};
}
constexpr auto _0s = uint64_t(0);
constexpr auto _1s = ~_0s;
auto const fragment = Endian::big(_1s << ((128 - numBits) % 64));
auto const hi = numBits <= 64 ? fragment : _1s;
auto const lo = numBits <= 64 ? _0s : fragment;
uint64_t const parts[] = {hi, lo};
ByteArray16 arr;
std::memcpy(arr.data(), parts, sizeof(parts));
return arr;
}
// public static
......@@ -488,525 +498,4 @@ bool IPAddressV6::inBinarySubnet(const std::array<uint8_t, 2> addr,
auto masked = mask(numBits);
return (std::memcmp(addr.data(), masked.bytes(), 2) == 0);
}
// static private
const std::array<ByteArray16, 129> IPAddressV6::masks_ = {{
/* /0 */ {{ 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /1 */ {{ 0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /2 */ {{ 0xc0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /3 */ {{ 0xe0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /4 */ {{ 0xf0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /5 */ {{ 0xf8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /6 */ {{ 0xfc,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /7 */ {{ 0xfe,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /8 */ {{ 0xff,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /9 */ {{ 0xff,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /10 */ {{ 0xff,0xc0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /11 */ {{ 0xff,0xe0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /12 */ {{ 0xff,0xf0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /13 */ {{ 0xff,0xf8,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /14 */ {{ 0xff,0xfc,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /15 */ {{ 0xff,0xfe,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /16 */ {{ 0xff,0xff,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /17 */ {{ 0xff,0xff,0x80,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /18 */ {{ 0xff,0xff,0xc0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /19 */ {{ 0xff,0xff,0xe0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /20 */ {{ 0xff,0xff,0xf0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /21 */ {{ 0xff,0xff,0xf8,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /22 */ {{ 0xff,0xff,0xfc,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /23 */ {{ 0xff,0xff,0xfe,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /24 */ {{ 0xff,0xff,0xff,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /25 */ {{ 0xff,0xff,0xff,0x80,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /26 */ {{ 0xff,0xff,0xff,0xc0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /27 */ {{ 0xff,0xff,0xff,0xe0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /28 */ {{ 0xff,0xff,0xff,0xf0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /29 */ {{ 0xff,0xff,0xff,0xf8,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /30 */ {{ 0xff,0xff,0xff,0xfc,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /31 */ {{ 0xff,0xff,0xff,0xfe,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /32 */ {{ 0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /33 */ {{ 0xff,0xff,0xff,0xff,
0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /34 */ {{ 0xff,0xff,0xff,0xff,
0xc0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /35 */ {{ 0xff,0xff,0xff,0xff,
0xe0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /36 */ {{ 0xff,0xff,0xff,0xff,
0xf0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /37 */ {{ 0xff,0xff,0xff,0xff,
0xf8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /38 */ {{ 0xff,0xff,0xff,0xff,
0xfc,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /39 */ {{ 0xff,0xff,0xff,0xff,
0xfe,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /40 */ {{ 0xff,0xff,0xff,0xff,
0xff,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /41 */ {{ 0xff,0xff,0xff,0xff,
0xff,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /42 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xc0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /43 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xe0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /44 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xf0,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /45 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xf8,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /46 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xfc,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /47 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xfe,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /48 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /49 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0x80,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /50 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xc0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /51 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xe0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /52 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xf0,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /53 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xf8,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /54 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xfc,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /55 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xfe,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /56 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /57 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x80,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /58 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xc0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /59 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xe0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /60 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf0,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /61 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf8,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /62 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfc,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /63 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfe,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /64 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /65 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /66 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xc0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /67 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xe0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /68 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xf0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /69 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xf8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /70 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xfc,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /71 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xfe,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /72 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /73 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0x80,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /74 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xc0,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /75 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xe0,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /76 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xf0,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /77 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xf8,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /78 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xfc,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /79 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xfe,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /80 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0x00,0x00,
0x00,0x00,0x00,0x00 }},
/* /81 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0x80,0x00,
0x00,0x00,0x00,0x00 }},
/* /82 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xc0,0x00,
0x00,0x00,0x00,0x00 }},
/* /83 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xe0,0x00,
0x00,0x00,0x00,0x00 }},
/* /84 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xf0,0x00,
0x00,0x00,0x00,0x00 }},
/* /85 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xf8,0x00,
0x00,0x00,0x00,0x00 }},
/* /86 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xfc,0x00,
0x00,0x00,0x00,0x00 }},
/* /87 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xfe,0x00,
0x00,0x00,0x00,0x00 }},
/* /88 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,
0x00,0x00,0x00,0x00 }},
/* /89 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x80,
0x00,0x00,0x00,0x00 }},
/* /90 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xc0,
0x00,0x00,0x00,0x00 }},
/* /91 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xe0,
0x00,0x00,0x00,0x00 }},
/* /92 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf0,
0x00,0x00,0x00,0x00 }},
/* /93 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf8,
0x00,0x00,0x00,0x00 }},
/* /94 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfc,
0x00,0x00,0x00,0x00 }},
/* /95 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfe,
0x00,0x00,0x00,0x00 }},
/* /96 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0x00 }},
/* /97 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0x80,0x00,0x00,0x00 }},
/* /98 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xc0,0x00,0x00,0x00 }},
/* /99 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xe0,0x00,0x00,0x00 }},
/* /100 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xf0,0x00,0x00,0x00 }},
/* /101 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xf8,0x00,0x00,0x00 }},
/* /102 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xfc,0x00,0x00,0x00 }},
/* /103 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xfe,0x00,0x00,0x00 }},
/* /104 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0x00,0x00,0x00 }},
/* /105 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0x80,0x00,0x00 }},
/* /106 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xc0,0x00,0x00 }},
/* /107 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xe0,0x00,0x00 }},
/* /108 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xf0,0x00,0x00 }},
/* /109 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xf8,0x00,0x00 }},
/* /110 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xfc,0x00,0x00 }},
/* /111 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xfe,0x00,0x00 }},
/* /112 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0x00,0x00 }},
/* /113 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0x80,0x00 }},
/* /114 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xc0,0x00 }},
/* /115 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xe0,0x00 }},
/* /116 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xf0,0x00 }},
/* /117 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xf8,0x00 }},
/* /118 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xfc,0x00 }},
/* /119 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xfe,0x00 }},
/* /120 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00 }},
/* /121 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x80 }},
/* /122 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xc0 }},
/* /123 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xe0 }},
/* /124 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf0 }},
/* /125 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xf8 }},
/* /126 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfc }},
/* /127 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xfe }},
/* /128 */ {{ 0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff }},
}};
} // folly
......@@ -354,8 +354,6 @@ class IPAddressV6 {
// are *not* link-local.
uint16_t scope_{0};
static const std::array<ByteArray16, 129> masks_;
/**
* Set the current IPAddressV6 object to have the address specified by bytes.
* @throws IPAddressFormatException if bytes.size() is not 16.
......
......@@ -21,6 +21,7 @@
#include <folly/MacAddress.h>
#include <folly/String.h>
#include <folly/detail/IPAddressSource.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
using namespace folly;
......@@ -1251,3 +1252,66 @@ INSTANTIATE_TEST_CASE_P(IPAddress,
INSTANTIATE_TEST_CASE_P(IPAddress,
IPAddressBitAccessorTest,
::testing::ValuesIn(validAddressProvider));
TEST(IPAddressV6, fetchMask) {
using ByteArray8 = std::array<uint8_t, 8>;
struct X : private IPAddressV6 {
using IPAddressV6::fetchMask;
};
auto join = [](std::array<ByteArray8, 2> parts) {
ByteArray16 _return;
std::memcpy(_return.data(), parts.data(), _return.size());
return _return;
};
EXPECT_THAT(
X::fetchMask(0),
::testing::ElementsAreArray(join({{
ByteArray8{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
ByteArray8{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}})));
EXPECT_THAT(
X::fetchMask(1),
::testing::ElementsAreArray(join({{
ByteArray8{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
ByteArray8{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}})));
EXPECT_THAT(
X::fetchMask(63),
::testing::ElementsAreArray(join({{
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}},
ByteArray8{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}})));
EXPECT_THAT(
X::fetchMask(64),
::testing::ElementsAreArray(join({{
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
ByteArray8{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}})));
EXPECT_THAT(
X::fetchMask(65),
::testing::ElementsAreArray(join({{
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
ByteArray8{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}})));
EXPECT_THAT(
X::fetchMask(127),
::testing::ElementsAreArray(join({{
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}},
}})));
EXPECT_THAT(
X::fetchMask(128),
::testing::ElementsAreArray(join({{
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
ByteArray8{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
}})));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment