IPNetworkHelper 3.0.1

dotnet add package IPNetworkHelper --version 3.0.1                
NuGet\Install-Package IPNetworkHelper -Version 3.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IPNetworkHelper" Version="3.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IPNetworkHelper --version 3.0.1                
#r "nuget: IPNetworkHelper, 3.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IPNetworkHelper as a Cake Addin
#addin nuget:?package=IPNetworkHelper&version=3.0.1

// Install IPNetworkHelper as a Cake Tool
#tool nuget:?package=IPNetworkHelper&version=3.0.1                

logo IPNetworkHelper

Provides helper (extension)methods for working with (IPv4 and/or IPv6) IPNetworks. These include splitting and extracting networks from larger networks. Available as NuGet package.

Note that since version 2.0 we use the System.Net.IPNetwork struct, which, unfortunately, is only available in .NET 8.0 and later. If you need support for earlier versions of .NET, use version 1.0 of this library.

Version 3.0 has a breaking change where Contains() now does the more intuitive thing and checks if the network is entirely contained within another network. If you want to check if two networks overlap, use the Overlaps() method.

Quickstart

All of the below examples use IPv4 but IPv6 works just as well.

// Parse a network
var network = IPNetwork.Parse("192.168.0.0/16");

// Tries to parse network, returns true when succeeded, false otherwise and the parsed network
if (IPNetwork.TryParse("192.168.0.0/16", out var othernetwork))
{
    // ...
}

// Get last IP from network
var first = network.GetFirstIP();   // Network      (192.168.0.0)
var last = network.GetLastIP();     // Broadcast    (192.168.255.255)

// Splits a network into two halves
var (left, right) = network.Split();    // Returns 192.168.0.0/17 and 192.168.128.0/17

// Extract a subnet from a network
var desired = IPNetwork.Parse("192.168.10.16/28");
var result = network.Extract(desired);

// Result:
// 192.168.0.0/21
// 192.168.8.0/23
// 192.168.10.0/28
// 192.168.10.16/28 <- desired
// 192.168.10.32/27
// 192.168.10.64/26
// 192.168.10.128/25
// 192.168.11.0/24
// 192.168.12.0/22
// 192.168.16.0/20
// 192.168.32.0/19
// 192.168.64.0/18
// 192.168.128.0/17

The Contains(IPNetwork) method can be used to check if a network is contained (entirely) within another network and the Overlaps(IPNetwork) method can be used to check if two networks overlap.

This library also includes an IPAddressComparer and IPNetworkComparer to be used when sorting IPAddresses or networks:

var ips = new[] { "192.168.64.0", "192.168.10.32", "192.168.0.0", "192.168.10.16", "192.168.10.0" }
    .Select(IPAddress.Parse)
    .OrderBy(n => n, IPAddressComparer.Default);

var networks = new[] { "192.168.64.0/18", "192.168.10.32/27", "192.168.0.0/16", "192.168.10.16/28", "192.168.10.0/28" }
    .Select(IPNetwork.Parse)
    .OrderBy(n => n, IPNetworkComparer.Default);

Icon made by prettycons from www.flaticon.com is licensed by CC 3.0.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.1 533 3/29/2024
3.0.0 168 3/1/2024
2.0.2 113 3/1/2024
2.0.1 108 3/1/2024
2.0.0 114 2/29/2024
1.2.0 1,158 7/7/2023
1.1.0 720 2/2/2023
1.0.5 2,099 2/10/2022
1.0.4 418 2/10/2022
1.0.3 1,665 9/13/2021
1.0.2 323 9/13/2021
1.0.1 323 9/13/2021
1.0.0 323 9/13/2021

Added Contains(IPNetwork) extension method