Ejemplo n.º 1
0
        public void GetMaskBytes()
        {
            var actual   = Ba(255, 255, 248, 0);
            var m        = new NetMask(actual);
            var expected = m.GetMaskBytes();

            Assert.IsTrue(actual.SequenceEqual(expected));
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <summary>Gets the number of siblings an <see cref="T:System.Net.IPAddress"/> can have in a given network.</summary>
        /// <param name="mask">The net mask of the network</param>
        /// <param name="options">Options which addresses to include an which not</param>
        /// <returns>The number of siblings an <see cref="T:System.Net.IPAddress"/> can have in the given network.</returns>
        public static int GetSiblingCount(this NetMask mask, SiblingOptions options)
        {
            if (mask == null)
            {
                throw new ArgumentNullException(nameof(mask));
            }

            bool includeSelf              = BitHelper.IsOptionSet(options, SiblingOptions.IncludeSelf);
            bool includeBroadcast         = BitHelper.IsOptionSet(options, SiblingOptions.IncludeBroadcast);
            bool includeNetworkIdentifier = BitHelper.IsOptionSet(options, SiblingOptions.IncludeNetworkIdentifier);

            var hostPartBits = mask.GetMaskBytes().CountFromRight(false);
            var total        = 1 << hostPartBits;

            total -= includeSelf ? 0 : 1;
            total -= includeBroadcast ? 0 : 1;
            total -= includeNetworkIdentifier ? 0 : 1;

            // TODO: Testing

            return(total);
        }
Ejemplo n.º 5
0
 public void GetMaskBytes()
 {
     var actual = Ba(255, 255, 248, 0);
     var m = new NetMask(actual);
     var expected = m.GetMaskBytes();
     Assert.IsTrue(actual.SequenceEqual(expected));
 }