Network

The Network registry includes a range of utilities for working with network resources, such as IP addresses, CIDR blocks, and network interfaces.

You can easily import all the functions from the network registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/network"

parseIP

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.

Signature

{{ parseIP "10.42.0.1" }} // Output: 10.42.0.1
{{ parseIP "2001:db8::" }} // Output: 2001:db8::

parseMAC

ParseMAC parses a string representation of a MAC address and returns its net.HardwareAddr form. It attempts to parse the string as a MAC address.

Signature

{{ parseMAC "01:23:45:67:89:ab" }} // Output: 01:23:45:67:89:ab

parseCIDR

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.

Signature

{{ parseCIDR "192.168.0.0/24" }} // Output: 192.168.0.0/24
{{ parseCIDR "2001:db8::/32" }} // Output: 2001:db8::/32

ipVersion

IPVersion determines the IP version (IPv4 or IPv6) from a string representation of an IP address. It returns the IP version as an integer (4 or 6) or an error if the provided string is not a valid IP address.

Signature

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

ipIsLoopback

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

Signature

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

ipIsGlobalUnicast

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 on the public internet (not multicast, loopback, or private).

Signature

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

ipIsMulticast

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.

Signature

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

ipIsPrivate

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

Signature

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

ipIncrement

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.

Signature

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

ipDecrement

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.

Signature

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

cidrContains

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.

Signature

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

cidrSize

CIDRSize calculates the total number of IP addresses in the given CIDR block. It works for both IPv4 and IPv6 CIDR blocks. The function returns the total number of IP addresses as a big.Int value, which can be used for large CIDR blocks.

Signature

{{ cidrSize "192.168.0.0/24" }}   // Output: 256
{{ cidrSize "2001:db8::/32" }}    // Output: 79228162514264337593543950336

cidrRangeList

CIDRRangeList generates a list of all IP addresses within the given CIDR block. It works for both IPv4 and IPv6 CIDR blocks, returning a list of all IP addresses as strings. This function is useful for iterating over all IP addresses in a CIDR block or generating a list of IP addresses for further processing.

Be careful, this method can generate numerous IP addresses for large CIDR blocks, which may consume a significant amount of memory and processing time.

Signature

{{ cidrRangeList "10.42.1.1/32" }} // Output: [10.42.1.1]
{{ cidrRangeList "2001:db8::/128" }} // Output: [2001:db8::]

cidrFirst

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

Signature

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

cidrLast

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

Signature

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

cidrOverlap

CIDROverlap checks if two CIDR blocks overlap. It parses both CIDR blocks and determines whether they overlap. Two CIDR blocks overlap if they have at least one IP address in common. The function returns true if the CIDR blocks overlap, and false otherwise.

Signature

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

Last updated