IPNetwork
repository·master·Indexed 19 days ago
https://github.com/lduchosal/ipnetworkA utility suite for .NET developers providing a C# library (IPNetwork2) and a Native AOT command-line tool for complex IP networking calculations. It supports IPv4 and IPv6, CIDR notation, subnetting, supernetting, and symmetric difference subtraction. Key features include CIDR guessing strategies, IANA reserved block constants, IPv6 Unique Local Address (ULA) generation, and the ability to convert IP ranges into optimal CIDR blocks.
What's inside IPNetwork
- IPNetwork is a C# library and command-line tool designed for .NET developers to handle complex networking calculations. It provides utilities for working with IPv4 and IPv6, netmasks, CIDR notation, subnets, subnetting, supernets, and supernetting.
Subnet and Supernet networks
masterSubnetting
Divide a network into smaller subnets using the
Subnet(byte newCidr)method. This returns anIPNetworkCollectionthat can be iterated over.Supernetting
Combine multiple networks into a single larger network using the
Supernet(IPNetwork2[])static method or the+operator.IPNetwork2 wholeInternet = IPNetwork2.Parse("::/0"); IPNetworkCollection subneted = wholeInternet.Subnet(2); // Supernetting IPNetwork2 ipnetwork1 = IPNetwork2.Parse("192.168.0.0/24"); IPNetwork2 ipnetwork2 = IPNetwork2.Parse("192.168.1.0/24"); // Using static method IPNetwork2[] supernetted = IPNetwork2.Supernet(new[] { ipnetwork1, ipnetwork2 }); // Using + operator IPNetwork2[] supernettedOp = ipnetwork1 + ipnetwork2;// Subnetting IPNetwork2 wholeInternet = IPNetwork2.Parse("::/0"); IPNetworkCollection subneted = wholeInternet.Subnet(2); // Supernetting IPNetwork2 ipnetwork1 = IPNetwork2.Parse("192.168.0.0/24"); IPNetwork2 ipnetwork2 = IPNetwork2.Parse("192.168.1.0/24"); // Using static method IPNetwork2[] supernetted = IPNetwork2.Supernet(new[] { ipnetwork1, ipnetwork2 }); // Using + operator IPNetwork2[] supernettedOp = ipnetwork1 + ipnetwork2;Install the IPNetwork CLI
masterThe IPNetwork CLI is a Native AOT binary that does not require a .NET runtime. You can install it using the following package managers:
- macOS / Linux (Homebrew):
brew install lduchosal/ipnetwork/ipnetwork - Windows (Chocolatey):
choco install ipnetwork - Linux (Manual): Download and extract the appropriate tar.gz for your architecture to
/usr/local/bin.
# macOS / Linux (Homebrew) brew install lduchosal/ipnetwork/ipnetwork # Windows (Chocolatey) choco install ipnetwork # Linux (manual) x64 curl -sL https://github.com/lduchosal/ipnetwork/releases/latest/download/ipnetwork-linux-x64.tar.gz | tar xz -C /usr/local/bin # Linux (manual) ARM64 curl -sL https://github.com/lduchosal/ipnetwork/releases/latest/download/ipnetwork-linux-arm64.tar.gz | tar xz -C /usr/local/bin- macOS / Linux (Homebrew):
Install the IPNetwork2 .NET Library
masterTo use IPNetwork utility classes in your .NET projects, install the
IPNetwork2NuGet package.PM> nuget install IPNetwork2Install IPNetwork2 via NuGet
masterTo use the IPNetwork utility classes in your .NET project, install the
IPNetwork2package using the NuGet package manager.nugget install IPNetwork2Install IPNetwork via Chocolatey
masterYou can install the IPNetwork command-line tool using the Chocolatey package manager. The package provides a Native AOT compiled binary, meaning no .NET runtime is required on your system after installation.
# Note: The specific install command is typically executed via Chocolatey CLI # choco install ipnetworkInstall the IPNetwork CLI
masterYou can install the IPNetwork command-line utility using Chocolatey.
choco install ipnetworkParse networks with different CIDR guessing strategies
masterWhen parsing a network string without a CIDR suffix, you can specify a
CidrGuessstrategy:CidrGuess.ClassFull(Default):- IPv4: Uses Class A (/8), B (/16), or C (/24) based on the first octet.
- IPv6: Defaults to /64.
CidrGuess.ClassLess:- IPv4: Defaults to /32.
- IPv6: Defaults to /128.
// IPv4 ClassFull (Default) IPNetwork2 classFullParse = IPNetwork2.Parse("192.168.0.0", CidrGuess.ClassFull); // IPv4 ClassLess IPNetwork2 classLessParse = IPNetwork2.Parse("192.168.0.0", CidrGuess.ClassLess); // IPv6 ClassFull IPNetwork2 ipv6Default = IPNetwork2.Parse("::1"); // Defaults to /64 // IPv6 ClassLess IPNetwork2 ipv6ClassLess = IPNetwork2.Parse("::1", CidrGuess.ClassLess); // Defaults to /128Troubleshoot IPNetwork installation issues
masterIf you encounter issues while using the IPNetwork package, follow these steps:
Package Installation Fails
- Verify that the download URL is accessible.
- Ensure the checksum matches the uploaded file.
Command Not Found After Installation
If the
ipnetworkcommand is not recognized after a successful installation:- Try refreshing your environment variables by running:
refreshenv - If that fails, restart your PowerShell or Command Prompt session.
refreshenvSupernet networks using the CLI
masterThe CLI allows you to combine multiple networks into larger ones (supernetting).
-w: Supernet networks into the smallest possible subnets.-W: Supernet networks into one single subnet.
Example (Smallest possible subnets):
ipnetwork -w 192.168.0.0/24 192.168.1.0/24Example (Single subnet):
ipnetwork -W 192.168.0.0/24 192.168.129.0/24ipnetwork -w 192.168.0.0/24 192.168.1.0/24 ipnetwork -W 192.168.0.0/24 192.168.129.0/24Split a network into CIDR subnets using the CLI
masterUse the
-sflag followed by the desired CIDR size to split a network into multiple subnets.Example: Split
10.0.0.0/8into/9subnets:ipnetwork -s 9 10.0.0.0/8Check if a network contains an IP or overlaps another network
masterUse the following flags to perform containment and overlap checks:
-C network: Test if a network contains the specified IP or network.-o network: Test if a network overlaps with another network.
Example (Containment):
ipnetwork -C 10.0.0.1 10.0.0.0/8 10.0.1.0/24Example (Overlap):
ipnetwork -o 10.0.0.1/24 10.0.0.0/8 10.0.1.0/24ipnetwork -C 10.0.0.1 10.0.0.0/8 10.0.1.0/24 ipnetwork -o 10.0.0.1/24 10.0.0.0/8 10.0.1.0/24