-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip6.go
58 lines (47 loc) · 1.06 KB
/
ip6.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package iana
import (
"encoding/xml"
"os"
"path/filepath"
"github.com/rspamd/goasn/sources"
"github.com/asergeyev/nradix"
)
type IANAIP6Record struct {
Prefix string `xml:"prefix"`
Description string `xml:"description"`
}
type IANAIP6Registry struct {
Records []IANAIP6Record `xml:"record"`
}
type IANAIP6Info struct {
XMLName xml.Name `xml:"registry"`
Registry IANAIP6Registry `xml:"registry"`
}
func GetReservedIP6(appCacheDir string) (*nradix.Tree, error) {
tree := nradix.NewTree(0)
ianaIP6File := sources.MustBasename(sources.IANA_IP6)
xmlPath := filepath.Join(appCacheDir, ianaIP6File)
f, err := os.Open(xmlPath)
if err != nil {
return tree, err
}
defer f.Close()
dec := xml.NewDecoder(f)
res := new(IANAIP6Info)
err = dec.Decode(res)
if err != nil {
return tree, err
}
for _, rec := range res.Registry.Records {
switch rec.Description {
case "Reserved by IETF":
fallthrough
case "Link-Scoped Unicast":
fallthrough
case "Unique Local Unicast":
tree.AddCIDR(rec.Prefix, 0)
default:
}
}
return tree, nil
}