Pull request: 2508 ip conversion vol.1
Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa0d67aa251Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 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 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42abba74859eAuthor: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
@@ -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) {
|
||||
@@ -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}))
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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())
|
||||
@@ -632,19 +632,18 @@ 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)
|
||||
}
|
||||
@@ -652,7 +651,7 @@ func v4Create(conf V4ServerConf) (DHCPServer, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -16,119 +16,119 @@ 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.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: notify4,
|
||||
}
|
||||
s, err := v4Create(conf)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 0, len(ls))
|
||||
assert.Empty(t, ls)
|
||||
|
||||
// add static lease
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("192.168.10.150").To4()
|
||||
l.IP = net.IP{192, 168, 10, 150}
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// try to add the same static lease - fail
|
||||
assert.True(t, s.AddStaticLease(l) != nil)
|
||||
assert.NotNil(t, s.AddStaticLease(l))
|
||||
|
||||
// check
|
||||
ls = s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "192.168.10.150", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
||||
|
||||
// try to remove static lease - fail
|
||||
l.IP = net.ParseIP("192.168.10.110").To4()
|
||||
l.IP = net.IP{192, 168, 10, 110}
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.RemoveStaticLease(l) != nil)
|
||||
assert.NotNil(t, s.RemoveStaticLease(l))
|
||||
|
||||
// remove static lease
|
||||
l.IP = net.ParseIP("192.168.10.150").To4()
|
||||
l.IP = net.IP{192, 168, 10, 150}
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.RemoveStaticLease(l) == nil)
|
||||
assert.Nil(t, s.RemoveStaticLease(l))
|
||||
|
||||
// check
|
||||
ls = s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 0, len(ls))
|
||||
assert.Empty(t, ls)
|
||||
}
|
||||
|
||||
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.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: notify4,
|
||||
}
|
||||
sIface, err := v4Create(conf)
|
||||
s := sIface.(*v4Server)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// add dynamic lease
|
||||
ld := Lease{}
|
||||
ld.IP = net.ParseIP("192.168.10.150").To4()
|
||||
ld.IP = net.IP{192, 168, 10, 150}
|
||||
ld.HWAddr, _ = net.ParseMAC("11:aa:aa:aa:aa:aa")
|
||||
s.addLease(&ld)
|
||||
|
||||
// add dynamic lease
|
||||
{
|
||||
ld := Lease{}
|
||||
ld.IP = net.ParseIP("192.168.10.151").To4()
|
||||
ld.IP = net.IP{192, 168, 10, 151}
|
||||
ld.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
|
||||
s.addLease(&ld)
|
||||
}
|
||||
|
||||
// add static lease with the same IP
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("192.168.10.150").To4()
|
||||
l.IP = net.IP{192, 168, 10, 150}
|
||||
l.HWAddr, _ = net.ParseMAC("33:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// add static lease with the same MAC
|
||||
l = Lease{}
|
||||
l.IP = net.ParseIP("192.168.10.152").To4()
|
||||
l.IP = net.IP{192, 168, 10, 152}
|
||||
l.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// check
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 2, len(ls))
|
||||
assert.Len(t, ls, 2)
|
||||
|
||||
assert.Equal(t, "192.168.10.150", ls[0].IP.String())
|
||||
assert.Equal(t, "33:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
||||
|
||||
assert.Equal(t, "192.168.10.152", ls[1].IP.String())
|
||||
assert.Equal(t, "22:aa:aa:aa:aa:aa", ls[1].HWAddr.String())
|
||||
assert.True(t, ls[1].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[1].Expiry.Unix())
|
||||
}
|
||||
|
||||
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.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: notify4,
|
||||
}
|
||||
sIface, err := v4Create(conf)
|
||||
s := sIface.(*v4Server)
|
||||
assert.True(t, err == nil)
|
||||
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()}
|
||||
assert.Nil(t, err)
|
||||
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
||||
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("192.168.10.150").To4()
|
||||
l.IP = net.IP{192, 168, 10, 150}
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// "Discover"
|
||||
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
@@ -160,12 +160,12 @@ func TestV4StaticLeaseGet(t *testing.T) {
|
||||
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
||||
|
||||
dnsAddrs := resp.DNS()
|
||||
assert.Equal(t, 1, len(dnsAddrs))
|
||||
assert.Len(t, dnsAddrs, 1)
|
||||
assert.Equal(t, "192.168.10.1", dnsAddrs[0].String())
|
||||
|
||||
// check lease
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "192.168.10.150", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
}
|
||||
@@ -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.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: notify4,
|
||||
Options: []string{
|
||||
"81 hex 303132",
|
||||
@@ -185,8 +185,8 @@ func TestV4DynamicLeaseGet(t *testing.T) {
|
||||
}
|
||||
sIface, err := v4Create(conf)
|
||||
s := sIface.(*v4Server)
|
||||
assert.True(t, err == nil)
|
||||
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()}
|
||||
assert.Nil(t, err)
|
||||
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
||||
|
||||
// "Discover"
|
||||
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
@@ -220,19 +220,19 @@ func TestV4DynamicLeaseGet(t *testing.T) {
|
||||
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
||||
|
||||
dnsAddrs := resp.DNS()
|
||||
assert.Equal(t, 1, len(dnsAddrs))
|
||||
assert.Len(t, dnsAddrs, 1)
|
||||
assert.Equal(t, "192.168.10.1", dnsAddrs[0].String())
|
||||
|
||||
// check lease
|
||||
ls := s.GetLeases(LeasesDynamic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "192.168.10.100", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
|
||||
start := net.ParseIP("192.168.10.100").To4()
|
||||
stop := net.ParseIP("192.168.10.200").To4()
|
||||
assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.10.99").To4()))
|
||||
assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.100").To4()))
|
||||
assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.201").To4()))
|
||||
assert.True(t, ip4InRange(start, stop, net.ParseIP("192.168.10.100").To4()))
|
||||
start := net.IP{192, 168, 10, 100}
|
||||
stop := net.IP{192, 168, 10, 200}
|
||||
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 10, 99}))
|
||||
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 11, 100}))
|
||||
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 11, 201}))
|
||||
assert.True(t, ip4InRange(start, stop, net.IP{192, 168, 10, 100}))
|
||||
}
|
||||
|
||||
@@ -21,40 +21,40 @@ func TestV6StaticLeaseAddRemove(t *testing.T) {
|
||||
notify: notify6,
|
||||
}
|
||||
s, err := v6Create(conf)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 0, len(ls))
|
||||
assert.Empty(t, ls)
|
||||
|
||||
// add static lease
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("2001::1")
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// try to add static lease - fail
|
||||
assert.True(t, s.AddStaticLease(l) != nil)
|
||||
assert.NotNil(t, s.AddStaticLease(l))
|
||||
|
||||
// check
|
||||
ls = s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "2001::1", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
||||
|
||||
// try to remove static lease - fail
|
||||
l.IP = net.ParseIP("2001::2")
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.RemoveStaticLease(l) != nil)
|
||||
assert.NotNil(t, s.RemoveStaticLease(l))
|
||||
|
||||
// remove static lease
|
||||
l.IP = net.ParseIP("2001::1")
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.RemoveStaticLease(l) == nil)
|
||||
assert.Nil(t, s.RemoveStaticLease(l))
|
||||
|
||||
// check
|
||||
ls = s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 0, len(ls))
|
||||
assert.Empty(t, ls)
|
||||
}
|
||||
|
||||
func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) {
|
||||
@@ -65,7 +65,7 @@ func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) {
|
||||
}
|
||||
sIface, err := v6Create(conf)
|
||||
s := sIface.(*v6Server)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// add dynamic lease
|
||||
ld := Lease{}
|
||||
@@ -85,25 +85,25 @@ func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) {
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("2001::1")
|
||||
l.HWAddr, _ = net.ParseMAC("33:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// add static lease with the same MAC
|
||||
l = Lease{}
|
||||
l.IP = net.ParseIP("2001::3")
|
||||
l.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// check
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 2, len(ls))
|
||||
assert.Len(t, ls, 2)
|
||||
|
||||
assert.Equal(t, "2001::1", ls[0].IP.String())
|
||||
assert.Equal(t, "33:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
||||
|
||||
assert.Equal(t, "2001::3", ls[1].IP.String())
|
||||
assert.Equal(t, "22:aa:aa:aa:aa:aa", ls[1].HWAddr.String())
|
||||
assert.True(t, ls[1].Expiry.Unix() == leaseExpireStatic)
|
||||
assert.EqualValues(t, leaseExpireStatic, ls[1].Expiry.Unix())
|
||||
}
|
||||
|
||||
func TestV6GetLease(t *testing.T) {
|
||||
@@ -114,7 +114,7 @@ func TestV6GetLease(t *testing.T) {
|
||||
}
|
||||
sIface, err := v6Create(conf)
|
||||
s := sIface.(*v6Server)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")}
|
||||
s.sid = dhcpv6.Duid{
|
||||
Type: dhcpv6.DUID_LLT,
|
||||
@@ -125,7 +125,7 @@ func TestV6GetLease(t *testing.T) {
|
||||
l := Lease{}
|
||||
l.IP = net.ParseIP("2001::1")
|
||||
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
assert.True(t, s.AddStaticLease(l) == nil)
|
||||
assert.Nil(t, s.AddStaticLease(l))
|
||||
|
||||
// "Solicit"
|
||||
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
@@ -156,12 +156,12 @@ func TestV6GetLease(t *testing.T) {
|
||||
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
|
||||
|
||||
dnsAddrs := resp.Options.DNS()
|
||||
assert.Equal(t, 1, len(dnsAddrs))
|
||||
assert.Len(t, dnsAddrs, 1)
|
||||
assert.Equal(t, "2000::1", dnsAddrs[0].String())
|
||||
|
||||
// check lease
|
||||
ls := s.GetLeases(LeasesStatic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "2001::1", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
}
|
||||
@@ -174,7 +174,7 @@ func TestV6GetDynamicLease(t *testing.T) {
|
||||
}
|
||||
sIface, err := v6Create(conf)
|
||||
s := sIface.(*v6Server)
|
||||
assert.True(t, err == nil)
|
||||
assert.Nil(t, err)
|
||||
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")}
|
||||
s.sid = dhcpv6.Duid{
|
||||
Type: dhcpv6.DUID_LLT,
|
||||
@@ -209,17 +209,17 @@ func TestV6GetDynamicLease(t *testing.T) {
|
||||
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
|
||||
|
||||
dnsAddrs := resp.Options.DNS()
|
||||
assert.Equal(t, 1, len(dnsAddrs))
|
||||
assert.Len(t, dnsAddrs, 1)
|
||||
assert.Equal(t, "2000::1", dnsAddrs[0].String())
|
||||
|
||||
// check lease
|
||||
ls := s.GetLeases(LeasesDynamic)
|
||||
assert.Equal(t, 1, len(ls))
|
||||
assert.Len(t, ls, 1)
|
||||
assert.Equal(t, "2001::2", ls[0].IP.String())
|
||||
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
|
||||
|
||||
assert.True(t, !ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::1")))
|
||||
assert.True(t, !ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2002::2")))
|
||||
assert.False(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::1")))
|
||||
assert.False(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2002::2")))
|
||||
assert.True(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::2")))
|
||||
assert.True(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::3")))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user