diff --git a/host/lux_host.go b/host/lux_host.go index 5f76f66..91a6217 100644 --- a/host/lux_host.go +++ b/host/lux_host.go @@ -25,8 +25,8 @@ func NewLuxHost(hostname string, hostKey crypto.LuxKey, ks crypto.LuxKeyStore) L } } -func (host *LuxHost) AddNode(node proto.LuxID, chType net.LuxChannelType, udpAddr string) error { - return host.router.CreateOutboundRoute(node, chType, udpAddr) +func (host *LuxHost) AddNode(node proto.LuxID, udpAddr string) error { + return host.router.CreateOutboundRoute(node, net.LuxChannelExterior, udpAddr) } func (host *LuxHost) AddOptionProvider(provider LuxOptionProvider) { diff --git a/net/lux_channel.go b/net/lux_channel.go index 9b8e601..f4d20ff 100644 --- a/net/lux_channel.go +++ b/net/lux_channel.go @@ -9,6 +9,17 @@ const ( LuxChannelExterior = 1 ) +func (chType *LuxChannelType) String() string { + switch *chType { + case LuxChannelInterior: + return "interior" + case LuxChannelExterior: + return "exterior" + default: + return "?" + } +} + type LuxChannel struct { Type LuxChannelType Address *net.UDPAddr diff --git a/net/lux_router.go b/net/lux_router.go index d96fddb..5203557 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -26,31 +26,51 @@ type LuxRoute struct { Nonces LuxNonceList } -// !!!!! -// TODO: map key can be destination ID, while Key (+ ID) in route struct would be SOURCE +func (route *LuxRoute) String() string { + var dir string + switch route.Type { + case LuxRouteFromSource: + dir = "->" + case LuxRouteToTarget: + dir = "<-" + default: + dir = "?" + } + + return fmt.Sprintf("%s %s %s %s %s", + route.Target.String(), dir, route.Source.String(), + route.Associated.Type.String(), route.Destination.IP.String()) +} + /* Routing Map Table - key TargetID <-> value Route (SourceID, SourceKey, Transport..) + key TargetID <-> value Route (SourceID, SourceKey, Transport..) Host routing: - node 1111 <-> source 0001 host A - node 1112 <-> source 0001 host A - node 1113 <-> source 0001 host A - node 1114 <-> source 0001 host A + + node 1111 <-> source 0001 host A + node 1112 <-> source 0001 host A + node 1113 <-> source 0001 host A + node 1114 <-> source 0001 host A Node routing: - host 0001 <-> source 1111 node A - host 0002 <-> source 1111 node A - host 0003 <-> source 1111 node A - host 0004 <-> source 1111 node A - node 1112 <-> source 1111 node A + + host 0001 <-> source 1111 node A + host 0002 <-> source 1111 node A + host 0003 <-> source 1111 node A + host 0004 <-> source 1111 node A + node 1112 <-> source 1111 node A + Node and host: - Receives and decrypts with host key, which is TargetID (as well as LuxPacket Target) - Sends to host with host key, which is TargetID + + Receives and decrypts with host key, which is TargetID (as well as LuxPacket Target) + Sends to host with host key, which is TargetID + Node 1111 and node 1112: - Node 1111 receives and decrypts with node 1112 key, which is TargetID - Node 1111 sends to node 1112 with node 1112 key, which is TargetID + + Node 1111 receives and decrypts with node 1112 key, which is TargetID + Node 1111 sends to node 1112 with node 1112 key, which is TargetID But host uses host key when communicating to node, so target ID in host routing table cannot be used. To overcome this, a "direction" field must be introduced to routing entry, that @@ -88,18 +108,6 @@ func (r *LuxRouter) GetRouterType() proto.LuxType { return r.thisKey.Type } -func (r *LuxRouter) channelTypeToRouteType(chType LuxChannelType) LuxRouteType { - switch chType { - case LuxChannelInterior: - return LuxRouteToTarget - case LuxChannelExterior: - return LuxRouteFromSource - default: - log.Panicf("can't translate chType %d to lux route type", chType) - return -1 - } -} - func (r *LuxRouter) addOutboundChannel(ch LuxChannel) *LuxChannel { r.channelLock.Lock() @@ -136,13 +144,15 @@ func (r *LuxRouter) CreateOutboundRoute(id proto.LuxID, chType LuxChannelType, u } r.routes[key.Id] = &LuxRoute{ - Type: r.channelTypeToRouteType(chType), + Type: LuxRouteToTarget, Target: id, Source: r.thisKey.Id, Destination: channel.Address, Associated: r.addOutboundChannel(channel), Nonces: NewLuxNonceList(), } + log.Debugf("outbound route: %s", r.routes[key.Id].String()) + return nil } @@ -267,11 +277,6 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { var err error var packet LuxPacket - /* BUG: - * nodes share same UUID. When updating routes by UUID, it will overwrite others' nodes routes. - * this is current limitation of protocol - */ - // first we look key from routes if route, ok := r.GetRoute(dgram.Target); ok { key, _ := r.GetRouteKey(route) @@ -339,7 +344,7 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { } route = r.routes[key.Id] - log.Debugf("established route %s <-> %s", route.Target.String(), route.Destination.String()) + log.Debugf("established route: %s", route.String()) } // rotate nonce diff --git a/tests/lux_host_test.go b/tests/lux_host_test.go index 7b396af..d532f51 100644 --- a/tests/lux_host_test.go +++ b/tests/lux_host_test.go @@ -28,18 +28,18 @@ func TestHeartbeatMulticast(t *testing.T) { ks.Put(keyHost) nodeA := net.NewLuxRouter(keyNodeA, ks) - nodeA.CreateInboundChannel(net.LuxChannelInterior, "127.0.0.1:9979") + nodeA.CreateInboundChannel(net.LuxChannelExterior, "127.0.0.1:9979") nodeA.Start() defer nodeA.Stop() nodeB := net.NewLuxRouter(keyNodeB, ks) - nodeB.CreateInboundChannel(net.LuxChannelInterior, "127.0.0.2:9979") + nodeB.CreateInboundChannel(net.LuxChannelExterior, "127.0.0.2:9979") nodeB.Start() defer nodeB.Stop() hostA := host.NewLuxHost("testhost", keyHost, ks) - hostA.AddNode(keyNodeA.Id, net.LuxChannelInterior, "127.0.0.1:9979") - hostA.AddNode(keyNodeB.Id, net.LuxChannelInterior, "127.0.0.2:9979") + hostA.AddNode(keyNodeA.Id, "127.0.0.1:9979") + hostA.AddNode(keyNodeB.Id, "127.0.0.2:9979") hostA.AddOptionProvider(&DummyWANProvider{}) if err := hostA.Heartbeat(); err != nil {