subnet

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package subnet provides interfaces for subnet calculation operations.

Package subnet provides core domain types and calculations for IP subnet operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToAddr

func AddToAddr(addr netip.Addr, n int) netip.Addr

AddToAddr adds n to an IP address, returning the resulting address.

func CalculateBroadcastAddr

func CalculateBroadcastAddr(networkAddr, subnetMask netip.Addr) netip.Addr

CalculateBroadcastAddr computes the broadcast address from network address and subnet mask.

func CalculateMaxHosts

func CalculateMaxHosts(addrBits, maskBits int) *big.Int

CalculateMaxHosts returns the number of usable host addresses in a subnet. Uses big.Int to handle IPv6 networks without overflow.

func CalculateSubnetMask

func CalculateSubnetMask(maskBits, addrBits int) netip.Addr

CalculateSubnetMask generates a subnet mask for the given mask bits and address size.

Types

type Calculator

type Calculator interface {
	// Calculate creates a Network from a CIDR string.
	Calculate(cidr string) (Network, error)

	// Split divides a network into subnets of the specified prefix length.
	Split(network *Network, targetBits int) error
}

Calculator defines the interface for subnet calculation operations.

type DefaultCalculator

type DefaultCalculator struct{}

DefaultCalculator is the standard implementation of Calculator.

func NewCalculator

func NewCalculator() *DefaultCalculator

NewCalculator creates a new DefaultCalculator.

func (*DefaultCalculator) Calculate

func (c *DefaultCalculator) Calculate(cidr string) (Network, error)

Calculate creates a Network from a CIDR string.

func (*DefaultCalculator) Split

func (c *DefaultCalculator) Split(network *Network, targetBits int) error

Split divides a network into subnets of the specified prefix length.

type Network

type Network struct {
	CIDR          netip.Prefix `json:"cidr"`
	NetworkAddr   netip.Addr   `json:"networkAddr"`
	BroadcastAddr netip.Addr   `json:"broadcastAddr"`
	FirstHostIP   netip.Addr   `json:"firstIP"`
	LastHostIP    netip.Addr   `json:"lastIP"`
	SubnetMask    netip.Addr   `json:"subnetMask"`
	MaskBits      int          `json:"maskBits"`
	MaxHosts      *big.Int     `json:"maxHosts"`
	Subnets       []Network    `json:"subnets,omitempty"`
}

Network represents an IP network with calculated properties.

func GenerateSubnets

func GenerateSubnets(prefix netip.Prefix, targetBits int) []Network

GenerateSubnets creates all subnets of targetBits size within the given prefix.

func NewNetwork

func NewNetwork(cidr string) (Network, error)

NewNetwork creates a Network from a CIDR string.

func NewNetworkFromPrefix

func NewNetworkFromPrefix(prefix netip.Prefix) Network

NewNetworkFromPrefix creates a Network from a netip.Prefix.

func (*Network) Split

func (n *Network) Split(targetBits int) error

Split divides this network into subnets of the specified prefix length.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL