netutil

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const (
	PrivateNetwork10Dot  string = "10.0.0.0/8"
	PrivateNetwork172Dot string = "172.16.0.0/12"
	PrivateNetwork192Dot string = "192.168.0.0/16"
)

Variables

This section is empty.

Functions

func CalculateBroadcastAddr

func CalculateBroadcastAddr(ipNet *net.IPNet) (string, error)

CalculateBroadcastAddr calculates the broadcast address for a given IPNet.

func CalculateHostCapacity

func CalculateHostCapacity(ipNet *net.IPNet) (int, error)

CalculateHostCapacity calculates the number of hosts that can be accommodated in a given IPNet.

func CalculateNetworkAddr

func CalculateNetworkAddr(ipNet *net.IPNet) (string, error)

CalculateNetworkAddr calculates the network address for a given IPNet.

func CalculateSupernet added in v0.9.10

func CalculateSupernet(cidrs []string) (string, error)

CalculateSupernet calculates the supernet of the given CIDRs.

func DeriveVNetAndSubnets added in v0.9.10

func DeriveVNetAndSubnets(baseIP net.IP, subnetSize, subnetCount int) (string, []string, net.IP, error)

DeriveVNetAndSubnets calculates the CIDR blocks for a VNet and its subnets based on the given parameters.

func GetBroadcastAddr

func GetBroadcastAddr(cidrBlock string) (string, error)

GetBroadcastAddr calculates the broadcast address for a given CIDR block.

func GetNetmask

func GetNetmask(cidrBlock string) (string, error)

GetNetmask calculates the netmask for a given CIDR block.

func GetNetworkAddr

func GetNetworkAddr(cidrBlock string) (string, error)

GetNetworkAddr calculates the network address for a given CIDR block.

func GetPrefix

func GetPrefix(cidrBlock string) (int, error)

GetPrefix calculates the prefix for a given CIDR block.

func GetSizeOfHosts

func GetSizeOfHosts(cidrBlock string) (int, error)

GetSizeOfHosts calculates the number of hosts that can be accommodated in a given CIDR block.

func IpToUint32

func IpToUint32(ip net.IP) uint32

IpToUint32 converts an IP address to a uint32.

func NextSubnet

func NextSubnet(currentSubnetCIDR string, baseNetworkCIDR string) (string, error)

/////////////////////////////////////////////////////////////////////////////////// NextSubnet find and check the next subnet based on the base/parent network.

func PreviousSubnet

func PreviousSubnet(currentSubnet string, baseNetworkCIDR string) (string, error)

PreviousSubnet find and check the previous subnet based on the base/parent network.

func SubnettingByMinimumHosts

func SubnettingByMinimumHosts(cidrBlock string, hostsPerSubnet int) ([]string, error)

SubnettingByMinimumHosts divides a CIDR block into subnets based on the number of hosts required for one subnet.

func SubnettingByMinimumSubnetCount

func SubnettingByMinimumSubnetCount(cidrBlock string, minSubnets int) ([]string, error)

SubnettingByMinimumSubnetCount divides the CIDR block into subnets to accommodate the minimum number of subnets entered.

func Uint32ToIP

func Uint32ToIP(n uint32) net.IP

Uint32ToIP converts a uint32 to an IP address.

func ValidateNetwork

func ValidateNetwork(network Network) error

/////////////////////////////////////////////////////////////////// ValidateNetwork recursively validates the network and its subnets.

func WhichPrivateNetworkByCidr added in v0.10.7

func WhichPrivateNetworkByCidr(cidr string) (string, error)

WhichPrivateNetworkByCidr identifies the private network of the given CIDR block. The private network includes 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16.

func WhichPrivateNetworkByIp added in v0.10.7

func WhichPrivateNetworkByIp(ip string) (string, error)

WhichPrivateNetworkByIp identifies the private network of the given IP address. The private network includes 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16.

Types

type Network

type Network struct {
	CidrBlock string    `json:"cidrBlock"`
	Name      string    `json:"name,omitempty"`
	Subnets   []Network `json:"subnets,omitempty"`
}

func NewNetwork

func NewNetwork(cidrBlock string) (*Network, error)

New creates a new NetworkDetails object.

func SubnettingBy

func SubnettingBy(request SubnettingRequest) (Network, error)

Functions for subnetting SubnettingBy divides a CIDR block into subnets based on the given rules.

func (*Network) GetCIDRBlock

func (n *Network) GetCIDRBlock() string

func (*Network) GetName

func (n *Network) GetName() string

func (*Network) GetSubnets

func (n *Network) GetSubnets() []Network

type NetworkConfig

type NetworkConfig struct {
	NetworkConfiguration Network `json:"networkConfiguration"`
}

Models

type NetworkDetails

type NetworkDetails struct {
	Network
	NetworkAddress   string `json:"networkAddress,omitempty"`
	BroadcastAddress string `json:"broadcastAddress,omitempty"`
	Prefix           int    `json:"prefix,omitempty"`
	Netmask          string `json:"netmask,omitempty"`
	HostCapacity     int    `json:"hostCapacity,omitempty"`
}

func NewNetworkDetails

func NewNetworkDetails(cidrBlock string) (*NetworkDetails, error)

New creates a new NetworkDetails object.

func (*NetworkDetails) GetBroadcastAddress

func (n *NetworkDetails) GetBroadcastAddress() string

func (*NetworkDetails) GetHostCapacity

func (n *NetworkDetails) GetHostCapacity() int

func (*NetworkDetails) GetNetmask

func (n *NetworkDetails) GetNetmask() string

func (*NetworkDetails) GetNetworkAddress

func (n *NetworkDetails) GetNetworkAddress() string

Getters

func (*NetworkDetails) GetPrefix

func (n *NetworkDetails) GetPrefix() int

type NetworkInterface

type NetworkInterface interface {
	GetCIDRBlock() string
	GetName() string
	GetSubnets() []Network
}

NetworkInterface defines the methods that both Network and NetworkDetails should implement.

type SubnettingRequest

type SubnettingRequest struct {
	CIDRBlock       string           `json:"cidrBlock" example:"192.168.0.0/16"`
	SubnettingRules []SubnettingRule `json:"subnettingRules"`
}

Models for subnetting

type SubnettingRule

type SubnettingRule struct {
	Type  SubnettingRuleType `json:"type" example:"minSubnets" enum:"minSubnets,minHosts"`
	Value int                `json:"value" example:"2"`
}

type SubnettingRuleType

type SubnettingRuleType string

/////////////////////////////////////////////////////////////////// SubnettingRuleType defines the type for subnetting rules.

const (
	SubnettingRuleTypeMinSubnets SubnettingRuleType = "minSubnets"
	SubnettingRuleTypeMinHosts   SubnettingRuleType = "minHosts"
)

SubnettingRuleType constants.

Jump to

Keyboard shortcuts

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