cidr

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 8 Imported by: 24

README

CIDR

Features

  • easy to iterate through each ip in segment
  • check ipv4 or ipv6 segment
  • check whether segment contain ip
  • segments sort、split、merge
  • ip incr & decr
  • ip compare

Code Example

package main

import (
	"fmt"
	"github.com/3th1nk/cidr"
)

func main() {
	// parses a network segment as a CIDR
	c, _ := cidr.Parse("192.168.1.0/28")
	fmt.Println("network:", c.Network(), "broadcast:", c.Broadcast(), "mask", net.IP(c.Mask()))

	// ip range
	fmt.Println("ip range:", c.StartIP(), c.EndIP())

	// iterate through each ip
	fmt.Println("ip total:", c.IPCount())
	c.Each(func(ip string) bool {
		fmt.Println("\t", ip)
		return true
	})
	c.EachFrom("192.168.1.10", func(ip string) bool {
		fmt.Println("\t", ip)
		return true
	})

	fmt.Println("subnet plan based on the subnets num:")
	cs, _ := c.SubNetting(cidr.MethodSubnetNum, 4)
	for _, c := range cs {
		fmt.Println("\t", c.String())
	}

	fmt.Println("subnet plan based on the hosts num:")
	cs, _ = c.SubNetting(cidr.MethodHostNum, 4)
	for _, c := range cs {
		fmt.Println("\t", c.String())
	}

	fmt.Println("merge network:")
	c, _ = cidr.SuperNetting([]string{
		"2001:db8::/66",
		"2001:db8:0:0:8000::/66",
		"2001:db8:0:0:4000::/66",
		"2001:db8:0:0:c000::/66",
	})
	fmt.Println("\t", c.String())
}

Documentation

Index

Constants

View Source
const (
	// MethodSubnetNum SubNetting based on the number of subnets
	MethodSubnetNum = SubNettingMethod(0)
	// MethodHostNum SubNetting based on the number of hosts
	MethodHostNum = SubNettingMethod(1)
	// MethodSubnetMask SubNetting based on the mask prefix length of subnets
	MethodSubnetMask = SubNettingMethod(2)
)

Variables

This section is empty.

Functions

func IP4Distance added in v0.2.0

func IP4Distance(src, dst string) (int64, error)

IP4Distance return the number of ip between two v4 ip

func IP4IntToStr added in v0.2.0

func IP4IntToStr(n int64) string

IP4IntToStr number to ipv4 ip

func IP4StrToInt added in v0.2.0

func IP4StrToInt(s string) int64

IP4StrToInt ipv4 ip to number

func IPCompare added in v0.2.0

func IPCompare(a, b net.IP) int

IPCompare returns an integer comparing two ip

The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

func IPDecr added in v0.2.0

func IPDecr(ip net.IP)

IPDecr ip decrease

func IPDecr2 added in v0.3.0

func IPDecr2(ip net.IP) net.IP

IPDecr2 input ip no change

func IPEqual added in v0.2.0

func IPEqual(a, b net.IP) bool

IPEqual reports whether a and b are the same IP

func IPIncr added in v0.2.0

func IPIncr(ip net.IP)

IPIncr ip increase

func IPIncr2 added in v0.3.0

func IPIncr2(ip net.IP) net.IP

IPIncr2 input ip no change

func SortCIDRAsc added in v0.2.0

func SortCIDRAsc(cs []*CIDR)

SortCIDRAsc sort cidr slice order by ip,mask asc

func SortCIDRDesc added in v0.2.0

func SortCIDRDesc(cs []*CIDR)

SortCIDRDesc sort cidr slice order by ip,mask desc

Types

type CIDR

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

CIDR https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

func Parse added in v0.2.0

func Parse(s string) (*CIDR, error)

Parse parses s as a CIDR notation IP address and mask length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC4632 and RFC4291

func ParseNoError added in v0.2.0

func ParseNoError(s string) *CIDR

