-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
52 lines (45 loc) · 1.33 KB
/
functions.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
)
func checkErr(err error) {
if err != nil {
log.Fatalf("%s", err)
}
return
}
func decodeJSON(v interface{}, r io.Reader) error {
return json.NewDecoder(r).Decode(v)
}
func generateStatic(n string, a string) (string, error) {
r := new(Router)
//rt := route{Net: "192.168.0.0/16", Address: "10.87.89.1"}
//rt := route{Net: "2001:420:2cff::/48", Address: "2001:420:2cff:1204::1"}
rt := route{Net: n, Address: a}
ip, _, err := net.ParseCIDR(n)
if err != nil {
return "", err
}
// To4 converts the IPv4 address ip to a 4-byte representation. If ip is not an IPv4 address, To4 returns nil.
if ip.To4() != nil {
r.Static.AddressFamily.IPv4.Unicast.Routes = append(r.Static.AddressFamily.IPv4.Unicast.Routes, rt)
} else {
r.Static.AddressFamily.IPv6.Unicast.Routes = append(r.Static.AddressFamily.IPv6.Unicast.Routes, rt)
}
b, err := json.Marshal(r)
if err != nil {
return "", err
}
return string(b), err
}
func readStatic(r Router) {
// Just examples for now
fmt.Printf("data4: %v\n", r.Static.AddressFamily.IPv4.Unicast.Routes[0])
fmt.Printf("data4: %v\n", r.Static.AddressFamily.IPv4.Unicast.Routes[1].Net)
fmt.Printf("data4: %v\n", r.Static.AddressFamily.IPv4.Unicast.Routes[1].Address)
fmt.Printf("data6: %v\n", r.Static.AddressFamily.IPv6.Unicast.Routes[0])
}