Skip to content

Commit

Permalink
Fix issue with dhcp choosing outside of ipv4 range
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Nov 15, 2024
1 parent f68c713 commit 4c209e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
30 changes: 19 additions & 11 deletions internal/data/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,35 @@ func incrementIP(ip net.IP, inc uint) net.IP {

}

func getNextIP(subnet string) (string, error) {
func chooseInitalIP(cidr *net.IPNet) (net.IP, error) {

serverIP, cidr, err := net.ParseCIDR(subnet)
if err != nil {
return "", err
}

max := 32
if serverIP.To16() != nil {
max = 128
max := 128
if cidr.IP.To4() != nil {
max = 32
}

used, _ := cidr.Mask.Size()
maxNumberOfAddresses := int(math.Pow(2, float64(max-used))) - 2 // Do not allocate largest address or 0
if maxNumberOfAddresses < 1 {
return "", errors.New("subnet is too small to contain a new device")
return nil, errors.New("subnet is too small to contain a new device")
}

// Choose a random number that cannot be 0
addressAttempt := rand.Intn(maxNumberOfAddresses) + 1
addr := incrementIP(cidr.IP, uint(addressAttempt))
return incrementIP(cidr.IP, uint(addressAttempt)), nil
}

func getNextIP(subnet string) (string, error) {

serverIP, cidr, err := net.ParseCIDR(subnet)
if err != nil {
return "", err
}

addr, err := chooseInitalIP(cidr)
if err != nil {
return "", err
}

lease, err := clientv3.NewLease(etcd).Grant(context.Background(), 3)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions internal/data/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,34 @@ func TestIncrementIPOverflow(t *testing.T) {
})
}
}

func TestChooseInitial(t *testing.T) {

_, cidr, err := net.ParseCIDR("192.168.3.4/24")
if err != nil {
t.Fatal(err)
}

addr, err := chooseInitalIP(cidr)
if err != nil {
t.Fatal(err)
}

if !cidr.Contains(addr) {
t.Fatalf("does not contain address, %s", addr)
}

_, cidr, err = net.ParseCIDR("2001:db8:abcd:1234:c000::/66")
if err != nil {
t.Fatal(err)
}

addr, err = chooseInitalIP(cidr)
if err != nil {
t.Fatal(err)
}

if !cidr.Contains(addr) {
t.Fatalf("does not contain address, %s", addr)
}
}
2 changes: 1 addition & 1 deletion internal/mfaportal/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func New(firewall *router.Firewall, errChan chan<- error) (m *MfaPortal, err err
tunnel.GetOrPost("/", mfaPortal.index)

address := config.Values.Wireguard.ServerAddress.String()
if config.Values.Wireguard.ServerAddress.To16() != nil {
if config.Values.Wireguard.ServerAddress.To4() == nil && config.Values.Wireguard.ServerAddress.To16() != nil {
address = "[" + address + "]"
}

Expand Down
1 change: 1 addition & 0 deletions internal/routetypes/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func parseAddress(address string) (resultAddresses []net.IPNet, err error) {
if addr.To16() != nil {
addedSomething = true
resultAddresses = append(resultAddresses, net.IPNet{IP: addr.To16(), Mask: net.CIDRMask(128, 128)})
continue
}

}
Expand Down

0 comments on commit 4c209e6

Please sign in to comment.