5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 21:10:29 +00:00

have dht store a pointer to router instead of core

This commit is contained in:
Arceliar 2019-08-23 20:35:54 -05:00
parent ebd806f27a
commit e7024a00e7
2 changed files with 18 additions and 18 deletions

View File

@ -65,7 +65,7 @@ type dhtReqKey struct {
// The main DHT struct. // The main DHT struct.
type dht struct { type dht struct {
core *Core router *router
reconfigure chan chan error reconfigure chan chan error
nodeID crypto.NodeID nodeID crypto.NodeID
reqs map[dhtReqKey]time.Time // Keeps track of recent outstanding requests reqs map[dhtReqKey]time.Time // Keeps track of recent outstanding requests
@ -76,8 +76,8 @@ type dht struct {
} }
// Initializes the DHT. // Initializes the DHT.
func (t *dht) init(c *Core) { func (t *dht) init(r *router) {
t.core = c t.router = r
t.reconfigure = make(chan chan error, 1) t.reconfigure = make(chan chan error, 1)
go func() { go func() {
for { for {
@ -85,7 +85,7 @@ func (t *dht) init(c *Core) {
e <- nil e <- nil
} }
}() }()
t.nodeID = *t.core.NodeID() t.nodeID = *t.router.core.NodeID()
t.callbacks = make(map[dhtReqKey][]dht_callbackInfo) t.callbacks = make(map[dhtReqKey][]dht_callbackInfo)
t.reset() t.reset()
} }
@ -190,10 +190,10 @@ func dht_ordered(first, second, third *crypto.NodeID) bool {
// Update info about the node that sent the request. // Update info about the node that sent the request.
func (t *dht) handleReq(req *dhtReq) { func (t *dht) handleReq(req *dhtReq) {
// Send them what they asked for // Send them what they asked for
loc := t.core.switchTable.getLocator() loc := t.router.core.switchTable.getLocator()
coords := loc.getCoords() coords := loc.getCoords()
res := dhtRes{ res := dhtRes{
Key: t.core.boxPub, Key: t.router.core.boxPub,
Coords: coords, Coords: coords,
Dest: req.Dest, Dest: req.Dest,
Infos: t.lookup(&req.Dest, false), Infos: t.lookup(&req.Dest, false),
@ -221,17 +221,17 @@ func (t *dht) handleReq(req *dhtReq) {
func (t *dht) sendRes(res *dhtRes, req *dhtReq) { func (t *dht) sendRes(res *dhtRes, req *dhtReq) {
// Send a reply for a dhtReq // Send a reply for a dhtReq
bs := res.encode() bs := res.encode()
shared := t.core.router.sessions.getSharedKey(&t.core.boxPriv, &req.Key) shared := t.router.sessions.getSharedKey(&t.router.core.boxPriv, &req.Key)
payload, nonce := crypto.BoxSeal(shared, bs, nil) payload, nonce := crypto.BoxSeal(shared, bs, nil)
p := wire_protoTrafficPacket{ p := wire_protoTrafficPacket{
Coords: req.Coords, Coords: req.Coords,
ToKey: req.Key, ToKey: req.Key,
FromKey: t.core.boxPub, FromKey: t.router.core.boxPub,
Nonce: *nonce, Nonce: *nonce,
Payload: payload, Payload: payload,
} }
packet := p.encode() packet := p.encode()
t.core.router.out(packet) t.router.out(packet)
} }
type dht_callbackInfo struct { type dht_callbackInfo struct {
@ -285,17 +285,17 @@ func (t *dht) handleRes(res *dhtRes) {
func (t *dht) sendReq(req *dhtReq, dest *dhtInfo) { func (t *dht) sendReq(req *dhtReq, dest *dhtInfo) {
// Send a dhtReq to the node in dhtInfo // Send a dhtReq to the node in dhtInfo
bs := req.encode() bs := req.encode()
shared := t.core.router.sessions.getSharedKey(&t.core.boxPriv, &dest.key) shared := t.router.sessions.getSharedKey(&t.router.core.boxPriv, &dest.key)
payload, nonce := crypto.BoxSeal(shared, bs, nil) payload, nonce := crypto.BoxSeal(shared, bs, nil)
p := wire_protoTrafficPacket{ p := wire_protoTrafficPacket{
Coords: dest.coords, Coords: dest.coords,
ToKey: dest.key, ToKey: dest.key,
FromKey: t.core.boxPub, FromKey: t.router.core.boxPub,
Nonce: *nonce, Nonce: *nonce,
Payload: payload, Payload: payload,
} }
packet := p.encode() packet := p.encode()
t.core.router.out(packet) t.router.out(packet)
rq := dhtReqKey{dest.key, req.Dest} rq := dhtReqKey{dest.key, req.Dest}
t.reqs[rq] = time.Now() t.reqs[rq] = time.Now()
} }
@ -306,10 +306,10 @@ func (t *dht) ping(info *dhtInfo, target *crypto.NodeID) {
if target == nil { if target == nil {
target = &t.nodeID target = &t.nodeID
} }
loc := t.core.switchTable.getLocator() loc := t.router.core.switchTable.getLocator()
coords := loc.getCoords() coords := loc.getCoords()
req := dhtReq{ req := dhtReq{
Key: t.core.boxPub, Key: t.router.core.boxPub,
Coords: coords, Coords: coords,
Dest: *target, Dest: *target,
} }
@ -384,7 +384,7 @@ func (t *dht) getImportant() []*dhtInfo {
}) })
// Keep the ones that are no further than the closest seen so far // Keep the ones that are no further than the closest seen so far
minDist := ^uint64(0) minDist := ^uint64(0)
loc := t.core.switchTable.getLocator() loc := t.router.core.switchTable.getLocator()
important := infos[:0] important := infos[:0]
for _, info := range infos { for _, info := range infos {
dist := uint64(loc.dist(info.coords)) dist := uint64(loc.dist(info.coords))
@ -413,12 +413,12 @@ func (t *dht) getImportant() []*dhtInfo {
// Returns true if this is a node we need to keep track of for the DHT to work. // Returns true if this is a node we need to keep track of for the DHT to work.
func (t *dht) isImportant(ninfo *dhtInfo) bool { func (t *dht) isImportant(ninfo *dhtInfo) bool {
if ninfo.key == t.core.boxPub { if ninfo.key == t.router.core.boxPub {
return false return false
} }
important := t.getImportant() important := t.getImportant()
// Check if ninfo is of equal or greater importance to what we already know // Check if ninfo is of equal or greater importance to what we already know
loc := t.core.switchTable.getLocator() loc := t.router.core.switchTable.getLocator()
ndist := uint64(loc.dist(ninfo.coords)) ndist := uint64(loc.dist(ninfo.coords))
minDist := ^uint64(0) minDist := ^uint64(0)
for _, info := range important { for _, info := range important {

View File

@ -73,7 +73,7 @@ func (r *router) init(core *Core) {
r.core.config.Mutex.RLock() r.core.config.Mutex.RLock()
r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy) r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy)
r.core.config.Mutex.RUnlock() r.core.config.Mutex.RUnlock()
r.dht.init(r.core) r.dht.init(r)
r.searches.init(r.core) r.searches.init(r.core)
r.sessions.init(r.core) r.sessions.init(r.core)
} }