From bc8c4e7a0f97b4fc59e3aee843315b43548edcce Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 17 Jan 2025 09:23:11 +0200 Subject: [PATCH] fix route id matching, now node-to-host replies works --- host/lux_host.go | 4 ++++ net/lux_router.go | 20 +++++++++++++++++++- tests/lux_host_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/host/lux_host.go b/host/lux_host.go index 91a6217..8757196 100644 --- a/host/lux_host.go +++ b/host/lux_host.go @@ -33,6 +33,10 @@ func (host *LuxHost) AddOptionProvider(provider LuxOptionProvider) { host.providers = append(host.providers, provider) } +func (host *LuxHost) GetRouter() *net.LuxRouter { + return &host.router +} + func (host *LuxHost) CaptureState() (LuxState, error) { state := LuxState{ Hostname: host.hostname, diff --git a/net/lux_router.go b/net/lux_router.go index e257908..1624192 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -221,6 +221,10 @@ func (r *LuxRouter) Start() { for _, inbound := range r.inbound { go channelReceiver(r, &inbound) } + for _, outbound := range r.outbound { + go channelReceiver(r, &outbound) + } + r.channelLock.RUnlock() } @@ -299,7 +303,21 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { } // check if LuxID matches - if !bytes.Equal(packet.Target.UUID[:], route.Target.UUID[:]) { + // Node replies to host shall be matched by source ID on the host + var routeId *proto.LuxID + targetKey, _ := r.keyStore.Get(route.Target) + if r.GetRouterType() == proto.LuxTypeHost { + if targetKey.Type == proto.LuxTypeNode { + routeId = &route.Source + } else { + routeId = &route.Target + } + } else { + // on the node, we always operate by target ID + routeId = &route.Target + } + + if !bytes.Equal(packet.Target.UUID[:], routeId.UUID[:]) { // not matches.. we discard route and throw away packet log.Infof("packet from %s received at route %v has targetID %s that mismatches associated target UUID %s", dgram.Target.String(), route, packet.Target.String(), route.Target.String()) diff --git a/tests/lux_host_test.go b/tests/lux_host_test.go index 03f1212..c28579c 100644 --- a/tests/lux_host_test.go +++ b/tests/lux_host_test.go @@ -81,4 +81,29 @@ func TestHeartbeatMulticast(t *testing.T) { t.Fatal("WAN IPs aren't equal") } } + + // node to host reply + reply := net.LuxPacket{ + Target: keyHost.Id, + Type: 0x0203, + Buffer: proto.FromSlice([]byte{1, 2, 3, 4}), + } + if err := nodeA.Send(reply); err != nil { + t.Fatal(err) + } + + hostA.GetRouter().Start() + defer hostA.GetRouter().Stop() + + reply2, err := hostA.GetRouter().Recv() + if err != nil { + t.Fatal(err) + } + + if reply2.Type != reply.Type { + t.Fatal(".Type != reply.Type") + } + if !bytes.Equal(reply2.Buffer.AllBytes()[:4], reply.Buffer.AllBytes()) { + t.Fatal("reply payloads not equal!") + } }