diff --git a/host/lux_option.go b/host/lux_option.go index a751455..e6e4825 100644 --- a/host/lux_option.go +++ b/host/lux_option.go @@ -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) } diff --git a/host/lux_wan.go b/host/lux_wan.go new file mode 100644 index 0000000..0d417fa --- /dev/null +++ b/host/lux_wan.go @@ -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 +} diff --git a/tests/lux_host_test.go b/tests/lux_host_test.go index c28579c..a1d8e1b 100644 --- a/tests/lux_host_test.go +++ b/tests/lux_host_test.go @@ -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") } } diff --git a/tests/lux_node_test.go b/tests/lux_node_test.go index 572d792..da1f035 100644 --- a/tests/lux_node_test.go +++ b/tests/lux_node_test.go @@ -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) {