netlist

package module
v0.0.0-...-ab9e4bc Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: GPL-3.0 Imports: 10 Imported by: 0

README

net-list

高性能IP路由表匹配库,内存对齐,二分搜索,仅占用一个堆对象,匹配过程零堆占用。


从string reader批量加载CIDR格式的IP

NewListFromReader(reader io.Reader) (*List, error)

从文件批量加载CIDR格式的IP

NewListFromFile(file string) (*List, error) 

逐一添加

l := NewList()

// 从 net.IP 添加

ip := net.IP{222, 222, 222, 222}
ipv6, err := Conv(ip)
if err != nil {
    ...
}
n := NewNet(ipv6, 24+96) // 相当于 222.222.222.222/24
l.Append(n)

// 从 string 添加

n, err = ParseCIDR("1.2.3.4/24")
if err != nil {
    ...
}
l.Append(n)

...

l.Sort()  // 添加完后需对列表排序才能匹配Contains,否则Contains()会panic

匹配 Contains

l := NewList()
...

ip := net.IP{111, 111, 111, 111}
ipv6, err := Conv(ip)
if err != nil {
    ...
}

l.Sort() // 调用Contains()前l必需是sorted状态
l.Contains(ipv6) // true or false

Documentation

Index

Constants

View Source
const (

	//IPSize = 2 or 4
	IPSize = 128 / intSize
)

Variables

View Source
var (
	//ErrNotIPv6 raised by Conv()
	ErrNotIPv6 = errors.New("ip is not a valid ipv6 address")
)
View Source
var (
	//ErrParseCIDR raised by ParseCIDR()
	ErrParseCIDR = errors.New("error CIDR format")
)

Functions

This section is empty.

Types

type IPv6

type IPv6 [IPSize]uint

IPv6 represents a ipv6 addr

func Conv

func Conv(ip net.IP) (ipv6 IPv6, err error)

Conv converts ip to type IPv6. ip must be a valid ipv6 address, or ErrNotIPv6 will return.

type List

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

List is a list of Nets. All Nets will be in ipv6 format, even it's ipv4 addr. Cause we use bin search.

func NewListFromFile

func NewListFromFile(file string) (*List, error)

NewListFromFile read IP list from a file, if no valid IP addr was found, it will return a empty NetList, NOT nil. NetList will be a sorted list.

func NewListFromReader

func NewListFromReader(reader io.Reader) (*List, error)

NewListFromReader read IP list from a reader, if no valid IP addr was found, it will return a empty NetList, NOT nil. NetList will be a sorted list.

func NewNetList

func NewNetList() *List

NewNetList returns a NetList, list can not be nil.

func (*List) Append

func (list *List) Append(newNet ...Net)

Append appends new Nets to the list. This modified list, call Sort() before call next Contains()

func (*List) Contains

func (list *List) Contains(ipv6 IPv6) bool

Contains reports whether the list includes given ipv6. list must be sorted, or Contains will panic.

func (*List) Len

func (list *List) Len() int

implement sort Interface

func (*List) Less

func (list *List) Less(i, j int) bool

func (*List) Merge

func (list *List) Merge(srcList *List)

Merge merges srcList with list This modified list, call Sort() before call next Contains()

func (*List) Sort

func (list *List) Sort()

Sort sorts the list, this must be called everytime after list was modified.

func (*List) Swap

func (list *List) Swap(i, j int)

type Net

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

Net represents a ip network

func NewNet

func NewNet(ipv6 IPv6, mask uint) (n Net)

NewNet returns a new IPNet, mask should be an ipv6 mask, which means you should +96 if you have an ipv4 mask.

func ParseCIDR

func ParseCIDR(s string) (Net, error)

ParseCIDR parses s as a CIDR notation IP address and prefix length. As defined in RFC 4632 and RFC 4291.

func (Net) Contains

func (net Net) Contains(ip IPv6) bool

Contains reports whether the ipnet includes ip.

Jump to

Keyboard shortcuts

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