This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
with examples and inettree as example interface implementation
- Loading branch information
Showing
7 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/gaissmai/go-inet/examples | ||
|
||
go 1.16 | ||
|
||
require github.com/gaissmai/go-inet/v2 v2.0.0 |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Command ipam-tree, read blocks from Stdin, print sorted Tree | ||
package main | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/gaissmai/go-inet/v2/inettree" | ||
"github.com/gaissmai/go-inet/v2/tree" | ||
) | ||
|
||
func main() { | ||
r := csv.NewReader(os.Stdin) | ||
r.FieldsPerRecord = 2 | ||
|
||
records, err := r.ReadAll() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
bs := make([]tree.Interface, 0, len(records)) | ||
|
||
for i := range records { | ||
b := records[i][0] | ||
c := records[i][1] | ||
|
||
item, err := inettree.NewItem(b, c) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
text := item.Block.String() | ||
item.Text = text + " " + strings.Repeat(".", 40-len(text)) + " " + c | ||
bs = append(bs, item) | ||
} | ||
|
||
t, err := tree.NewTree(bs) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package inettree implements the tree.Interface for inet.Block | ||
package inettree | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/gaissmai/go-inet/v2/inet" | ||
"github.com/gaissmai/go-inet/v2/tree" | ||
) | ||
|
||
// Item augments inet.Block, implementing the tree.Interface | ||
type Item struct { | ||
// the augmented Block | ||
inet.Block | ||
|
||
// augment Block with additional text, see example | ||
Text string | ||
} | ||
|
||
// NewItem augments inet.ParseBlock or inet.FromStdIPNet | ||
func NewItem(i interface{}, str string) (Item, error) { | ||
switch v := i.(type) { | ||
case string: | ||
bb, err := inet.ParseBlock(v) | ||
return Item{bb, str}, err | ||
case net.IPNet: | ||
bb, err := inet.FromStdIPNet(v) | ||
return Item{bb, str}, err | ||
default: | ||
return Item{}, fmt.Errorf("invalid type: %T", i) | ||
} | ||
} | ||
|
||
// Less implements the tree.Interface for Item | ||
func (a Item) Less(i tree.Interface) bool { | ||
b := i.(Item) | ||
return a.Block.Less(b.Block) | ||
} | ||
|
||
// Equal implements the tree.Interface for Item | ||
func (a Item) Equal(i tree.Interface) bool { | ||
b := i.(Item) | ||
return a.Block == b.Block | ||
} | ||
|
||
// Covers implements the tree.Interface for Item | ||
func (a Item) Covers(i tree.Interface) bool { | ||
b := i.(Item) | ||
return a.Block.Covers(b.Block) | ||
} | ||
|
||
// String implements the tree.Interface for Item | ||
func (a Item) String() string { | ||
if a.Text == "" { | ||
// print just the Block as string | ||
return a.Block.String() | ||
} | ||
// augment the Block with additonal text | ||
return a.Text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package inettree | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
) | ||
|
||
// augment inet.ParseBlock or inet.FromStdIPNet | ||
func TestNewItem(t *testing.T) { | ||
s := "::1" | ||
_, err := NewItem(s, "") | ||
if err != nil { | ||
t.Errorf("NewItem(string), got error: %v", err) | ||
} | ||
|
||
_, cidr, _ := net.ParseCIDR("::/0") | ||
_, err = NewItem(*cidr, "") | ||
if err != nil { | ||
t.Errorf("NewItem(net.IPNet), got error: %v", err) | ||
} | ||
|
||
_, err = NewItem(5, "") | ||
if err == nil { | ||
t.Errorf("NewItem(int), expected error, got: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package inettree_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gaissmai/go-inet/v2/inettree" | ||
"github.com/gaissmai/go-inet/v2/tree" | ||
) | ||
|
||
var iana = map[string]string{ | ||
"::/8": "Reserved by IETF [RFC3513][RFC4291]", | ||
"100::/8": "Reserved by IETF [RFC3513][RFC4291]", | ||
"200::/7": "Reserved by IETF [RFC4048]", | ||
"400::/6": "Reserved by IETF [RFC3513][RFC4291]", | ||
"800::/5": "Reserved by IETF [RFC3513][RFC4291]", | ||
"1000::/4": "Reserved by IETF [RFC3513][RFC4291]", | ||
"2000::/3": "Global Unicast [RFC3513][RFC4291]", | ||
"2000::/4": "Test", | ||
"3000::/4": "FREE", | ||
"4000::/3": "Reserved by IETF [RFC3513][RFC4291]", | ||
"6000::/3": "Reserved by IETF [RFC3513][RFC4291]", | ||
} | ||
|
||
func Example_usage() { | ||
bs := make([]tree.Interface, 0, len(iana)) | ||
|
||
for k, v := range iana { | ||
item, err := inettree.NewItem(k, k+" ... "+v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
bs = append(bs, item) | ||
} | ||
|
||
t, err := tree.NewTree(bs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(t) | ||
|
||
// Output: | ||
// ▼ | ||
// ├─ ::/8 ... Reserved by IETF [RFC3513][RFC4291] | ||
// ├─ 100::/8 ... Reserved by IETF [RFC3513][RFC4291] | ||
// ├─ 200::/7 ... Reserved by IETF [RFC4048] | ||
// ├─ 400::/6 ... Reserved by IETF [RFC3513][RFC4291] | ||
// ├─ 800::/5 ... Reserved by IETF [RFC3513][RFC4291] | ||
// ├─ 1000::/4 ... Reserved by IETF [RFC3513][RFC4291] | ||
// ├─ 2000::/3 ... Global Unicast [RFC3513][RFC4291] | ||
// │ ├─ 2000::/4 ... Test | ||
// │ └─ 3000::/4 ... FREE | ||
// ├─ 4000::/3 ... Reserved by IETF [RFC3513][RFC4291] | ||
// └─ 6000::/3 ... Reserved by IETF [RFC3513][RFC4291] | ||
|
||
} |