NodeStore.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/AddressCalc.h"
  16. #include "dht/Address.h"
  17. #include "dht/dhtcore/Node.h"
  18. #include "dht/dhtcore/NodeStore.h"
  19. #include "dht/dhtcore/NodeList.h"
  20. #include "util/AddrTools.h"
  21. #include "util/Assert.h"
  22. #include "util/Bits.h"
  23. #include "util/log/Log.h"
  24. #include "util/version/Version.h"
  25. #include "switch/NumberCompress.h"
  26. #include "switch/LabelSplicer.h"
  27. #include "util/Gcc.h"
  28. #include <stdbool.h>
  29. #include <inttypes.h>
  30. #include <uv-private/tree.h>
  31. /** A list of DHT nodes. */
  32. struct NodeStore_pvt
  33. {
  34. struct NodeStore pub;
  35. /** A fake link where we are both the parent and child. */
  36. struct Node_Link* selfLink;
  37. /** A tree containing all nodes ordered by ipv6 */
  38. struct NodeRBTree {
  39. struct Node_Two* rbh_root;
  40. } nodeTree;
  41. struct Allocator* alloc;
  42. /** The maximum number of nodes which can be allocated. TODO: make use of */
  43. int capacity;
  44. /** The means for this node store to log. */
  45. struct Log* logger;
  46. Identity
  47. };
  48. // My memory is really bad
  49. #define A_COMES_FIRST 1
  50. #define B_COMES_FIRST -1
  51. static int comparePeers(const struct Node_Link* la, const struct Node_Link* lb)
  52. {
  53. Identity_check(lb);
  54. uint64_t a = la->cannonicalLabel;
  55. uint64_t b = lb->cannonicalLabel;
  56. if (a == b) {
  57. return 0;
  58. }
  59. int log2Diff = Bits_log2x64(b) - Bits_log2x64(a);
  60. if (log2Diff) {
  61. return log2Diff;
  62. }
  63. if (Bits_bitReverse64(a) < Bits_bitReverse64(b)) {
  64. return A_COMES_FIRST;
  65. }
  66. return B_COMES_FIRST;
  67. }
  68. RB_GENERATE_STATIC(PeerRBTree, Node_Link, peerTree, comparePeers)
  69. static int compareNodes(const struct Node_Two* na, const struct Node_Two* nb)
  70. {
  71. Identity_check(nb);
  72. int ret;
  73. ret = Address_xorcmp(0, na->address.ip6.ints.one_be, nb->address.ip6.ints.one_be);
  74. if (ret) { return ret; }
  75. ret = Address_xorcmp(0, na->address.ip6.ints.two_be, nb->address.ip6.ints.two_be);
  76. if (ret) { return ret; }
  77. ret = Address_xorcmp(0, na->address.ip6.ints.three_be, nb->address.ip6.ints.three_be);
  78. if (ret) { return ret; }
  79. ret = Address_xorcmp(0, na->address.ip6.ints.four_be, nb->address.ip6.ints.four_be);
  80. return ret;
  81. }
  82. RB_GENERATE_STATIC(NodeRBTree, Node_Two, nodeTree, compareNodes)
  83. static void freeLink(struct Node_Link* link, struct NodeStore_pvt* store)
  84. {
  85. Allocator_realloc(store->alloc, link, 0);
  86. }
  87. static struct Node_Link* getLink(struct NodeStore_pvt* store)
  88. {
  89. return Allocator_calloc(store->alloc, sizeof(struct Node_Link), 1);
  90. }
  91. static void logLink(struct NodeStore_pvt* store,
  92. struct Node_Link* link,
  93. char* message)
  94. {
  95. #ifndef Log_DEBUG
  96. return;
  97. #endif
  98. uint8_t parent[40];
  99. uint8_t child[40];
  100. AddrTools_printIp(parent, link->parent->address.ip6.bytes);
  101. AddrTools_printIp(child, link->child->address.ip6.bytes);
  102. uint8_t path[20];
  103. AddrTools_printPath(path, link->cannonicalLabel);
  104. Log_debug(store->logger, "link[%s]->[%s] [%s] %s", parent, child, path, message);
  105. }
  106. /*
  107. static void _assertNoLoop(struct Node_Two* node, struct NodeStore_pvt* store, char* file, int line)
  108. {
  109. //Log_debug(store->logger, "Beginning check for loops");
  110. struct Node_Link* parent = node->bestParent;
  111. for (int i = 0; i < 1000; i++) {
  112. if (!node->bestParent) { return; }
  113. if (store->pub.selfNode == parent->parent) { return; }
  114. //logLink(store, parent, "Checking for loops");
  115. Assert_fileLine(node != parent->parent, file, line);
  116. parent = parent->parent->bestParent;
  117. }
  118. // loop higher up the chain...
  119. _assertNoLoop(parent->child, store, file, line);
  120. }
  121. #define assertNoLoop(node, store) _assertNoLoop(node, store, Gcc_SHORT_FILE, Gcc_LINE)
  122. */
  123. static void _verifyNode(struct Node_Two* node, struct NodeStore_pvt* store, char* file, int line)
  124. {
  125. #ifndef PARANOIA
  126. return;
  127. #endif
  128. Assert_true(node->address.path ==
  129. EncodingScheme_convertLabel(store->pub.selfNode->encodingScheme,
  130. node->address.path,
  131. EncodingScheme_convertLabel_convertTo_CANNONICAL));
  132. struct Node_Link* link;
  133. for (link = node->reversePeers; link; link = link->nextPeer) {
  134. Assert_fileLine(link->child == node, file, line);
  135. Assert_fileLine(RB_FIND(PeerRBTree, &link->parent->peerTree, link), file, line);
  136. }
  137. struct Node_Link* lastLink = NULL;
  138. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  139. Assert_fileLine(node->bestParent || link->child->bestParent != link, file, line);
  140. Assert_fileLine(link->linkAddr == (uintptr_t)link, file, line);
  141. Assert_fileLine(link->parent == node, file, line);
  142. Assert_fileLine(!lastLink || link->cannonicalLabel != lastLink->cannonicalLabel,
  143. file, line);
  144. struct Node_Link* rlink = NULL;
  145. for (rlink = link->child->reversePeers; rlink; rlink = rlink->nextPeer) {
  146. if (rlink->parent == node) {
  147. break;
  148. }
  149. }
  150. Assert_fileLine(rlink, file, line);
  151. lastLink = link;
  152. }
  153. if (node->bestParent) {
  154. Assert_fileLine(node->bestParent->parent->pathQuality > node->pathQuality
  155. || node == store->pub.selfNode, file, line);
  156. Assert_fileLine(node->address.path != UINT64_MAX, file, line);
  157. Assert_fileLine(node->pathQuality != 0, file, line);
  158. } else {
  159. Assert_fileLine(node->address.path == UINT64_MAX, file, line);
  160. Assert_fileLine(node->pathQuality == 0, file, line);
  161. }
  162. }
  163. #define verifyNode(node, store) _verifyNode(node, store, Gcc_SHORT_FILE, Gcc_LINE)
  164. static void _check(struct NodeStore_pvt* store, char* file, int line)
  165. {
  166. Assert_true(store->pub.selfNode->bestParent == store->selfLink || !store->selfLink);
  167. struct Node_Two* nn = NULL;
  168. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  169. _verifyNode(nn, store, file, line);
  170. }
  171. }
  172. #define check(store) _check(store, Gcc_SHORT_FILE, Gcc_LINE)
  173. /**
  174. * Extend a route by splicing on another link.
  175. * This will modify the Encoding Form of the first Director in next section of the route to make
  176. * it's size greater than or equal to the size of the return route through the parent node in the
  177. * link.
  178. *
  179. * @param routeLabel the label for reaching the parent node
  180. * @param link the link to extend the route with
  181. * @param previousLinkEncoding the encoding used for the parent's interface back to the grandparent
  182. */
  183. static uint64_t extendRoute(uint64_t routeLabel,
  184. struct EncodingScheme* parentScheme,
  185. uint64_t parentChildLabel,
  186. int previousLinkEncoding)
  187. {
  188. uint64_t next = parentChildLabel;
  189. int nextLinkEncoding = EncodingScheme_getFormNum(parentScheme, next);
  190. if (previousLinkEncoding > nextLinkEncoding) {
  191. next = EncodingScheme_convertLabel(parentScheme, next, previousLinkEncoding);
  192. }
  193. Assert_true(next != EncodingScheme_convertLabel_INVALID);
  194. return LabelSplicer_splice(next, routeLabel);
  195. }
  196. static void unreachable(struct Node_Two* node, struct NodeStore_pvt* store)
  197. {
  198. struct Node_Link* next = NULL;
  199. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  200. if (next->child->bestParent == next) { unreachable(next->child, store); }
  201. }
  202. node->bestParent = NULL;
  203. node->address.path = UINT64_MAX;
  204. node->pathQuality = 0;
  205. }
  206. static int updateBestPathCycle(struct Node_Two* node,
  207. int cycle,
  208. int limit,
  209. struct NodeStore_pvt* store)
  210. {
  211. Assert_always(cycle < 1000);
  212. if (cycle < limit) {
  213. int total = 0;
  214. struct Node_Link* next = NULL;
  215. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  216. if (next->child->bestParent == next && next->child != node) {
  217. total += updateBestPathCycle(next->child, cycle+1, limit, store);
  218. }
  219. }
  220. return total;
  221. }
  222. struct Node_Link* newBestLink = node->bestParent;
  223. struct Node_Two* newBest = newBestLink->parent;
  224. uint64_t bestPath = extendRoute(newBest->address.path,
  225. newBest->encodingScheme,
  226. newBestLink->cannonicalLabel,
  227. newBest->bestParent->inverseLinkEncodingFormNumber);
  228. if (bestPath == UINT64_MAX) {
  229. unreachable(node, store);
  230. return 1;
  231. }
  232. #ifdef Log_DEBUG
  233. if (node->address.path != bestPath) {
  234. uint8_t pathStr[20];
  235. AddrTools_printPath(pathStr, bestPath);
  236. uint8_t addrStr[40];
  237. AddrTools_printIp(addrStr, node->address.ip6.bytes);
  238. Log_debug(store->logger, "New best path [%s@%s]", addrStr, pathStr);
  239. }
  240. #endif
  241. node->address.path = bestPath;
  242. return 1;
  243. }
  244. static void updateBestPath(struct Node_Two* node, struct NodeStore_pvt* store)
  245. {
  246. for (int i = 0; i < 10000; i++) {
  247. if (!updateBestPathCycle(node, 0, i, store)) {
  248. check(store);
  249. return;
  250. }
  251. check(store);
  252. }
  253. Assert_true(0);
  254. }
  255. /**
  256. * This is called when we have no idea what the reach should be for the next hop
  257. * because the path we previously used to get to it is broken and we need to use
  258. * a different one. Take a somewhat educated guess as to what it might be in a way
  259. * that will make the reach non-zero.
  260. */
  261. static uint32_t guessReachOfChild(struct Node_Link* link)
  262. {
  263. // return 3/4 of the parent's reach if it's 1 hop, 1/2 otherwise.
  264. uint32_t r = link->parent->pathQuality / 2;
  265. if (r < (1<<12)) {
  266. r = link->parent->pathQuality - 1;
  267. } else if (r < (1<<16)) {
  268. r = link->parent->pathQuality - Bits_log2x64(link->cannonicalLabel);
  269. }
  270. Assert_true(r < link->parent->pathQuality && r != 0);
  271. return r;
  272. }
  273. static void handleGoodNews(struct Node_Two* node,
  274. uint32_t newReach,
  275. struct NodeStore_pvt* store)
  276. {
  277. // TODO: Paths longer than 1024 will blow up, handle more gracefully
  278. Assert_always(newReach != UINT32_MAX);
  279. Assert_true(newReach > node->pathQuality);
  280. // The nodestore thinks it's unreachable, we can't very well update the reach.
  281. if (node->bestParent == NULL) { return; }
  282. if (newReach+1 > node->bestParent->parent->pathQuality) {
  283. handleGoodNews(node->bestParent->parent, newReach+1, store);
  284. }
  285. node->pathQuality = newReach;
  286. struct Node_Link* link = NULL;
  287. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  288. struct Node_Two* child = link->child;
  289. if (!child->bestParent || child->bestParent->parent->pathQuality < newReach) {
  290. uint32_t nextReach = guessReachOfChild(link);
  291. if (child->pathQuality > nextReach) { continue; }
  292. child->pathQuality = nextReach;
  293. child->bestParent = link;
  294. updateBestPath(child, store);
  295. }
  296. }
  297. }
  298. /**
  299. * The news has hit (in handleBadNewsOne) and now all of the nodes in the affected zone have
  300. * been knocked down. Now lets see if there's a better path for any of them.
  301. */
  302. static void handleBadNewsTwo(struct Node_Link* link, struct NodeStore_pvt* store)
  303. {
  304. struct Node_Link* next = NULL;
  305. RB_FOREACH_REVERSE(next, PeerRBTree, &link->child->peerTree) {
  306. if (!next) { continue; }
  307. if (next->child->bestParent != next) { continue; }
  308. if (next == store->selfLink) { continue; }
  309. handleBadNewsTwo(next, store);
  310. }
  311. // node was relinked by a recursion of this function.
  312. if (link->child->bestParent != link) { return; }
  313. struct Node_Two* node = link->child;
  314. struct Node_Link* rp = link->child->reversePeers;
  315. struct Node_Link* best = node->bestParent;
  316. while (rp) {
  317. if (rp->parent->pathQuality >= best->parent->pathQuality) {
  318. if (rp->parent->pathQuality > best->parent->pathQuality
  319. || rp->parent->address.path < best->parent->address.path)
  320. {
  321. best = rp;
  322. }
  323. }
  324. rp = rp->nextPeer;
  325. }
  326. if (best == node->bestParent) { return; }
  327. uint32_t nextReach = guessReachOfChild(best);
  328. if (nextReach <= node->pathQuality) { return; }
  329. Assert_true(node->pathQuality < best->parent->pathQuality);
  330. check(store);
  331. node->bestParent = best;
  332. check(store);
  333. handleGoodNews(node, nextReach, store);
  334. check(store);
  335. updateBestPath(node, store);
  336. check(store);
  337. }
  338. /**
  339. * First thing we do is knock down everybody's reach.
  340. * This way they don't all cling to eachother for safety making
  341. * endless routing loops and stupid processing.
  342. */
  343. static uint32_t handleBadNewsOne(struct Node_Link* link,
  344. uint32_t newReach,
  345. struct NodeStore_pvt* store)
  346. {
  347. struct Node_Link* next = NULL;
  348. uint32_t highestRet = 0;
  349. RB_FOREACH_REVERSE(next, PeerRBTree, &link->child->peerTree) {
  350. if (next->child->bestParent != next) { continue; }
  351. if (next == store->selfLink) { continue; }
  352. if (next->child->pathQuality < newReach) { continue; }
  353. uint32_t ret = handleBadNewsOne(next, newReach, store);
  354. if (ret > highestRet) { highestRet = ret; }
  355. }
  356. if (highestRet > newReach) { newReach = highestRet; }
  357. if (highestRet == 0) { highestRet = newReach; }
  358. Assert_true(link->child != store->pub.selfNode);
  359. if (!highestRet) {
  360. unreachable(link->child, store);
  361. } else {
  362. link->child->pathQuality = highestRet;
  363. }
  364. return highestRet+1;
  365. }
  366. static void handleBadNews(struct Node_Two* node,
  367. uint32_t newReach,
  368. struct NodeStore_pvt* store)
  369. {
  370. Assert_true(newReach < node->pathQuality);
  371. // no bestParent implies a reach of 0
  372. Assert_true(node->bestParent);
  373. Assert_true(node->bestParent != store->selfLink);
  374. // might be destroyed by handleBadNewsOne()
  375. struct Node_Link* bp = node->bestParent;
  376. handleBadNewsOne(node->bestParent, newReach, store);
  377. // If our bad news actually improved the reach number for the node (because it was previously
  378. // 0 and that node has children) then we need to handle it as good news as well.
  379. if (node->bestParent) {
  380. if (node->pathQuality >= node->bestParent->parent->pathQuality) {
  381. handleGoodNews(node->bestParent->parent, node->pathQuality+1, store);
  382. }
  383. Assert_true(node->pathQuality < node->bestParent->parent->pathQuality);
  384. }
  385. check(store);
  386. handleBadNewsTwo(bp, store);
  387. check(store);
  388. }
  389. static void handleNews(struct Node_Two* node, uint32_t newReach, struct NodeStore_pvt* store)
  390. {
  391. // This is because reach is used to prevent loops so it must be 1 more for each hop closer
  392. // to the root.
  393. if (newReach > (UINT32_MAX - 1024)) { newReach = (UINT32_MAX - 1024); }
  394. check(store);
  395. if (newReach < node->pathQuality) {
  396. handleBadNews(node, newReach, store);
  397. check(store);
  398. }
  399. if (newReach > node->pathQuality) {
  400. handleGoodNews(node, newReach, store);
  401. check(store);
  402. }
  403. }
  404. static void unlinkNodes(struct Node_Link* link, struct NodeStore_pvt* store)
  405. {
  406. struct Node_Two* child = Identity_check(link->child);
  407. struct Node_Two* parent = Identity_check(link->parent);
  408. check(store);
  409. // Remove the entry from the reversePeers
  410. struct Node_Link* current = child->reversePeers;
  411. struct Node_Link** currentP = &child->reversePeers;
  412. while (current) {
  413. if (current == link) {
  414. *currentP = current->nextPeer;
  415. break;
  416. }
  417. currentP = &(current->nextPeer);
  418. current = *currentP;
  419. }
  420. Assert_true(current);
  421. // Change the best parent and path if necessary
  422. if (child->bestParent == link) {
  423. handleBadNews(child, 0, store);
  424. }
  425. if (child->bestParent == link) {
  426. unreachable(child, store);
  427. }
  428. // Remove the RBTree entry
  429. Assert_ifParanoid(link == RB_FIND(PeerRBTree, &parent->peerTree, link));
  430. RB_REMOVE(PeerRBTree, &parent->peerTree, link);
  431. freeLink(link, store);
  432. check(store);
  433. }
  434. static void update(struct Node_Link* link,
  435. int64_t linkStateDiff,
  436. struct NodeStore_pvt* store)
  437. {
  438. /** TODO: Link state is not taken into account yet
  439. if (linkStateDiff + link->linkState > UINT32_MAX) {
  440. link->linkState = UINT32_MAX;
  441. logLink(store, link, "link state set to maximum");
  442. } else if (linkStateDiff + link->linkState < 0) {
  443. link->linkState = UINT32_MAX;
  444. logLink(store, link, "link state set to zero");
  445. } else {
  446. link->linkState += linkStateDiff;
  447. }
  448. */
  449. }
  450. /**
  451. * Link two nodes in the graph together.
  452. * If a parent of the child node is also a parent of the parent node, they are
  453. * unlinked (the link is split and the child is inserted in the middle).
  454. *
  455. * @param parent the current end of the graph
  456. * @param child the new node to extend the graph
  457. * @param cannonicalLabel the label for getting from the parent to the child.
  458. * @param linkStateDiff how much to change the link state for this link.
  459. * @param store
  460. */
  461. static struct Node_Link* linkNodes(struct Node_Two* parent,
  462. struct Node_Two* child,
  463. uint64_t cannonicalLabel,
  464. int64_t linkStateDiff,
  465. int inverseLinkEncodingFormNumber,
  466. uint64_t discoveredPath,
  467. struct NodeStore_pvt* store)
  468. {
  469. check(store);
  470. #ifdef Log_DEBUG
  471. uint8_t parentIp[40];
  472. uint8_t childIp[40];
  473. AddrTools_printIp(parentIp, parent->address.ip6.bytes);
  474. AddrTools_printIp(childIp, child->address.ip6.bytes);
  475. uint8_t printedLabel[20];
  476. AddrTools_printPath(printedLabel, cannonicalLabel);
  477. Log_debug(store->logger, "Linking [%s] with [%s] with label fragment [%s]",
  478. parentIp, childIp, printedLabel);
  479. #endif
  480. // It's ok to link a node with itself via some loopey route.
  481. // in practice it should never actually be used and it might yield some interesting
  482. // information when the link is split, self-routes are not allowed unless the self
  483. // link is being set up :)
  484. Assert_true(cannonicalLabel != 1 || store->selfLink == NULL);
  485. #ifdef PARANOIA
  486. uint64_t definitelyCannonical =
  487. EncodingScheme_convertLabel(parent->encodingScheme,
  488. cannonicalLabel,
  489. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  490. Assert_true(definitelyCannonical == cannonicalLabel);
  491. #endif
  492. struct Node_Link* link;
  493. RB_FOREACH_REVERSE(link, PeerRBTree, &parent->peerTree) {
  494. Identity_check(link);
  495. if (link->child == child) {
  496. if (link->cannonicalLabel != cannonicalLabel) {
  497. // multiple paths between A and B are ok because they
  498. // will have divergent paths following the first director.
  499. continue;
  500. } else if (link->inverseLinkEncodingFormNumber != inverseLinkEncodingFormNumber) {
  501. logLink(store, link, "Relinking nodes with different encoding form");
  502. // This can happen when C renumbers but B->C is the same because B did
  503. // not renumber, EG: if C restarts.
  504. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  505. }
  506. update(link, linkStateDiff, store);
  507. return link;
  508. }
  509. }
  510. struct Node_Link dummy = { .cannonicalLabel = cannonicalLabel };
  511. link = Identity_ncheck(RB_FIND(PeerRBTree, &parent->peerTree, &dummy));
  512. if (link) {
  513. logLink(store, link, "Attempted to create alternate link with same label!");
  514. Assert_true(0);
  515. return link;
  516. }
  517. link = getLink(store);
  518. // set it up
  519. link->cannonicalLabel = cannonicalLabel;
  520. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  521. link->child = child;
  522. link->parent = parent;
  523. link->linkAddr = (uintptr_t)link;
  524. link->discoveredPath = discoveredPath;
  525. Identity_set(link);
  526. // reverse link
  527. link->nextPeer = child->reversePeers;
  528. child->reversePeers = link;
  529. // forward link
  530. Assert_ifParanoid(!RB_FIND(PeerRBTree, &parent->peerTree, link));
  531. RB_INSERT(PeerRBTree, &parent->peerTree, link);
  532. if (!child->bestParent) {
  533. if (parent->bestParent) {
  534. child->bestParent = link;
  535. child->pathQuality = guessReachOfChild(link);
  536. updateBestPath(child, store);
  537. } else {
  538. unreachable(child, store);
  539. }
  540. }
  541. // update the child's link state and possibly change it's preferred path
  542. update(link, linkStateDiff, store);
  543. check(store);
  544. return link;
  545. }
  546. /**
  547. * Find the closest node to the given path.
  548. * Pay especially close attention to the comments in this function, they're critical to
  549. * understanting what it actually does.
  550. *
  551. * @param path the path to the node which we want the closest node to.
  552. * @param output a pointer to be set to the link to the closest node.
  553. * @param hops a pointer to an integer which is initially the limit on the number of allowed hops.
  554. * If there are more than this number of hope in the label, the search will terminate
  555. * early. At the end this will be set to the actual number of hops until the find.
  556. * @param store
  557. * @return the label fragment linking outputNode with the given path.
  558. */
  559. #define findClosest_INVALID (~((uint64_t)0))
  560. static uint64_t findClosest(const uint64_t path,
  561. struct Node_Link** output,
  562. uint32_t* hops,
  563. struct NodeStore_pvt* store)
  564. {
  565. struct Node_Link tmpl = {
  566. // The path from us is always cannonical
  567. .cannonicalLabel = path
  568. };
  569. struct Node_Link* nextLink;
  570. struct Node_Link* link = store->selfLink;
  571. uint32_t actualHops = 0;
  572. for (; !hops || actualHops < *hops; actualHops++) {
  573. // First we splice off the parent's Director leaving the child's Director.
  574. tmpl.cannonicalLabel = LabelSplicer_unsplice(tmpl.cannonicalLabel, link->cannonicalLabel);
  575. // Then we cannoicalize the child's Director
  576. if (link != store->selfLink) {
  577. int formNum =
  578. EncodingScheme_getFormNum(link->child->encodingScheme, tmpl.cannonicalLabel);
  579. // Check that they didn't send us an obviously invalid route.
  580. if (formNum < link->inverseLinkEncodingFormNumber) {
  581. Assert_ifTesting(!"invalid route");
  582. Log_info(store->logger, "Invalid route");
  583. return findClosest_INVALID;
  584. }
  585. uint64_t cannonical =
  586. EncodingScheme_convertLabel(link->child->encodingScheme,
  587. tmpl.cannonicalLabel,
  588. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  589. // Check that they didn't waste space by sending an oversize encoding form.
  590. if (formNum > link->inverseLinkEncodingFormNumber
  591. && cannonical != tmpl.cannonicalLabel)
  592. {
  593. Assert_ifTesting(!"wasting space");
  594. Log_info(store->logger, "Wasted space");
  595. //return findClosest_INVALID;
  596. }
  597. tmpl.cannonicalLabel = cannonical;
  598. }
  599. Assert_true(tmpl.cannonicalLabel != EncodingScheme_convertLabel_INVALID);
  600. // Then we search for the next peer in the path
  601. nextLink = Identity_ncheck(RB_NFIND(PeerRBTree, &link->child->peerTree, &tmpl));
  602. while (nextLink
  603. && !LabelSplicer_routesThrough(tmpl.cannonicalLabel, nextLink->cannonicalLabel))
  604. {
  605. //logLink(store, nextLink, "GETTING NEXT LINK");
  606. nextLink = Identity_ncheck(RB_NEXT(PeerRBTree, NULL, nextLink));
  607. }
  608. if (!nextLink || nextLink == store->selfLink) {
  609. // ignore the comments, they're mostly wrong anyway
  610. break;
  611. }
  612. Identity_check(nextLink);
  613. Assert_true(nextLink->child->encodingScheme);
  614. if (tmpl.cannonicalLabel == nextLink->cannonicalLabel) {
  615. //logLink(store, nextLink, "Exact match");
  616. tmpl.cannonicalLabel = 1;
  617. *output = nextLink;
  618. if (hops) { *hops = actualHops; }
  619. return 1;
  620. }
  621. if (!LabelSplicer_routesThrough(tmpl.cannonicalLabel, nextLink->cannonicalLabel)) {
  622. // child of next link is not in the path, we reached the end.
  623. break;
  624. }
  625. /*#ifdef Log_DEBUG
  626. uint8_t labelA[20];
  627. uint8_t labelB[20];
  628. uint8_t searchingFor[20];
  629. AddrTools_printPath(labelA, tmpl.cannonicalLabel);
  630. AddrTools_printPath(searchingFor, path);
  631. AddrTools_printPath(labelB, link->cannonicalLabel);
  632. Log_debug(store->logger, "[%s] is behind [%s] searching for [%s]",
  633. labelA, labelB, searchingFor);
  634. #endif*/
  635. link = nextLink;
  636. }
  637. /*#ifdef Log_DEBUG
  638. uint8_t labelA[20];
  639. uint8_t labelB[20] = "NONE";
  640. uint8_t labelC[20];
  641. AddrTools_printPath(labelA, tmpl.cannonicalLabel);
  642. if (nextLink) {
  643. AddrTools_printPath(labelB, nextLink->cannonicalLabel);
  644. }
  645. AddrTools_printPath(labelC, link->cannonicalLabel);
  646. Log_debug(store->logger, "[%s] is not behind [%s] closest: [%s]", labelA, labelB, labelC);
  647. #endif*/
  648. Assert_true(tmpl.cannonicalLabel);/// TODO remove this
  649. *output = link;
  650. if (hops) { *hops = actualHops; }
  651. return tmpl.cannonicalLabel;
  652. }
  653. static struct Node_Two* nodeForIp(struct NodeStore_pvt* store, uint8_t ip[16])
  654. {
  655. struct Node_Two fakeNode;
  656. Identity_set(&fakeNode);
  657. Bits_memcpyConst(fakeNode.address.ip6.bytes, ip, 16);
  658. return Identity_ncheck(RB_FIND(NodeRBTree, &store->nodeTree, &fakeNode));
  659. }
  660. static struct Node_Link* discoverLink(struct NodeStore_pvt* store,
  661. struct Node_Link* closest,
  662. uint64_t pathParentChild,
  663. struct Node_Two* child,
  664. uint64_t discoveredPath,
  665. int inverseLinkEncodingFormNumber)
  666. {
  667. #ifdef Log_DEBUG
  668. uint8_t printedAddr[60];
  669. Address_print(printedAddr, &child->address);
  670. Log_debug(store->logger, "discoverLink(%s)", printedAddr);
  671. #endif
  672. struct Node_Two* parent = closest->child;
  673. // link parent to child
  674. // TODO: linking every node with 0 link state, this can't be right.
  675. struct Node_Link* parentLink = linkNodes(parent,
  676. child,
  677. pathParentChild,
  678. 0,
  679. inverseLinkEncodingFormNumber,
  680. discoveredPath,
  681. store);
  682. if (!RB_FIND(NodeRBTree, &store->nodeTree, child)) {
  683. verifyNode(child, store);
  684. RB_INSERT(NodeRBTree, &store->nodeTree, child);
  685. }
  686. check(store);
  687. // Check whether the parent is already linked with a node which is "behind" the child.
  688. // previous appears to be a "sibling link" to the closest->node link but in reality the
  689. // previous link should be split and node should be inserted in the middle.
  690. struct Node_Link* splitLink = RB_MIN(PeerRBTree, &parent->peerTree);
  691. while (splitLink) {
  692. if (splitLink->cannonicalLabel <= pathParentChild) {
  693. if (splitLink->cannonicalLabel == pathParentChild) {
  694. Assert_true(splitLink->child == child);
  695. splitLink = PeerRBTree_RB_NEXT(splitLink);
  696. continue;
  697. } else {
  698. // Since they're in order, definitely not found.
  699. break;
  700. }
  701. }
  702. if (!LabelSplicer_routesThrough(splitLink->cannonicalLabel, pathParentChild)) {
  703. splitLink = PeerRBTree_RB_NEXT(splitLink);
  704. continue;
  705. }
  706. struct Node_Two* grandChild = splitLink->child;
  707. // unsplice and cannonicalize so we now have a path from child to grandchild
  708. uint64_t childToGrandchild =
  709. LabelSplicer_unsplice(splitLink->cannonicalLabel, pathParentChild);
  710. childToGrandchild =
  711. EncodingScheme_convertLabel(child->encodingScheme,
  712. childToGrandchild,
  713. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  714. // just so we're on the same page here
  715. Assert_true(splitLink->parent == parent);
  716. Assert_true(childToGrandchild < UINT64_MAX);
  717. #ifdef Log_DEBUG
  718. {
  719. uint8_t parentStr[40];
  720. uint8_t childStr[40];
  721. uint8_t pathStr[20];
  722. AddrTools_printIp(parentStr, splitLink->parent->address.ip6.bytes);
  723. AddrTools_printIp(childStr, splitLink->child->address.ip6.bytes);
  724. AddrTools_printPath(pathStr, splitLink->cannonicalLabel);
  725. Log_debug(store->logger, "Splitting link [%s]->[%s] [%s]",
  726. parentStr, childStr, pathStr);
  727. AddrTools_printIp(parentStr, splitLink->parent->address.ip6.bytes);
  728. AddrTools_printIp(childStr, child->address.ip6.bytes);
  729. AddrTools_printPath(pathStr, pathParentChild);
  730. Log_debug(store->logger, "New parent [%s]->[%s] [%s]", parentStr, childStr, pathStr);
  731. AddrTools_printIp(parentStr, child->address.ip6.bytes);
  732. AddrTools_printIp(childStr, splitLink->child->address.ip6.bytes);
  733. AddrTools_printPath(pathStr, childToGrandchild);
  734. Log_debug(store->logger, "New child [%s]->[%s] [%s]", parentStr, childStr, pathStr);
  735. }
  736. #endif
  737. if (grandChild->bestParent == splitLink && child->pathQuality <= grandChild->pathQuality) {
  738. // We know that the grandchild decends from the parent because splitLink is parent-->gc
  739. // Two possibilities:
  740. // someRoute-->child-->parent
  741. // someRoute-->parent-->child
  742. check(store);
  743. if (parent->pathQuality >= child->pathQuality) {
  744. // Parent definitely does not decend from child.
  745. Assert_true(grandChild->pathQuality < UINT32_MAX);
  746. child->bestParent = parentLink;
  747. } else {
  748. // Parent may decend from child, if it does we cannot safely re-root child
  749. // if not then we could but if we believe the reach of the child is better,
  750. // we might as well use the route which goes via the child rather than re-rooting
  751. // it anyway.
  752. }
  753. handleGoodNews(child, grandChild->pathQuality+1, store);
  754. check(store);
  755. }
  756. struct Node_Link* lcg = NULL;
  757. if (child == grandChild) {
  758. // There's an existing link from the parent to the child and it loops
  759. // it takes a detour over to some other nodes and then comes back to the grandChild
  760. Log_debug(store->logger, "replace existing link which contains a loop...");
  761. } else {
  762. Assert_true(splitLink->cannonicalLabel != pathParentChild);
  763. Assert_true(childToGrandchild != 1);
  764. lcg = discoverLink(store,
  765. parentLink,
  766. childToGrandchild,
  767. grandChild,
  768. discoveredPath,
  769. splitLink->inverseLinkEncodingFormNumber);
  770. Assert_true(lcg->child == grandChild);
  771. //lcg = linkNodes(child, grandChild, childToGrandchild, splitLink->linkState,
  772. // splitLink->inverseLinkEncodingFormNumber, addr->path, store);
  773. }
  774. if (grandChild->bestParent == splitLink) {
  775. Assert_true(grandChild->bestParent->parent->pathQuality > grandChild->pathQuality);
  776. check(store);
  777. grandChild->bestParent = (lcg) ? lcg : parentLink;
  778. Assert_true(grandChild->bestParent->parent->pathQuality > grandChild->pathQuality);
  779. check(store);
  780. updateBestPath(grandChild, store);
  781. check(store);
  782. }
  783. struct Node_Link* unlinkMe = splitLink;
  784. splitLink = PeerRBTree_RB_NEXT(splitLink);
  785. unlinkNodes(unlinkMe, store);
  786. }
  787. check(store);
  788. return parentLink;
  789. }
  790. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  791. struct Address* addr,
  792. struct EncodingScheme* scheme,
  793. int inverseLinkEncodingFormNumber,
  794. uint32_t reach)
  795. {
  796. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  797. check(store);
  798. struct Node_Two* child = nodeForIp(store, addr->ip6.bytes);
  799. #ifdef Log_DEBUG
  800. uint8_t printedAddr[60];
  801. Address_print(printedAddr, addr);
  802. Log_debug(store->logger, "Discover node [%s]", printedAddr);
  803. #endif
  804. struct Allocator* alloc = NULL;
  805. if (!child) {
  806. alloc = Allocator_child(store->alloc);
  807. child = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  808. child->alloc = alloc;
  809. Bits_memcpyConst(&child->address, addr, sizeof(struct Address));
  810. child->encodingScheme = EncodingScheme_clone(scheme, child->alloc);
  811. Identity_set(child);
  812. }
  813. Assert_true(child->address.protocolVersion);
  814. Assert_true(EncodingScheme_equals(scheme, child->encodingScheme));//TODO
  815. struct Node_Link* closest = NULL;
  816. uint64_t pathParentChild = findClosest(addr->path, &closest, NULL, store);
  817. if (pathParentChild == findClosest_INVALID) {
  818. if (alloc) {
  819. Allocator_free(alloc);
  820. }
  821. check(store);
  822. Assert_true(0);
  823. Log_debug(store->logger, "Invalid path");
  824. return NULL;
  825. }
  826. struct Node_Two* parent = closest->child;
  827. for (;;) {
  828. if (parent == child) {
  829. if (pathParentChild == 1) {
  830. // Link is already known.
  831. update(closest, 0, store);
  832. //Log_debug(store->logger, "Already known");
  833. return closest;
  834. } else {
  835. logLink(store, closest, "Loopey route");
  836. // parent->child->somenode->child
  837. // we'll link them and then hope the link is never used, when it's split
  838. // we'll find a nice link to somenode.
  839. break;
  840. }
  841. } else if (pathParentChild == 1) {
  842. logLink(store, closest, "Node at end of path appears to have changed");
  843. // This is disabled because RouterModule really wants this node to exist before talking
  844. // to it.
  845. /*if (closest->discoveredPath < addr->path) {
  846. // Minor defense against being lied to, trust the shortest path.
  847. // TODO: send a ping to check if it's still correct?
  848. Log_info(store->logger, "Not replacing link because discovery path is longer");
  849. if (alloc) {
  850. Allocator_free(alloc);
  851. }
  852. check(store);
  853. Log_debug(store->logger, "Better path already known");
  854. return NULL;
  855. }*/
  856. unlinkNodes(closest, store);
  857. pathParentChild = findClosest(addr->path, &closest, NULL, store);
  858. Assert_always(pathParentChild != findClosest_INVALID);
  859. parent = closest->child;
  860. check(store);
  861. } else {
  862. break;
  863. }
  864. }
  865. struct Node_Link* link = discoverLink(store,
  866. closest,
  867. pathParentChild,
  868. child,
  869. addr->path,
  870. inverseLinkEncodingFormNumber);
  871. handleNews(link->child, reach, store);
  872. check(store);
  873. return link;
  874. }
  875. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16])
  876. {
  877. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  878. struct Node_Two* n = nodeForIp(store, addr);
  879. if (n && n->address.path == UINT64_MAX) {
  880. #ifdef Log_DEBUG
  881. uint8_t addrStr[40];
  882. AddrTools_printIp(addrStr, n->address.ip6.bytes);
  883. Log_debug(store->logger, "No way to represent path to [%s]", addrStr);
  884. #endif
  885. return NULL;
  886. }
  887. return n;
  888. }
  889. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path)
  890. {
  891. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  892. struct Node_Link* out = NULL;
  893. findClosest(path, &out, NULL, store);
  894. if (!out) { return NULL; }
  895. return Identity_check(out->child);
  896. }
  897. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path)
  898. {
  899. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  900. struct Node_Link* out = NULL;
  901. uint64_t pathParentChild = findClosest(path, &out, NULL, store);
  902. if (pathParentChild != 1) { return NULL; }
  903. return Identity_check(out);
  904. }
  905. struct Node_Link* NodeStore_getLinkOnPath(struct NodeStore* nodeStore,
  906. uint64_t routeLabel,
  907. uint32_t hopNum)
  908. {
  909. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  910. struct Node_Link* link = NULL;
  911. uint32_t num = hopNum;
  912. uint64_t path = findClosest(routeLabel, &link, &num, store);
  913. if (path == findClosest_INVALID || num < hopNum) {
  914. return NULL;
  915. }
  916. return link;
  917. }
  918. struct Node_Link* NodeStore_getLink(struct Node_Two* parent, uint32_t linkNum)
  919. {
  920. struct Node_Link* link = NULL;
  921. RB_FOREACH_REVERSE(link, PeerRBTree, &parent->peerTree) {
  922. if (!linkNum--) {
  923. return link;
  924. }
  925. }
  926. return NULL;
  927. }
  928. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal)
  929. {
  930. switch (returnVal) {
  931. case NodeStore_getRouteLabel_PARENT_NOT_FOUND:
  932. return "NodeStore_getRouteLabel_PARENT_NOT_FOUND";
  933. case NodeStore_getRouteLabel_CHILD_NOT_FOUND:
  934. return "NodeStore_getRouteLabel_CHILD_NOT_FOUND";
  935. default: return NULL;
  936. }
  937. }
  938. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  939. uint64_t pathToParent,
  940. uint64_t pathParentToChild)
  941. {
  942. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  943. struct Node_Link* linkToParent;
  944. if (findClosest(pathToParent, &linkToParent, NULL, store) != 1) {
  945. return NodeStore_getRouteLabel_PARENT_NOT_FOUND;
  946. }
  947. logLink(store, linkToParent, "NodeStore_getRouteLabel() PARENT");
  948. struct Node_Link* linkToChild = NULL;
  949. RB_FOREACH_REVERSE(linkToChild, PeerRBTree, &linkToParent->child->peerTree) {
  950. if (pathParentToChild == linkToChild->cannonicalLabel) {
  951. if (linkToParent == store->selfLink) {
  952. return linkToChild->cannonicalLabel;
  953. }
  954. return extendRoute(pathToParent,
  955. linkToChild->parent->encodingScheme,
  956. linkToChild->cannonicalLabel,
  957. linkToParent->inverseLinkEncodingFormNumber);
  958. }
  959. }
  960. return NodeStore_getRouteLabel_CHILD_NOT_FOUND;
  961. }
  962. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path)
  963. {
  964. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  965. struct Node_Link* linkToParent;
  966. uint64_t next = findClosest(path, &linkToParent, NULL, store);
  967. if (next == findClosest_INVALID) {
  968. return NodeStore_optimizePath_INVALID;
  969. }
  970. if (linkToParent == store->selfLink) {
  971. if (next == 1) { return 1; }
  972. return path;
  973. }
  974. if (next == 1) { return linkToParent->child->address.path; }
  975. if (linkToParent->child->bestParent) {
  976. linkToParent = linkToParent->child->bestParent;
  977. }
  978. uint64_t optimized = extendRoute(linkToParent->child->address.path,
  979. linkToParent->child->encodingScheme,
  980. next,
  981. linkToParent->inverseLinkEncodingFormNumber);
  982. if (optimized < UINT64_MAX) {
  983. return optimized;
  984. }
  985. return path;
  986. }
  987. uint32_t NodeStore_linkCount(struct Node_Two* node)
  988. {
  989. uint32_t i = 0;
  990. struct Node_Link* link;
  991. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  992. i++;
  993. }
  994. return i;
  995. }
  996. /** See: NodeStore.h */
  997. struct NodeStore* NodeStore_new(struct Address* myAddress,
  998. const uint32_t capacity,
  999. struct Allocator* allocator,
  1000. struct Log* logger)
  1001. {
  1002. struct Allocator* alloc = Allocator_child(allocator);
  1003. struct NodeStore_pvt* out = Allocator_clone(alloc, (&(struct NodeStore_pvt) {
  1004. .capacity = capacity,
  1005. .logger = logger,
  1006. .alloc = alloc
  1007. }));
  1008. Identity_set(out);
  1009. // Create the self node
  1010. struct Node_Two* selfNode = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1011. Bits_memcpyConst(&selfNode->address, myAddress, sizeof(struct Address));
  1012. selfNode->encodingScheme = NumberCompress_defineScheme(alloc);
  1013. selfNode->alloc = alloc;
  1014. Identity_set(selfNode);
  1015. out->pub.selfNode = selfNode;
  1016. selfNode->bestParent = linkNodes(selfNode, selfNode, 1, 0xffffffffu, 0, 1, out);
  1017. selfNode->address.path = 1;
  1018. selfNode->pathQuality = UINT32_MAX;
  1019. out->selfLink = selfNode->reversePeers;
  1020. RB_INSERT(NodeRBTree, &out->nodeTree, selfNode);
  1021. out->pub.selfAddress = &out->selfLink->child->address;
  1022. return &out->pub;
  1023. }
  1024. //////////////////////////////////////////////////////////////////////////////////////////////
  1025. //////////////////////////////////////////////////////////////////////////////////////////////
  1026. //////////////////////////////////////////////////////////////////////////////////////////////
  1027. /**
  1028. * Dump the table, one node at a time.
  1029. */
  1030. struct Node_Two* NodeStore_dumpTable(struct NodeStore* nodeStore, uint32_t index)
  1031. {
  1032. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1033. // TODO: Schlameil the painter
  1034. uint32_t i = 0;
  1035. struct Node_Two* nn = NULL;
  1036. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1037. if (i++ == index) { return nn; }
  1038. }
  1039. return NULL;
  1040. }
  1041. static struct Node_Two* getBestCycleB(struct Node_Two* node,
  1042. struct Address* target,
  1043. struct NodeStore_pvt* store)
  1044. {
  1045. uint32_t targetPfx = Address_getPrefix(target);
  1046. uint32_t ourDistance = Address_getPrefix(store->pub.selfAddress) ^ targetPfx;
  1047. struct Node_Link* next = NULL;
  1048. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1049. if (next->child->bestParent != next || next == store->selfLink) { continue; }
  1050. if (next->child->address.path == UINT64_MAX) { continue; }
  1051. if ((Address_getPrefix(&next->child->address) ^ targetPfx) >= ourDistance) { continue; }
  1052. return next->child;
  1053. }
  1054. return NULL;
  1055. }
  1056. static int getBestCycle(struct Node_Two* node,
  1057. struct Address* target,
  1058. struct Node_Two** output,
  1059. int limit,
  1060. int cycle,
  1061. struct NodeStore_pvt* store)
  1062. {
  1063. Assert_always(cycle < 1000);
  1064. if (cycle < limit) {
  1065. int total = 0;
  1066. struct Node_Link* next = NULL;
  1067. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1068. if (*output) { return total; }
  1069. if (next->child->bestParent != next || next == store->selfLink) { continue; }
  1070. total += getBestCycle(next->child, target, output, limit, cycle+1, store);
  1071. }
  1072. return total;
  1073. }
  1074. *output = getBestCycleB(node, target, store);
  1075. return 1;
  1076. }
  1077. struct Node_Two* NodeStore_getBest(struct Address* targetAddress, struct NodeStore* nodeStore)
  1078. {
  1079. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1080. struct Node_Two* n = NodeStore_nodeForAddr(nodeStore, targetAddress->ip6.bytes);
  1081. if (n && n->bestParent) { return n; }
  1082. for (int i = 0; i < 10000; i++) {
  1083. int ret = getBestCycle(store->pub.selfNode, targetAddress, &n, i, 0, store);
  1084. if (n || !ret) { return n; }
  1085. }
  1086. return NULL;
  1087. }
  1088. struct NodeList* NodeStore_getPeers(uint64_t label,
  1089. const uint32_t max,
  1090. struct Allocator* allocator,
  1091. struct NodeStore* nodeStore)
  1092. {
  1093. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1094. check(store);
  1095. // truncate the label to the part which this node uses...
  1096. label &= Bits_maxBits64(NumberCompress_bitsUsedForLabel(label));
  1097. struct NodeList* out = Allocator_calloc(allocator, sizeof(struct NodeList), 1);
  1098. out->nodes = Allocator_calloc(allocator, sizeof(char*), max);
  1099. struct Node_Link* next = NULL;
  1100. RB_FOREACH_REVERSE(next, PeerRBTree, &store->pub.selfNode->peerTree) {
  1101. uint64_t p = next->child->address.path;
  1102. int j;
  1103. for (j = 0; j < (int)max; j++) {
  1104. if (out->nodes[j] && (out->nodes[j]->address.path ^ label) < (p ^ label)) {
  1105. break;
  1106. }
  1107. }
  1108. switch (j) {
  1109. default: Bits_memmove(out->nodes, &out->nodes[1], (j - 1) * sizeof(char*));
  1110. case 1: out->nodes[j - 1] = next->child;
  1111. case 0:;
  1112. }
  1113. }
  1114. out->size = 0;
  1115. for (int i = 0; i < (int)max; i++) {
  1116. if (out->nodes[i]) {
  1117. out->nodes = &out->nodes[i];
  1118. out->size = max - i;
  1119. break;
  1120. }
  1121. }
  1122. for (int i = 0; i < (int)out->size; i++) {
  1123. Identity_check(out->nodes[i]);
  1124. verifyNode(out->nodes[i], store);
  1125. Assert_true(out->nodes[i]->address.path);
  1126. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1127. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1128. }
  1129. check(store);
  1130. return out;
  1131. }
  1132. static bool isOkAnswer(struct Node_Two* node,
  1133. uint32_t compatVer,
  1134. struct NodeStore_pvt* store)
  1135. {
  1136. if (node->address.path == UINT64_MAX) {
  1137. // (very) unreachable
  1138. return false;
  1139. }
  1140. if (!Version_isCompatible(compatVer, node->address.protocolVersion)) {
  1141. return false;
  1142. }
  1143. if (node == store->pub.selfNode) {
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. /** See: NodeStore.h */
  1149. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* nodeStore,
  1150. struct Address* targetAddress,
  1151. const uint32_t count,
  1152. uint32_t compatVer,
  1153. struct Allocator* allocator)
  1154. {
  1155. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1156. struct NodeList* out = Allocator_malloc(allocator, sizeof(struct NodeList));
  1157. out->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  1158. out->size = count;
  1159. struct Node_Two fakeNode = { .pathQuality = 0 };
  1160. Bits_memcpyConst(&fakeNode.address, targetAddress, sizeof(struct Address));
  1161. struct Node_Two* next = Identity_ncheck(RB_NFIND(NodeRBTree, &store->nodeTree, &fakeNode));
  1162. if (!next) {
  1163. out->size = 0;
  1164. return out;
  1165. }
  1166. struct Node_Two* prev = Identity_ncheck(NodeRBTree_RB_PREV(next));
  1167. int idx = out->size-1;
  1168. while (idx > -1) {
  1169. if (prev && (!next || Address_closest(targetAddress, &next->address, &prev->address) > 0)) {
  1170. if (isOkAnswer(prev, compatVer, store)) { out->nodes[idx--] = prev; }
  1171. prev = Identity_ncheck(NodeRBTree_RB_PREV(prev));
  1172. continue;
  1173. }
  1174. if (next && (!prev || Address_closest(targetAddress, &next->address, &prev->address) < 0)) {
  1175. if (isOkAnswer(next, compatVer, store)) { out->nodes[idx--] = next; }
  1176. next = Identity_ncheck(NodeRBTree_RB_NEXT(next));
  1177. continue;
  1178. }
  1179. break;
  1180. }
  1181. out->nodes = &out->nodes[idx+1];
  1182. out->size -= idx+1;
  1183. for (int i = 0; i < (int)out->size; i++) {
  1184. Identity_check(out->nodes[i]);
  1185. Assert_true(out->nodes[i]->address.path);
  1186. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1187. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1188. }
  1189. check(store);
  1190. return out;
  1191. }
  1192. void NodeStore_updateReach(struct NodeStore* nodeStore, struct Node_Two* node, uint32_t newReach)
  1193. {
  1194. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1195. check(store);
  1196. handleNews(node, newReach, store);
  1197. check(store);
  1198. }
  1199. int NodeStore_nonZeroNodes(struct NodeStore* nodeStore)
  1200. {
  1201. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1202. int nonZeroNodes = 0;
  1203. struct Node_Two* nn = NULL;
  1204. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1205. nonZeroNodes += (nn->pathQuality > 0);
  1206. }
  1207. return nonZeroNodes;
  1208. }
  1209. int NodeStore_size(struct NodeStore* nodeStore)
  1210. {
  1211. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1212. int size = 0;
  1213. struct Node_Two* nn = NULL;
  1214. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1215. size++;
  1216. }
  1217. return size;
  1218. }
  1219. void NodeStore_brokenPath(uint64_t path, struct NodeStore* nodeStore)
  1220. {
  1221. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1222. #ifdef Log_DEBUG
  1223. uint8_t pathStr[20];
  1224. AddrTools_printPath(pathStr, path);
  1225. Log_debug(store->logger, "NodeStore_brokenPath(%s)", pathStr);
  1226. #endif
  1227. struct Node_Link* nl = NodeStore_linkForPath(nodeStore, path);
  1228. if (nl && nl->child->bestParent == nl && nl->child->pathQuality > 0) {
  1229. handleBadNews(nl->child, 0, store);
  1230. }
  1231. check(store);
  1232. }