implement neighbor list
This commit is contained in:
parent
16a95dc7d8
commit
0f3b59fda6
2 changed files with 34 additions and 3 deletions
|
|
@ -295,6 +295,11 @@ func (r *LuxRouter) GetRouteKey(route *LuxRoute) (crypto.LuxKey, bool) {
|
||||||
return crypto.LuxKey{}, false
|
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) {
|
func (r *LuxRouter) Recv() (LuxPacket, error) {
|
||||||
dgram := r.RecvDgram()
|
dgram := r.RecvDgram()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,16 @@ import (
|
||||||
"lux/net"
|
"lux/net"
|
||||||
"lux/proto"
|
"lux/proto"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
ipnet "net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LuxNode struct {
|
type LuxNode struct {
|
||||||
router net.LuxRouter
|
router net.LuxRouter
|
||||||
running bool
|
running bool
|
||||||
|
|
||||||
|
neighbors map[proto.LuxID]*ipnet.UDPAddr
|
||||||
|
|
||||||
state LuxNodeState
|
state LuxNodeState
|
||||||
stateLock sync.RWMutex
|
stateLock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
@ -20,6 +24,7 @@ func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode {
|
||||||
return LuxNode{
|
return LuxNode{
|
||||||
router: net.NewLuxRouter(nodeKey, ks),
|
router: net.NewLuxRouter(nodeKey, ks),
|
||||||
running: false,
|
running: false,
|
||||||
|
neighbors: make(map[proto.LuxID]*ipnet.UDPAddr),
|
||||||
state: NewNodeState(),
|
state: NewNodeState(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -40,6 +45,27 @@ func (node *LuxNode) AddExterior(udpAddr string) error {
|
||||||
return node.router.CreateInboundChannel(net.LuxChannelExterior, udpAddr)
|
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 {
|
func (node *LuxNode) GetState() *LuxNodeState {
|
||||||
return &node.state
|
return &node.state
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue