network

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NetworkRegistry

type NetworkRegistry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *NetworkRegistry

NewRegistry creates a new instance of your registry with the embedded Handler.

func (*NetworkRegistry) CIDRContains

func (nr *NetworkRegistry) CIDRContains(cidrStr string, ip string) (bool, error)

CIDRContains checks if a given IP address is contained within a specified CIDR block.

It parses both the CIDR block and the IP address, and checks whether the IP falls within the network range defined by the CIDR. If either the CIDR or the IP address is invalid, an error is returned.

Parameters:

cidrStr string - the string representation of the CIDR block.
ip string - the string representation of the IP address to check.

Returns:

bool - true if the IP address is within the CIDR block, false otherwise.
error - an error if the CIDR block or IP address cannot be parsed.

Example:

{{ cidrContains "192.168.0.0/24" "192.168.0.1" }}  // Output: true
{{ cidrContains "192.168.0.0/24" "10.0.0.1" }}     // Output: false

func (*NetworkRegistry) CIDRFirst

func (nr *NetworkRegistry) CIDRFirst(cidrStr string) (string, error)

CIDRFirst returns the first IP address in the given CIDR block.

Parameters:

cidrStr string - the string representation of the CIDR block.

Returns:

string - the first IP address as a string.
error - an error if the CIDR block cannot be parsed.

Example:

{{ cidrFirst "10.42.0.0/24" }} // Output: 10.42.0.0
{{ cidrFirst "2001:db8::/32" }} // Output: 2001:db8::

func (*NetworkRegistry) CIDRLast

func (nr *NetworkRegistry) CIDRLast(cidrStr string) (string, error)

CIDRLast returns the last IP address in the given CIDR block.

Parameters:

cidrStr string - the string representation of the CIDR block.

Returns:

string - the last IP address as a string.
error - an error if the CIDR block cannot be parsed.

Example:

{{ cidrLast "10.42.0.0/24" }} // Output: 10.42.0.255
{{ cidrLast "2001:db8::/32" }} // Output: 2001:db8::ffff:ffff

func (*NetworkRegistry) CIDROverlap

func (nr *NetworkRegistry) CIDROverlap(cidrStrA, cidrStrB string) (bool, error)

CIDROverlap checks if two CIDR blocks overlap. It parses both CIDR blocks and determines whether they overlap.

Parameters:

cidrStrA string - the first CIDR block.
cidrStrB string - the second CIDR block.

Returns:

bool - true if the two CIDR blocks overlap, false otherwise.
error - an error if either of the CIDR blocks cannot be parsed.

Example:

{{ cidrOverlap "10.42.0.0/24" "10.42.0.0/16" }} // Output: true
{{ cidrOverlap "192.168.1.0/24" "192.168.2.0/24" }} // Output: false
{{ cidrOverlap "2001:db8::/64" "2001:db8::/32" }} // Output: true
{{ cidrOverlap "2001:db8::/64" "2001:db8:1::/64" }} // Output: false

func (*NetworkRegistry) CIDRRangeList

func (nr *NetworkRegistry) CIDRRangeList(cidrStr string) ([]net.IP, error)

CIDRRangeList generates a list of all IP addresses within the given CIDR block. It works for both IPv4 and IPv6 CIDR blocks, ! WARNING that generating all IPs in a large IPv4/IPv6 block may consume ! significant memory and processing time.

Parameters:

cidr string - the string representation of the CIDR block.

Returns:

[]net.IP - a slice containing all IP addresses within the CIDR block.
error - an error if the CIDR block cannot be parsed.

Example:

{{ range cidrRangeList "10.42.1.1/32" }}{{ . }}{{ end }} // Output: 10.42.1.1
{{ range cidrRangeList "2001:db8::/128" }}{{ . }}{{ end }} // Output: 2001:db8::

func (*NetworkRegistry) CIDRSize

func (nr *NetworkRegistry) CIDRSize(cidrStr string) (*big.Int, error)

CIDRSize calculates the total number of IP addresses in the given CIDR block. It works for both IPv4 and IPv6 CIDR blocks.

