netaddr

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2021 License: Apache-2.0 Imports: 6 Imported by: 7

README

netaddr package for go

This repo contains a library to complement the go net library and provides containers and utilities like in python's netaddr.

Please see the api documentation for details. The authoritative source for this library is found on github. We encourage importing this code using the stable, versioned URL provided by gopkg.in. Once imported, refer to it as netaddr in your code (without the version).

import "gopkg.in/netaddr.v1"

comparison with python's netaddr

This netaddr library was written to complement the existing net package in go just filling in a few gaps that existed. See the table below for a side-by-side comparison of python netaddr features and the corresponding features in this library or elsewhere in go packages.

Python netaddr Go
EUI ???
IPAddress Use IP from net*
IPNetwork Use IPNet from net**
IPSet Use IPSet
IPRange Use [IPRange]
IPGlob Not yet implemented

* The net package in golang parses IPv4 address as IPv4 encoded IPv6 addresses. I found this design choice frustrating. Hence, there is a ParseIP in this package that always parses IPv4 as 4 byte addresses.

** This package provides a few extra convenience utilities for IPNet. See ParseNet, NetSize, BroadcastAddr, and NetworkAddr.

help

This needs a lot of work. Help if you can!

  • More test coverage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BroadcastAddr

func BroadcastAddr(n *net.IPNet) net.IP

BroadcastAddr returns the last address in the given network, or the broadcast address.

func ContainsNet added in v1.3.0

func ContainsNet(net1, net2 *net.IPNet) bool

ContainsNet returns true if net2 is a subset of net1. To be clear, it returns true if net1 == net2 also.

func IPLessThan added in v1.4.0

func IPLessThan(a, b net.IP) bool

IPLessThan compare two ip addresses true ordered by ipv4 first, then ipv6 later then by section left-most is most significant e.g. 10.0.0.0 10.0.0.1 192.169.0.1 2001:db8::

func IPMax added in v1.4.0

func IPMax(a, b net.IP) net.IP

IPMax returns the maximum of a and b

func IPMin added in v1.4.0

func IPMin(a, b net.IP) net.IP

IPMin returns the minimum of a and b

func IPv4 added in v1.5.0

func IPv4(a, b, c, d byte) net.IP

IPv4 returns the IP address (in 4-byte form) of the IPv4 address a.b.c.d.

func IPv4Net added in v1.5.0

func IPv4Net(a, b, c, d byte, p int) net.IPNet

IPv4Net returns the IPNet (in 4-byte form) of the IPv4 address a.b.c.d/p.

func NetSize

func NetSize(n *net.IPNet) *big.Int

NetSize returns the size of the given IPNet in terms of the number of addresses. It always includes the network and broadcast addresses.

func NetworkAddr

func NetworkAddr(n *net.IPNet) net.IP

NetworkAddr returns the first address in the given network, or the network address.

func NewIP

func NewIP(size int) net.IP

NewIP returns a new IP with the given size. The size must be 4 for IPv4 and 16 for IPv6.

func ParseCIDR added in v1.2.0

func ParseCIDR(cidr string) (net.IP, *net.IPNet, error)

ParseCIDR is like net.ParseCIDR except that it parses IPv4 addresses as 4 byte addresses instead of 16-byte mapped IPv6 addresses. Much like ParseIP.

func ParseCIDRToNet added in v1.2.0

func ParseCIDRToNet(cidr string) (*net.IPNet, error)

ParseCIDRToNet is like ParseCIDR except that it only returns one *net.IPNet that unifies the IP address and the mask. It leaves out the network address which ParseCIDR returns. This may be considered an abuse of the IPNet construct as it is documented that IP is supposed to be the "network number". However, the public IPNet interface does not dissallow it and this usage has been spotted in the wild.

func ParseIP

func ParseIP(address string) net.IP

ParseIP is like net.ParseIP except that it parses IPv4 addresses as 4 byte addresses instead of 16-byte mapped IPv6 addresses. This has been one of my biggest gripes against the net package.

func ParseNet

func ParseNet(cidr string) (parsed *net.IPNet, err error)

ParseNet parses an IP network from a CIDR. Unlike net.ParseCIDR, it does not allow a CIDR where the host part is non-zero. For example, the following CIDRs will result in an error: 203.0.113.1/24, 2001:db8::1/64, 10.0.20.0/20

Types

type IPRange added in v1.4.0

type IPRange struct {
	First, Last net.IP
}

IPRange range of ips not necessarily aligned to a power of 2

func IPRangeFromIPNet added in v1.4.0

func IPRangeFromIPNet(cidr *net.IPNet) *IPRange

IPRangeFromIPNet get an IPRange from an *ip.Net

func (*IPRange) Contains added in v1.4.0

func (r *IPRange) Contains(b *IPRange) bool

Contains returns true if b is contained in r

func (*IPRange) Minus added in v1.4.0

func (r *IPRange) Minus(b *IPRange) []*IPRange

Minus returns the ranges in r that are not in b

func (*IPRange) String added in v1.4.0

func (r *IPRange) String() string

type IPSet

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

IPSet is a set of IP addresses

func (*IPSet) Contains

func (s *IPSet) Contains(ip net.IP) bool

Contains returns true iff this IPSet contains the the given IP address

func (*IPSet) ContainsNet

func (s *IPSet) ContainsNet(net *net.IPNet) bool

ContainsNet returns true iff this IPSet contains all IPs in the given network

func (*IPSet) Difference

func (s *IPSet) Difference(other *IPSet) (newSet *IPSet)

Difference computes the set difference between this IPSet and another one It returns the result as a new set.

func (*IPSet) GetIPs

func (s *IPSet) GetIPs(limit int) (ips []net.IP)

GetIPs retrieves a slice of the first IPs in the set ordered by address up to the given limit.

func (*IPSet) GetNetworks added in v1.3.0

func (s *IPSet) GetNetworks() []*net.IPNet

GetNetworks retrieves a list of all networks included in the ipTree

func (*IPSet) Insert

func (s *IPSet) Insert(ip net.IP)

Insert ensures this IPSet has the given IP

func (*IPSet) InsertNet

func (s *IPSet) InsertNet(net *net.IPNet)

InsertNet ensures this IPSet has the entire given IP network

func (*IPSet) Intersection

func (s *IPSet) Intersection(set1 *IPSet) (interSect *IPSet)

Intersection computes the set intersect between this IPSet and another one It returns a new set which is the intersection.

func (*IPSet) Remove

func (s *IPSet) Remove(ip net.IP)

Remove ensures this IPSet does not contain the given IP

func (*IPSet) RemoveNet

func (s *IPSet) RemoveNet(net *net.IPNet)

RemoveNet ensures that all of the IPs in the given network are removed from the set if present.

func (*IPSet) String

func (s *IPSet) String() (str []string)

String returns a list of IP Networks

func (*IPSet) Union

func (s *IPSet) Union(other *IPSet) (newSet *IPSet)

Union computes the union of this IPSet and another set. It returns the result as a new set.

Jump to

Keyboard shortcuts

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