-
Notifications
You must be signed in to change notification settings - Fork 8
/
dhcp.go
39 lines (32 loc) · 878 Bytes
/
dhcp.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
package sabakan
import (
"errors"
"net"
"time"
)
// DefaultLeaseDuration is 60 minutes.
const DefaultLeaseDuration = 60 * time.Minute
// DHCPConfig is a set of DHCP configurations.
type DHCPConfig struct {
LeaseMinutes uint `json:"lease-minutes"`
DNSServers []string `json:"dns-servers,omitempty"`
// obsoleted fields
GatewayOffset uint `json:"gateway-offset"`
}
// LeaseDuration returns lease duration for IP addreses.
func (c *DHCPConfig) LeaseDuration() time.Duration {
if c.LeaseMinutes == 0 {
return DefaultLeaseDuration
}
return time.Duration(c.LeaseMinutes) * time.Minute
}
// Validate validates configurations
func (c *DHCPConfig) Validate() error {
for _, server := range c.DNSServers {
ip := net.ParseIP(server)
if ip == nil || ip.To4() == nil {
return errors.New("invalid IPv4 address in dns-servers: " + server)
}
}
return nil
}