network

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package network provides functionality for working with IP networks and subnet hosts.

The package includes functions for iterating over hosts within a subnet, calculating the total number of hosts in a subnet, parsing CIDR notation, and obtaining the first and last IP addresses from an IP network.

Examples:

ipNet := &net.IPNet{IP: net.IPv4(192, 168, 0, 0), Mask: net.CIDRMask(24, 32)}
it := network.NewSubnetHostsIterator(ipNet)

for ip := it.Next(); ip != nil; ip = it.Next() {
	// Process the host IP
	fmt.Println(ip.String())
}

cidr := "192.168.0.0/24"
totalHosts, err := network.CalculateTotalHostsFromCIDRString(cidr)
if err != nil {
	fmt.Println("Error:", err)
	return
}
fmt.Println("Total hosts:", totalHosts)

firstIP := network.GetFirstIPAddressFromIPNet(ipNet)
lastIP := network.GetLastIPAddressFromIPNet(ipNet)
fmt.Println("First IP:", firstIP)
fmt.Println("Last IP:", lastIP)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateTotalHosts added in v1.0.0

func CalculateTotalHosts(ipNet *net.IPNet) int

CalculateTotalHosts calculates the total number of hosts based on the provided IP network.

func CalculateTotalHostsFromCIDRString added in v1.0.0

func CalculateTotalHostsFromCIDRString(cidr string) (int, error)

CalculateTotalHostsFromCIDRString calculates the total number of hosts based on the provided CIDR string.

func GetFirstIPAddressFromIPNet added in v1.0.0

func GetFirstIPAddressFromIPNet(ipNet *net.IPNet) net.IP

GetFirstIPAddressFromIPNet returns the first host IP address within the given IP network.

func GetLastIPAddressFromIPNet added in v1.0.0

func GetLastIPAddressFromIPNet(ipNet *net.IPNet) net.IP

GetLastIPAddressFromIPNet returns the last host IP address within the given IP network.

Types

type SubnetHostsIterator added in v1.0.0

type SubnetHostsIterator struct {
	// IPNet represents the subnet to iterate over.
	IPNet *net.IPNet

	// CurrentIP represents the current host IP.
	CurrentIP *net.IP

	// FirstIP represents the first host IP in the subnet.
	FirstIP net.IP

	// LastIP represents the last host IP in the subnet.
	LastIP net.IP

	// TotalHosts represents the total number of hosts in the subnet.
	TotalHosts int
	// contains filtered or unexported fields
}

SubnetHostsIterator represents an iterator over the hosts within a subnet.

func NewSubnetHostsIterator added in v1.0.0

func NewSubnetHostsIterator(ipNet *net.IPNet) *SubnetHostsIterator

NewSubnetHostsIterator creates a new SubnetHostsIterator for the given IP network. It initializes the iterator with the first and last host IPs, the IP network, the current IP, and the total number of hosts in the subnet.

func NewSubnetHostsIteratorFromCIDRString added in v1.0.0

func NewSubnetHostsIteratorFromCIDRString(cidr string) (*SubnetHostsIterator, error)

NewSubnetHostsIteratorFromCIDRString creates a new SubnetHostsIterator for the given CIDR string. It parses the CIDR string, creates an IP network, and initializes the iterator with the necessary values.

func (*SubnetHostsIterator) Next added in v1.0.0

func (it *SubnetHostsIterator) Next() *net.IP

Next returns the next host IP in the subnet. It locks the iterator for thread-safety. If it's the first call to Next, it returns the first host IP in the subnet. If there are no more hosts in the subnet or if the current IP is outside the subnet, it returns nil.

Jump to

Keyboard shortcuts

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