239 lines
8.5 KiB
Go
Raw Normal View History

2017-12-28 22:16:20 -06:00
package yggdrasil
// This thing manages search packets
// The basic idea is as follows:
// We may know a NodeID (with a mask) and want to connect
// We begin a search by initializing a list of all nodes in our DHT, sorted by closest to the destination
// We then iteratively ping nodes from the search, marking each pinged node as visited
// We add any unvisited nodes from ping responses to the search, truncating to some maximum search size
// This stops when we either run out of nodes to ping (we hit a dead end where we can't make progress without going back), or we reach the destination
// A new search packet is sent immediately after receiving a response
// A new search packet is sent periodically, once per second, in case a packet was dropped (this slowly causes the search to become parallel if the search doesn't timeout but also doesn't finish within 1 second for whatever reason)
2017-12-28 22:16:20 -06:00
// TODO?
// Some kind of max search steps, in case the node is offline, so we don't crawl through too much of the network looking for a destination that isn't there?
2018-06-12 17:50:08 -05:00
import (
"errors"
"sort"
2018-06-12 17:50:08 -05:00
"time"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
2018-06-12 17:50:08 -05:00
)
2017-12-28 22:16:20 -06:00
// This defines the time after which we time out a search (so it can restart).
const search_RETRY_TIME = 3 * time.Second
2020-02-06 18:37:58 -06:00
const search_STEP_TIME = 100 * time.Millisecond
// Information about an ongoing search.
2019-01-09 11:44:45 +02:00
// Includes the target NodeID, the bitmask to match it to an IP, and the list of nodes to visit / already visited.
2017-12-28 22:16:20 -06:00
type searchInfo struct {
searches *searches
dest crypto.NodeID
mask crypto.NodeID
time time.Time
toVisit []*dhtInfo
visited crypto.NodeID // Closest address visited so far
callback func(*sessionInfo, error)
// TODO context.Context for timeout and cancellation
send uint64 // log number of requests sent
recv uint64 // log number of responses received
2017-12-28 22:16:20 -06:00
}
// This stores a map of active searches.
2017-12-28 22:16:20 -06:00
type searches struct {
router *router
searches map[crypto.NodeID]*searchInfo
2017-12-28 22:16:20 -06:00
}
// Initializes the searches struct.
func (s *searches) init(r *router) {
s.router = r
s.searches = make(map[crypto.NodeID]*searchInfo)
2017-12-28 22:16:20 -06:00
}
2019-08-28 19:31:04 +01:00
func (s *searches) reconfigure() {
// This is where reconfiguration would go, if we had anything to do
}
// Creates a new search info, adds it to the searches struct, and returns a pointer to the info.
func (s *searches) createSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
2018-01-04 22:37:51 +00:00
info := searchInfo{
searches: s,
dest: *dest,
mask: *mask,
time: time.Now(),
callback: callback,
2018-01-04 22:37:51 +00:00
}
s.searches[*dest] = &info
return &info
2017-12-28 22:16:20 -06:00
}
////////////////////////////////////////////////////////////////////////////////
// Checks if there's an ongoing search related to a dhtRes.
// If there is, it adds the response info to the search and triggers a new search step.
// If there's no ongoing search, or we if the dhtRes finished the search (it was from the target node), then don't do anything more.
func (sinfo *searchInfo) handleDHTRes(res *dhtRes) {
var doStep bool
2020-02-06 18:37:58 -06:00
if res != nil {
sinfo.recv++
if sinfo.checkDHTRes(res) {
return // Search finished successfully
}
// Add results to the search
sinfo.addToSearch(res)
// FIXME check this elsewhere so we don't need to create a from struct
from := dhtInfo{key: res.Key, coords: res.Coords}
doStep = sinfo.visited == *from.getNodeID()
} else {
doStep = true
2020-02-06 18:37:58 -06:00
}
if doStep {
2020-02-06 18:37:58 -06:00
// Continue the search
sinfo.doSearchStep()
2018-06-01 23:34:21 -05:00
}
}
// Adds the information from a dhtRes to an ongoing search.
// Info about a node that has already been visited is not re-added to the search.
func (sinfo *searchInfo) addToSearch(res *dhtRes) {
// Get a (deduplicated) list of known nodes to check
temp := make(map[crypto.NodeID]*dhtInfo, len(sinfo.toVisit)+len(res.Infos))
for _, info := range sinfo.toVisit {
temp[*info.getNodeID()] = info
}
// Add new results to the list
2018-06-02 16:30:05 -05:00
for _, info := range res.Infos {
temp[*info.getNodeID()] = info
}
// Move list to toVisit
delete(temp, sinfo.visited)
sinfo.toVisit = sinfo.toVisit[:0]
for _, info := range temp {
2020-02-06 20:21:17 -06:00
sinfo.toVisit = append(sinfo.toVisit, info)
}
// Sort
sort.SliceStable(sinfo.toVisit, func(i, j int) bool {
// Should return true if i is closer to the destination than j
return dht_ordered(&sinfo.dest, sinfo.toVisit[i].getNodeID(), sinfo.toVisit[j].getNodeID())
})
// Remove anything too far away to be useful
2020-02-06 20:21:17 -06:00
for idx, info := range sinfo.toVisit {
if !dht_ordered(&sinfo.dest, info.getNodeID(), &sinfo.visited) {
2020-02-06 20:21:17 -06:00
sinfo.toVisit = sinfo.toVisit[:idx]
break
2018-06-01 23:34:21 -05:00
}
}
}
// If there are no nodes left toVisit, then this cleans up the search.
// Otherwise, it pops the closest node to the destination (in keyspace) off of the toVisit list and sends a dht ping.
func (sinfo *searchInfo) doSearchStep() {
if len(sinfo.toVisit) == 0 {
if time.Since(sinfo.time) > search_RETRY_TIME {
// Dead end and no response in too long, do cleanup
delete(sinfo.searches.searches, sinfo.dest)
sinfo.callback(nil, errors.New("search reached dead end"))
}
2018-06-01 23:34:21 -05:00
return
}
// Send to the next search target
2020-02-06 18:37:58 -06:00
if len(sinfo.toVisit) > 0 {
next := sinfo.toVisit[0]
sinfo.toVisit = sinfo.toVisit[1:]
2019-12-25 18:55:29 -06:00
rq := dhtReqKey{next.key, sinfo.dest}
sinfo.searches.router.dht.addCallback(&rq, sinfo.handleDHTRes)
sinfo.searches.router.dht.ping(next, &sinfo.dest)
sinfo.send++
2019-12-25 18:55:29 -06:00
}
2018-06-01 23:34:21 -05:00
}
2019-11-29 11:45:02 +02:00
// If we've recently sent a ping for this search, do nothing.
// Otherwise, doSearchStep and schedule another continueSearch to happen after search_RETRY_TIME.
func (sinfo *searchInfo) continueSearch() {
sinfo.doSearchStep()
// In case the search dies, try to spawn another thread later
// Note that this will spawn multiple parallel searches as time passes
// Any that die aren't restarted, but a new one will start later
2020-02-06 18:37:58 -06:00
time.AfterFunc(search_STEP_TIME, func() {
sinfo.searches.router.Act(nil, func() {
// FIXME this keeps the search alive forever if not for the searches map, fix that
newSearchInfo := sinfo.searches.searches[sinfo.dest]
if newSearchInfo != sinfo {
return
}
sinfo.continueSearch()
})
})
2018-06-01 23:34:21 -05:00
}
// Calls create search, and initializes the iterative search parts of the struct before returning it.
func (s *searches) newIterSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
sinfo := s.createSearch(dest, mask, callback)
sinfo.visited = s.router.dht.nodeID
loc := s.router.core.switchTable.getLocator()
sinfo.toVisit = append(sinfo.toVisit, &dhtInfo{
key: s.router.core.boxPub,
coords: loc.getCoords(),
}) // Start the search by asking ourself, useful if we're the destination
2018-06-01 23:34:21 -05:00
return sinfo
}
// Checks if a dhtRes is good (called by handleDHTRes).
// If the response is from the target, get/create a session, trigger a session ping, and return true.
// Otherwise return false.
func (sinfo *searchInfo) checkDHTRes(res *dhtRes) bool {
from := dhtInfo{key: res.Key, coords: res.Coords}
2020-02-06 20:21:17 -06:00
if *from.getNodeID() != sinfo.visited && dht_ordered(&sinfo.dest, from.getNodeID(), &sinfo.visited) {
// Closer to the destination, so update visited
sinfo.searches.router.core.log.Debugln("Updating search:", &sinfo.dest, from.getNodeID(), sinfo.send, sinfo.recv)
sinfo.visited = *from.getNodeID()
2020-02-06 18:37:58 -06:00
sinfo.time = time.Now()
}
them := from.getNodeID()
var destMasked crypto.NodeID
var themMasked crypto.NodeID
for idx := 0; idx < crypto.NodeIDLen; idx++ {
destMasked[idx] = sinfo.dest[idx] & sinfo.mask[idx]
themMasked[idx] = them[idx] & sinfo.mask[idx]
2018-06-01 23:34:21 -05:00
}
if themMasked != destMasked {
return false
}
2019-10-12 15:46:56 -05:00
finishSearch := func(sess *sessionInfo, err error) {
if sess != nil {
// FIXME (!) replay attacks could mess with coords? Give it a handle (tstamp)?
sess.Act(sinfo.searches.router, func() { sess.coords = res.Coords })
2019-10-12 15:46:56 -05:00
sess.ping(sinfo.searches.router)
}
if err != nil {
sinfo.callback(nil, err)
} else {
sinfo.callback(sess, nil)
}
// Cleanup
2020-02-06 20:21:17 -06:00
if _, isIn := sinfo.searches.searches[sinfo.dest]; isIn {
sinfo.searches.router.core.log.Debugln("Finished search:", &sinfo.dest, sinfo.send, sinfo.recv)
2020-02-06 20:21:17 -06:00
delete(sinfo.searches.searches, res.Dest)
}
2019-10-12 15:46:56 -05:00
}
2018-06-01 23:34:21 -05:00
// They match, so create a session and send a sessionRequest
2019-10-12 15:46:56 -05:00
var err error
sess, isIn := sinfo.searches.router.sessions.getByTheirPerm(&res.Key)
2018-06-01 23:34:21 -05:00
if !isIn {
2019-10-12 15:46:56 -05:00
// Don't already have a session
sess = sinfo.searches.router.sessions.createSession(&res.Key)
if sess == nil {
2019-10-12 15:46:56 -05:00
err = errors.New("session not allowed")
} else if _, isIn := sinfo.searches.router.sessions.getByTheirPerm(&res.Key); !isIn {
2018-06-01 23:34:21 -05:00
panic("This should never happen")
}
} else {
2019-10-12 15:46:56 -05:00
err = errors.New("session already exists")
2018-06-01 23:34:21 -05:00
}
2019-10-12 15:46:56 -05:00
finishSearch(sess, err)
2018-06-01 23:34:21 -05:00
return true
}