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
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
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
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
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
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
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
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
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
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
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
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
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
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
cidrFirst
CIDRFirst returns the first IP address in the given CIDR block.
Signature
cidrLast
CIDRLast returns the last IP address in the given CIDR block.
Signature
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
Last updated