subnet

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const MaxGeneratedSubnets = 1_000_000

MaxGeneratedSubnets is the safety limit for subnet splitting operations. This prevents accidental memory exhaustion from very large splits.

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