diff --git a/net/lux_router.go b/net/lux_router.go index 9fdef55..df29c6e 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -295,6 +295,11 @@ func (r *LuxRouter) GetRouteKey(route *LuxRoute) (crypto.LuxKey, bool) { return crypto.LuxKey{}, false } +func (r *LuxRouter) HasKeyFor(id proto.LuxID) bool { + _, ok := r.keyStore.Get(id) + return ok +} + func (r *LuxRouter) Recv() (LuxPacket, error) { dgram := r.RecvDgram() diff --git a/node/lux_node.go b/node/lux_node.go index e329fbe..52beb42 100644 --- a/node/lux_node.go +++ b/node/lux_node.go @@ -6,21 +6,26 @@ import ( "lux/net" "lux/proto" "sync" + + ipnet "net" ) type LuxNode struct { router net.LuxRouter running bool + neighbors map[proto.LuxID]*ipnet.UDPAddr + state LuxNodeState stateLock sync.RWMutex } func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode { return LuxNode{ - router: net.NewLuxRouter(nodeKey, ks), - running: false, - state: NewNodeState(), + router: net.NewLuxRouter(nodeKey, ks), + running: false, + neighbors: make(map[proto.LuxID]*ipnet.UDPAddr), + state: NewNodeState(), } } @@ -40,6 +45,27 @@ func (node *LuxNode) AddExterior(udpAddr string) error { return node.router.CreateInboundChannel(net.LuxChannelExterior, udpAddr) } +func (node *LuxNode) AddNeighbor(id proto.LuxID, udpAddr string) error { + udpIp, err := ipnet.ResolveUDPAddr("udp", udpAddr) + if err != nil { + return err + } + + if node.router.HasKeyFor(id) { + // we have key for this node, so we can route + err = node.router.CreateOutboundRoute(id, net.LuxChannelInterior, udpAddr) + if err != nil { + return err + } + } + + // just add to neighbor list to let other nodes know + node.neighbors[id] = udpIp + + log.Infof("added neighbor %s at %s", id.String(), udpIp.String()) + return nil +} + func (node *LuxNode) GetState() *LuxNodeState { return &node.state }