rework WAN option to always carry ipv4 and ipv6 in one option
This commit is contained in:
parent
1ac1c88644
commit
b60aa935ce
4 changed files with 64 additions and 63 deletions
|
|
@ -3,14 +3,12 @@ package host
|
|||
import (
|
||||
"fmt"
|
||||
"lux/proto"
|
||||
"net"
|
||||
)
|
||||
|
||||
type LuxOptionType uint
|
||||
|
||||
const (
|
||||
LuxOptionTypeWAN = 0
|
||||
LuxOptionTypeWAN6 = 1
|
||||
LuxOptionTypeWAN = 0
|
||||
)
|
||||
|
||||
type LuxOption interface {
|
||||
|
|
@ -20,53 +18,6 @@ type LuxOption interface {
|
|||
Type() LuxOptionType
|
||||
}
|
||||
|
||||
// TODO! TODO! TODO!
|
||||
// Rewrite this to use WriteIP
|
||||
|
||||
type LuxOptionWAN struct {
|
||||
Addr4 net.IP
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) Read(rd *proto.LuxBuffer) error {
|
||||
octets, err := rd.ReadNext(4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opt.Addr4 = net.IP(octets)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) Write(wd *proto.LuxBuffer) {
|
||||
wd.WriteBytes(opt.Addr4.To4())
|
||||
}
|
||||
|
||||
func (*LuxOptionWAN) Type() LuxOptionType {
|
||||
return LuxOptionTypeWAN
|
||||
}
|
||||
|
||||
type LuxOptionWAN6 struct {
|
||||
Addr6 net.IP
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN6) Read(rd *proto.LuxBuffer) error {
|
||||
octets, err := rd.ReadNext(16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opt.Addr6 = net.IP(octets)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN6) Write(wd *proto.LuxBuffer) {
|
||||
wd.WriteBytes(opt.Addr6.To16())
|
||||
}
|
||||
|
||||
func (*LuxOptionWAN6) Type() LuxOptionType {
|
||||
return LuxOptionTypeWAN6
|
||||
}
|
||||
|
||||
func ReadLuxOption(rd *proto.LuxBuffer) (LuxOption, error) {
|
||||
var err error
|
||||
optVal, err := rd.ReadUint16()
|
||||
|
|
@ -79,10 +30,6 @@ func ReadLuxOption(rd *proto.LuxBuffer) (LuxOption, error) {
|
|||
opt := &LuxOptionWAN{}
|
||||
err = opt.Read(rd)
|
||||
return opt, err
|
||||
case LuxOptionTypeWAN6:
|
||||
opt := &LuxOptionWAN6{}
|
||||
err = opt.Read(rd)
|
||||
return opt, err
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown option %d", optVal)
|
||||
}
|
||||
|
|
|
|||
53
host/lux_wan.go
Normal file
53
host/lux_wan.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"lux/proto"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type LuxOptionWAN struct {
|
||||
Addr4 netip.Addr
|
||||
Addr6 netip.Addr
|
||||
}
|
||||
|
||||
// always use this constructor, since if there is no IPv6 or no IPv4,
|
||||
// they must be zeroed
|
||||
func NewLuxOptionWAN() LuxOptionWAN {
|
||||
return LuxOptionWAN{
|
||||
Addr4: netip.IPv4Unspecified(),
|
||||
Addr6: netip.IPv6Unspecified(),
|
||||
}
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) HasIPv4() bool {
|
||||
return !opt.Addr4.IsUnspecified()
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) HasIPv6() bool {
|
||||
return !opt.Addr6.IsUnspecified()
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) Read(rd *proto.LuxBuffer) error {
|
||||
addr4, err := rd.ReadIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opt.Addr4 = addr4
|
||||
|
||||
addr6, err := rd.ReadIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opt.Addr6 = addr6
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt *LuxOptionWAN) Write(wd *proto.LuxBuffer) {
|
||||
wd.WriteIP(opt.Addr4)
|
||||
wd.WriteIP(opt.Addr6)
|
||||
}
|
||||
|
||||
func (*LuxOptionWAN) Type() LuxOptionType {
|
||||
return LuxOptionTypeWAN
|
||||
}
|
||||
|
|
@ -12,9 +12,10 @@ import (
|
|||
type DummyWANProvider struct{}
|
||||
|
||||
func (*DummyWANProvider) Provide() (host.LuxOption, error) {
|
||||
return &host.LuxOptionWAN{
|
||||
Addr4: []byte{1, 2, 3, 4},
|
||||
}, nil
|
||||
wan := host.NewLuxOptionWAN()
|
||||
wan.Addr4 = proto.LuxProtoIPToAddr([]byte{1, 2, 3, 4})
|
||||
|
||||
return &wan, nil
|
||||
}
|
||||
|
||||
func TestHeartbeatMulticast(t *testing.T) {
|
||||
|
|
@ -75,9 +76,8 @@ func TestHeartbeatMulticast(t *testing.T) {
|
|||
t.Fatal("stateA.Hostname != stateB.Hostname")
|
||||
}
|
||||
|
||||
//lint:ignore SA1021 cant include net.IP due package name collision
|
||||
if !bytes.Equal(stateA.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4,
|
||||
stateB.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4) {
|
||||
if !bytes.Equal(stateA.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4.AsSlice(),
|
||||
stateB.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4.AsSlice()) {
|
||||
t.Fatal("WAN IPs aren't equal")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,10 @@ import (
|
|||
type DummyWANProvider2 struct{}
|
||||
|
||||
func (*DummyWANProvider2) Provide() (host.LuxOption, error) {
|
||||
return &host.LuxOptionWAN{
|
||||
Addr4: []byte{1, 2, 3, 4},
|
||||
}, nil
|
||||
wan := host.NewLuxOptionWAN()
|
||||
wan.Addr4 = proto.LuxProtoIPToAddr([]byte{1, 2, 3, 4})
|
||||
|
||||
return &wan, nil
|
||||
}
|
||||
|
||||
func TestNodeHeartbeat(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue