netradix

package module
v0.0.0-...-90a0cc3 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2022 License: MIT Imports: 3 Imported by: 2

README

About

Go programming language (http://golang.org/) bindings for radix tree library for fast subnet (both IPv4 and IPv6) lookups.

Installing

$ go get github.com/thekvs/go-net-radix

After this command go-net-radix library is ready to use and will be located in:

$GOROOT/src/pkg/github.com/thekvs/go-net-radix

Basic usage example


package main

import (
        "github.com/thekvs/go-net-radix"

        "fmt"
        "log"
)

func main() {
        rtree, err := netradix.NewNetRadixTree()
        if err != nil {
                panic(err)
        }
        defer rtree.Close()

        if err = rtree.Add("217.72.192.0/20", "UDATA1"); err != nil {
                log.Fatal(err)
        }

        if err = rtree.Add("2001:220::/35", "UDATA2"); err != nil {
                log.Fatal(err)
        }

        var found bool
        var udata string

        found, udata, err = rtree.SearchBest("217.72.192.2")
        if err != nil {
                log.Fatal(err)
        }
        if found {
                fmt.Printf("found: %v\n", udata)
        }

        found, udata, err = rtree.SearchBest("2001:220::")
        if err != nil {
                log.Fatal(err)
        }
        if found {
                fmt.Printf("found: %v\n", udata)
        }

        if err = rtree.Remove("217.72.192.0/20"); err != nil {
                log.Fatal(err)
        }
}

Documentation

GoDoc

Licensing

All source code included in this distribution is covered by the MIT License found in the LICENSE file, unless specifically stated otherwise within each file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NetRadixTree

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

func NewNetRadixTree

func NewNetRadixTree() (*NetRadixTree, error)

NewNetRadixTree creates new NetRadixTree structure. Return tuple in which the first element specifies tree structure and the second element specifies error object or nil if no error has occured.

func (*NetRadixTree) Add

func (rtree *NetRadixTree) Add(addr, udata string) error

Add adds network or subnet specification and user defined payload string to the radix tree. If no mask width is specified, the longest possible mask is assumed, i.e. 32 bits for IPv4 network and 128 bits for IPv6 network. On success, returns nil, otherwise returns error object.

func (*NetRadixTree) Close

func (rtree *NetRadixTree) Close()

Close destroys radix tree.

func (*NetRadixTree) Remove

func (rtree *NetRadixTree) Remove(addr string) error

Remove deletes a node which exactly matches the address given. If no errors occured returns nil or error object otherwise.

func (*NetRadixTree) SearchBest

func (rtree *NetRadixTree) SearchBest(addr string) (found bool, udata string, err error)

SearchBest searches radix tree to find a matching node using usual subnetting rules for the address specified. If no mask width is specified, the longest possible mask for this type of address (IPv4 or IPv6) is assumed. Returns triple in which the first element indicates success of a search, the second element returns user payload (or empty string if not found) and the third element returns error object in case such an error occured or nil otherwise.

func (*NetRadixTree) SearchExact

func (rtree *NetRadixTree) SearchExact(addr string) (found bool, udata string, err error)

SearchExact searches radix tree to find a matching node. Its semantics are the same as in SearchBest() method except that the addr must match a node exactly.

Jump to

Keyboard shortcuts

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