cidr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: MIT Imports: 9 Imported by: 16

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", c.Mask())

	// ip range
	beginIP, endIP := c.IPRange()
	fmt.Println("ip range:", beginIP, 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.CIDR())
	}

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

	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.CIDR())
}

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

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

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 broadcast of the CIDR

func (CIDR) CIDR

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

CIDR returns the CIDR string. If the IP prefix of the input CIDR string is inaccurate, it returns the string which be corrected by the mask length.

For example, "192.0.2.10/24" return "192.0.2.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 iterate through each ip in the CIDR

func (CIDR) EachFrom added in v0.2.0

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

EachFrom begin with specified ip, iterate through each ip in the CIDR

func (CIDR) Equal

func (c CIDR) Equal(ns string) bool

Equal reports whether cidr and ns are the same CIDR

func (CIDR) IP

func (c CIDR) IP() net.IP

IP returns the original IP prefix of the input CIDR

func (CIDR) IPCount

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

IPCount returns ip total of the CIDR

func (CIDR) IPRange

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

IPRange returns begin 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

func (CIDR) Mask

func (c CIDR) Mask() net.IP

Mask returns mask of the CIDR

func (CIDR) MaskSize

func (c CIDR) MaskSize() (ones, bits int)

MaskSize returns the number of leading ones and total bits in the CIDR mask

func (CIDR) Network

func (c CIDR) Network() net.IP

Network returns network 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