The function parses the CIDR string, determines the IP version, and calculates the size of the network range based on the prefix length.

Parameters:

cidr string - the string representation of the CIDR block.

Returns:

*big.Int - the total number of IP addresses in the CIDR block.
error - an error if the CIDR block cannot be parsed.

Example:

{{ cidrSize "192.168.0.0/24" }}   // Output: 256
{{ cidrSize "2001:db8::/32" }}    // Output: 79228162514264337593543950336 (IPv6 range)

func (*NetworkRegistry) IPDecrement

func (nr *NetworkRegistry) IPDecrement(ip net.IP) (net.IP, error)

IPDecrement decrements the given IP address by one unit. This function works for both IPv4 and IPv6 addresses.

It converts the IP to the correct byte length depending on the version (IPv4 or IPv6) and decrements the address by 1. In case of an underflow (e.g., decrementing 0.0.0.0 in IPv4), an error is returned.

Parameters:

ip net.IP - the IP address to decrement.

Returns:

net.IP - the decremented IP address.
error - an error if the IP address underflows or if the IP version cannot be determined.

Example:

{{ parseIP "192.168.0.2" | ipDecrement }} // Output: 192.168.0.1
{{ parseIP "ffff::1" | ipDecrement }}     // Output: ffff::

func (*NetworkRegistry) IPIncrement

func (nr *NetworkRegistry) IPIncrement(ip net.IP) (net.IP, error)

IPIncrement increments the given IP address by one unit. This function works for both IPv4 and IPv6 addresses.

It converts the IP to the correct byte length depending on the version (IPv4 or IPv6) and increments the address by 1. In case of an overflow (e.g., incrementing 255.255.255.255 in IPv4), an error is returned.

Parameters:

ip net.IP - the IP address to increment.

Returns:

net.IP - the incremented IP address.
error - an error if the IP address overflows or if the IP version cannot be determined.

Example:

{{ parseIP "192.168.0.1" | ipIncrement }} // Output: 192.168.0.2
{{ parseIP "ffff::" | ipIncrement }}      // Output: ffff::1

func (*NetworkRegistry) IPIsGlobalUnicast

func (nr *NetworkRegistry) IPIsGlobalUnicast(ipStr string) (bool, error)

IPIsGlobalUnicast checks if the given IP address is a global unicast address.

It parses the provided string as an IP address and checks whether it is a global unicast address. Global unicast addresses are globally unique and routable (not multicast, loopback, or private).

Parameters:

ipStr string - the string representation of the IP address.

Returns:

bool - true if the IP address is a global unicast address.
error - an error if the IP address is invalid or cannot be parsed.

Example:

{{ ipIsGlobalUnicast "8.8.8.8" }} // Output: true
{{ ipIsGlobalUnicast "127.0.0.1" }} // Output: false

func (*NetworkRegistry) IPIsLoopback

func (nr *NetworkRegistry) IPIsLoopback(ipStr string) (bool, error)

IPIsLoopback checks if the given IP address is a loopback address.

It parses the provided string as an IP address and checks whether it is a loopback address (e.g., 127.0.0.1 for IPv4, ::1 for IPv6).

Parameters:

ipStr string - the string representation of the IP address.

Returns:

bool - true if the IP address is a loopback address.
error - an error if the IP address is invalid or cannot be parsed.

Example:

{{ ipIsLoopback "127.0.0.1" }}  // Output: true
{{ ipIsLoopback "192.168.0.1" }} // Output: false

func (*NetworkRegistry) IPIsMulticast

func (nr *NetworkRegistry) IPIsMulticast(ipStr string) (bool, error)

IPIsMulticast checks if the given IP address is a multicast address.

It parses the provided string as an IP address and checks whether it is a multicast address. Multicast addresses are used to send data to multiple receivers.

Parameters:

ipStr string - the string representation of the IP address.

Returns:

bool - true if the IP address is a multicast address.
error - an error if the IP address is invalid or cannot be parsed.

Example:

{{ ipIsMulticast "224.0.0.1" }} // Output: true
{{ ipIsMulticast "192.168.0.1" }} // Output: false

func (*NetworkRegistry) IPIsPrivate

