Skip to content

Commit

Permalink
dhcpd: remove net.IP <-> string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 12, 2021
1 parent cfd492c commit 0b955d9
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 107 deletions.
8 changes: 4 additions & 4 deletions internal/dhcpd/dhcpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func TestDB(t *testing.T) {

conf := V4ServerConf{
Enabled: true,
RangeStart: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
RangeStart: net.ParseIP("192.168.10.100"),
RangeEnd: net.ParseIP("192.168.10.200"),
GatewayIP: net.ParseIP("192.168.10.1"),
SubnetMask: net.ParseIP("255.255.255.0"),
notify: testNotify,
}
s.srv4, err = v4Create(conf)
Expand Down
116 changes: 56 additions & 60 deletions internal/dhcpd/dhcphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string
}

type v4ServerConfJSON struct {
GatewayIP string `json:"gateway_ip"`
SubnetMask string `json:"subnet_mask"`
RangeStart string `json:"range_start"`
RangeEnd string `json:"range_end"`
GatewayIP net.IP `json:"gateway_ip"`
SubnetMask net.IP `json:"subnet_mask"`
RangeStart net.IP `json:"range_start"`
RangeEnd net.IP `json:"range_end"`
LeaseDuration uint32 `json:"lease_duration"`
}

Expand All @@ -61,10 +61,10 @@ func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON {

func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf {
return V4ServerConf{
GatewayIP: j.GatewayIP,
SubnetMask: j.SubnetMask,
RangeStart: j.RangeStart,
RangeEnd: j.RangeEnd,
GatewayIP: j.GatewayIP.To4(),
SubnetMask: j.SubnetMask.To4(),
RangeStart: j.RangeStart.To4(),
RangeEnd: j.RangeEnd.To4(),
LeaseDuration: j.LeaseDuration,
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *Server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {

type staticLeaseJSON struct {
HWAddr string `json:"mac"`
IP string `json:"ip"`
IP net.IP `json:"ip"`
Hostname string `json:"hostname"`
}

Expand Down Expand Up @@ -225,10 +225,10 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {

type netInterfaceJSON struct {
Name string `json:"name"`
GatewayIP string `json:"gateway_ip"`
GatewayIP net.IP `json:"gateway_ip"`
HardwareAddr string `json:"hardware_address"`
Addrs4 []string `json:"ipv4_addresses"`
Addrs6 []string `json:"ipv6_addresses"`
Addrs4 []net.IP `json:"ipv4_addresses"`
Addrs6 []net.IP `json:"ipv6_addresses"`
Flags string `json:"flags"`
}

Expand Down Expand Up @@ -277,9 +277,9 @@ func (s *Server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
continue
}
if ipnet.IP.To4() != nil {
jsonIface.Addrs4 = append(jsonIface.Addrs4, ipnet.IP.String())
jsonIface.Addrs4 = append(jsonIface.Addrs4, ipnet.IP)
} else {
jsonIface.Addrs6 = append(jsonIface.Addrs6, ipnet.IP.String())
jsonIface.Addrs6 = append(jsonIface.Addrs6, ipnet.IP)
}
}
if len(jsonIface.Addrs4)+len(jsonIface.Addrs6) != 0 {
Expand Down Expand Up @@ -375,50 +375,46 @@ func (s *Server) handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request
err := json.NewDecoder(r.Body).Decode(&lj)
if err != nil {
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)

return
}

ip := net.ParseIP(lj.IP)
if ip != nil && ip.To4() == nil {
mac, err := net.ParseMAC(lj.HWAddr)
if lj.IP == nil {
httpError(r, w, http.StatusBadRequest, "invalid IP")

return
}

ip4 := lj.IP.To4()

mac, err := net.ParseMAC(lj.HWAddr)
lease := Lease{
HWAddr: mac,
}

if ip4 == nil {
lease.IP = lj.IP.To16()

if err != nil {
httpError(r, w, http.StatusBadRequest, "invalid MAC")
return
}

lease := Lease{
IP: ip,
HWAddr: mac,
return
}

err = s.srv6.AddStaticLease(lease)
if err != nil {
httpError(r, w, http.StatusBadRequest, "%s", err)
return
}
return
}

ip, _ = parseIPv4(lj.IP)
if ip == nil {
httpError(r, w, http.StatusBadRequest, "invalid IP")
return
}

mac, err := net.ParseMAC(lj.HWAddr)
if err != nil {
httpError(r, w, http.StatusBadRequest, "invalid MAC")
return
}

lease := Lease{
IP: ip,
HWAddr: mac,
Hostname: lj.Hostname,
}
lease.IP = ip4
lease.Hostname = lj.Hostname
err = s.srv4.AddStaticLease(lease)
if err != nil {
httpError(r, w, http.StatusBadRequest, "%s", err)

return
}
}
Expand All @@ -428,46 +424,46 @@ func (s *Server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Requ
err := json.NewDecoder(r.Body).Decode(&lj)
if err != nil {
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)

return
}

ip := net.ParseIP(lj.IP)
if ip != nil && ip.To4() == nil {
mac, err := net.ParseMAC(lj.HWAddr)
if lj.IP == nil {
httpError(r, w, http.StatusBadRequest, "invalid IP")

return
}

ip4 := lj.IP.To4()

mac, err := net.ParseMAC(lj.HWAddr)
lease := Lease{
HWAddr: mac,
}

if ip4 == nil {
lease.IP = lj.IP.To16()

if err != nil {
httpError(r, w, http.StatusBadRequest, "invalid MAC")
return
}

lease := Lease{
IP: ip,
HWAddr: mac,
return
}

err = s.srv6.RemoveStaticLease(lease)
if err != nil {
httpError(r, w, http.StatusBadRequest, "%s", err)
return
}
return
}

ip, _ = parseIPv4(lj.IP)
if ip == nil {
httpError(r, w, http.StatusBadRequest, "invalid IP")
return
}

mac, _ := net.ParseMAC(lj.HWAddr)

lease := Lease{
IP: ip,
HWAddr: mac,
Hostname: lj.Hostname,
}
lease.IP = ip4
lease.Hostname = lj.Hostname
err = s.srv4.RemoveStaticLease(lease)
if err != nil {
httpError(r, w, http.StatusBadRequest, "%s", err)

return
}
}
Expand Down
16 changes: 9 additions & 7 deletions internal/dhcpd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ func isTimeout(err error) bool {
return operr.Timeout()
}

func parseIPv4(text string) (net.IP, error) {
result := net.ParseIP(text)
if result == nil {
return nil, fmt.Errorf("%s is not an IP address", text)
func tryTo4(ip net.IP) (ip4 net.IP, err error) {
if ip == nil {
return nil, fmt.Errorf("%v is not an IP address", ip)
}
if result.To4() == nil {
return nil, fmt.Errorf("%s is not an IPv4 address", text)

ip4 = ip.To4()
if ip4 == nil {
return nil, fmt.Errorf("%v is not an IPv4 address", ip)
}
return result.To4(), nil

return ip4, nil
}

// Return TRUE if subnet mask is correct (e.g. 255.255.255.0)
Expand Down
8 changes: 4 additions & 4 deletions internal/dhcpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type V4ServerConf struct {
Enabled bool `yaml:"-"`
InterfaceName string `yaml:"-"`

GatewayIP string `yaml:"gateway_ip"`
SubnetMask string `yaml:"subnet_mask"`
GatewayIP net.IP `yaml:"gateway_ip"`
SubnetMask net.IP `yaml:"subnet_mask"`

// The first & the last IP address for dynamic leases
// Bytes [0..2] of the last allowed IP address must match the first IP
RangeStart string `yaml:"range_start"`
RangeEnd string `yaml:"range_end"`
RangeStart net.IP `yaml:"range_start"`
RangeEnd net.IP `yaml:"range_end"`

LeaseDuration uint32 `yaml:"lease_duration"` // in seconds

Expand Down
13 changes: 6 additions & 7 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,27 +632,26 @@ func v4Create(conf V4ServerConf) (DHCPServer, error) {
}

var err error
s.conf.routerIP, err = parseIPv4(s.conf.GatewayIP)
s.conf.routerIP, err = tryTo4(s.conf.GatewayIP)
if err != nil {
return s, fmt.Errorf("dhcpv4: %w", err)
}

subnet, err := parseIPv4(s.conf.SubnetMask)
if err != nil || !isValidSubnetMask(subnet) {
return s, fmt.Errorf("dhcpv4: invalid subnet mask: %s", s.conf.SubnetMask)
if s.conf.SubnetMask == nil {
return s, fmt.Errorf("dhcpv4: invalid subnet mask: %v", s.conf.SubnetMask)
}
s.conf.subnetMask = make([]byte, 4)
copy(s.conf.subnetMask, subnet)
copy(s.conf.subnetMask, s.conf.SubnetMask.To4())

s.conf.ipStart, err = parseIPv4(conf.RangeStart)
s.conf.ipStart, err = tryTo4(conf.RangeStart)
if s.conf.ipStart == nil {
return s, fmt.Errorf("dhcpv4: %w", err)
}
if s.conf.ipStart[0] == 0 {
return s, fmt.Errorf("dhcpv4: invalid range start IP")
}

s.conf.ipEnd, err = parseIPv4(conf.RangeEnd)
s.conf.ipEnd, err = tryTo4(conf.RangeEnd)
if s.conf.ipEnd == nil {
return s, fmt.Errorf("dhcpv4: %w", err)
}
Expand Down
32 changes: 16 additions & 16 deletions internal/dhcpd/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func notify4(flags uint32) {
func TestV4StaticLeaseAddRemove(t *testing.T) {
conf := V4ServerConf{
Enabled: true,
RangeStart: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
RangeStart: net.ParseIP("192.168.10.100"),
RangeEnd: net.ParseIP("192.168.10.200"),
GatewayIP: net.ParseIP("192.168.10.1"),
SubnetMask: net.ParseIP("255.255.255.0"),
notify: notify4,
}
s, err := v4Create(conf)
Expand Down Expand Up @@ -62,10 +62,10 @@ func TestV4StaticLeaseAddRemove(t *testing.T) {
func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) {
conf := V4ServerConf{
Enabled: true,
RangeStart: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
RangeStart: net.ParseIP("192.168.10.100"),
RangeEnd: net.ParseIP("192.168.10.200"),
GatewayIP: net.ParseIP("192.168.10.1"),
SubnetMask: net.ParseIP("255.255.255.0"),
notify: notify4,
}
sIface, err := v4Create(conf)
Expand Down Expand Up @@ -114,10 +114,10 @@ func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) {
func TestV4StaticLeaseGet(t *testing.T) {
conf := V4ServerConf{
Enabled: true,
RangeStart: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
RangeStart: net.ParseIP("192.168.10.100"),
RangeEnd: net.ParseIP("192.168.10.200"),
GatewayIP: net.ParseIP("192.168.10.1"),
SubnetMask: net.ParseIP("255.255.255.0"),
notify: notify4,
}
sIface, err := v4Create(conf)
Expand Down Expand Up @@ -173,10 +173,10 @@ func TestV4StaticLeaseGet(t *testing.T) {
func TestV4DynamicLeaseGet(t *testing.T) {
conf := V4ServerConf{
Enabled: true,
RangeStart: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
RangeStart: net.ParseIP("192.168.10.100"),
RangeEnd: net.ParseIP("192.168.10.200"),
GatewayIP: net.ParseIP("192.168.10.1"),
SubnetMask: net.ParseIP("255.255.255.0"),
notify: notify4,
Options: []string{
"81 hex 303132",
Expand Down
13 changes: 4 additions & 9 deletions internal/sysutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,21 @@ func IfaceSetStaticIP(ifaceName string) (err error) {
}

// GatewayIP returns IP address of interface's gateway.
func GatewayIP(ifaceName string) string {
func GatewayIP(ifaceName string) net.IP {
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
d, err := cmd.Output()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return ""
return nil
}

fields := strings.Fields(string(d))
// The meaningful "ip route" command output should contain the word
// "default" at first field and default gateway IP address at third
// field.
if len(fields) < 3 || fields[0] != "default" {
return ""
return nil
}

ip := net.ParseIP(fields[2])
if ip == nil {
return ""
}

return fields[2]
return net.ParseIP(fields[2])
}

0 comments on commit 0b955d9

Please sign in to comment.