iputil

package
v0.0.0-...-8ef8941 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const IPv4CIDRRegexPattern = IPv4RegexPattern + `\/([1-2]?[0-9]|3[0-2])`
View Source
const IPv4RangeRegexPattern = IPv4RegexPattern + `[-,]` + IPv4RegexPattern
View Source
const IPv4RegexPattern = `(` + V4Octet + `\.){3}` + V4Octet
View Source
const IPv6CIDRRegexPattern = IPv6RegexPattern + `\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])`
View Source
const IPv6RangeRegexPattern = IPv6RegexPattern + `[-,]` + IPv6RegexPattern
View Source
const IPv6RegexPattern = `` /* 659-byte string literal not displayed */
View Source
const V4Octet = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`

Variables

View Source
var (
	// ErrNotASN is returned when the input is not in proper ASN form.
	ErrNotASN = errors.New("not asn")

	// ErrNotIP is returned when the input is not in proper IP form.
	ErrNotIP = errors.New("not ip")

	// ErrNotCIDR is returned when the input is not in proper CIDR form.
	ErrNotCIDR = errors.New("not cidr")

	// ErrNotIPRange is returned when the input is not in proper IP range form.
	ErrNotIPRange = errors.New("not ip range")

	// ErrNotIP6Range is returned when the input is not in proper IPv6 range
	// form.
	ErrNotIP6Range = errors.New("not ipv6 range")

	// ErrNotFile is returned when the input is not an actual file.
	ErrNotFile = errors.New("not file")

	// ErrMissingCIDRsOrIPRange is returned when a CIDR or IP range is missing.
	ErrMissingCIDRsOrIPRange = errors.New("missing CIDRs or IP range")

	// ErrCannotMixCIDRAndIPs is returned when CIDRs and IPs are mixed.
	ErrCannotMixCIDRAndIPs = errors.New("cannot mix CIDRs and IPs")

	// ErrIPRangeRequiresTwoIPs is returned when the IP range is incomplete.
	ErrIPRangeRequiresTwoIPs = errors.New("IP range requires 2 IPs")

	// ErrInvalidInput is returned as a generic error for bad input.
	ErrInvalidInput = errors.New("invalid input")
)
View Source
var BogonIP4List []IPRange

these lists are initialized on startup inside this pkg's `init`.

View Source
var BogonIP6List []IP6Range
View Source
var BogonRange4Str []string = []string{
	"0.0.0.0/8",
	"10.0.0.0/8",
	"100.64.0.0/10",
	"127.0.0.0/8",
	"169.254.0.0/16",
	"172.16.0.0/12",
	"192.0.0.0/24",
	"192.0.2.0/24",
	"192.168.0.0/16",
	"198.18.0.0/15",
	"198.51.100.0/24",
	"203.0.113.0/24",
	"224.0.0.0/4",
	"240.0.0.0/4",
	"255.255.255.255/32",
}

list of bogon IPv4 IPs.

View Source
var BogonRange6Str []string = []string{
	"::/128",
	"::1/128",
	"::ffff:0:0/96",
	"::/96",
	"100::/64",
	"2001:10::/28",
	"2001:db8::/32",
	"fc00::/7",
	"fe80::/10",
	"fec0::/10",
	"ff00::/8",

	"2002::/24",
	"2002:a00::/24",
	"2002:7f00::/24",
	"2002:a9fe::/32",
	"2002:ac10::/28",
	"2002:c000::/40",
	"2002:c000:200::/40",
	"2002:c0a8::/32",
	"2002:c612::/31",
	"2002:c633:6400::/40",
	"2002:cb00:7100::/40",
	"2002:e000::/20",
	"2002:f000::/20",
	"2002:ffff:ffff::/48",

	"2001::/40",
	"2001:0:a00::/40",
	"2001:0:7f00::/40",
	"2001:0:a9fe::/48",
	"2001:0:ac10::/44",
	"2001:0:c000::/56",
	"2001:0:c000:200::/56",
	"2001:0:c0a8::/48",
	"2001:0:c612::/47",
	"2001:0:c633:6400::/56",
	"2001:0:cb00:7100::/56",
	"2001:0:e000::/36",
	"2001:0:f000::/36",
	"2001:0:ffff:ffff::/64",
}

list of bogon IPv6 IPs.

View Source
var CidrRegex *regexp.Regexp
View Source
var IpCidrRegex *regexp.Regexp
View Source
var IpRangeRegex *regexp.Regexp
View Source
var IpRegex *regexp.Regexp
View Source
var IpSubnetRegex *regexp.Regexp
View Source
var IpV4Regex *regexp.Regexp
View Source
var IpV6Regex *regexp.Regexp

MaxU128 is the largest possible U128 value.

View Source
var RangeRegex *regexp.Regexp
View Source
var SubnetRegex *regexp.Regexp
View Source
var U128BitMasks = [...]U128{}/* 129 elements not displayed */

Copyright 2021 The Inet.Af AUTHORS. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file at https://github.com/inetaf/netaddr/blob/main/LICENSE

U128BitMasks are bitmasks with the topmost n bits of a 128-bit number, where n is the array index.

generated with https://play.golang.org/p/64XKxaUSa_9

View Source
var V4CidrRegex *regexp.Regexp
View Source
var V4IpCidrRegex *regexp.Regexp
View Source
var V4IpRangeRegex *regexp.Regexp
View Source
var V4IpSubnetRegex *regexp.Regexp
View Source
var V4RangeRegex *regexp.Regexp
View Source
var V4SubnetRegex *regexp.Regexp
View Source
var V6CidrRegex *regexp.Regexp
View Source
var V6IpCidrRegex *regexp.Regexp
View Source
var V6IpRangeRegex *regexp.Regexp
View Source
var V6IpSubnetRegex *regexp.Regexp
View Source
var V6RangeRegex *regexp.Regexp
View Source
var V6SubnetRegex *regexp.Regexp

Functions

func CIDRsFromIP6RangeStrRaw

func CIDRsFromIP6RangeStrRaw(rStr string) ([]string, error)

CIDRsFromIP6RangeStrRaw returns a list of CIDR strings which cover the full range specified in the IP range string `rStr`.

`rStr` must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func CIDRsFromIPRangeStrRaw

func CIDRsFromIPRangeStrRaw(rStr string) ([]string, error)

CIDRsFromIPRangeStrRaw returns a list of CIDR strings which cover the full range specified in the IP range string `rStr`.

`rStr` must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func DecimalStrToIP

func DecimalStrToIP(decimal string, forceIPv6 bool) (net.IP, error)

DecimalStrToIP converts a decimal string to an IP address

func FileExists

func FileExists(pathToFile string) bool

FileExists checks if a file exists.

func GetInputFrom

func GetInputFrom(
	inputs []string,
	stdin bool,
	file bool,
	op func(input string, inputType INPUT_TYPE) error,
) error

GetInputFrom retrieves input data from various sources and processes it using the provided operation. The operation is called for each input string with input type.

Usage:
err := GetInputFrom(inputs,
	true,
	true,
	func(input string, inputType INPUT_TYPE) error {
		switch inputType {
		case INPUT_TYPE_IP:
			// Process IP here
		}
		return nil
	},
)

func IP4toInt

func IP4toInt(IPv4Address net.IP) int64

IP4toInt converts an IPv4 address to a big.Int

func IP6toInt

func IP6toInt(IPv6Address net.IP) *big.Int

IP6toInt converts an IPv6 address to a big.Int

func IPListFromAllSrcs

func IPListFromAllSrcs(inputs []string) ([]net.IP, error)

IPListFromAllSrcs returns a list of all IPs from all sources such as stdin, argument list and files.

func IPListFromCIDR

func IPListFromCIDR(cidrStr string) ([]net.IP, error)

IPListFromCIDR returns a list of IPs from a CIDR string.

func IPListFromCIDRs

func IPListFromCIDRs(cidrStrs []string) (ips []net.IP, err error)

IPListFromCIDRs returns a list of IPs from a list of CIDRs in string form.

func IPListFromFile

func IPListFromFile(pathToFile string) ([]net.IP, error)

IPListFromFile returns a list of IPs found in a file.

func IPListFromFiles

func IPListFromFiles(paths []string) (ips []net.IP, err error)

IPListFromFiles returns a list of IPs found in a list of files.

func IPListFromRange

func IPListFromRange(ipStrStart string, ipStrEnd string) ([]net.IP, error)

IPListFromRange returns a list of IPs from a start and end IP string.

func IPListFromRangeStr

func IPListFromRangeStr(rStr string) ([]net.IP, error)

IPListFromRangeStr returns a list of IPs given a range string.

`rStr` must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func IPListFromReader

func IPListFromReader(r io.Reader, breakOnEmptyLine bool) []net.IP

IPListFromReader returns a list of IPs after reading from a reader; the reader should have IPs per-line.

func IPListFromStdin

func IPListFromStdin() []net.IP

IPListFromStdin returns a list of IPs from a stdin; the IPs should be 1 per line.

func IPListWriteAllFromFile

func IPListWriteAllFromFile(pathToFile string) error

IPListWriteFromFile returns a list of IPs found in a file.

func IPListWriteAllFromFiles

func IPListWriteAllFromFiles(paths []string) error

IPListWriteFromFiles returns a list of IPs found in a list of files from all sources.

func IPListWriteAllFromReader

func IPListWriteAllFromReader(r io.Reader, breakOnEmptyLine bool)

IPListWriteFromReader returns a list of IPs after reading from a reader; the reader should have IPs per-line.

func IPListWriteAllFromStdin

func IPListWriteAllFromStdin()

IPListWriteFromStdin returns a list of IPs from a stdin; the IPs should be 1 per line.

func IPListWriteFrom

func IPListWriteFrom(
	inputs []string,
	stdin bool,
	ip bool,
	iprange bool,
	cidr bool,
	file bool,
) error

IPListFromWrite outputs a list of IPs from stdin and a list of inputs which are interpreted to contain IPs, IP ranges, IP CIDRs and files with IPs in them, all depending upon which flags are set.

func IPListWriteFromAllSrcs

func IPListWriteFromAllSrcs(inputs []string) error

IPListFromAllSrcsWrite is the same as IPListFromWrite with all flags turned on.

func IPListWriteFromCIDR

func IPListWriteFromCIDR(cidrStr string) error

IPListFromCIDRWrite is the same as IPListFromCIDR with O(1) memory by discarding IPs after printing.

func IPListWriteFromCIDRs

func IPListWriteFromCIDRs(cidrStrs []string) error

IPListFromCIDRsWrite outputs a list of IPs from a list of CIDRs in string form.

func IPListWriteFromFile

func IPListWriteFromFile(
	pathToFile string,
	ip bool,
	iprange bool,
	cidr bool,
) error

IPListWriteFromSrcFile returns a list of IPs from selected sources found in a file.

func IPListWriteFromFiles

func IPListWriteFromFiles(
	paths []string,
	ip bool,
	iprange bool,
	cidr bool,
) error

IPListWriteFromFiles returns a list of IPs found in a list of files from select sources.

func IPListWriteFromIPRange

func IPListWriteFromIPRange(ipStrStart string, ipStrEnd string) error

IPListFromIPRangeWrite is the same as IPListFromRange with O(1) memory by discarding IPs after printing.

func IPListWriteFromIPRangeStr

func IPListWriteFromIPRangeStr(rStr string) error

IPListFromIPRangeStrWrite outputs all IPs in an IP range.

`rStr` must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func IPListWriteFromReader

func IPListWriteFromReader(
	r io.Reader,
	ip bool,
	iprange bool,
	cidr bool,
	breakOnEmptyLine bool,
)

IPListWriteFromReader returns a list of IPs after reading from a reader from selected sources; the reader should have IPs per-line.

func IPListWriteFromStdin

func IPListWriteFromStdin(
	ip bool,
	iprange bool,
	cidr bool,
)

IPListWriteFromStdin returns a list of IPs from a stdin from selected sources; the IPs should be 1 per line.

func IPtoDecimalStr

func IPtoDecimalStr(strIP string) (string, error)

IPtoDecimalStr converts an IP address to a decimal string

func InputHelper

func InputHelper(str string, op func(string, INPUT_TYPE) error) error

func IpAdd

func IpAdd(input string, delta int) net.IP

func IsBogonIP4

func IsBogonIP4(ip uint32) bool

IsBogonIP4 returns true if IPv4 is a BogonIP.

func IsBogonIP6

func IsBogonIP6(ip6 U128) bool

IsBogonIP6 returns true if IPv6 is a BogonIP.

func NetAndHostMasks

func NetAndHostMasks(size uint32) (uint32, uint32)

NetAndHostMasks returns network and host masks where the `size` most-significant bits are set to 1 and the rest set to 0 in the network mask, and the host mask is the bitwise-negation of the network mask.

func NetAndHostMasks6

func NetAndHostMasks6(size uint32) (U128, U128)

NetAndHostMasks returns network and host masks where the `size` most-significant bits are set to 1 and the rest set to 0 in the network mask, and the host mask is the bitwise-negation of the network mask.

func ProcessStringsFromFile

func ProcessStringsFromFile(filename string, op func(input string, inputType INPUT_TYPE) error) error

ProcessStringsFromFile reads strings from a file and passes it to op, one per line.

func ProcessStringsFromStdin

func ProcessStringsFromStdin(op func(input string, inputType INPUT_TYPE) error) error

ProcessStringsFromStdin reads strings from stdin until an empty line is entered.

func RandIP4

func RandIP4(noBogon bool) net.IP

RandIP4 returns a new randomly generated IPv4 address.

func RandIP4List

func RandIP4List(n int, noBogon bool) []net.IP

RandIP4List returns a list of new randomly generated IPv4 addresses.

func RandIP4ListWrite

func RandIP4ListWrite(n int, noBogon bool)

RandIP4ListWrite prints a list of randomly generated IPv4 addresses.

func RandIP4Range

func RandIP4Range(iprange IP4Range, noBogon bool) (net.IP, error)

RandIP4Range returns a list of randomly generated IPv4 addresses within the range specified by `IP4Range`.

note: `NewIP4Range` must be called before this function as this function assumes `IP4Range` provided to the function is a correct range.

func RandIP4RangeListWrite

func RandIP4RangeListWrite(
	startIP string,
	endIP string,
	n int,
	noBogon bool,
	unique bool,
) error

RandIP4RangeListWrite prints a list of randomly generated IPv4 addresses. `startIP` and `endIP` are the start & end IPs to generate IPs between. `n` is the number of IPs to generate. `noBogon`, if true, will ensure that none of the generated IPs are bogons. `unique`, if true, will ensure every IP generated is unique.

func RandIP6

func RandIP6(noBogon bool) net.IP

RandIP6 returns a new randomly generated IPv6 address.

func RandIP6List

func RandIP6List(n int, noBogon bool) []net.IP

RandIP6List returns a list of new randomly generated IPv6 addresses.

func RandIP6ListWrite

func RandIP6ListWrite(n int, noBogon bool)

RandIP6ListWrite prints a list of randomly generated IPv6 addresses.

func RandIP6Range

func RandIP6Range(ipRange IP6RangeInt, noBogon bool) net.IP

RandIP6Range returns a list of randomly generated IPv6 addresses within the range specified by `IP6RangeInt`.

note: `NewIP6RangeInt` must be called before this function as this function assumes `IP6RangeInt` provided to the function is a correct range.

func RandIP6RangeListWrite

func RandIP6RangeListWrite(
	startIP string,
	endIP string,
	n int,
	noBogon bool,
	unique bool,
) error

RandIP6RangeListWrite prints a list of randomly generated IPv6 addresses. `startIP` and `endIP` are the start & end IPs to generate IPs between. `n` is the number of IPs to generate. `noBogon`, if true, will ensure that none of the generated IPs are bogons. `unique`, if true, will ensure every IP generated is unique.

func StrIsASNStr

func StrIsASNStr(asn string) bool

StrIsASNStr checks whether an ASN string really is an ASN of the form "asX" or "ASX" where "X" is the ASN's number.

func StrIsCIDR6Str

func StrIsCIDR6Str(cidrStr string) bool

StrIsCIDR6Str checks whether a string is in proper CIDR IPv6 form.

func StrIsCIDRStr

func StrIsCIDRStr(cidrStr string) bool

StrIsCIDRStr checks whether a string is in proper CIDR form.

func StrIsIP6RangeStr

func StrIsIP6RangeStr(r string) bool

StrIsIP6RangeStr checks whether a string is an IPv6 range string.

The string must be of any of these forms to be considered an IP range:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func StrIsIP6Str

func StrIsIP6Str(ipStr string) bool

StrIsIP6Str checks whether a string is an IPv6.

func StrIsIPRangeStr

func StrIsIPRangeStr(r string) bool

StrIsIPRangeStr checks whether a string is an IP range.

The string must be of any of these forms to be considered an IP range:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func StrIsIPStr

func StrIsIPStr(ipStr string) bool

StrIsIPStr checks whether a string is an IP.

func StrIsIPv4Str

func StrIsIPv4Str(expression string) bool

StrIsIPv4Str checks if the given string is an IPv4 address

func StrIsIPv6Str

func StrIsIPv6Str(expression string) bool

StrIsIPv6Str checks if the given string is an IPv6 address

func StringSliceRev

func StringSliceRev(s []string)

StringSliceRev reverses the order of elements inside of a string slice.

Types

type CIDR

type CIDR struct {
	IP      net.IP
	Network *net.IPNet
}

CIDR represens a Classless Inter-Domain Routing structure.

func NewCidr

func NewCidr(s string) *CIDR

NewCidr creates a NewCidr CIDR structure.

func NewCidrList

func NewCidrList(s []string) []*CIDR

NewCidrList returns a slice of sorted CIDR structures.

func (*CIDR) MaskLen

func (c *CIDR) MaskLen() uint32

MaskLen returns a network mask length.

func (*CIDR) PrefixUint32

func (c *CIDR) PrefixUint32() uint32

PrefixUint32 returns a prefix.

func (*CIDR) Size

func (c *CIDR) Size() int

Size returns a size of a CIDR range.

func (*CIDR) String

func (c *CIDR) String() string

type INPUT_TYPE

type INPUT_TYPE string
const (
	INPUT_TYPE_IP       INPUT_TYPE = "IP"
	INPUT_TYPE_IP_RANGE INPUT_TYPE = "IPRange"
	INPUT_TYPE_CIDR     INPUT_TYPE = "CIDR"
	INPUT_TYPE_ASN      INPUT_TYPE = "ASN"
	INPUT_TYPE_UNKNOWN  INPUT_TYPE = "Unknown"
)

type IP

type IP uint32

IP is a numerical representation of an IPv4 address. The number must be in big-endian form.

func IPFromStdIP

func IPFromStdIP(ip net.IP) IP

IPFromStdIP returns a new IPv4 address representation from the standard library's IP representation.

func NewIP

func NewIP(ip uint32) IP

NewIP returns a new IPv4 address representation. `ip` must already be in big-endian form.

func (IP) String

func (ip IP) String() string

String returns the IPv4 string representation of `ip`.

type IP4Range

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

IP4Range is a numerical representation of an IPv4 range.

func NewIP4Range

func NewIP4Range(
	startIP string,
	endIP string,
) (IP4Range, error)

NewIP4Range returns a new IP4Range given the input start & end IPs.

note: starting and ending IPs must be valid IPv4 string formats.

type IP6

type IP6 struct {
	N U128
}

IP6 is a 128-bit number representation of a IPv6 address in big endian byte order.

The number is internally represented as 2 64-bit numbers.

func IP6FromByteSlice

func IP6FromByteSlice(b []byte) IP6

IP6FromByteSlice is the same as IP6FromBytes but from a byte slice which may be longer than 16 bytes long.

<16 byte slice will cause a panic.

func IP6FromBytes

func IP6FromBytes(b [16]byte) IP6

IP6FromBytes returns a new IP6 given 16 bytes representing the 16 bytes of the IPv6 address in big-endian order.

func IP6FromIP4Bytes

func IP6FromIP4Bytes(a uint8, b uint8, c uint8, d uint8) IP6

IP6FromIP4Bytes returns a new IP6 from the IPv4 address of bytes `a.b.c.d`.

func IP6FromStdIP

func IP6FromStdIP(ip net.IP) (IP6, bool)

IP6FromStdIP returns a new IP6 from a standard library `net.IP`, and whether the conversion succeeded.

func IP6FromU128

func IP6FromU128(n U128) IP6

IP6FromU128 returns a new IP6 from a U128 number.

func NewIP6

func NewIP6(hi uint64, lo uint64) IP6

NewIP6 returns a new IP6 based off of `hi` and `lo` numeric parts.

func (IP6) Cmp

func (ip1 IP6) Cmp(ip2 IP6) int

Cmp compares `ip1` and `ip2` and returns:

- -1 if `ip1<ip2` - 0 if `ip1==ip2` - 1 if `ip1>ip2`

func (IP6) Eq

func (ip1 IP6) Eq(ip2 IP6) bool

Eq returns whether `ip1 == ip2` numerically.

func (IP6) Gt

func (ip1 IP6) Gt(ip2 IP6) bool

Gt returns whether `ip1 > ip2` numerically.

func (IP6) Gte

func (ip1 IP6) Gte(ip2 IP6) bool

Gte returns whether `ip1 >= ip2` numerically.

func (IP6) Lt

func (ip1 IP6) Lt(ip2 IP6) bool

Lt returns whether `ip1 < ip2` numerically.

func (IP6) Lte

func (ip1 IP6) Lte(ip2 IP6) bool

Lte returns whether `ip1 <= ip2` numerically.

func (IP6) String

func (ip IP6) String() string

String returns the IPv6 string representation of `ip`.

func (IP6) To16ByteSlice

func (ip IP6) To16ByteSlice() []byte

To16ByteSlice returns the 16-byte slice representation of an IPv6 address in big-endian byte order.

func (IP6) To16Bytes

func (ip IP6) To16Bytes() [16]byte

To16Bytes returns the 16-byte array representation of an IPv6 address in big-endian byte order.

func (IP6) ToStdIP

func (ip IP6) ToStdIP() net.IP

ToStdIP returns the 16-byte slice standard library IPv6 representation.

type IP6CIDR

type IP6CIDR string

IP6CIDR is the representation of a IPv6 subnet in CIDR form.

type IP6Range

type IP6Range struct {
	// Start is the first IP in the IPv6 range.
	Start IP6

	// End is the last IP in the IPv6 range.
	End IP6
}

IP6Range represents a range of IPv6 addresses [Start, End].

func GetBogonRange6

func GetBogonRange6() []IP6Range

GetBogonRange6 returns list of IPRange of all IPv6 bogon IPs.

func IP6RangeFromCIDR

func IP6RangeFromCIDR(cidrStr string) (IP6Range, error)

IP6RangeFromCIDR returns an IP6Range given a CIDR in string form.

func NewIP6Range

func NewIP6Range(start IP6, end IP6) IP6Range

IP6Range returns a new IPv6 address range given a start and end IPv6 address.

func (IP6Range) LargestIP6Subnet

func (r IP6Range) LargestIP6Subnet() IP6Subnet

LargestIP6Subnet returns the largest subnet mask within this range.

func (IP6Range) ToCIDRs

func (r IP6Range) ToCIDRs() []string

ToCIDRs returns a list of CIDR strings which cover the full IP range.

func (IP6Range) ToIP6Subnets

func (r IP6Range) ToIP6Subnets() []IP6Subnet

ToIP6Subnets returns a list of subnet masks which cover the full IP range.

type IP6RangeInt

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

IP6Range is a numerical representation of an IPv6 range.

func NewIP6RangeInt

func NewIP6RangeInt(
	startIP string,
	endIP string,
) (IP6RangeInt, error)

NewIP6RangeInt returns a new IP6RangeInt given the input start & end IPs.

note: starting and ending IPs must be valid IPv6 string formats.

type IP6RangeStr

type IP6RangeStr struct {
	// Start is the first IP in the IPv6 range.
	Start string

	// End is the last IP in the IPv6 range.
	End string
}

IP6RangeStr represents a range of IPv6 addresses [Start, End] in string form.

func IP6RangeStrFromCIDR

func IP6RangeStrFromCIDR(cidrStr string) (IP6RangeStr, error)

IP6RangeStrFromCIDR returns the start and end IPv6 strings of a CIDR.

func IP6RangeStrFromStr

func IP6RangeStrFromStr(r string) (IP6RangeStr, error)

IP6RangeStrFromStr returns the two IPv6 parts (start and end) of an IPv6 range string.

The string must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func NewIP6RangeStr

func NewIP6RangeStr(start string, end string) IP6RangeStr

NewIP6RangeStr returns a new IP range string given a start & end IP.

func (IP6RangeStr) String

func (r IP6RangeStr) String() string

String returns the IPv6 range string as `<start>-<end>`.

func (IP6RangeStr) StringDelim

func (r IP6RangeStr) StringDelim(d string) string

StringDelim is the same as String but allows a custom delimiter.

func (IP6RangeStr) ToCIDRs

func (rStr IP6RangeStr) ToCIDRs() []string

ToCIDRs returns a list of CIDR strings which cover the full range specified in the IP range string `r`.

func (IP6RangeStr) ToIP6Range

func (r IP6RangeStr) ToIP6Range() IP6Range

ToIP6Range converts the string form of the range into a numerical form.

type IP6Subnet

type IP6Subnet struct {
	// NetBitCnt is the number of bits in the network part of the subnet.
	NetBitCnt uint32

	// NetMask is the subnet mask of the network part of the subnet.
	NetMask U128

	// HostBitCnt is the number of bits in the host part of the subnet.
	HostBitCnt uint32

	// HostMask is the subnet mask of the host part of the subnet.
	HostMask U128

	// LoIP is the big-endian representation of the lowest IP in the subnet.
	LoIP IP6

	// HiIP is the big-endian representation of the highest IP in the subnet.
	HiIP IP6
}

IP6Subnet is the representation of a IPv6 subnet.

func IP6SubnetFromCidr

func IP6SubnetFromCidr(cidr string) (IP6Subnet, error)

IP6SubnetFromCidr converts a CIDR notation to IP6Subnet.

func (IP6Subnet) SplitCIDR

func (s IP6Subnet) SplitCIDR(split int) ([]IP6Subnet, error)

SplitCIDR returns a list of smaller IPSubnet after splitting a larger CIDR into `split`.

func (IP6Subnet) ToCIDR

func (s IP6Subnet) ToCIDR() string

ToCIDR converts a IPSubnet to CIDR notation.

type IPCIDR

type IPCIDR string

IPCIDR is the representation of a IPv4 subnet in CIDR form.

type IPRange

type IPRange struct {
	// Start is the first IP in the IPv4 range.
	Start IP

	// End is the last IP in the IPv4 range.
	End IP
}

IPRange represents a range of IPv4 addresses [Start, End].

func GetBogonRange4

func GetBogonRange4() []IPRange

GetBogonRange4 returns list of IPRange of all IPv4 bogon IPs.

func IPRangeFromCIDR

func IPRangeFromCIDR(cidrStr string) (IPRange, error)

IPRangeFromCIDR returns the start and end IPs in big endian byte order of a CIDR in string form.

func NewIPRange

func NewIPRange(start IP, end IP) IPRange

NewIPRange returns a new IP range given a start and end IP.

func (IPRange) LargestIPSubnet

func (r IPRange) LargestIPSubnet() IPSubnet

LargestIPSubnet returns the largest subnet mask within this range.

func (IPRange) ToCIDRs

func (r IPRange) ToCIDRs() []string

ToCIDRs returns a list of CIDR strings which cover the full IP range.

func (IPRange) ToIPSubnets

func (r IPRange) ToIPSubnets() []IPSubnet

ToIPSubnets returns a list of subnet masks which cover the full IP range.

type IPRangeStr

type IPRangeStr struct {
	// Start is the first IP in the IPv4 range.
	Start string

	// End is the last IP in the IPv4 range.
	End string
}

IPRangeStr represents a range of IPv4 addresses [Start, End] in string form.

func IPRangeStrFromCIDR

func IPRangeStrFromCIDR(cidrStr string) (IPRangeStr, error)

IPRangeStrFromCIDR returns the start and end IP strings of a CIDR.

func IPRangeStrFromStr

func IPRangeStrFromStr(r string) (IPRangeStr, error)

IPRangeStrFromStr returns the two IP parts (start and end) of an IP range string.

The string must be of any of these forms:

<ip_range_start>-<ip_range_end>
<ip_range_start>,<ip_range_end>

func NewIPRangeStr

func NewIPRangeStr(start string, end string) IPRangeStr

NewIPRangeStr returns a new IP range string given a start & end IP.

func (IPRangeStr) String

func (r IPRangeStr) String() string

String returns the IP range string as `<start>-<end>`.

func (IPRangeStr) StringDelim

func (r IPRangeStr) StringDelim(d string) string

StringDelim is the same as String but allows a custom delimiter.

func (IPRangeStr) ToCIDRs

func (rStr IPRangeStr) ToCIDRs() []string

ToCIDRs returns a list of CIDR strings which cover the full range specified in the IP range string `r`.

func (IPRangeStr) ToIPRange

func (r IPRangeStr) ToIPRange() IPRange

ToIPRange converts the string form of the range into a numerical form.

type IPStr

type IPStr string

IPStr is a string representation of an IPv4 address.

type IPSubnet

type IPSubnet struct {
	// NetBitCnt is the number of bits in the network part of the subnet.
	NetBitCnt uint32

	// NetMask is the subnet mask of the network part of the subnet.
	NetMask uint32

	// HostBitCnt is the number of bits in the host part of the subnet.
	HostBitCnt uint32

	// HostMask is the subnet mask of the host part of the subnet.
	HostMask uint32

	// LoIP is the big-endian representation of the lowest IP in the subnet.
	LoIP IP

	// HiIP is the big-endian representation of the highest IP in the subnet.
	HiIP IP
}

IPSubnet is the representation of a IPv4 subnet.

func IPSubnetFromCidr

func IPSubnetFromCidr(cidr string) (IPSubnet, error)

IPSubnetFromCidr converts a CIDR notation to IPSubnet.

func (IPSubnet) SplitCIDR

func (s IPSubnet) SplitCIDR(split int) ([]IPSubnet, error)

SplitCIDR returns a list of smaller IPSubnet after splitting a larger CIDR into `split`.

func (IPSubnet) ToCIDR

func (s IPSubnet) ToCIDR() string

ToCIDR converts a IPSubnet to CIDR notation.

type U128

type U128 struct {
	Hi uint64
	Lo uint64
}

U128 is an unsigned 128-bit number type in big-endian form.

The endianness of `Hi` and `Lo` themselves is machine-dependent.

var ZeroU128 U128

ZeroU128 is a zero-valued U128.

func NewU128

func NewU128(hi uint64, lo uint64) U128

NewU128 returns the U128 value .

func U128From64

func U128From64(v uint64) U128

U128From64 converts a uint64 to a U128 value.

func (U128) Add

func (v1 U128) Add(v2 U128) (U128, uint64)

Add returns `v1 + v2` and any carry on overflow. The carry is guaranteed to be 0 or 1.

func (U128) Add64

func (v1 U128) Add64(v2 uint64) (U128, uint64)

Add64 returns `v1 + v2` and any carry on overflow. The carry is guaranteed to be 0 or 1.

func (U128) AddOne

func (v U128) AddOne() U128

AddOne returns `v1 + 1`. If overflow occurred, then IsZero will be true on the result.

func (U128) And

func (v1 U128) And(v2 U128) U128

And returns `v1 & v2`.

func (U128) And64

func (v1 U128) And64(v2 uint64) U128

And64 returns `v1 & v2`.

func (U128) ClearBitsFrom

func (v U128) ClearBitsFrom(i uint8) U128

ClearBitsFrom clears all bits from and including the `i`th bit, where `i` starts from 0 which refers to the most-significant bit.

func (U128) ClearBitsUpto

func (v U128) ClearBitsUpto(i uint8) U128

ClearBitsUpto clears all bits from the most-significant bit up to the `ith` bit, where `i` starts from 0 which refers to the most-significant bit.

func (U128) Cmp

func (v1 U128) Cmp(v2 U128) int

Cmp compares `v1` and `v2` and returns:

- -1 if `v1<v2` - 0 if `v1==v2` - 1 if `v1>v2`

func (U128) Eq

func (v1 U128) Eq(v2 U128) bool

Eq returns whether `v1 == v2`.

func (U128) Gt

func (v1 U128) Gt(v2 U128) bool

Gt returns whether `v1 > v2`.

func (U128) Gte

func (v1 U128) Gte(v2 U128) bool

Gte returns whether `v1 >= v2`.

func (U128) IsMax

func (v U128) IsMax() bool

IsMax returns whether `v` is the maximum U128 number.

func (U128) IsZero

func (v U128) IsZero() bool

IsZero returns whether `v` is 0.

func (U128) LeadingZeros

func (v U128) LeadingZeros() int

LeadingZeros returns the number of leading zero bits in `v`. Returns 128 for `v == 0`.

func (U128) Len

func (v U128) Len() int

Len returns the minimum number of bits required to represent `v`. Returns 0 for `v == 0`.

func (U128) Lt

func (v1 U128) Lt(v2 U128) bool

Lt returns whether `v1 < v2`.

func (U128) Lte

func (v1 U128) Lte(v2 U128) bool

Lte returns whether `v1 <= v2`.

func (U128) Not

func (v U128) Not() U128

Not returns `^v`.

func (U128) OnesCount

func (v U128) OnesCount() int

OnesCount returns the number of "1" bits in `v`. This is also sometimes referred to as the "population count" of `v`.

func (U128) Or

func (v1 U128) Or(v2 U128) U128

Or returns `v1 | v2`.

func (U128) Or64

func (v1 U128) Or64(v2 uint64) U128

Or64 returns `v1 | v2`.

func (U128) Reverse

func (v U128) Reverse() U128

Reverse returns a version of `v` with all bits reversed.

func (U128) ReverseBytes

func (v U128) ReverseBytes() U128

ReverseBytes returns a version of `v` with all bytes reversed.

func (U128) SetBitsFrom

func (v U128) SetBitsFrom(i uint8) U128

SetBitsFrom sets all bits from and including the `i`th bit, where `i` starts from 0 which refers to the most-significant bit.

func (U128) SetBitsUpto

func (v U128) SetBitsUpto(i uint8) U128

SetBitsUpto sets all bits from the most-significant bit up to the `ith` bit, where `i` starts from 0 which refers to the most-significant bit.

func (U128) Sub

func (v1 U128) Sub(v2 U128) (U128, uint64)

Sub returns `v1 - v2` and any borrow on underflow. The borrow is guaranteed to be 0 or 1.

func (U128) Sub64

func (v1 U128) Sub64(v2 uint64) (U128, uint64)

Sub64 returns `v1 - v2` and any borrow on underflow. The borrow is guaranteed to be 0 or 1.

func (U128) SubOne

func (v U128) SubOne() U128

SubOne returns `v1 - 1`. If underflow occurred, then IsMax will be true on the result.

func (U128) TrailingZeros

func (v U128) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits in v. Returns 128 for `v == 0`.

func (U128) Xor

func (v1 U128) Xor(v2 U128) U128

Xor returns `v1 ^ v2`.

func (U128) Xor64

func (v1 U128) Xor64(v2 uint64) U128

Xor64 returns `v1 ^ v2`.

Jump to

Keyboard shortcuts

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