NodeStore.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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 "dht/Address.h"
  16. #include "dht/dhtcore/Node.h"
  17. #include "dht/dhtcore/NodeStore.h"
  18. #include "dht/dhtcore/NodeList.h"
  19. #include "util/AddrTools.h"
  20. #include "util/Assert.h"
  21. #include "util/Bits.h"
  22. #include "util/log/Log.h"
  23. #include "util/version/Version.h"
  24. #include "switch/NumberCompress.h"
  25. #include "switch/LabelSplicer.h"
  26. #include "util/Gcc.h"
  27. #include <tree.h>
  28. struct NodeStore_AsyncSplitLinks;
  29. struct NodeStore_AsyncSplitLinks
  30. {
  31. struct Node_Two* node;
  32. uint64_t path;
  33. int inverseLinkEncodingFormNumber;
  34. struct NodeStore_AsyncSplitLinks* next;
  35. };
  36. /** A list of DHT nodes. */
  37. struct NodeStore_pvt
  38. {
  39. struct NodeStore pub;
  40. /** A fake link where we are both the parent and child. */
  41. struct Node_Link* selfLink;
  42. /** A tree containing all nodes ordered by ipv6 */
  43. struct NodeRBTree {
  44. struct Node_Two* rbh_root;
  45. } nodeTree;
  46. struct Allocator* alloc;
  47. /**
  48. * The links to be freed next time freePendingLinks() is called.
  49. */
  50. struct Node_Link* linksToFree;
  51. /** Nodes which have very likely been reset. */
  52. struct RumorMill* renumberMill;
  53. /**
  54. * The links which were split with asyncSplitLink() and should be re-connected after
  55. * discoverNode is complete.
  56. */
  57. struct NodeStore_AsyncSplitLinks* asyncSplitLinks;
  58. struct Allocator* asyncSplitLinksAlloc;
  59. /** The means for this node store to log. */
  60. struct Log* logger;
  61. Identity
  62. };
  63. // My memory is really bad
  64. #define A_COMES_FIRST 1
  65. #define B_COMES_FIRST -1
  66. static int comparePeers(const struct Node_Link* la, const struct Node_Link* lb)
  67. {
  68. Identity_check(lb);
  69. uint64_t a = la->cannonicalLabel;
  70. uint64_t b = lb->cannonicalLabel;
  71. int log2Diff = Bits_log2x64(b) - Bits_log2x64(a);
  72. if (log2Diff) {
  73. return log2Diff;
  74. }
  75. if (Bits_bitReverse64(a) < Bits_bitReverse64(b)) {
  76. return A_COMES_FIRST;
  77. } else if (a == b) {
  78. return 0;
  79. }
  80. return B_COMES_FIRST;
  81. }
  82. RB_GENERATE_STATIC(PeerRBTree, Node_Link, peerTree, comparePeers)
  83. static int compareNodes(const struct Node_Two* na, const struct Node_Two* nb)
  84. {
  85. Identity_check(nb);
  86. int ret;
  87. ret = Address_xorcmp(0, na->address.ip6.ints.one_be, nb->address.ip6.ints.one_be);
  88. if (ret) { return ret; }
  89. ret = Address_xorcmp(0, na->address.ip6.ints.two_be, nb->address.ip6.ints.two_be);
  90. if (ret) { return ret; }
  91. ret = Address_xorcmp(0, na->address.ip6.ints.three_be, nb->address.ip6.ints.three_be);
  92. if (ret) { return ret; }
  93. ret = Address_xorcmp(0, na->address.ip6.ints.four_be, nb->address.ip6.ints.four_be);
  94. return ret;
  95. }
  96. RB_GENERATE_STATIC(NodeRBTree, Node_Two, nodeTree, compareNodes)
  97. static void freeLink(struct Node_Link* link, struct NodeStore_pvt* store)
  98. {
  99. Allocator_realloc(store->alloc, link, 0);
  100. store->pub.linkCount--;
  101. }
  102. static struct Node_Link* getLink(struct NodeStore_pvt* store)
  103. {
  104. store->pub.linkCount++;
  105. return Allocator_calloc(store->alloc, sizeof(struct Node_Link), 1);
  106. }
  107. static void logLink(struct NodeStore_pvt* store,
  108. struct Node_Link* link,
  109. char* message)
  110. {
  111. #ifndef Log_DEBUG
  112. return;
  113. #endif
  114. uint8_t parent[40];
  115. uint8_t child[40];
  116. AddrTools_printIp(parent, link->parent->address.ip6.bytes);
  117. AddrTools_printIp(child, link->child->address.ip6.bytes);
  118. uint8_t path[20];
  119. AddrTools_printPath(path, link->cannonicalLabel);
  120. Log_debug(store->logger, "link[%s]->[%s] [%s] %s", parent, child, path, message);
  121. }
  122. static void _checkNode(struct Node_Two* node, struct NodeStore_pvt* store, char* file, int line)
  123. {
  124. #ifndef PARANOIA
  125. return;
  126. #endif
  127. Assert_true(node->address.path ==
  128. EncodingScheme_convertLabel(store->pub.selfNode->encodingScheme,
  129. node->address.path,
  130. EncodingScheme_convertLabel_convertTo_CANNONICAL));
  131. struct Node_Link* link;
  132. for (link = node->reversePeers; link; link = link->nextPeer) {
  133. Assert_fileLine(link->child == node, file, line);
  134. Assert_fileLine(RB_FIND(PeerRBTree, &link->parent->peerTree, link) == link, file, line);
  135. }
  136. struct Node_Link* lastLink = NULL;
  137. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  138. Assert_fileLine(!EncodingScheme_isSelfRoute(link->parent->encodingScheme,
  139. link->cannonicalLabel)
  140. || link == store->selfLink,
  141. file, line);
  142. Assert_fileLine(Node_getBestParent(node) || Node_getBestParent(link->child) != link,
  143. file, line);
  144. Assert_fileLine(link->parent == node, file, line);
  145. Assert_fileLine(link->child != node || link == store->selfLink, file, line);
  146. Assert_fileLine(!lastLink || link->cannonicalLabel != lastLink->cannonicalLabel,
  147. file, line);
  148. Assert_fileLine(link->cannonicalLabel < UINT64_MAX && link->cannonicalLabel > 0,
  149. file, line);
  150. struct Node_Link* rlink = NULL;
  151. for (rlink = link->child->reversePeers; rlink; rlink = rlink->nextPeer) {
  152. if (rlink == link) {
  153. break;
  154. }
  155. }
  156. Assert_fileLine(rlink && "child contains reverse link", file, line);
  157. lastLink = link;
  158. }
  159. if (Node_getBestParent(node)) {
  160. Assert_fileLine(Node_getReach(Node_getBestParent(node)->parent) > Node_getReach(node)
  161. || node == store->pub.selfNode, file, line);
  162. Assert_fileLine(node->address.path != UINT64_MAX, file, line);
  163. // Should never get as low as 512...
  164. Assert_fileLine(Node_getReach(node) > 512, file, line);
  165. struct Node_Two* nn = node;
  166. do {
  167. Assert_fileLine(
  168. LabelSplicer_routesThrough(nn->address.path,
  169. Node_getBestParent(nn)->parent->address.path),
  170. file,
  171. line
  172. );
  173. nn = Node_getBestParent(nn)->parent;
  174. } while (nn != store->pub.selfNode);
  175. } else {
  176. Assert_fileLine(node->address.path == UINT64_MAX, file, line);
  177. Assert_fileLine(Node_getReach(node) == 0, file, line);
  178. }
  179. }
  180. #define checkNode(node, store) _checkNode(node, store, Gcc_SHORT_FILE, Gcc_LINE)
  181. static void _verifyNode(struct Node_Two* node, struct NodeStore_pvt* store, char* file, int line)
  182. {
  183. #ifndef PARANOIA
  184. return;
  185. #endif
  186. // #1 check the node (do the basic checks)
  187. _checkNode(node, store, file, line);
  188. // #2 make sure all of the node's outgoing links are split properly
  189. struct Node_Link* link = NULL;
  190. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  191. // make sure any peers of this node are split properly
  192. struct Node_Link* linkB = link;
  193. struct Node_Link* linkC = link;
  194. RB_FOREACH_REVERSE_FROM(linkB, PeerRBTree, linkC) {
  195. if (linkB == link || link == store->selfLink) { continue; }
  196. Assert_fileLine(
  197. !LabelSplicer_routesThrough(linkB->cannonicalLabel, link->cannonicalLabel),
  198. file, line
  199. );
  200. }
  201. }
  202. // #3 make sure looking for the node by address will actually find the correct node.
  203. if (Node_getBestParent(node)) {
  204. Assert_fileLine(node == NodeStore_closestNode(&store->pub, node->address.path), file, line);
  205. }
  206. }
  207. #define verifyNode(node, store) _verifyNode(node, store, Gcc_SHORT_FILE, Gcc_LINE)
  208. // Verify is more thorough than check because it makes sure all links are split properly.
  209. static void _verify(struct NodeStore_pvt* store, char* file, int line)
  210. {
  211. #ifndef PARANOIA
  212. return;
  213. #endif
  214. Assert_true(Node_getBestParent(store->pub.selfNode) == store->selfLink || !store->selfLink);
  215. struct Node_Two* nn = NULL;
  216. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  217. _verifyNode(nn, store, file, line);
  218. }
  219. }
  220. #define verify(store) _verify(store, Gcc_SHORT_FILE, Gcc_LINE)
  221. static void _check(struct NodeStore_pvt* store, char* file, int line)
  222. {
  223. #ifndef PARANOIA
  224. return;
  225. #endif
  226. Assert_true(Node_getBestParent(store->pub.selfNode) == store->selfLink || !store->selfLink);
  227. struct Node_Two* nn = NULL;
  228. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  229. _checkNode(nn, store, file, line);
  230. }
  231. }
  232. #define check(store) _check(store, Gcc_SHORT_FILE, Gcc_LINE)
  233. /**
  234. * Extend a route by splicing on another link.
  235. * This will modify the Encoding Form of the first Director in next section of the route to make
  236. * it's size greater than or equal to the size of the return route through the parent node in the
  237. * link.
  238. *
  239. * @param routeLabel the label for reaching the parent node
  240. * @param parentScheme the label encoding scheme used by the parent node
  241. * @param parentChildLabel the cannonicalLabel for the link from parent to child
  242. * @param previousLinkEncoding the encoding used for the parent's interface back to the grandparent
  243. */
  244. static uint64_t extendRoute(uint64_t routeLabel,
  245. struct EncodingScheme* parentScheme,
  246. uint64_t parentChildLabel,
  247. int previousLinkEncoding)
  248. {
  249. uint64_t next = parentChildLabel;
  250. int nextLinkEncoding = EncodingScheme_getFormNum(parentScheme, next);
  251. if (previousLinkEncoding > nextLinkEncoding) {
  252. next = EncodingScheme_convertLabel(parentScheme, next, previousLinkEncoding);
  253. }
  254. Assert_true(next != EncodingScheme_convertLabel_INVALID);
  255. return LabelSplicer_splice(next, routeLabel);
  256. }
  257. static void unreachable(struct Node_Two* node, struct NodeStore_pvt* store)
  258. {
  259. struct Node_Link* next = NULL;
  260. #ifdef Log_INFO
  261. for (next = node->reversePeers; next; next = next->nextPeer) {
  262. if (next->parent == store->pub.selfNode
  263. && LabelSplicer_isOneHop(next->cannonicalLabel))
  264. {
  265. uint8_t addr[40];
  266. AddrTools_printIp(addr, node->address.ip6.bytes);
  267. Log_info(store->logger, "Direct peer [%s] is unreachable", addr);
  268. }
  269. }
  270. #endif
  271. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  272. if (Node_getBestParent(next->child) == next) { unreachable(next->child, store); }
  273. }
  274. Node_setParentReachAndPath(node, NULL, 0, UINT64_MAX);
  275. }
  276. /**
  277. * This is called when we have no idea what the reach should be for the next hop
  278. * because the path we previously used to get to it is broken and we need to use
  279. * a different one. Take a somewhat educated guess as to what it might be in a way
  280. * that will make the reach non-zero.
  281. */
  282. static uint32_t guessReachOfChild(struct Node_Link* link)
  283. {
  284. // return 3/4 of the parent's reach if it's 1 hop, 1/2 otherwise.
  285. uint32_t r = Node_getReach(link->parent) / 2;
  286. if (r < (1<<12)) {
  287. r = Node_getReach(link->parent) - 1;
  288. } else if (r < (1<<16)) {
  289. r = Node_getReach(link->parent) - Bits_log2x64(link->cannonicalLabel);
  290. }
  291. Assert_true(r < Node_getReach(link->parent) && r != 0);
  292. return r;
  293. }
  294. static int updateBestParentCycle(struct Node_Link* newBestLink,
  295. int cycle,
  296. int limit,
  297. uint32_t nextReach,
  298. struct NodeStore_pvt* store)
  299. {
  300. Assert_true(cycle < 1000);
  301. struct Node_Two* node = newBestLink->child;
  302. if (cycle < limit) {
  303. int total = 0;
  304. struct Node_Link* next = NULL;
  305. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  306. if (Node_getBestParent(next->child) == next && next->child != node) {
  307. total += updateBestParentCycle(next, cycle+1, limit, nextReach, store);
  308. }
  309. }
  310. return total;
  311. }
  312. struct Node_Two* newBest = newBestLink->parent;
  313. uint64_t bestPath = extendRoute(newBest->address.path,
  314. newBest->encodingScheme,
  315. newBestLink->cannonicalLabel,
  316. Node_getBestParent(newBest)->inverseLinkEncodingFormNumber);
  317. if (bestPath == UINT64_MAX) {
  318. unreachable(node, store);
  319. return 1;
  320. }
  321. /*#ifdef Log_DEBUG
  322. if (node->address.path != bestPath) {
  323. uint8_t pathStr[20];
  324. AddrTools_printPath(pathStr, bestPath);
  325. uint8_t addrStr[40];
  326. AddrTools_printIp(addrStr, node->address.ip6.bytes);
  327. Log_debug(store->logger, "New best path [%s@%s]", addrStr, pathStr);
  328. }
  329. #endif*/
  330. if (limit) {
  331. // We're only altering the reach of the top node in the chain.
  332. // If we want to deduce reach of further nodes along the path, here's the place.
  333. nextReach = Node_getReach(node);
  334. }
  335. Node_setParentReachAndPath(node, newBestLink, nextReach, bestPath);
  336. checkNode(node, store);
  337. return 1;
  338. }
  339. /**
  340. * Update the best parent of this node.
  341. * propigating path changes out through the tree.
  342. *
  343. * @param newBestParent the new best link to the node. The affected node is newBestParent->child.
  344. * @param nextReach the reach to set the node to.
  345. * @param store the nodestore.
  346. */
  347. static void updateBestParent(struct Node_Link* newBestParent,
  348. uint32_t nextReach,
  349. struct NodeStore_pvt* store)
  350. {
  351. check(store);
  352. Assert_true(newBestParent);
  353. for (int i = 0; i < 10000; i++) {
  354. if (!updateBestParentCycle(newBestParent, 0, i, nextReach, store)) {
  355. check(store);
  356. return;
  357. }
  358. }
  359. Assert_true(0);
  360. }
  361. static void handleGoodNews(struct Node_Two* node,
  362. uint32_t newReach,
  363. struct NodeStore_pvt* store)
  364. {
  365. // TODO(cjd): Paths longer than 1024 will blow up, handle more gracefully
  366. Assert_true(newReach != UINT32_MAX);
  367. Assert_true(newReach > 1023);
  368. Assert_true(newReach > Node_getReach(node));
  369. // The nodestore thinks it's unreachable, we can't very well update the reach.
  370. if (Node_getBestParent(node) == NULL) { return; }
  371. struct Node_Two* bp = Node_getBestParent(node)->parent;
  372. if (newReach+1 > Node_getReach(bp)) {
  373. handleGoodNews(bp, newReach+1, store);
  374. }
  375. Node_setReach(node, newReach);
  376. struct Node_Link* link = NULL;
  377. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  378. Identity_check(link);
  379. struct Node_Two* child = link->child;
  380. struct Node_Link* childBestParent = Node_getBestParent(child);
  381. if (!childBestParent || Node_getReach(childBestParent->parent) < newReach) {
  382. uint32_t nextReach = guessReachOfChild(link);
  383. if (Node_getReach(child) > nextReach) { continue; }
  384. updateBestParent(link, nextReach, store);
  385. }
  386. }
  387. }
  388. /**
  389. * The news has hit (in handleBadNewsOne) and now all of the nodes in the affected zone have
  390. * been knocked down. Now lets see if there's a better path for any of them.
  391. */
  392. static void handleBadNewsTwo(struct Node_Link* link, struct NodeStore_pvt* store)
  393. {
  394. struct Node_Link* next = NULL;
  395. RB_FOREACH_REVERSE(next, PeerRBTree, &link->child->peerTree) {
  396. if (!next) { continue; }
  397. if (Node_getBestParent(next->child) != next) { continue; }
  398. if (next == store->selfLink) { continue; }
  399. handleBadNewsTwo(next, store);
  400. }
  401. // node was relinked by a recursion of this function.
  402. if (Node_getBestParent(link->child) != link) { return; }
  403. struct Node_Two* node = link->child;
  404. struct Node_Link* rp = link->child->reversePeers;
  405. struct Node_Link* best = Node_getBestParent(node);
  406. while (rp) {
  407. if (Node_getReach(rp->parent) >= Node_getReach(best->parent)) {
  408. if (Node_getReach(rp->parent) > Node_getReach(best->parent)
  409. || rp->parent->address.path < best->parent->address.path)
  410. {
  411. best = rp;
  412. }
  413. }
  414. rp = rp->nextPeer;
  415. }
  416. if (best == Node_getBestParent(node)) { return; }
  417. uint32_t nextReach = guessReachOfChild(best);
  418. if (nextReach <= Node_getReach(node)) { return; }
  419. Assert_true(Node_getReach(node) < Node_getReach(best->parent));
  420. check(store);
  421. updateBestParent(best, nextReach, store);
  422. check(store);
  423. }
  424. /**
  425. * First thing we do is knock down everybody's reach.
  426. * This way they don't all cling to eachother for safety making
  427. * endless routing loops and stupid processing.
  428. */
  429. static uint32_t handleBadNewsOne(struct Node_Link* link,
  430. uint32_t newReach,
  431. struct NodeStore_pvt* store)
  432. {
  433. struct Node_Link* next = NULL;
  434. uint32_t highestRet = 0;
  435. RB_FOREACH_REVERSE(next, PeerRBTree, &link->child->peerTree) {
  436. if (Node_getBestParent(next->child) != next) { continue; }
  437. if (next == store->selfLink) { continue; }
  438. if (Node_getReach(next->child) < newReach) { continue; }
  439. uint32_t ret = handleBadNewsOne(next, newReach, store);
  440. if (ret > highestRet) { highestRet = ret; }
  441. }
  442. if (!highestRet) { highestRet = newReach; }
  443. Assert_true(link->child != store->pub.selfNode);
  444. if (!highestRet) {
  445. unreachable(link->child, store);
  446. } else {
  447. Node_setReach(link->child, highestRet);
  448. }
  449. if (highestRet < 1023) { highestRet = 1023; }
  450. return highestRet+1;
  451. }
  452. static void handleBadNews(struct Node_Two* node,
  453. uint32_t newReach,
  454. struct NodeStore_pvt* store)
  455. {
  456. Assert_true(newReach < Node_getReach(node));
  457. // might be destroyed by handleBadNewsOne()
  458. struct Node_Link* bp = Node_getBestParent(node);
  459. // no bestParent implies a reach of 0
  460. Assert_true(bp && bp != store->selfLink);
  461. Assert_true(!newReach || newReach > 1023);
  462. handleBadNewsOne(bp, newReach, store);
  463. check(store);
  464. handleBadNewsTwo(bp, store);
  465. check(store);
  466. }
  467. static void handleNews(struct Node_Two* node, uint32_t newReach, struct NodeStore_pvt* store)
  468. {
  469. // This is because reach is used to prevent loops so it must be 1 more for each hop closer
  470. // to the root.
  471. if (newReach > (UINT32_MAX - 1024)) { newReach = (UINT32_MAX - 1024); }
  472. if (newReach < 1024) { newReach = 1024; }
  473. check(store);
  474. if (newReach < Node_getReach(node)) {
  475. handleBadNews(node, newReach, store);
  476. check(store);
  477. } else if (newReach > Node_getReach(node)) {
  478. handleGoodNews(node, newReach, store);
  479. check(store);
  480. }
  481. }
  482. void NodeStore_unlinkNodes(struct NodeStore* nodeStore, struct Node_Link* link)
  483. {
  484. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*) nodeStore);
  485. struct Node_Two* child = Identity_check(link->child);
  486. struct Node_Two* parent = Identity_check(link->parent);
  487. check(store);
  488. #ifdef Log_INFO
  489. if (parent == store->pub.selfNode && LabelSplicer_isOneHop(link->cannonicalLabel)) {
  490. uint8_t addr[40];
  491. AddrTools_printIp(addr, child->address.ip6.bytes);
  492. Log_info(store->logger, "Direct peer [%s] has been removed from NodeStore", addr);
  493. }
  494. #endif
  495. // Change the best parent and path if necessary
  496. if (Node_getBestParent(child) == link) {
  497. handleBadNews(child, 0, store);
  498. }
  499. if (Node_getBestParent(child) == link) {
  500. unreachable(child, store);
  501. }
  502. check(store);
  503. // Remove the entry from the reversePeers
  504. struct Node_Link* current = child->reversePeers;
  505. struct Node_Link** currentP = &child->reversePeers;
  506. while (current) {
  507. if (current == link) {
  508. *currentP = current->nextPeer;
  509. break;
  510. }
  511. currentP = &(current->nextPeer);
  512. current = *currentP;
  513. }
  514. Assert_true(current);
  515. // Remove the RBTree entry
  516. Assert_ifParanoid(link == RB_FIND(PeerRBTree, &parent->peerTree, link));
  517. RB_REMOVE(PeerRBTree, &parent->peerTree, link);
  518. link->nextPeer = store->linksToFree;
  519. store->linksToFree = link;
  520. // prevent double-free of link.
  521. link->parent = NULL;
  522. link->child = NULL;
  523. check(store);
  524. }
  525. static void update(struct Node_Link* link,
  526. int64_t linkStateDiff,
  527. struct NodeStore_pvt* store)
  528. {
  529. /** TODO(cjd): Link state is not taken into account yet
  530. if (linkStateDiff + link->linkState > UINT32_MAX) {
  531. link->linkState = UINT32_MAX;
  532. logLink(store, link, "link state set to maximum");
  533. } else if (linkStateDiff + link->linkState < 0) {
  534. link->linkState = UINT32_MAX;
  535. logLink(store, link, "link state set to zero");
  536. } else {
  537. link->linkState += linkStateDiff;
  538. }
  539. */
  540. }
  541. /**
  542. * Link two nodes in the graph together.
  543. * If a parent of the child node is also a parent of the parent node, they are
  544. * unlinked (the link is split and the child is inserted in the middle).
  545. *
  546. * @param parent the current end of the graph
  547. * @param child the new node to extend the graph
  548. * @param cannonicalLabel the label for getting from the parent to the child.
  549. * @param linkStateDiff how much to change the link state for this link.
  550. * @param store
  551. */
  552. static struct Node_Link* linkNodes(struct Node_Two* parent,
  553. struct Node_Two* child,
  554. uint64_t cannonicalLabel,
  555. int64_t linkStateDiff,
  556. int inverseLinkEncodingFormNumber,
  557. uint64_t discoveredPath,
  558. struct NodeStore_pvt* store)
  559. {
  560. check(store);
  561. #ifdef Log_DEBUG
  562. uint8_t parentIp[40];
  563. uint8_t childIp[40];
  564. AddrTools_printIp(parentIp, parent->address.ip6.bytes);
  565. AddrTools_printIp(childIp, child->address.ip6.bytes);
  566. uint8_t printedLabel[20];
  567. AddrTools_printPath(printedLabel, cannonicalLabel);
  568. Log_debug(store->logger, "Linking [%s] with [%s] with label fragment [%s]",
  569. parentIp, childIp, printedLabel);
  570. #endif
  571. // It's ok to link a node with itself via some loopey route.
  572. // in practice it should never actually be used and it might yield some interesting
  573. // information when the link is split, self-routes are not allowed unless the self
  574. // link is being set up :)
  575. Assert_true(cannonicalLabel != 1 || store->selfLink == NULL);
  576. #ifdef PARANOIA
  577. uint64_t definitelyCannonical =
  578. EncodingScheme_convertLabel(parent->encodingScheme,
  579. cannonicalLabel,
  580. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  581. Assert_true(definitelyCannonical == cannonicalLabel);
  582. #endif
  583. struct Node_Link* link;
  584. RB_FOREACH_REVERSE(link, PeerRBTree, &parent->peerTree) {
  585. Identity_check(link);
  586. if (link->child == child) {
  587. if (link->cannonicalLabel != cannonicalLabel) {
  588. // multiple paths between A and B are ok because they
  589. // will have divergent paths following the first director.
  590. continue;
  591. } else if (link->inverseLinkEncodingFormNumber != inverseLinkEncodingFormNumber) {
  592. logLink(store, link, "Relinking nodes with different encoding form");
  593. // This can happen when C renumbers but B->C is the same because B did
  594. // not renumber, EG: if C restarts.
  595. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  596. }
  597. update(link, linkStateDiff, store);
  598. return link;
  599. }
  600. }
  601. struct Node_Link dummy = { .cannonicalLabel = cannonicalLabel };
  602. link = Identity_ncheck(RB_FIND(PeerRBTree, &parent->peerTree, &dummy));
  603. if (link) {
  604. logLink(store, link, "Attempted to create alternate link with same label!");
  605. Assert_true(0);
  606. return link;
  607. }
  608. link = getLink(store);
  609. // set it up
  610. link->cannonicalLabel = cannonicalLabel;
  611. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  612. link->child = child;
  613. link->parent = parent;
  614. link->discoveredPath = discoveredPath;
  615. Identity_set(link);
  616. // reverse link
  617. link->nextPeer = child->reversePeers;
  618. child->reversePeers = link;
  619. // forward link
  620. Assert_ifParanoid(!RB_FIND(PeerRBTree, &parent->peerTree, link));
  621. RB_INSERT(PeerRBTree, &parent->peerTree, link);
  622. if (!Node_getBestParent(child)) {
  623. if (Node_getBestParent(parent)) {
  624. updateBestParent(link, guessReachOfChild(link), store);
  625. } else {
  626. unreachable(child, store);
  627. }
  628. }
  629. // update the child's link state and possibly change it's preferred path
  630. update(link, linkStateDiff, store);
  631. check(store);
  632. return link;
  633. }
  634. #define removeLinkFromLabel_IMPOSSIBLE UINT64_MAX
  635. #define removeLinkFromLabel_OVERSIZE (UINT64_MAX-1)
  636. #define removeLinkFromLabel_ERR(x) (((uint64_t)x) >> 63)
  637. // TODO(cjd): This does not depend on nodeStore or alter the link, consider moving to Node.c
  638. static uint64_t removeLinkFromLabel(struct Node_Link* link, uint64_t label)
  639. {
  640. // First we splice off the parent's Director leaving the child's Director.
  641. uint64_t unspliced = LabelSplicer_unsplice(label, link->cannonicalLabel);
  642. int formNum = EncodingScheme_getFormNum(link->child->encodingScheme, unspliced);
  643. if (formNum < link->inverseLinkEncodingFormNumber) {
  644. // Can't get there from here.
  645. return removeLinkFromLabel_IMPOSSIBLE;
  646. }
  647. uint64_t cannonical =
  648. EncodingScheme_convertLabel(link->child->encodingScheme,
  649. unspliced,
  650. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  651. // Check that they didn't waste space by sending an oversize encoding form.
  652. if (formNum > link->inverseLinkEncodingFormNumber && cannonical != unspliced) {
  653. return removeLinkFromLabel_OVERSIZE;
  654. }
  655. Assert_true(cannonical != EncodingScheme_convertLabel_INVALID);
  656. return cannonical;
  657. }
  658. /**
  659. * Find the next hop on a given path.
  660. * Given a label representing a path from parentLink to some destination, set
  661. * outLink to the first link along that journey and return the path from outLink
  662. * to the original destination.
  663. * Feeding outLink back in to parentLink and the return value back into label argument
  664. * will allow you to iteratively walk a path.
  665. *
  666. * @param label the path from parentLink to some unspecified destination.
  667. * @param outLink a pointer to a location which will receive the first link in the path.
  668. * @param parentLink the link where to begin the trek.
  669. * @param store
  670. * @return a label which would take you from the node in memory location outLink to the
  671. * destination provided by the label argument. OR: firstHopInPath_INVALID if the
  672. * label argument traverces a node whose encoding scheme is inconsistent with
  673. * the label. OR: firstHopInPath_NO_NEXT_LINK if there are no *known* further
  674. * links along the path. If the result is firstHopInPath_INVALID, outLink will
  675. * still be set to the node. Use firstHopInPath_ERR() to check if the return
  676. * is an error code.
  677. */
  678. #define firstHopInPath_INVALID UINT64_MAX
  679. #define firstHopInPath_NO_NEXT_LINK (UINT64_MAX-1)
  680. #define firstHopInPath_ERR(path) (path >= firstHopInPath_NO_NEXT_LINK)
  681. static uint64_t firstHopInPath(uint64_t label,
  682. struct Node_Link** outLink,
  683. struct Node_Link* parentLink,
  684. struct NodeStore_pvt* store)
  685. {
  686. // Then we search for the next peer in the path
  687. // RB_NFIND will find a link for which we know that no link before it is in the path.
  688. // Unfortunately I have not found a way to store links in a tree where the search time
  689. // is less than O(n) where n = peers of a given node.
  690. struct Node_Link tmpl = { .cannonicalLabel = label };
  691. struct Node_Link* nextLink =
  692. Identity_ncheck(RB_NFIND(PeerRBTree, &parentLink->child->peerTree, &tmpl));
  693. // Now we walk back through the potential candidates looking for a path which it routes though.
  694. while (nextLink && !LabelSplicer_routesThrough(label, nextLink->cannonicalLabel)) {
  695. nextLink = Identity_ncheck(RB_NEXT(PeerRBTree, NULL, nextLink));
  696. }
  697. // This node has no peers, if it's us then it always has a peer (which is the selfLink)
  698. if (!nextLink || nextLink == store->selfLink) {
  699. return firstHopInPath_NO_NEXT_LINK;
  700. }
  701. // check for a looping link, this should never happen but adding the assert helps me
  702. // refactor this function a little more agressively.
  703. Assert_true(nextLink != parentLink);
  704. if (label == nextLink->cannonicalLabel) {
  705. //logLink(store, nextLink, "Exact match");
  706. *outLink = nextLink;
  707. return 1;
  708. }
  709. if (!LabelSplicer_routesThrough(label, nextLink->cannonicalLabel)) {
  710. // child of next link is not in the path, we reached the end.
  711. return firstHopInPath_NO_NEXT_LINK;
  712. }
  713. *outLink = nextLink;
  714. // Cannoicalize the child's Director
  715. label = removeLinkFromLabel(nextLink, label);
  716. if (removeLinkFromLabel_ERR(label)) {
  717. return firstHopInPath_INVALID;
  718. }
  719. return label;
  720. }
  721. #define findClosest_INVALID (~((uint64_t)0))
  722. static uint64_t findClosest(uint64_t path,
  723. struct Node_Link** output,
  724. struct Node_Link* parentLink,
  725. struct NodeStore_pvt* store)
  726. {
  727. for (;;) {
  728. struct Node_Link* nextLink = NULL;
  729. uint64_t nextPath = firstHopInPath(path, &nextLink, parentLink, store);
  730. if (nextPath == firstHopInPath_NO_NEXT_LINK) {
  731. *output = parentLink;
  732. return path;
  733. }
  734. if (firstHopInPath_INVALID == nextPath) {
  735. return findClosest_INVALID;
  736. }
  737. Assert_true(nextLink);
  738. path = nextPath;
  739. parentLink = nextLink;
  740. }
  741. }
  742. static struct Node_Two* nodeForIp(struct NodeStore_pvt* store, uint8_t ip[16])
  743. {
  744. struct Node_Two fakeNode;
  745. Identity_set(&fakeNode);
  746. Bits_memcpyConst(fakeNode.address.ip6.bytes, ip, 16);
  747. return Identity_ncheck(RB_FIND(NodeRBTree, &store->nodeTree, &fakeNode));
  748. }
  749. static void freePendingLinks(struct NodeStore_pvt* store)
  750. {
  751. struct Node_Link* link;
  752. while ((link = store->linksToFree)) {
  753. store->linksToFree = link->nextPeer;
  754. freeLink(link, store);
  755. }
  756. }
  757. static void asyncSplitLink(struct NodeStore_pvt* store,
  758. struct Node_Link* splitLink,
  759. struct Node_Link* splitLinkParent)
  760. {
  761. logLink(store, splitLink, "Asynchronously splitting link");
  762. if (!store->asyncSplitLinksAlloc) {
  763. store->asyncSplitLinksAlloc = Allocator_child(store->alloc);
  764. }
  765. struct NodeStore_AsyncSplitLinks* asl =
  766. Allocator_malloc(store->asyncSplitLinksAlloc, sizeof(struct NodeStore_AsyncSplitLinks));
  767. asl->next = store->asyncSplitLinks;
  768. store->asyncSplitLinks = asl;
  769. Assert_true(splitLinkParent->child == splitLink->parent);
  770. asl->inverseLinkEncodingFormNumber = splitLink->inverseLinkEncodingFormNumber;
  771. asl->node = splitLink->child;
  772. asl->path = extendRoute(splitLink->parent->address.path,
  773. splitLink->parent->encodingScheme,
  774. splitLink->cannonicalLabel,
  775. splitLinkParent->inverseLinkEncodingFormNumber);
  776. }
  777. static struct Node_Link* discoverLinkB(struct NodeStore_pvt* store,
  778. struct Node_Link* closestKnown,
  779. uint64_t pathKnownParentChild,
  780. struct Node_Two* child,
  781. uint64_t discoveredPath,
  782. int inverseLinkEncodingFormNumber)
  783. {
  784. // Make sure this link cannot be split before beginning.
  785. struct Node_Link* closest = NULL;
  786. uint64_t pathParentChild = findClosest(pathKnownParentChild, &closest, closestKnown, store);
  787. if (pathParentChild == findClosest_INVALID) {
  788. return NULL;
  789. }
  790. struct Node_Two* parent = closest->child;
  791. #ifdef Log_DEBUG
  792. {
  793. uint8_t parentStr[40];
  794. uint8_t childStr[40];
  795. uint8_t pathStr[20];
  796. AddrTools_printIp(parentStr, parent->address.ip6.bytes);
  797. AddrTools_printIp(childStr, child->address.ip6.bytes);
  798. AddrTools_printPath(pathStr, pathParentChild);
  799. Log_debug(store->logger, "discoverLinkB( [%s]->[%s] [%s] )", parentStr, childStr, pathStr);
  800. }
  801. #endif
  802. if (parent == child) {
  803. if (pathParentChild == 1) {
  804. // Link is already known.
  805. update(closest, 0, store);
  806. //Log_debug(store->logger, "Already known");
  807. return closest;
  808. }
  809. Log_debug(store->logger, "Loopey route");
  810. // lets not bother storing this link, a link with the same parent and child is
  811. // invalid according to verify() and it's just going to take up space in the store
  812. // we'll return closest which is a perfectly valid path to the same node.
  813. return closest;
  814. }
  815. while (pathParentChild == 1) {
  816. logLink(store, closest, "Node at end of path appears to have changed");
  817. // This should never happen for a direct peer or for a direct decendent in a split link.
  818. Assert_true(closestKnown != closest);
  819. // This probably means the parent node has renumbered it's switch...
  820. RumorMill_addNode(store->renumberMill, &closest->parent->address);
  821. check(store);
  822. // But it's possible someone is just lieing to us.
  823. return NULL;
  824. }
  825. // link parent to child
  826. //
  827. // ACKTUNG: From this point forward, the nodeStore is in an invalid state, calls to _verify()
  828. // will fail (calls to _check() will still succeed). We have linked parent with child
  829. // but we have not split all of the splitLinks from parent.
  830. //
  831. // TODO(cjd): linking every node with 0 link state, this can't be right.
  832. struct Node_Link* parentLink = linkNodes(parent,
  833. child,
  834. pathParentChild,
  835. 0,
  836. inverseLinkEncodingFormNumber,
  837. discoveredPath,
  838. store);
  839. if (!RB_FIND(NodeRBTree, &store->nodeTree, child)) {
  840. checkNode(child, store);
  841. RB_INSERT(NodeRBTree, &store->nodeTree, child);
  842. store->pub.nodeCount++;
  843. }
  844. check(store);
  845. #ifdef PARANOIA
  846. int verifyOrder = 0;
  847. #endif
  848. // Check whether the parent is already linked with a node which is "behind" the child.
  849. // splitLink appears to be a "sibling link" to the closest->node link but in reality the
  850. // splitLink link should be split and node should be inserted in the middle.
  851. struct Node_Link* splitLink = RB_MIN(PeerRBTree, &parent->peerTree);
  852. while (splitLink) {
  853. if (splitLink == parentLink) {
  854. #ifdef PARANOIA
  855. verifyOrder = 1;
  856. splitLink = PeerRBTree_RB_NEXT(splitLink);
  857. continue;
  858. #else
  859. // Since they're in order, definitely not found.
  860. break;
  861. #endif
  862. }
  863. if (!LabelSplicer_routesThrough(splitLink->cannonicalLabel, pathParentChild)) {
  864. splitLink = PeerRBTree_RB_NEXT(splitLink);
  865. continue;
  866. }
  867. #ifdef PARANOIA
  868. Assert_true(!verifyOrder);
  869. #endif
  870. struct Node_Two* grandChild = splitLink->child;
  871. if (child == grandChild) {
  872. // loopey route, kill it and let the bestParent pivit over to parentLink
  873. } else if (Node_getBestParent(grandChild) != splitLink) {
  874. // If the best parent is splitLink, we have to split the link *NOW* but if it is
  875. // not, we can just kill the link (without disrupting the child node) and wait
  876. // until the discoverLink process is complete then call discoverLink for the child
  877. // nodes which we have killed off.
  878. // This will prevent hellish bugs when this function recurses and alters the same
  879. // node which it is currently discovering.
  880. asyncSplitLink(store, splitLink, closest);
  881. } else {
  882. // So the grandChild's bestParent is the parent, this is kind of annoying because
  883. // if we were to split it asynchronously, we would unlink it and it would wreak
  884. // havoc on the grandChild and her decendents, possibly even making them unreachable.
  885. logLink(store, splitLink, "Synchronously splitting link");
  886. // unsplice and cannonicalize so we now have a path from child to grandchild
  887. uint64_t childToGrandchild =
  888. LabelSplicer_unsplice(splitLink->cannonicalLabel, parentLink->cannonicalLabel);
  889. childToGrandchild =
  890. EncodingScheme_convertLabel(child->encodingScheme,
  891. childToGrandchild,
  892. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  893. Assert_true(childToGrandchild < UINT64_MAX);
  894. Assert_true(childToGrandchild != 1);
  895. Assert_true(splitLink->cannonicalLabel != parentLink->cannonicalLabel);
  896. // child might actually decend from grandChild or some other wacky crap but when
  897. // we kill the link from parent to grandChild, things will wort themselves out...
  898. discoverLinkB(store, parentLink, childToGrandchild, grandChild,
  899. discoveredPath, parentLink->inverseLinkEncodingFormNumber);
  900. }
  901. check(store);
  902. // Recursion schanigans should not be possible...
  903. Assert_true(splitLink->parent);
  904. struct Node_Link* next = PeerRBTree_RB_NEXT(splitLink);
  905. NodeStore_unlinkNodes(&store->pub, splitLink);
  906. splitLink = next;
  907. }
  908. check(store);
  909. return parentLink;
  910. }
  911. static struct Node_Link* discoverLink(struct NodeStore_pvt* store,
  912. struct Node_Link* closestKnown,
  913. uint64_t pathKnownParentChild,
  914. struct Node_Two* child,
  915. uint64_t discoveredPath,
  916. int inverseLinkEncodingFormNumber)
  917. {
  918. struct Node_Link* link =
  919. discoverLinkB(store, closestKnown, pathKnownParentChild, child,
  920. discoveredPath, inverseLinkEncodingFormNumber);
  921. if (link) { Assert_true(link->child); }
  922. while (store->asyncSplitLinks) {
  923. struct NodeStore_AsyncSplitLinks* asl = store->asyncSplitLinks;
  924. store->asyncSplitLinks = asl->next;
  925. discoverLinkB(store, store->selfLink, asl->path, asl->node,
  926. discoveredPath, asl->inverseLinkEncodingFormNumber);
  927. }
  928. if (link && !link->child) {
  929. uint64_t pathParentChild = findClosest(pathKnownParentChild, &link, closestKnown, store);
  930. // This should always be 1 because the link is gone only because it was just split!
  931. Assert_true(pathParentChild == 1);
  932. }
  933. if (store->asyncSplitLinksAlloc) {
  934. Allocator_free(store->asyncSplitLinksAlloc);
  935. store->asyncSplitLinksAlloc = NULL;
  936. }
  937. return link;
  938. }
  939. static struct Node_Two* whichIsWorse(struct Node_Two* one,
  940. struct Node_Two* two,
  941. struct NodeStore_pvt* store)
  942. {
  943. if (one->address.protocolVersion != two->address.protocolVersion) {
  944. if (one->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  945. if (two->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  946. return one;
  947. }
  948. } else if (two->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  949. if (one->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  950. return two;
  951. }
  952. }
  953. }
  954. if (Node_getReach(one) < Node_getReach(two)) { return one; }
  955. if (Node_getReach(two) < Node_getReach(one)) { return two; }
  956. if (Address_closest(&store->pub.selfNode->address, &one->address, &two->address) > 0) {
  957. return one;
  958. }
  959. return two;
  960. }
  961. static bool markBestNodes(struct NodeStore_pvt* store,
  962. struct Address* targetAddress,
  963. const uint32_t count)
  964. {
  965. struct Allocator* nodeListAlloc = Allocator_child(store->alloc);
  966. struct NodeList* nodeList = Allocator_malloc(nodeListAlloc, sizeof(struct NodeList));
  967. nodeList->nodes = Allocator_calloc(nodeListAlloc, count, sizeof(char*));
  968. nodeList->size = 0;
  969. struct Node_Two* nn = NULL;
  970. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  971. if (Address_closest(targetAddress, store->pub.selfAddress, &nn->address) > 0) {
  972. // This node is closer to the destination than we are.
  973. struct Node_Two* newNode = nn;
  974. struct Node_Two* tempNode = NULL;
  975. for (uint32_t i = 0 ; i < count ; i++) {
  976. if (nodeList->size < i+1) {
  977. // The list isn't full yet, so insert at the end.
  978. nodeList->size = i+1;
  979. nodeList->nodes[i] = newNode;
  980. break;
  981. }
  982. if ( (newNode->marked && !nodeList->nodes[i]->marked) ||
  983. (Node_getReach(nodeList->nodes[i]) < Node_getReach(newNode)) ) {
  984. // If we've already marked nodes because they're a bestParent,
  985. // lets give them priority in the bucket since we need to keep
  986. // them either way.
  987. // Otherwise, highest reach wins.
  988. // Insertion sorted list.
  989. tempNode = nodeList->nodes[i];
  990. nodeList->nodes[i] = newNode;
  991. newNode = tempNode;
  992. }
  993. }
  994. }
  995. }
  996. bool retVal = false;
  997. if (nodeList->size > 0) { retVal = true; }
  998. for (uint32_t i = 0; i < nodeList->size; i++) {
  999. // Now mark the nodes in the list to protect them.
  1000. Identity_check(nodeList->nodes[i]);
  1001. nodeList->nodes[i]->marked = 1;
  1002. }
  1003. // Cleanup
  1004. Allocator_free(nodeListAlloc);
  1005. return retVal;
  1006. }
  1007. #define Kademlia_bucketSize 8
  1008. static void markKeyspaceNodes(struct NodeStore_pvt* store)
  1009. {
  1010. struct Address addr = *store->pub.selfAddress;
  1011. uint8_t emptyBuckets = 0;
  1012. uint8_t byte = 0;
  1013. uint8_t bit = 0;
  1014. for (uint8_t i = 0; i < 128 ; i++) {
  1015. // Bitwise walk across keyspace
  1016. if (63 < i && i < 72) {
  1017. // We want to leave the 0xfc alone
  1018. continue;
  1019. }
  1020. // Figure out which bit of the address to flip for this step in keyspace.
  1021. // This looks ugly because of the rot64 done in distance calculations.
  1022. if (i < 64) { byte = 8 + (i/8); }
  1023. else { byte = (i/8) - 8; }
  1024. bit = (i % 8);
  1025. // Flip that bit.
  1026. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  1027. // Mark the best nodes for this hop.
  1028. // TODO(arceliar): Current implementation (calling markBestNodes on everything)
  1029. // scales poorly. Temporary workaround is to catch when we've found some
  1030. // number of empty buckets and then exit. (done)
  1031. // Better implementation would be to iterate over the tree *once* to fill NodeLists
  1032. // for every bucket. Then iterate over all lists marking the nodes in the lists.
  1033. if (!markBestNodes(store, &addr, Kademlia_bucketSize)) { emptyBuckets++; }
  1034. if ( emptyBuckets > 16 ) { return; }
  1035. // Flip the bit back and continue.
  1036. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  1037. }
  1038. }
  1039. /**
  1040. * We define the worst node the node with the lowest reach, excluding nodes which are required for
  1041. * the DHT, and nodes which are somebody's bestParent (only relevant if they're the bestParent of
  1042. * a DHT-required node, as otherwise their child would always be lower reach).
  1043. * If two nodes tie (e.g. two unreachable nodes with 0 reach) then the node which is
  1044. * further from us in keyspace is worse.
  1045. */
  1046. static struct Node_Two* getWorstNode(struct NodeStore_pvt* store)
  1047. {
  1048. struct Node_Two* worst = NULL;
  1049. struct Node_Two* nn = NULL;
  1050. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1051. // first cycle we clear all markings as we go and set markings
  1052. // so markings remain if they are behind us
  1053. nn->marked = 0;
  1054. struct Node_Link* parentLink = Node_getBestParent(nn);
  1055. if (parentLink) {
  1056. parentLink->parent->marked = 1;
  1057. } else if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1058. // this time around we're only addressing nodes which are unreachable.
  1059. worst = nn;
  1060. }
  1061. }
  1062. if (worst) { return worst; }
  1063. // Mark the nodes that we need to protect for keyspace reasons.
  1064. markKeyspaceNodes(store);
  1065. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1066. // second cycle we set the markings as we go but if they are behind the
  1067. // node which would have marked them, they are already set.
  1068. struct Node_Link* parentLink = Node_getBestParent(nn);
  1069. if (parentLink) {
  1070. parentLink->parent->marked = 1;
  1071. }
  1072. if (nn->marked) { continue; }
  1073. if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1074. worst = nn;
  1075. }
  1076. }
  1077. if (worst) { return worst; }
  1078. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1079. // third cycle, every node is apparently important but we need to get rid of someone
  1080. // get whoever is worst if we ignore markings
  1081. // by definition, this shouldn't be a bestParent, because their children have lower reach
  1082. // so we're potentially creating a keyspace hole (routing blackhole) when we do this.
  1083. // TODO(arceliar): protect keyspace, evict the worst bestParent instead?
  1084. // Would require something like a forgetNode() to splice links together between
  1085. // that node's bestParent and all its children, before we kill it.
  1086. if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1087. worst = nn;
  1088. }
  1089. }
  1090. // somebody has to be at the end of the line, not *everyone* can be someone's best parent!
  1091. Assert_true(worst);
  1092. return worst;
  1093. }
  1094. static void destroyNode(struct Node_Two* node, struct NodeStore_pvt* store)
  1095. {
  1096. struct Node_Link* link;
  1097. RB_FOREACH(link, PeerRBTree, &node->peerTree) {
  1098. Identity_check(link);
  1099. NodeStore_unlinkNodes(&store->pub, link);
  1100. }
  1101. // If the node has a bestParent, it will be changed a number
  1102. // of times as we kill off all of it's parent links.
  1103. // This is an optimization:
  1104. #ifndef PARANOIA
  1105. Node_setParentReachAndPath(node, NULL, 0, UINT64_MAX);
  1106. #endif
  1107. link = node->reversePeers;
  1108. while (link) {
  1109. struct Node_Link* nextLink = link->nextPeer;
  1110. NodeStore_unlinkNodes(&store->pub, link);
  1111. link = nextLink;
  1112. }
  1113. Assert_ifParanoid(!Node_getBestParent(node));
  1114. Assert_ifParanoid(node == RB_FIND(NodeRBTree, &store->nodeTree, node));
  1115. RB_REMOVE(NodeRBTree, &store->nodeTree, node);
  1116. store->pub.nodeCount--;
  1117. Allocator_free(node->alloc);
  1118. }
  1119. // Must be at least 2 to avoid multiplying by 0.
  1120. // If too large, path choice may become unstable due to a guess we make in calcNextReach.
  1121. // This is fixable by storing reach based on links. A lot of work.
  1122. // In the mean time, just don't use a large value.
  1123. #define NodeStore_latencyWindow 8
  1124. static uint32_t reachAfterDecay(const uint32_t oldReach)
  1125. {
  1126. // Reduce the reach by 1/Xth where X = NodeStore_latencyWindow
  1127. // This is used to keep a weighted rolling average
  1128. return (oldReach - (oldReach / NodeStore_latencyWindow));
  1129. }
  1130. static uint32_t reachAfterTimeout(const uint32_t oldReach)
  1131. {
  1132. // TODO(arceliar) just use reachAfterDecay?... would be less penalty for timeouts...
  1133. return (oldReach / 2);
  1134. }
  1135. static uint32_t calcNextReach(const uint32_t oldReach, const uint32_t millisecondsLag)
  1136. {
  1137. int64_t out = reachAfterDecay(oldReach) +
  1138. ((UINT32_MAX / NodeStore_latencyWindow) / (millisecondsLag + 1));
  1139. if (!oldReach) {
  1140. // We don't know the old reach for this path.
  1141. // If every response comes in after same millisecondsLag, then we expect that the
  1142. // reach will stabilize to a value of (out * NodeStoare_latencyWindow).
  1143. // Lets guess what the reach will stabilize to, but try to be a little conservative,
  1144. // so we don't cause bestParents to switch unless the new route is appreciably better.
  1145. out = out * (NodeStore_latencyWindow - 1);
  1146. }
  1147. // TODO(arceliar): is this safe?
  1148. Assert_true(out < (UINT32_MAX - 1024) && out > 0);
  1149. return out;
  1150. }
  1151. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  1152. struct Address* addr,
  1153. struct EncodingScheme* scheme,
  1154. int inverseLinkEncodingFormNumber,
  1155. uint64_t milliseconds)
  1156. {
  1157. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1158. verify(store);
  1159. // conservative guess of what the reach would stabilize to
  1160. uint32_t reach = calcNextReach(0, milliseconds);
  1161. struct Node_Two* child = nodeForIp(store, addr->ip6.bytes);
  1162. #ifdef Log_DEBUG
  1163. uint8_t printedAddr[60];
  1164. Address_print(printedAddr, addr);
  1165. Log_debug(store->logger, "Discover node [%s]", printedAddr);
  1166. #endif
  1167. struct Allocator* alloc = NULL;
  1168. if (!child) {
  1169. alloc = Allocator_child(store->alloc);
  1170. child = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1171. child->alloc = alloc;
  1172. Bits_memcpyConst(&child->address, addr, sizeof(struct Address));
  1173. child->encodingScheme = EncodingScheme_clone(scheme, child->alloc);
  1174. Identity_set(child);
  1175. }
  1176. struct Node_Link* link = NULL;
  1177. for (;;) {
  1178. link = discoverLink(store,
  1179. store->selfLink,
  1180. addr->path,
  1181. child,
  1182. addr->path,
  1183. inverseLinkEncodingFormNumber);
  1184. if (link) { break; }
  1185. // We might have a broken link in the store which is causing new links to be rejected.
  1186. // On the other hand, this path might actually be garbage :)
  1187. // There's a DoS risk in that someone might use garbage paths to evict all of the
  1188. // existing good paths.
  1189. // While an attacker can send in a packet, it will necessarily follow a ridiculous path
  1190. // in order that the path contains one of their nodes.
  1191. // To resolve this, we'll walk the path looking for the "bad" link, then we'll check that
  1192. // node to see if the path we took to reach it is actually the *best* path to that node.
  1193. uint64_t path = addr->path;
  1194. struct Node_Link* lastLink = store->selfLink;
  1195. do {
  1196. struct Node_Link* nextLink = NULL;
  1197. path = firstHopInPath(path, &nextLink, lastLink, store);
  1198. lastLink = nextLink;
  1199. if (path == firstHopInPath_NO_NEXT_LINK) {
  1200. // discoverNode() failed for some other reason.
  1201. lastLink = NULL;
  1202. break;
  1203. }
  1204. } while (firstHopInPath_INVALID != path);
  1205. if (lastLink && LabelSplicer_routesThrough(addr->path, lastLink->child->address.path)) {
  1206. // checking for sillyness...
  1207. Assert_true(lastLink != store->selfLink);
  1208. NodeStore_unlinkNodes(&store->pub, lastLink);
  1209. continue;
  1210. }
  1211. if (alloc) {
  1212. Allocator_free(alloc);
  1213. }
  1214. verify(store);
  1215. Log_debug(store->logger, "Invalid path");
  1216. return NULL;
  1217. }
  1218. if (link->parent == store->pub.selfNode && !Node_getBestParent(link->child)) {
  1219. updateBestParent(link, reach, store);
  1220. }
  1221. handleNews(link->child, reach, store);
  1222. freePendingLinks(store);
  1223. while (store->pub.nodeCount >= store->pub.nodeCapacity
  1224. || store->pub.linkCount >= store->pub.linkCapacity)
  1225. {
  1226. struct Node_Two* worst = getWorstNode(store);
  1227. #ifdef Log_DEBUG
  1228. uint8_t worstAddr[60];
  1229. Address_print(worstAddr, &worst->address);
  1230. Log_debug(store->logger, "store full, removing worst node: [%s] nodes [%d] links [%d]",
  1231. worstAddr, store->pub.nodeCount, store->pub.linkCount);
  1232. #endif
  1233. destroyNode(worst, store);
  1234. freePendingLinks(store);
  1235. }
  1236. verify(store);
  1237. return link;
  1238. }
  1239. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16])
  1240. {
  1241. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1242. struct Node_Two* n = nodeForIp(store, addr);
  1243. if (n && n->address.path == UINT64_MAX) {
  1244. #ifdef Log_DEBUG
  1245. uint8_t addrStr[40];
  1246. AddrTools_printIp(addrStr, n->address.ip6.bytes);
  1247. Log_debug(store->logger, "No way to represent path to [%s]", addrStr);
  1248. #endif
  1249. return NULL;
  1250. }
  1251. return n;
  1252. }
  1253. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path)
  1254. {
  1255. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1256. struct Node_Link* out = NULL;
  1257. findClosest(path, &out, store->selfLink, store);
  1258. if (!out) { return NULL; }
  1259. return Identity_check(out->child);
  1260. }
  1261. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path)
  1262. {
  1263. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1264. struct Node_Link* out = NULL;
  1265. uint64_t pathParentChild = findClosest(path, &out, store->selfLink, store);
  1266. if (pathParentChild != 1) { return NULL; }
  1267. return Identity_check(out);
  1268. }
  1269. struct Node_Link* NodeStore_firstHopInPath(struct NodeStore* nodeStore,
  1270. uint64_t path,
  1271. uint64_t* correctedPath,
  1272. struct Node_Link* startingPoint)
  1273. {
  1274. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1275. if (!startingPoint) { startingPoint = store->selfLink; }
  1276. if (!Node_getBestParent(startingPoint->parent)) { return NULL; }
  1277. struct Node_Link* out = NULL;
  1278. path = firstHopInPath(path, &out, startingPoint, store);
  1279. if (firstHopInPath_ERR(path)) { return NULL; }
  1280. if (correctedPath) { *correctedPath = path; }
  1281. return out;
  1282. }
  1283. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal)
  1284. {
  1285. switch (returnVal) {
  1286. case NodeStore_getRouteLabel_PARENT_NOT_FOUND:
  1287. return "NodeStore_getRouteLabel_PARENT_NOT_FOUND";
  1288. case NodeStore_getRouteLabel_CHILD_NOT_FOUND:
  1289. return "NodeStore_getRouteLabel_CHILD_NOT_FOUND";
  1290. default: return NULL;
  1291. }
  1292. }
  1293. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  1294. uint64_t pathToParent,
  1295. uint64_t pathParentToChild)
  1296. {
  1297. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1298. struct Node_Link* linkToParent;
  1299. if (findClosest(pathToParent, &linkToParent, store->selfLink, store) != 1) {
  1300. return NodeStore_getRouteLabel_PARENT_NOT_FOUND;
  1301. }
  1302. logLink(store, linkToParent, "NodeStore_getRouteLabel() PARENT");
  1303. struct Node_Link* linkToChild = NULL;
  1304. RB_FOREACH_REVERSE(linkToChild, PeerRBTree, &linkToParent->child->peerTree) {
  1305. if (pathParentToChild == linkToChild->cannonicalLabel) {
  1306. if (linkToParent == store->selfLink) {
  1307. return linkToChild->cannonicalLabel;
  1308. }
  1309. return extendRoute(pathToParent,
  1310. linkToChild->parent->encodingScheme,
  1311. linkToChild->cannonicalLabel,
  1312. linkToParent->inverseLinkEncodingFormNumber);
  1313. }
  1314. }
  1315. return NodeStore_getRouteLabel_CHILD_NOT_FOUND;
  1316. }
  1317. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path)
  1318. {
  1319. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1320. struct Node_Link* linkToParent;
  1321. uint64_t next = findClosest(path, &linkToParent, store->selfLink, store);
  1322. if (next == findClosest_INVALID) {
  1323. return NodeStore_optimizePath_INVALID;
  1324. }
  1325. if (EncodingScheme_isSelfRoute(linkToParent->child->encodingScheme, next)) {
  1326. // cannoicalize all the other wild ways that they can represent self routes.
  1327. // TODO(cjd): this has been the source of assert failures and we might be sweeping
  1328. // a significant bug under the carpet.
  1329. next = 1;
  1330. }
  1331. if (linkToParent == store->selfLink) {
  1332. if (next == 1) { return 1; }
  1333. return path;
  1334. }
  1335. if (next == 1) { return linkToParent->child->address.path; }
  1336. struct Node_Link* childBestParent = Node_getBestParent(linkToParent->child);
  1337. if (childBestParent) {
  1338. linkToParent = childBestParent;
  1339. }
  1340. uint64_t optimized = extendRoute(linkToParent->child->address.path,
  1341. linkToParent->child->encodingScheme,
  1342. next,
  1343. linkToParent->inverseLinkEncodingFormNumber);
  1344. if (optimized < UINT64_MAX) {
  1345. return optimized;
  1346. }
  1347. return path;
  1348. }
  1349. struct Node_Link* NodeStore_nextLink(struct Node_Two* parent, struct Node_Link* startLink)
  1350. {
  1351. if (!startLink) {
  1352. startLink = RB_MIN(PeerRBTree, &parent->peerTree);
  1353. if (!startLink) { return NULL; }
  1354. }
  1355. return PeerRBTree_RB_NEXT(startLink);
  1356. }
  1357. /** See: NodeStore.h */
  1358. struct NodeStore* NodeStore_new(struct Address* myAddress,
  1359. struct Allocator* allocator,
  1360. struct Log* logger,
  1361. struct RumorMill* renumberMill)
  1362. {
  1363. struct Allocator* alloc = Allocator_child(allocator);
  1364. struct NodeStore_pvt* out = Allocator_clone(alloc, (&(struct NodeStore_pvt) {
  1365. .pub = {
  1366. .nodeCapacity = NodeStore_DEFAULT_NODE_CAPACITY,
  1367. .linkCapacity = NodeStore_DEFAULT_LINK_CAPACITY
  1368. },
  1369. .renumberMill = renumberMill,
  1370. .logger = logger,
  1371. .alloc = alloc
  1372. }));
  1373. Identity_set(out);
  1374. // Create the self node
  1375. struct Node_Two* selfNode = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1376. Bits_memcpyConst(&selfNode->address, myAddress, sizeof(struct Address));
  1377. selfNode->encodingScheme = NumberCompress_defineScheme(alloc);
  1378. selfNode->alloc = alloc;
  1379. Identity_set(selfNode);
  1380. out->pub.selfNode = selfNode;
  1381. struct Node_Link* selfLink = linkNodes(selfNode, selfNode, 1, 0xffffffffu, 0, 1, out);
  1382. Node_setParentReachAndPath(selfNode, selfLink, UINT32_MAX, 1);
  1383. out->selfLink = selfLink;
  1384. RB_INSERT(NodeRBTree, &out->nodeTree, selfNode);
  1385. out->pub.selfAddress = &out->selfLink->child->address;
  1386. return &out->pub;
  1387. }
  1388. //////////////////////////////////////////////////////////////////////////////////////////////
  1389. //////////////////////////////////////////////////////////////////////////////////////////////
  1390. //////////////////////////////////////////////////////////////////////////////////////////////
  1391. /**
  1392. * Dump the table, one node at a time.
  1393. */
  1394. struct Node_Two* NodeStore_dumpTable(struct NodeStore* nodeStore, uint32_t index)
  1395. {
  1396. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1397. // TODO(cjd): Schlameil the painter
  1398. uint32_t i = 0;
  1399. struct Node_Two* nn = NULL;
  1400. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1401. if (i++ == index) { return nn; }
  1402. }
  1403. return NULL;
  1404. }
  1405. static struct Node_Two* getBestCycleB(struct Node_Two* node,
  1406. struct Address* target,
  1407. struct NodeStore_pvt* store)
  1408. {
  1409. uint32_t targetPfx = Address_getPrefix(target);
  1410. uint32_t ourDistance = Address_getPrefix(store->pub.selfAddress) ^ targetPfx;
  1411. struct Node_Link* next = NULL;
  1412. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1413. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1414. if (next->child->address.path == UINT64_MAX) { continue; }
  1415. if ((Address_getPrefix(&next->child->address) ^ targetPfx) >= ourDistance) { continue; }
  1416. return next->child;
  1417. }
  1418. return NULL;
  1419. }
  1420. static int getBestCycle(struct Node_Two* node,
  1421. struct Address* target,
  1422. struct Node_Two** output,
  1423. int limit,
  1424. int cycle,
  1425. struct NodeStore_pvt* store)
  1426. {
  1427. Assert_true(cycle < 1000);
  1428. if (cycle < limit) {
  1429. int total = 0;
  1430. struct Node_Link* next = NULL;
  1431. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1432. if (*output) { return total; }
  1433. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1434. total += getBestCycle(next->child, target, output, limit, cycle+1, store);
  1435. }
  1436. return total;
  1437. }
  1438. *output = getBestCycleB(node, target, store);
  1439. return 1;
  1440. }
  1441. struct Node_Two* NodeStore_getBest(struct Address* targetAddress, struct NodeStore* nodeStore)
  1442. {
  1443. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1444. struct Node_Two* n = NodeStore_nodeForAddr(nodeStore, targetAddress->ip6.bytes);
  1445. if (n && Node_getBestParent(n)) { return n; }
  1446. for (int i = 0; i < 10000; i++) {
  1447. int ret = getBestCycle(store->pub.selfNode, targetAddress, &n, i, 0, store);
  1448. if (n || !ret) { return n; }
  1449. }
  1450. return NULL;
  1451. }
  1452. struct NodeList* NodeStore_getPeers(uint64_t label,
  1453. const uint32_t max,
  1454. struct Allocator* allocator,
  1455. struct NodeStore* nodeStore)
  1456. {
  1457. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1458. // truncate the label to the part which this node uses...
  1459. label &= Bits_maxBits64(NumberCompress_bitsUsedForLabel(label));
  1460. struct NodeList* out = Allocator_calloc(allocator, sizeof(struct NodeList), 1);
  1461. out->nodes = Allocator_calloc(allocator, sizeof(char*), max);
  1462. struct Node_Link* next = NULL;
  1463. RB_FOREACH_REVERSE(next, PeerRBTree, &store->pub.selfNode->peerTree) {
  1464. uint64_t p = next->child->address.path;
  1465. if (p > (((uint64_t)1)<<63)) { continue; }
  1466. int j;
  1467. for (j = 0; j < (int)max; j++) {
  1468. if (out->nodes[j] && (out->nodes[j]->address.path ^ label) < (p ^ label)) {
  1469. break;
  1470. }
  1471. }
  1472. switch (j) {
  1473. default: Bits_memmove(out->nodes, &out->nodes[1], (j - 1) * sizeof(char*));
  1474. case 1: out->nodes[j - 1] = next->child;
  1475. case 0:;
  1476. }
  1477. }
  1478. out->size = 0;
  1479. for (int i = 0; i < (int)max; i++) {
  1480. if (out->nodes[i]) {
  1481. out->nodes = &out->nodes[i];
  1482. out->size = max - i;
  1483. break;
  1484. }
  1485. }
  1486. for (int i = 0; i < (int)out->size; i++) {
  1487. Identity_check(out->nodes[i]);
  1488. checkNode(out->nodes[i], store);
  1489. Assert_true(out->nodes[i]->address.path);
  1490. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1491. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1492. }
  1493. return out;
  1494. }
  1495. static bool isOkAnswer(struct Node_Two* node,
  1496. uint32_t compatVer,
  1497. struct NodeStore_pvt* store)
  1498. {
  1499. if (node->address.path == UINT64_MAX) {
  1500. // (very) unreachable
  1501. return false;
  1502. }
  1503. if (!Version_isCompatible(compatVer, node->address.protocolVersion)) {
  1504. return false;
  1505. }
  1506. if (node == store->pub.selfNode) {
  1507. return false;
  1508. }
  1509. return true;
  1510. }
  1511. /** See: NodeStore.h */
  1512. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* nodeStore,
  1513. struct Address* targetAddress,
  1514. const uint32_t count,
  1515. uint32_t compatVer,
  1516. struct Allocator* allocator)
  1517. {
  1518. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1519. struct NodeList* out = Allocator_malloc(allocator, sizeof(struct NodeList));
  1520. out->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  1521. out->size = count;
  1522. struct Node_Two fakeNode = { .marked = 0 };
  1523. Bits_memcpyConst(&fakeNode.address, targetAddress, sizeof(struct Address));
  1524. struct Node_Two* next = Identity_ncheck(RB_NFIND(NodeRBTree, &store->nodeTree, &fakeNode));
  1525. if (!next) {
  1526. out->size = 0;
  1527. return out;
  1528. }
  1529. struct Node_Two* prev = Identity_ncheck(NodeRBTree_RB_PREV(next));
  1530. int idx = out->size-1;
  1531. while (idx > -1) {
  1532. if (prev && (!next || Address_closest(targetAddress, &next->address, &prev->address) > 0)) {
  1533. if (isOkAnswer(prev, compatVer, store)) { out->nodes[idx--] = prev; }
  1534. prev = Identity_ncheck(NodeRBTree_RB_PREV(prev));
  1535. continue;
  1536. }
  1537. if (next && (!prev || Address_closest(targetAddress, &next->address, &prev->address) < 0)) {
  1538. if (isOkAnswer(next, compatVer, store)) { out->nodes[idx--] = next; }
  1539. next = Identity_ncheck(NodeRBTree_RB_NEXT(next));
  1540. continue;
  1541. }
  1542. break;
  1543. }
  1544. out->nodes = &out->nodes[idx+1];
  1545. out->size -= idx+1;
  1546. for (int i = 0; i < (int)out->size; i++) {
  1547. Identity_check(out->nodes[i]);
  1548. Assert_true(out->nodes[i]->address.path);
  1549. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1550. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1551. }
  1552. return out;
  1553. }
  1554. // TODO(cjd): There's no such thing as a "broken path", there's a broken *link* but we don't
  1555. // know exactly which link is broken, we need to interpret the incoming error message
  1556. // better and determine which link is likely broken and then send a getPeers message
  1557. // to the node before it to check if the next link is nolonger valid.
  1558. void NodeStore_brokenPath(uint64_t path, struct NodeStore* nodeStore)
  1559. {
  1560. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1561. verify(store);
  1562. #ifdef Log_DEBUG
  1563. uint8_t pathStr[20];
  1564. AddrTools_printPath(pathStr, path);
  1565. Log_debug(store->logger, "NodeStore_brokenPath(%s)", pathStr);
  1566. #endif
  1567. struct Node_Link* nl = NodeStore_linkForPath(nodeStore, path);
  1568. if (nl && Node_getReach(nl->child) > 0) {
  1569. handleBadNews(nl->child, 0, store);
  1570. }
  1571. verify(store);
  1572. }
  1573. // When a response comes in, we need to pay attention to the path used.
  1574. static void updatePathReach(struct NodeStore_pvt* store, const uint64_t path, uint32_t newReach)
  1575. {
  1576. struct Node_Link* link = store->selfLink;
  1577. uint64_t pathFrag = path;
  1578. for (;;) {
  1579. struct Node_Link* nextLink = NULL;
  1580. uint64_t nextPath = firstHopInPath(pathFrag, &nextLink, link, store);
  1581. if (firstHopInPath_ERR(nextPath)) {
  1582. break;
  1583. }
  1584. // expecting behavior of nextLinkOnPath()
  1585. Assert_ifParanoid(nextLink->parent == link->child);
  1586. if (Node_getBestParent(nextLink->child) == nextLink) {
  1587. // This is a performance hack, if nextLink->child->bestParent->parent is this node
  1588. // we'll skip updating reach here since even if we did, it would be updated all over
  1589. // again by the recursion inside of handleGoodNews because we're not deincrementing
  1590. // newReach per hop.
  1591. } else if (Node_getReach(link->child) >= newReach) {
  1592. // Node already has enough reach...
  1593. // selfNode reach == UINT32_MAX so this case handles it.
  1594. } else if (!LabelSplicer_routesThrough(path, link->child->address.path)) {
  1595. // The path the packet came in on is not actually the best known path to the node.
  1596. } else {
  1597. handleNews(link->child, newReach, store);
  1598. }
  1599. pathFrag = nextPath;
  1600. link = nextLink;
  1601. }
  1602. // Now we have to unconditionally update the reach for the last link in the chain.
  1603. if (link->child && link->child->address.path == path) {
  1604. // Behavior of nextLinkOnPath()
  1605. Assert_ifParanoid(pathFrag == 1);
  1606. handleNews(link->child, newReach, store);
  1607. }
  1608. }
  1609. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds)
  1610. {
  1611. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1612. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1613. if (!link) { return; }
  1614. struct Node_Two* node = link->child;
  1615. uint32_t newReach;
  1616. if (node->address.path == path) {
  1617. // Use old reach value to calculate new reach
  1618. newReach = calcNextReach(Node_getReach(node), milliseconds);
  1619. }
  1620. else {
  1621. // Old reach value doesn't relate to this path, so we should do something different
  1622. // FIXME(arceliar): calcNextReach is guessing what the reach would stabilize to
  1623. // I think actually fixing this would require storing reach (or latency?) per link,
  1624. // so we can calculate the expected reach for an arbitrary path
  1625. newReach = calcNextReach(0, milliseconds);
  1626. }
  1627. updatePathReach(store, path, newReach);
  1628. }
  1629. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path)
  1630. {
  1631. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1632. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1633. if (!link || link->child->address.path != path) { return; }
  1634. struct Node_Two* node = link->child;
  1635. uint32_t newReach = reachAfterTimeout(Node_getReach(node));
  1636. #ifdef Log_DEBUG
  1637. uint8_t addr[60];
  1638. Address_print(addr, &node->address);
  1639. Log_debug(store->logger,
  1640. "Ping timeout for %s. changing reach from %u to %u\n",
  1641. addr,
  1642. Node_getReach(node),
  1643. newReach);
  1644. #endif
  1645. handleNews(node, newReach, store);
  1646. }