mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-26 08:53:05 +00:00
Do not issue "network" or "broadcast" addresses (0 or 255)
This commit is contained in:
parent
d3349aa4d1
commit
ea615e3a26
2 changed files with 20 additions and 24 deletions
26
utils.go
26
utils.go
|
@ -79,21 +79,6 @@ func (h *Headscale) getAvailableIP() (*netaddr.IP, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// for _, ip := range usedIps {
|
|
||||||
// nextIP := ip.Next()
|
|
||||||
|
|
||||||
// if !containsIPs(usedIps, nextIP) && ipPrefix.Contains(nextIP) {
|
|
||||||
// return &nextIP, nil
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // If there are no IPs in use, we are starting fresh and
|
|
||||||
// // can issue IPs from the beginning of the prefix.
|
|
||||||
// ip := ipPrefix.IP()
|
|
||||||
// return &ip, nil
|
|
||||||
|
|
||||||
// return nil, fmt.Errorf("failed to find any available IP in %s", ipPrefix)
|
|
||||||
|
|
||||||
// Get the first IP in our prefix
|
// Get the first IP in our prefix
|
||||||
ip := ipPrefix.IP()
|
ip := ipPrefix.IP()
|
||||||
|
|
||||||
|
@ -102,8 +87,19 @@ func (h *Headscale) getAvailableIP() (*netaddr.IP, error) {
|
||||||
return nil, fmt.Errorf("could not find any suitable IP in %s", ipPrefix)
|
return nil, fmt.Errorf("could not find any suitable IP in %s", ipPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Some OS (including Linux) does not like when IPs ends with 0 or 255, which
|
||||||
|
// is typically called network or broadcast. Lets avoid them and continue
|
||||||
|
// to look when we get one of those traditionally reserved IPs.
|
||||||
|
ipRaw := ip.As4()
|
||||||
|
if ipRaw[3] == 0 || ipRaw[3] == 255 {
|
||||||
|
ip = ip.Next()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if ip.IsZero() &&
|
if ip.IsZero() &&
|
||||||
ip.IsLoopback() {
|
ip.IsLoopback() {
|
||||||
|
|
||||||
|
ip = ip.Next()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ func (s *Suite) TestGetAvailableIp(c *check.C) {
|
||||||
|
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
expected := netaddr.MustParseIP("10.27.0.0")
|
expected := netaddr.MustParseIP("10.27.0.1")
|
||||||
|
|
||||||
c.Assert(ip.String(), check.Equals, expected.String())
|
c.Assert(ip.String(), check.Equals, expected.String())
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ func (s *Suite) TestGetUsedIps(c *check.C) {
|
||||||
|
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
expected := netaddr.MustParseIP("10.27.0.0")
|
expected := netaddr.MustParseIP("10.27.0.1")
|
||||||
|
|
||||||
c.Assert(ips[0], check.Equals, expected)
|
c.Assert(ips[0], check.Equals, expected)
|
||||||
|
|
||||||
|
@ -91,20 +91,20 @@ func (s *Suite) TestGetMultiIp(c *check.C) {
|
||||||
|
|
||||||
c.Assert(len(ips), check.Equals, 350)
|
c.Assert(len(ips), check.Equals, 350)
|
||||||
|
|
||||||
c.Assert(ips[0], check.Equals, netaddr.MustParseIP("10.27.0.0"))
|
c.Assert(ips[0], check.Equals, netaddr.MustParseIP("10.27.0.1"))
|
||||||
c.Assert(ips[9], check.Equals, netaddr.MustParseIP("10.27.0.9"))
|
c.Assert(ips[9], check.Equals, netaddr.MustParseIP("10.27.0.10"))
|
||||||
c.Assert(ips[300], check.Equals, netaddr.MustParseIP("10.27.1.44"))
|
c.Assert(ips[300], check.Equals, netaddr.MustParseIP("10.27.1.47"))
|
||||||
|
|
||||||
// Check that we can read back the IPs
|
// Check that we can read back the IPs
|
||||||
m1, err := h.GetMachineByID(1)
|
m1, err := h.GetMachineByID(1)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(m1.IPAddress, check.Equals, netaddr.MustParseIP("10.27.0.0").String())
|
c.Assert(m1.IPAddress, check.Equals, netaddr.MustParseIP("10.27.0.1").String())
|
||||||
|
|
||||||
m50, err := h.GetMachineByID(50)
|
m50, err := h.GetMachineByID(50)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(m50.IPAddress, check.Equals, netaddr.MustParseIP("10.27.0.49").String())
|
c.Assert(m50.IPAddress, check.Equals, netaddr.MustParseIP("10.27.0.50").String())
|
||||||
|
|
||||||
expectedNextIP := netaddr.MustParseIP("10.27.1.94")
|
expectedNextIP := netaddr.MustParseIP("10.27.1.97")
|
||||||
nextIP, err := h.getAvailableIP()
|
nextIP, err := h.getAvailableIP()
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
|
||||||
ip, err := h.getAvailableIP()
|
ip, err := h.getAvailableIP()
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
expected := netaddr.MustParseIP("10.27.0.0")
|
expected := netaddr.MustParseIP("10.27.0.1")
|
||||||
|
|
||||||
c.Assert(ip.String(), check.Equals, expected.String())
|
c.Assert(ip.String(), check.Equals, expected.String())
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue