/// <summary>Gets the host identifier (rest) an <see cref="T:System.Net.IPAddress"/>.</summary> /// <param name="address">The address</param> /// <param name="mask">The net mask of the network</param> /// <returns>The host identifier (rest) an <see cref="T:System.Net.IPAddress"/></returns> public static IPAddress GetHostIdentifier(this IPAddress address, NetMask mask) { if (address == null) { throw new ArgumentNullException(nameof(address)); } if (mask == null) { throw new ArgumentNullException(nameof(mask)); } if (address.AddressFamily != Sockets.AddressFamily.InterNetwork) { throw new NotSupportedException(OnlyIPv4Supported); } var maskBits = mask.GetMaskBytes(); var ipBits = address.GetAddressBytes(); // ~Mask & IP var retVal = maskBits.Not().And(ipBits); var bytes = new byte[NetMask.MaskLength]; Buffer.BlockCopy(retVal, 0, bytes, 0, bytes.Length); return(new IPAddress(bytes)); }
/// <summary>Gets the broadcast address of an <see cref="T:System.Net.IPAddress"/>.</summary> /// <param name="address">The address</param> /// <param name="mask">The net mask of the network</param> /// <returns>The broadcast address of an <see cref="T:System.Net.IPAddress"/></returns> public static IPAddress GetBroadcastAddress(this IPAddress address, NetMask mask) { if (address == null) { throw new ArgumentNullException("address"); } if (mask == null) { throw new ArgumentNullException("mask"); } if (address.AddressFamily != Sockets.AddressFamily.InterNetwork) { throw new NotSupportedException(OnlyIPv4Supported); } // TODO: Test var ipBytes = address.GetAddressBytes(); var notMaskBytes = mask.GetMaskBytes().Not(); var broadcastAddressBytes = notMaskBytes.Or(ipBytes); return(new IPAddress(broadcastAddressBytes)); }