-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconf.go
103 lines (81 loc) · 2.11 KB
/
conf.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
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"errors"
"net"
"os"
"strings"
"time"
"gopkg.in/yaml.v2"
)
// Pool conf object
type PoolConf struct {
Name string `yaml:"name"`
MyIp string `yaml:"myip"`
Network string `yaml:"network"`
Subnet string `yaml:"subnet"`
Netmask string `yaml:"mask"`
Start string `yaml:"start"`
End string `yaml:"end"`
Router []string `yaml:"routers"`
Dns []string `yaml:"dns"`
LeaseTime uint32 `yaml:"leasetime"`
// TODO: add arbitrary options aside from just router/dns
ReservedHosts []HostConf `yaml:"hosts"`
}
func (pc PoolConf) ToPool() (*Pool, error) {
pool := NewPool()
pool.Name = pc.Name
if strings.Contains(pool.Name, "/") {
return nil, errors.New("Pool names cannot contain slashes as they are used in file names")
}
// FIXME: input validation for all of these
pool.Network = net.ParseIP(pc.Network)
pool.Netmask = net.ParseIP(pc.Netmask)
pool.Start = net.ParseIP(pc.Start)
pool.End = net.ParseIP(pc.End)
pool.MyIp = IpToFixedV4(net.ParseIP(pc.MyIp))
pool.LeaseTime = time.Second * time.Duration(pc.LeaseTime)
pool.Broadcast = calcBroadcast(pool.Network, pool.Netmask)
for _, ip := range pc.Router {
pool.Router = append(pool.Router, net.ParseIP(ip))
}
for _, ip := range pc.Dns {
pool.Dns = append(pool.Dns, net.ParseIP(ip))
}
for _, host := range pc.ReservedHosts {
if err := pool.AddReservedHost(host.ToHost()); err != nil {
return nil, err
}
}
return pool, nil
}
type HostConf struct {
IP string `yaml:"ip"`
Mac string `yaml:"hw"`
Hostname string `yaml:"hostname"`
// TODO: add custom options scoped to host
}
func (hc *HostConf) ToHost() *ReservedHost {
return &ReservedHost{
Mac: StrToMac(hc.Mac),
IP: IpToFixedV4(net.ParseIP(hc.IP)),
}
}
// Root yaml conf
type Conf struct {
Pools []PoolConf `yaml:"pools"`
Leasedir string `yaml:"leasedir"`
Interfaces []string `yaml:"interfaces`
}
func ParseConf(path string) (*Conf, error) {
conf := &Conf{}
var err error
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(content, &conf); err != nil {
return nil, err
}
return conf, nil
}