ParseNoError parses s as a CIDR notation IP address and mask length, but ignores any error. Use with caution.

func SuperNetting

func SuperNetting(ns []string) (*CIDR, error)

SuperNetting merge network segments, must be contiguous

func (CIDR) Broadcast

func (c CIDR) Broadcast() net.IP

Broadcast returns the broadcast address of the CIDR (only valid for IPv4)

func (CIDR) CIDR

func (c CIDR) CIDR() *net.IPNet

CIDR returns the normalized network address based on the mask, not the original input.

For example, if the original input was "192.168.1.10/24", this returns a *net.IPNet representing "192.168.1.0/24".

func (CIDR) Contains

func (c CIDR) Contains(ip string) bool

Contains reports whether the CIDR includes ip

func (CIDR) Each added in v0.2.0

func (c CIDR) Each(iterator func(ip string) bool)

Each iterates over all IPs in the CIDR

func (CIDR) EachFrom added in v0.2.0

func (c CIDR) EachFrom(beginIP string, iterator func(ip string) bool) error

EachFrom iterates over all IPs in the CIDR from a given IP

func (CIDR) EndIP added in v0.3.0

func (c CIDR) EndIP() net.IP

EndIP returns the end IP of the CIDR

func (CIDR) Equal

func (c CIDR) Equal(ns string) bool

Equal reports whether cidr and ns are the same CIDR (excluding IPv4-mapped)

func (CIDR) EqualFold added in v0.3.0

func (c CIDR) EqualFold(ns string) bool

EqualFold reports whether cidr and ns are the same CIDR (including IPv4-mapped)

func (CIDR) IP

func (c CIDR) IP() net.IP

IP returns the normalized IP prefix of the CIDR.

This method returns the IP address after processing IPv4-compatible and IPv4-mapped normalizations,

but unlike Network() method, it does not correct the IP prefix based on the mask.

For example, if the original input was "192.168.1.10/24", this returns "192.168.1.10",

while Network() would return "192.168.1.0" (the network address with host bits set to zero).

func (CIDR) IPCount

func (c CIDR) IPCount() *big.Int

IPCount returns the number of IPs in the CIDR

func (CIDR) IPRange

func (c CIDR) IPRange() (start, end net.IP)

IPRange returns the start and end IP of the CIDR

func (CIDR) IsIPv4

func (c CIDR) IsIPv4() bool

IsIPv4 reports whether the CIDR is IPv4

func (CIDR) IsIPv6

func (c CIDR) IsIPv6() bool

IsIPv6 reports whether the CIDR is IPv6 (including IPv4-compatible and IPv4-mapped)

func (CIDR) IsPureIPv6 added in v0.3.0

func (c CIDR) IsPureIPv6() bool

IsPureIPv6 reports whether the CIDR is IPv6 (excluding IPv4-compatible and IPv4-mapped)

func (CIDR) Mask

func (c CIDR) Mask() net.IPMask

Mask returns the network mask of the CIDR as a net.IPMask.

Note that calling mask.String() directly returns a hex string without separators (e.g., "ffffff00"),

which is not human-readable.

Use net.IP(mask).String() to get a human-readable representation:
- for IPv4, dotted decimal notation (e.g., "255.255.255.0")
- for IPv6, colon-separated hexadecimal notation (e.g., "ffff:ffff:ffff:ffff::")

func (CIDR) Network

func (c CIDR) Network() net.IP

Network returns the network address of the CIDR

func (CIDR) StartIP added in v0.3.0

func (c CIDR) StartIP() net.IP

StartIP returns the start IP of the CIDR

func (CIDR) String added in v0.3.0

func (c CIDR) String() string

String returns the normalized string representation of the CIDR

func (CIDR) SubNetting

func (c CIDR) SubNetting(method SubNettingMethod, num int) ([]*CIDR, error)

SubNetting split network segment based on the number of hosts or subnets

type SubNettingMethod added in v0.2.0

type SubNettingMethod int

Jump to

Keyboard shortcuts

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