forked from netbirdio/netbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace IP allocation logic (netbirdio#342)
The peer IP allocation logic was allocating sequential peer IP from the 100.64.0.0/10 address block. Each account is created with a random subnet from 100.64.0.0/10. The total amount of potential subnets is 64. The new logic allocates random peer IP from the account subnet. This gives us flexibility to add support for multi subnet accounts without overlapping IPs.
- Loading branch information
Showing
9 changed files
with
121 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestNewNetwork(t *testing.T) { | ||
network := NewNetwork() | ||
|
||
// generated net should be a subnet of a larger 100.64.0.0/10 net | ||
ipNet := net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}} | ||
assert.Equal(t, ipNet.Contains(network.Net.IP), true) | ||
} | ||
|
||
func TestAllocatePeerIP(t *testing.T) { | ||
|
||
ipNet := net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 255, 255, 0}} | ||
var ips []net.IP | ||
for i := 0; i < 253; i++ { | ||
ip, err := AllocatePeerIP(ipNet, ips) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ips = append(ips, ip) | ||
} | ||
|
||
assert.Len(t, ips, 253) | ||
|
||
uniq := make(map[string]struct{}) | ||
for _, ip := range ips { | ||
if _, ok := uniq[ip.String()]; !ok { | ||
uniq[ip.String()] = struct{}{} | ||
} else { | ||
t.Errorf("found duplicate IP %s", ip.String()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters