public IEnumerable <IpAddressNetworkV4> Split(byte maskIncrement = 1) { int newMaskInt = _mask + maskIncrement; if (newMaskInt <= _mask || newMaskInt > 32) { throw new ArgumentOutOfRangeException($"The increment ({maskIncrement}) must place the new mask between this networks mask ({_mask}) and 32", nameof(maskIncrement)); } byte newMask = (byte)newMaskInt; // Determine last network // We use this to determine when to stop producing networks. Other approaches may lead to infinite loops (when running unchecked arithmetics) uint networksBitmask = uint.MaxValue >> (32 - maskIncrement); IpAddressV4 lastNetwork = new IpAddressV4(_networkAddress.Address | (networksBitmask << (32 - newMask))); // Create increment value uint incrementVal = 1U << (32 - newMask); IpAddressV4 currentNet = _networkAddress; // Ensure we can fail fast on the arguments IEnumerable <IpAddressNetworkV4> Enumerate() { // Produce networks as long as we're within our current network yield return(new IpAddressNetworkV4(currentNet, newMask)); do { currentNet = new IpAddressV4(currentNet.Address + incrementVal); yield return(new IpAddressNetworkV4(currentNet, newMask)); } while (currentNet != lastNetwork); } return(Enumerate()); }
public void Setup() { _addressV4 = (IpAddressV4)"174.64.25.18"; _networkV4 = (IpAddressNetworkV4)"174.64.25.18/24"; _addressV6 = (IpAddressV6)"2001:0dff:44ff:0:1744:ffff"; _networkV6 = (IpAddressNetworkV6)"2001:0dff:44ff:0:1744:ffff/64"; _sb = new StringBuilder(); _charBuffer = new char[60]; }
public void IPv4Endianness() { void Test(IpAddressV4 ip) => ip.Address.Should().Be(0xC0A80A40); // Tests that IP adresses are parsed to Big-endian order internally Test(new IpAddressV4(IPAddress.Parse("192.168.10.64"))); Test(new IpAddressV4(0xC0A80A40)); Test(new IpAddressV4(192, 168, 10, 64)); Test(IpAddressV4.Parse("192.168.10.64")); }
public void AddressToBytesTests(string networks, byte[] expected) { IpAddressV4 network = (IpAddressV4)networks; network.AddressToBytes().Should().Equal(expected); byte[] res = new byte[4]; network.AddressToBytes(res); res.Should().Equal(expected); }
/// <inheritdoc cref="Docs.IIPAddressNetworkDocs{IpAddressNetworkV4}.ContainsOrEqual(IpAddressNetworkV4)"/> public bool ContainsOrEqual(IpAddressNetworkV4 other) { if (other.Mask < _mask) { return(false); } IpAddressV4 sharedNetwork = NetworkMask & other.NetworkAddress; return(sharedNetwork == _networkAddress); }
public void ComparisonTests(string smallers, string largers) { IpAddressV4 smaller = (IpAddressV4)smallers; IpAddressV4 larger = (IpAddressV4)largers; (smaller < larger).Should().BeTrue(); (smaller <= larger).Should().BeTrue(); (larger > smaller).Should().BeTrue(); (larger >= smaller).Should().BeTrue(); smaller.CompareTo(larger).Should().BeNegative(); larger.CompareTo(smaller).Should().BePositive(); }
public void EqualTests(string lefts, string rights) { IpAddressV4 left = (IpAddressV4)lefts; IpAddressV4 right = (IpAddressV4)rights; (left < right).Should().BeFalse(); (left <= right).Should().BeTrue(); (left == right).Should().BeTrue(); (right > left).Should().BeFalse(); (right >= left).Should().BeTrue(); (right == left).Should().BeTrue(); left.CompareTo(right).Should().Be(0); right.CompareTo(left).Should().Be(0); }
public IpAddressNetworkV4(uint address, byte mask) { if (mask > 32) { throw new ArgumentException("Mask cannot be greater than 32 bits", nameof(mask)); } _mask = mask; if (mask == 0) { _networkAddress = IpAddressV4.Min; } else { _networkAddress = new IpAddressV4(address & (uint.MaxValue << (32 - _mask))); } }
public static bool TryParse(ReadOnlySpan <char> value, out IpAddressV4 result) { Tokenizer tokenizer = new Tokenizer(value); if (!TryParse(ref tokenizer, out result)) { return(false); } // We now expect the end ParsedToken token = tokenizer.ParseAndAdvance(); if (token.Type != TokenType.None) { // Something other than EOF was found result = default; return(false); } return(true); }
/// <summary> /// Creates an instance of <see cref="IpAddressNetworkV4"/> from a network and broadcast address. /// </summary> /// <param name="networkAddress">The network address. Note that it must be a network start address such as 10.0.0.0</param> /// <param name="broadcastAddress">The broadcast address. Note that it must be a broadcast address such as 10.255.255.255</param> public IpAddressNetworkV4(uint networkAddress, uint broadcastAddress) { uint ip = networkAddress ^ broadcastAddress; //count the number of 0 bits set byte mask = 32; while (ip != 0) { ip >>= 1; mask--; } _mask = mask; if (mask == 0) { _networkAddress = IpAddressV4.Min; } else { _networkAddress = new IpAddressV4(networkAddress & (uint.MaxValue << (32 - _mask))); } }
internal static bool TryParse(ref Tokenizer tokenizer, out IpAddressV4 result) { // Shortest IPv4 is 1 char (0) // Longest IPv4 is 15 chars (255.255.255.255) if (tokenizer.Length < 1 || tokenizer.Length > 15) { result = default; return(false); } uint ip = 0; ParsedToken token = default; // Parse the IP int i; for (i = 0; i < 4; i++) { token = tokenizer.ParseAndAdvance(false); // Read a dot, or break on slashes if (i > 0) { if (token.Type == TokenType.None) { // We're at the end, and we could have parsed a number. To support partial IPv4's ("192.168"), we break here break; } if (token.Type != TokenType.Dot) { // We expected a dot, but we didn't get one result = default; return(false); } // Advance once more token = tokenizer.ParseAndAdvance(false); } // Read a number if (token.Type != TokenType.Number || token.Value > byte.MaxValue) { // We expected a 0..255 number, but we didn't get one result = default; return(false); } ip <<= 8; ip += token.Value; } // Assume the remainder of the IP is 0's for (; i < 4; i++) { ip <<= 8; } result = new IpAddressV4(ip); return(true); }
public static bool TryParse(string value, out IpAddressV4 result) { return(TryParse(value.AsSpan(), out result)); }
public void ToStringTests(string str, string expected) { IpAddressV4 network = (IpAddressV4)str; network.ToString().Should().Be(expected); }
public bool TryGet(IpAddressV4 address, out IpAddressNetworkV4?foundNetwork, out TValue?value) => TryGet((IpAddressNetworkV4)address, out foundNetwork, out value);
public bool Contains(IpAddressV4 other) { IpAddressV4 mask = NetworkMask; return((mask & other) == NetworkAddress); }
public void IPv4Formats(string address) { Assert.True(IpAddressV4.TryParse(address, out _)); }