fix ipv4 overlay assignment

This commit is contained in:
Leo Antunes 2019-03-29 22:15:17 +01:00
parent 944fca4dfd
commit c8f0d71a54
2 changed files with 24 additions and 10 deletions

View File

@ -58,7 +58,7 @@ func newWGConfig(iface string, port int) (*wgState, error) {
func (wg *wgState) assignOverlayAddr(ipnet *net.IPNet, name string) { func (wg *wgState) assignOverlayAddr(ipnet *net.IPNet, name string) {
// TODO: this is way too brittle and opaque // TODO: this is way too brittle and opaque
bits, size := ipnet.Mask.Size() bits, size := ipnet.Mask.Size()
ip := make([]byte, net.IPv6len) ip := make([]byte, len(ipnet.IP))
copy(ip, []byte(ipnet.IP)) copy(ip, []byte(ipnet.IP))
h := fnv.New128a() h := fnv.New128a()

View File

@ -43,20 +43,22 @@ func Test_wgState_assignOverlayAddr(t *testing.T) {
ipnet *net.IPNet ipnet *net.IPNet
name string name string
} }
_, ipv4net, _ := net.ParseCIDR("10.0.0.0/8")
_, ipv6net, _ := net.ParseCIDR("2001:db8::/32")
tests := []struct { tests := []struct {
name string name string
args args args args
want net.IP want string
}{ }{
{ {
"assign in big ipv4 net", "assign in big ipv4 net",
args{&net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}, "test"}, args{ipv4net, "test"},
net.ParseIP("10.221.153.165"), // if we ever have to change this, we should probably also mark it as a breaking change "10.221.153.165", // if we ever have to change this, we should probably also mark it as a breaking change
}, },
{ {
"assign in ipv6 net", "assign in ipv6 net",
args{&net.IPNet{IP: net.ParseIP("2001:db8::"), Mask: net.CIDRMask(32, 128)}, "test"}, args{ipv6net, "test"},
net.ParseIP("2001:db8:c575:7277:b806:e994:13dd:99a5"), // if we ever have to change this, we should probably also mark it as a breaking change "2001:db8:c575:7277:b806:e994:13dd:99a5", // if we ever have to change this, we should probably also mark it as a breaking change
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -64,7 +66,7 @@ func Test_wgState_assignOverlayAddr(t *testing.T) {
wg := &wgState{} wg := &wgState{}
wg.assignOverlayAddr(tt.args.ipnet, tt.args.name) wg.assignOverlayAddr(tt.args.ipnet, tt.args.name)
if !reflect.DeepEqual(wg.OverlayAddr, tt.want) { if !reflect.DeepEqual(wg.OverlayAddr.String(), tt.want) {
t.Errorf("assignOverlayAddr() set = %s, want %s", wg.OverlayAddr, tt.want) t.Errorf("assignOverlayAddr() set = %s, want %s", wg.OverlayAddr, tt.want)
} }
}) })
@ -74,7 +76,7 @@ func Test_wgState_assignOverlayAddr(t *testing.T) {
// This is just to ensure - if we ever change the hashing function - that it spreads the results in a way that at least // This is just to ensure - if we ever change the hashing function - that it spreads the results in a way that at least
// avoids the most obvious collisions. // avoids the most obvious collisions.
func Test_wgState_assignOverlayAddr_no_obvious_collisions(t *testing.T) { func Test_wgState_assignOverlayAddr_no_obvious_collisions(t *testing.T) {
ipnet := &net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(24, 32)} _, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
assignments := make(map[string]string) assignments := make(map[string]string)
for _, n := range []string{"test", "test1", "test2", "1test", "2test"} { for _, n := range []string{"test", "test1", "test2", "1test", "2test"} {
wg := &wgState{} wg := &wgState{}
@ -87,8 +89,8 @@ func Test_wgState_assignOverlayAddr_no_obvious_collisions(t *testing.T) {
} }
// This should ensure the obvious fact that the same name should map to the same IP if called twice. // This should ensure the obvious fact that the same name should map to the same IP if called twice.
func Test_wgState_assignOverlayAddr_repeatable(t *testing.T) { func Test_wgState_assignOverlayAddr_consistent(t *testing.T) {
ipnet := &net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(24, 32)} _, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
wg1 := &wgState{} wg1 := &wgState{}
wg1.assignOverlayAddr(ipnet, "test") wg1.assignOverlayAddr(ipnet, "test")
wg2 := &wgState{} wg2 := &wgState{}
@ -97,3 +99,15 @@ func Test_wgState_assignOverlayAddr_repeatable(t *testing.T) {
t.Errorf("assignOverlayAddr() %s != %s", wg1.OverlayAddr, wg2.OverlayAddr) t.Errorf("assignOverlayAddr() %s != %s", wg1.OverlayAddr, wg2.OverlayAddr)
} }
} }
func Test_wgState_assignOverlayAddr_repeatable(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
wg := &wgState{}
wg.assignOverlayAddr(ipnet, "test")
gen1 := wg.OverlayAddr.String()
wg.assignOverlayAddr(ipnet, "test")
gen2 := wg.OverlayAddr.String()
if gen1 != gen2 {
t.Errorf("assignOverlayAddr() %s != %s", gen1, gen2)
}
}