shill: connection: Choose reasonable default Broadcast Address
Calculate the correct default broadcast address since the kernel
will not do so for us.
BUG=chromium-os:23930
TEST=New unit tests -- manual testing by setting a manual IP address
and prefix length only and attaching to a network without a DHCP server.
Change-Id: Ibfb7f942a100cdc2c33a7da915f7bfa5f27ef24e
Reviewed-on: https://gerrit.chromium.org/gerrit/21507
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Ready: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/ip_address.cc b/ip_address.cc
index 52a4aab..2ddaeb0 100644
--- a/ip_address.cc
+++ b/ip_address.cc
@@ -216,7 +216,18 @@
CHECK_EQ(family(), b.family());
ByteString address_bytes(address());
- address_bytes.ApplyMask(b.address());
+ address_bytes.BitwiseAnd(b.address());
+
+ return IPAddress(family(), address_bytes);
+}
+
+IPAddress IPAddress::MergeWith(const IPAddress &b) {
+ CHECK(IsValid());
+ CHECK(b.IsValid());
+ CHECK_EQ(family(), b.family());
+
+ ByteString address_bytes(address());
+ address_bytes.BitwiseOr(b.address());
return IPAddress(family(), address_bytes);
}
@@ -225,6 +236,13 @@
return MaskWith(GetAddressMaskFromPrefix(family(), prefix()));
}
+IPAddress IPAddress::GetDefaultBroadcast() {
+ ByteString broadcast_bytes(
+ GetAddressMaskFromPrefix(family(), prefix()).address());
+ broadcast_bytes.BitwiseInvert();
+ return MergeWith(IPAddress(family(), broadcast_bytes));
+}
+
bool IPAddress::CanReachAddress(const IPAddress &b) {
CHECK_EQ(family(), b.family());
IPAddress b_prefixed(b);