-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (84 loc) · 1.93 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/hex"
"flag"
"fmt"
"github.com/go-yaml/yaml"
"github.com/neilcook/dnroptions/options"
"log"
"os"
)
type DNROptions struct {
options.DHCPOptions `yaml:",inline"`
options.RAOptions `yaml:",inline"`
}
func LoadFromBytes(yamlContents []byte) (*DNROptions, error) {
d := DNROptions{}
err := yaml.Unmarshal(yamlContents, &d)
if err != nil {
return nil, err
}
err = d.DHCPOptions.Validate()
if err != nil {
return nil, err
}
err = d.RAOptions.Validate()
if err != nil {
return nil, err
}
return &d, nil
}
func LoadOptions(fpath string) (*DNROptions, error) {
yamlFile, err := os.ReadFile(fpath)
if err != nil {
return nil, err
}
d, err := LoadFromBytes(yamlFile)
return d, err
}
func hexEncode(b []byte, space bool) []byte {
if b == nil {
return nil
}
sep := ':'
if space {
sep = ' '
}
hexOpts := make([]byte, 0, 3*len(b))
x := hexOpts[1*len(b) : 3*len(b)]
hex.Encode(x, b)
for i := 0; i < len(x); i += 2 {
hexOpts = append(hexOpts, x[i], x[i+1], byte(sep))
}
hexOpts = hexOpts[:len(hexOpts)-1]
return hexOpts
}
func main() {
var configFile = flag.String("config", "config.yaml", "YAML config file")
var spaceHex = flag.Bool("hexspaces", false, "Use spaces to separate hex octets in output")
flag.Parse()
options, err := LoadOptions(*configFile)
if err != nil {
log.Fatalf("FATAL: Could not load -config %s : %v", *configFile, err)
}
encodedOpts, err := options.DHCPOptions.Serialize()
if err != nil {
log.Fatalf("FATAL: Could not serialize options: %v", err)
}
hexOpts := hexEncode(encodedOpts, *spaceHex)
if hexOpts != nil {
if options.V6 {
fmt.Printf("DHCPV6=%s\n", hexOpts)
} else {
fmt.Printf("DHCPV4=%s\n", hexOpts)
}
}
encodedOpts, err = options.RAOptions.Serialize()
if err != nil {
log.Fatalf("FATAL: Could not serialize options: %v", err)
}
hexOpts = hexEncode(encodedOpts, *spaceHex)
if hexOpts != nil {
fmt.Printf("IPV6RA=%s\n", hexOpts)
}
}