Skip to content

Commit

Permalink
Pull request: 2508 ip conversion vol.1
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 2508-ip-conversion to master

Updates AdguardTeam#2508.

Squashed commit of the following:

commit 3f64709
Merge: 5ac7faa 0d67aa2
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Jan 13 16:21:34 2021 +0300

    Merge branch 'master' into 2508-ip-conversion

commit 5ac7faa
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Jan 13 12:00:11 2021 +0300

    all: replace conditions with appropriate functions in tests

commit 9e3fa9a
Merge: db992a4 bba7485
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Jan 13 10:47:10 2021 +0300

    Merge branch 'master' into 2508-ip-conversion

commit db992a4
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 12 18:55:53 2021 +0300

    sysutil: fix linux tests

commit f629b15
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 12 18:41:20 2021 +0300

    all: improve code quality

commit 3bf03a7
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 12 17:33:26 2021 +0300

    sysutil: fix linux net.IP conversion

commit 5d5b699
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 12 14:57:26 2021 +0300

    dnsforward: remove redundant net.IP <-> string conversion

commit 0b955d9
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Jan 11 18:04:25 2021 +0300

    dhcpd: remove net.IP <-> string conversion
  • Loading branch information
EugeneOne1 authored and heyxkhoa committed Mar 17, 2023
1 parent 9d87ee9 commit df6004d
Show file tree
Hide file tree
Showing 39 changed files with 402 additions and 428 deletions.
38 changes: 19 additions & 19 deletions internal/dhcpd/dhcpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ 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.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200},
GatewayIP: net.IP{192, 168, 10, 1},
SubnetMask: net.IP{255, 255, 255, 0},
notify: testNotify,
}
s.srv4, err = v4Create(conf)
assert.True(t, err == nil)
assert.Nil(t, err)

s.srv6, err = v6Create(V6ServerConf{})
assert.True(t, err == nil)
assert.Nil(t, err)

l := Lease{}
l.IP = net.ParseIP("192.168.10.100").To4()
l.IP = net.IP{192, 168, 10, 100}
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
exp1 := time.Now().Add(time.Hour)
l.Expiry = exp1
s.srv4.(*v4Server).addLease(&l)

l2 := Lease{}
l2.IP = net.ParseIP("192.168.10.101").To4()
l2.IP = net.IP{192, 168, 10, 101}
l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb")
s.srv4.AddStaticLease(l2)

Expand All @@ -62,7 +62,7 @@ func TestDB(t *testing.T) {

assert.Equal(t, "aa:aa:aa:aa:aa:bb", ll[0].HWAddr.String())
assert.Equal(t, "192.168.10.101", ll[0].IP.String())
assert.Equal(t, int64(leaseExpireStatic), ll[0].Expiry.Unix())
assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix())

assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String())
assert.Equal(t, "192.168.10.100", ll[1].IP.String())
Expand All @@ -75,8 +75,8 @@ func TestIsValidSubnetMask(t *testing.T) {
assert.True(t, isValidSubnetMask([]byte{255, 255, 255, 0}))
assert.True(t, isValidSubnetMask([]byte{255, 255, 254, 0}))
assert.True(t, isValidSubnetMask([]byte{255, 255, 252, 0}))
assert.True(t, !isValidSubnetMask([]byte{255, 255, 253, 0}))
assert.True(t, !isValidSubnetMask([]byte{255, 255, 255, 1}))
assert.False(t, isValidSubnetMask([]byte{255, 255, 253, 0}))
assert.False(t, isValidSubnetMask([]byte{255, 255, 255, 1}))
}

func TestNormalizeLeases(t *testing.T) {
Expand All @@ -100,7 +100,7 @@ func TestNormalizeLeases(t *testing.T) {

leases := normalizeLeases(staticLeases, dynLeases)

assert.True(t, len(leases) == 3)
assert.Len(t, leases, 3)
assert.True(t, bytes.Equal(leases[0].HWAddr, []byte{1, 2, 3, 4}))
assert.True(t, bytes.Equal(leases[0].IP, []byte{0, 2, 3, 4}))
assert.True(t, bytes.Equal(leases[1].HWAddr, []byte{2, 2, 3, 4}))
Expand All @@ -109,22 +109,22 @@ func TestNormalizeLeases(t *testing.T) {

func TestOptions(t *testing.T) {
code, val := parseOptionString(" 12 hex abcdef ")
assert.Equal(t, uint8(12), code)
assert.EqualValues(t, 12, code)
assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val))

code, _ = parseOptionString(" 12 hex abcdef1 ")
assert.Equal(t, uint8(0), code)
assert.EqualValues(t, 0, code)

code, val = parseOptionString("123 ip 1.2.3.4")
assert.Equal(t, uint8(123), code)
assert.EqualValues(t, 123, code)
assert.Equal(t, "1.2.3.4", net.IP(string(val)).String())

code, _ = parseOptionString("256 ip 1.1.1.1")
assert.Equal(t, uint8(0), code)
assert.EqualValues(t, 0, code)
code, _ = parseOptionString("-1 ip 1.1.1.1")
assert.Equal(t, uint8(0), code)
assert.EqualValues(t, 0, code)
code, _ = parseOptionString("12 ip 1.1.1.1x")
assert.Equal(t, uint8(0), code)
assert.EqualValues(t, 0, code)
code, _ = parseOptionString("12 x 1.1.1.1")
assert.Equal(t, uint8(0), code)
assert.EqualValues(t, 0, code)
}
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
15 changes: 7 additions & 8 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (s *v4Server) Start() error {
s.conf.dnsIPAddrs = dnsIPAddrs

laddr := &net.UDPAddr{
IP: net.ParseIP("0.0.0.0"),
IP: net.IP{0, 0, 0, 0},
Port: dhcpv4.ServerPort,
}
s.srv, err = server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger())
Expand Down 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
Loading

0 comments on commit df6004d

Please sign in to comment.