IPNetwork

repository·master·Indexed 19 days ago

https://github.com/lduchosal/ipnetwork

A 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.

Tokens
8.1K
Snippets
40
Records
41
Agent score
66%

What's inside IPNetwork

  1. Overview of IPNetwork

    master
    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.
  2. Subnet and Supernet networks

    master

    Subnetting

    Divide a network into smaller subnets using the Subnet(byte newCidr) method. This returns an IPNetworkCollection that 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;
  3. Install the IPNetwork CLI

    master

    The 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
  4. Install IPNetwork via Chocolatey

    master

    You 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 ipnetwork
  5. Parse networks with different CIDR guessing strategies

    master

    When parsing a network string without a CIDR suffix, you can specify a CidrGuess strategy:

    • 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 /128
  6. Troubleshoot IPNetwork installation issues

    master

    If 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 ipnetwork command is not recognized after a successful installation:

    1. Try refreshing your environment variables by running: refreshenv
    2. If that fails, restart your PowerShell or Command Prompt session.
    refreshenv
  7. Supernet networks using the CLI

    master

    The 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/24

    Example (Single subnet):

    ipnetwork -W 192.168.0.0/24 192.168.129.0/24
    ipnetwork -w 192.168.0.0/24 192.168.1.0/24
    ipnetwork -W 192.168.0.0/24 192.168.129.0/24
  8. Check if a network contains an IP or overlaps another network

    master

    Use 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/24

    Example (Overlap):

    ipnetwork -o 10.0.0.1/24 10.0.0.0/8 10.0.1.0/24
    ipnetwork -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