resolved major routing, identification issues with host-node communication. tests are passing now

This commit is contained in:
mykola2312 2025-01-16 09:55:27 +02:00
parent 708819d981
commit fb48bfc113
3 changed files with 45 additions and 23 deletions

View file

@ -25,8 +25,8 @@ func NewLuxHost(hostname string, hostKey crypto.LuxKey, ks crypto.LuxKeyStore) L
}
}
func (host *LuxHost) AddNode(chType net.LuxChannelType, udpAddr string) error {
return host.router.CreateOutboundRoute(host.router.GetThisKey().Id, chType, udpAddr)
func (host *LuxHost) AddNode(node proto.LuxID, chType net.LuxChannelType, udpAddr string) error {
return host.router.CreateOutboundRoute(node, chType, udpAddr)
}
func (host *LuxHost) AddOptionProvider(provider LuxOptionProvider) {

View file

@ -13,8 +13,8 @@ import (
type LuxRouteType int
const (
LuxRouteHostToNode = 0
LuxRouteNodeToNode = 1
LuxRouteFromSource = 0
LuxRouteToTarget = 1
)
type LuxRoute struct {
@ -84,14 +84,19 @@ func (r *LuxRouter) GetThisKey() crypto.LuxKey {
return r.thisKey
}
func channelTypeToRouteType(chType LuxChannelType) LuxRouteType {
func (r *LuxRouter) GetRouterType() proto.LuxType {
return r.thisKey.Type
}
func (r *LuxRouter) channelTypeToRouteType(chType LuxChannelType) LuxRouteType {
switch chType {
case LuxChannelInterior:
return LuxRouteNodeToNode
return LuxRouteToTarget
case LuxChannelExterior:
return LuxRouteHostToNode
return LuxRouteFromSource
default:
log.Panicf("can't translate chType %d to lux route type", chType)
return -1
}
}
@ -116,7 +121,7 @@ func (r *LuxRouter) addInboundChannel(ch LuxChannel) *LuxChannel {
}
// the ID is not destination, but rather peer associated for this route, like source ID.
// Destination router always know who is he, therefore we dont need target ID
// Destination router always know who is he, therefore we dont need target ID.
func (r *LuxRouter) CreateOutboundRoute(id proto.LuxID, chType LuxChannelType, udpAddr string) error {
// we gonna look up key by id from key store
key, ok := r.keyStore.Get(id)
@ -131,7 +136,7 @@ func (r *LuxRouter) CreateOutboundRoute(id proto.LuxID, chType LuxChannelType, u
}
r.routes[key.Id] = &LuxRoute{
Type: channelTypeToRouteType(chType),
Type: r.channelTypeToRouteType(chType),
Target: id,
Source: r.thisKey.Id,
Destination: channel.Address,
@ -147,12 +152,7 @@ func (r *LuxRouter) CreateInboundChannel(chType LuxChannelType, udpAddr string)
return err
}
r.routes[r.thisKey.Id] = &LuxRoute{
Source: r.thisKey.Id,
Destination: channel.Address,
Associated: r.addInboundChannel(channel),
Nonces: NewLuxNonceList(),
}
r.addInboundChannel(channel)
return nil
}
@ -250,6 +250,17 @@ func (r *LuxRouter) DeleteRoute(route *LuxRoute) {
delete(r.routes, route.Target)
}
func (r *LuxRouter) GetRouteKey(route *LuxRoute) (crypto.LuxKey, bool) {
switch route.Type {
case LuxRouteFromSource:
return r.keyStore.Get(route.Source)
case LuxRouteToTarget:
return r.keyStore.Get(route.Target)
}
return crypto.LuxKey{}, false
}
func (r *LuxRouter) Recv() (LuxPacket, error) {
dgram := r.RecvDgram()
@ -263,7 +274,7 @@ func (r *LuxRouter) Recv() (LuxPacket, error) {
// first we look key from routes
if route, ok := r.GetRoute(dgram.Target); ok {
key, _ := r.keyStore.Get(route.Target)
key, _ := r.GetRouteKey(route)
packet, err = DecryptLuxPacket(dgram, key)
if err != nil {
// do we really fail here?
@ -311,15 +322,24 @@ func (r *LuxRouter) Recv() (LuxPacket, error) {
// since packet arrived from different transport, we flush nonces
route.Nonces.Flush()
} else {
var routeType LuxRouteType
if key.Type == proto.LuxTypeHost {
routeType = LuxRouteFromSource
} else {
routeType = LuxRouteToTarget
}
r.routes[key.Id] = &LuxRoute{
Key: key,
Type: routeType,
Target: key.Id,
Source: r.thisKey.Id,
Destination: dgram.Target,
Associated: dgram.Channel,
Nonces: NewLuxNonceList(),
}
route = r.routes[key.Id]
log.Debugf("established route %s <-> %s", route.Key.Id.String(), route.Destination.String())
log.Debugf("established route %s <-> %s", route.Target.String(), route.Destination.String())
}
// rotate nonce
@ -343,9 +363,10 @@ func (r *LuxRouter) Send(packet LuxPacket) error {
if !ok {
return errors.New("no route to peer")
}
key, _ := r.GetRouteKey(route)
packet.Nonce = GenerateLuxNonce()
dgram, err := EncryptLuxPacket(packet, route.Key, route.Destination)
dgram, err := EncryptLuxPacket(packet, key, route.Destination)
if err != nil {
return err
}
@ -356,9 +377,10 @@ func (r *LuxRouter) Send(packet LuxPacket) error {
func (r *LuxRouter) Multicast(packet LuxPacket, group proto.LuxType) error {
for _, route := range r.routes {
if route.Key.Type == group {
key, _ := r.GetRouteKey(route)
if key.Type == group {
packet.Nonce = GenerateLuxNonce()
dgram, err := EncryptLuxPacket(packet, route.Key, route.Destination)
dgram, err := EncryptLuxPacket(packet, key, route.Destination)
if err != nil {
return err
}

View file

@ -38,8 +38,8 @@ func TestHeartbeatMulticast(t *testing.T) {
defer nodeB.Stop()
hostA := host.NewLuxHost("testhost", keyHost, ks)
hostA.AddNode(net.LuxChannelInterior, "127.0.0.1:9979")
hostA.AddNode(net.LuxChannelInterior, "127.0.0.2:9979")
hostA.AddNode(keyNodeA.Id, net.LuxChannelInterior, "127.0.0.1:9979")
hostA.AddNode(keyNodeB.Id, net.LuxChannelInterior, "127.0.0.2:9979")
hostA.AddOptionProvider(&DummyWANProvider{})
if err := hostA.Heartbeat(); err != nil {