func (nr *NetworkRegistry) IPIsPrivate(ipStr string) (bool, error)

IPIsPrivate checks if the given IP address is a private address.

It parses the provided string as an IP address and checks whether it is a private address. Private addresses are typically used for local communication within a network (e.g., 192.168.x.x).

Parameters:

ipStr string - the string representation of the IP address.

Returns:

bool - true if the IP address is a private address.
error - an error if the IP address is invalid or cannot be parsed.

Example:

{{ ipIsPrivate "192.168.0.1" }} // Output: true
{{ ipIsPrivate "8.8.8.8" }} // Output: false

func (*NetworkRegistry) IPVersion

func (nr *NetworkRegistry) IPVersion(ipStr string) (uint8, error)

IPVersion determines the IP version (IPv4 or IPv6) from a string representation of an IP address.

Parameters:

ipStr string - the string representation of the IP address.

Returns:

uint8 - the IP version, 4 for IPv4 or 16 for IPv6.
error - an error if the IP address is invalid or cannot be parsed.

Example:

{{ ipVersion "192.168.0.1" }} // Output: 4
{{ ipVersion "2001:db8::" }} // Output: 6

func (*NetworkRegistry) LinkHandler

func (nr *NetworkRegistry) LinkHandler(fh sprout.Handler) error

LinkHandler links the handler to the registry at runtime.

func (*NetworkRegistry) ParseCIDR

func (nr *NetworkRegistry) ParseCIDR(str string) (*net.IPNet, error)

ParseCIDR parses a string representation of an IP address and prefix length (CIDR notation) and returns its *net.IPNet form.

It attempts to parse the provided string as a CIDR (Classless Inter-Domain Routing) block. If the string is not valid CIDR notation, an error is returned.

Parameters:

str string - the string representation of the CIDR block.

Returns:

*net.IPNet - the parsed IP network in its *net.IPNet format.
error - an error if the string cannot be parsed as valid CIDR notation.

Example:

{{ parseCIDR "192.168.0.0/24" }}  // Output: &net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
{{ parseCIDR "2001:db8::/32" }}   // Output: &net.IPNet{IP: net.IP{32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Mask: net.CIDRMask(32, 128)}

func (*NetworkRegistry) ParseIP

func (nr *NetworkRegistry) ParseIP(str string) (net.IP, error)

ParseIP parses a string representation of an IP address and returns its net.IP form.

It attempts to parse the string as either an IPv4 or IPv6 address. If the provided string is not a valid IP address, an error is returned.

Parameters:

str string - the string representation of the IP address.

Returns:

net.IP - the parsed IP address in its net.IP format.
error - an error if the string cannot be parsed as a valid IP address.

Example:

{{ parseIP "10.42.0.1" }} // Output: net.IP{10, 42, 0, 1}
{{ parseIP "2001:db8::" }} // Output: net.IP{32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

func (*NetworkRegistry) ParseMAC

func (nr *NetworkRegistry) ParseMAC(str string) (net.HardwareAddr, error)

ParseMAC parses a string representation of a MAC address and returns its net.HardwareAddr form.

It attempts to parse the provided string as a MAC address. If the string is not a valid MAC address, an error is returned.

Parameters:

str string - the string representation of the MAC address.

Returns:

net.HardwareAddr - the parsed MAC address in its net.HardwareAddr format.
error - an error if the string cannot be parsed as a valid MAC address.

Example:

{{ parseMAC "01:23:45:67:89:ab" }} // Output: net.HardwareAddr{1, 35, 69, 103, 137, 171}

func (*NetworkRegistry) RegisterAliases

func (nr *NetworkRegistry) RegisterAliases(aliasMap sprout.FunctionAliasMap) error

func (*NetworkRegistry) RegisterFunctions

func (nr *NetworkRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error

RegisterFunctions registers all functions of the registry.

func (*NetworkRegistry) RegisterNotices

func (nr *NetworkRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error

func (*NetworkRegistry) Uid

func (nr *NetworkRegistry) Uid() string

Uid returns the unique identifier of the registry.

Jump to

Keyboard shortcuts

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