NodeStore.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  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 <https://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 "util/Defined.h"
  28. #include "util/Endian.h"
  29. #include "util/events/Time.h"
  30. #include <node_build/dependencies/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. /**
  43. * The links to be freed next time freePendingLinks() is called.
  44. */
  45. struct Node_Link* linksToFree;
  46. /** Nodes which have very likely been reset. */
  47. struct RumorMill* renumberMill;
  48. /** The means for this node store to log. */
  49. struct Log* logger;
  50. /** To track time, for e.g. figuring out when nodes were last pinged */
  51. struct EventBase* eventBase;
  52. // If this is non-zero then check() and verify() will be inactive.
  53. // Increment this if you're going to do the check yourself after the function you call is done.
  54. int disarmCheck;
  55. int fullVerify;
  56. Identity
  57. };
  58. // My memory is really bad
  59. #define A_COMES_FIRST 1
  60. #define B_COMES_FIRST -1
  61. static int comparePeers(const struct Node_Link* la, const struct Node_Link* lb)
  62. {
  63. Identity_check(lb);
  64. uint64_t a = la->cannonicalLabel;
  65. uint64_t b = lb->cannonicalLabel;
  66. int log2Diff = Bits_log2x64(b) - Bits_log2x64(a);
  67. if (log2Diff) {
  68. return log2Diff;
  69. }
  70. if (Bits_bitReverse64(a) < Bits_bitReverse64(b)) {
  71. return A_COMES_FIRST;
  72. } else if (a == b) {
  73. return 0;
  74. }
  75. return B_COMES_FIRST;
  76. }
  77. RB_GENERATE_STATIC(PeerRBTree, Node_Link, peerTree, comparePeers)
  78. static int compareNodes(const struct Node_Two* na, const struct Node_Two* nb)
  79. {
  80. Identity_check(nb);
  81. int ret;
  82. ret = Address_xorcmp(0, na->address.ip6.ints.one_be, nb->address.ip6.ints.one_be);
  83. if (ret) { return ret; }
  84. ret = Address_xorcmp(0, na->address.ip6.ints.two_be, nb->address.ip6.ints.two_be);
  85. if (ret) { return ret; }
  86. ret = Address_xorcmp(0, na->address.ip6.ints.three_be, nb->address.ip6.ints.three_be);
  87. if (ret) { return ret; }
  88. ret = Address_xorcmp(0, na->address.ip6.ints.four_be, nb->address.ip6.ints.four_be);
  89. return ret;
  90. }
  91. RB_GENERATE_STATIC(NodeRBTree, Node_Two, nodeTree, compareNodes)
  92. static void freeLink(struct Node_Link* link, struct NodeStore_pvt* store)
  93. {
  94. Allocator_realloc(store->alloc, link, 0);
  95. store->pub.linkCount--;
  96. }
  97. static struct Node_Link* getLink(struct NodeStore_pvt* store)
  98. {
  99. store->pub.linkCount++;
  100. return Allocator_calloc(store->alloc, sizeof(struct Node_Link), 1);
  101. }
  102. static void logLink(struct NodeStore_pvt* store,
  103. struct Node_Link* link,
  104. char* message)
  105. {
  106. if (!Defined(Log_DEBUG)) {
  107. return;
  108. }
  109. uint8_t parent[40];
  110. uint8_t child[40];
  111. AddrTools_printIp(parent, link->parent->address.ip6.bytes);
  112. AddrTools_printIp(child, link->child->address.ip6.bytes);
  113. uint8_t path[20];
  114. AddrTools_printPath(path, link->cannonicalLabel);
  115. Log_debug(store->logger, "link[%s]->[%s] [%s] %s", parent, child, path, message);
  116. }
  117. static void _checkNode(struct Node_Two* node, struct NodeStore_pvt* store, char* file, int line)
  118. {
  119. if (!Defined(PARANOIA) || (store->disarmCheck && !store->fullVerify)) { return; }
  120. Assert_true(node->address.path ==
  121. EncodingScheme_convertLabel(store->pub.selfNode->encodingScheme,
  122. node->address.path,
  123. EncodingScheme_convertLabel_convertTo_CANNONICAL));
  124. struct Node_Link* link;
  125. for (link = node->reversePeers; link; link = link->nextPeer) {
  126. Assert_fileLine(link->child == node, file, line);
  127. Assert_fileLine(RB_FIND(PeerRBTree, &link->parent->peerTree, link) == link, file, line);
  128. // This is for you arc
  129. int ok = 0;
  130. struct Node_Link* nl = NULL;
  131. while ((nl = NodeStore_nextLink(link->parent, nl))) {
  132. if (nl == link) { ok = 1; break; }
  133. }
  134. Assert_fileLine(ok, file, line);
  135. //
  136. }
  137. struct Node_Link* lastLink = NULL;
  138. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  139. Assert_fileLine(!EncodingScheme_isSelfRoute(link->parent->encodingScheme,
  140. link->cannonicalLabel)
  141. || link == store->selfLink,
  142. file, line);
  143. Assert_fileLine(Node_getBestParent(node) || Node_getBestParent(link->child) != link,
  144. file, line);
  145. Assert_fileLine(link->parent == node, file, line);
  146. // It's ok for a node to link back to itself via some loopy route
  147. //Assert_fileLine(link->child != node || link == store->selfLink, file, line);
  148. Assert_fileLine(!lastLink || link->cannonicalLabel != lastLink->cannonicalLabel,
  149. file, line);
  150. Assert_fileLine(link->cannonicalLabel < UINT64_MAX && link->cannonicalLabel > 0,
  151. file, line);
  152. // Make sure there isn't a link which has a completely wacky link encoding number.
  153. // Also make sure links are all flushed if a node is discovered to have changed it's
  154. // encoding scheme...
  155. Assert_fileLine(link->inverseLinkEncodingFormNumber < link->child->encodingScheme->count,
  156. file, line);
  157. struct Node_Link* rlink = NULL;
  158. for (rlink = link->child->reversePeers; rlink; rlink = rlink->nextPeer) {
  159. if (rlink == link) {
  160. break;
  161. }
  162. }
  163. Assert_fileLine(rlink && "child contains reverse link", file, line);
  164. lastLink = link;
  165. }
  166. if (Node_getBestParent(node)) {
  167. Assert_fileLine(node->address.path != UINT64_MAX, file, line);
  168. Assert_fileLine(Node_getCost(node) != UINT64_MAX, file, line);
  169. struct Node_Two* nn = node;
  170. do {
  171. Assert_fileLine(
  172. LabelSplicer_routesThrough(nn->address.path,
  173. Node_getBestParent(nn)->parent->address.path),
  174. file,
  175. line
  176. );
  177. nn = Node_getBestParent(nn)->parent;
  178. } while (nn != store->pub.selfNode);
  179. } else {
  180. Assert_fileLine(node->address.path == UINT64_MAX, file, line);
  181. Assert_fileLine(Node_getCost(node) == UINT64_MAX, file, line);
  182. }
  183. }
  184. #define checkNode(node, store) _checkNode(node, store, Gcc_SHORT_FILE, Gcc_LINE)
  185. static void _verifyNode(struct Node_Two* node,
  186. struct NodeStore_pvt* store,
  187. bool full,
  188. char* file,
  189. int line)
  190. {
  191. if (!Defined(PARANOIA) || (store->disarmCheck && !store->fullVerify)) { return; }
  192. // #1 check the node (do the basic checks)
  193. _checkNode(node, store, file, line);
  194. if (!full || !store->fullVerify) { return; }
  195. // #2 make sure all of the node's outgoing links are split properly
  196. struct Node_Link* link = NULL;
  197. RB_FOREACH_REVERSE(link, PeerRBTree, &node->peerTree) {
  198. // make sure any peers of this node are split properly
  199. struct Node_Link* linkB = link;
  200. struct Node_Link* linkC = link;
  201. RB_FOREACH_REVERSE_FROM(linkB, PeerRBTree, linkC) {
  202. if (linkB == link || link == store->selfLink) { continue; }
  203. Assert_fileLine(
  204. !LabelSplicer_routesThrough(linkB->cannonicalLabel, link->cannonicalLabel),
  205. file, line
  206. );
  207. }
  208. Assert_true(!link->nextInSplitList);
  209. }
  210. // #3 make sure looking for the node by address will actually find the correct node.
  211. if (Node_getBestParent(node)) {
  212. Assert_fileLine(node == NodeStore_closestNode(&store->pub, node->address.path), file, line);
  213. }
  214. // #5 no persistant markings are allowed.
  215. Assert_true(!node->marked);
  216. // #6 make sure the node is either unreachable or its cost is consistent
  217. struct Node_Link* bp = Node_getBestParent(node);
  218. if (!bp) {
  219. Assert_true(Node_getCost(node) == UINT64_MAX);
  220. } else {
  221. // Cost must equal the sum of the costs of the earlier links
  222. uint64_t cost = 0;
  223. while (bp->parent != bp->child) {
  224. cost += bp->linkCost;
  225. bp = Node_getBestParent(bp->parent);
  226. }
  227. Assert_true(Node_getCost(node) == cost);
  228. }
  229. }
  230. #define verifyNode(node, store) _verifyNode(node, store, true, Gcc_SHORT_FILE, Gcc_LINE)
  231. // Verify is more thorough than check because it makes sure all links are split properly.
  232. static void _verify(struct NodeStore_pvt* store, bool full, char* file, int line)
  233. {
  234. if (!Defined(PARANOIA) || (store->disarmCheck && !store->fullVerify)) {
  235. return;
  236. }
  237. Assert_true(Node_getBestParent(store->pub.selfNode) == store->selfLink || !store->selfLink);
  238. int linkedNodes = 0;
  239. int nodeCount = 0;
  240. struct Node_Two* nn = NULL;
  241. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  242. _verifyNode(nn, store, full, file, line);
  243. if (Node_getBestParent(nn)) { linkedNodes++; }
  244. nodeCount++;
  245. }
  246. Assert_fileLine(linkedNodes == store->pub.linkedNodes, file, line);
  247. Assert_fileLine(nodeCount == store->pub.nodeCount, file, line);
  248. }
  249. #define verify(store) _verify(store, true, Gcc_SHORT_FILE, Gcc_LINE)
  250. #define check(store) _verify(store, false, Gcc_SHORT_FILE, Gcc_LINE)
  251. /**
  252. * Extend a route by splicing on another link.
  253. * This will modify the Encoding Form of the first Director in next section of the route to make
  254. * it's size greater than or equal to the size of the return route through the parent node in the
  255. * link.
  256. *
  257. * @param routeToParent the label for reaching the parent node
  258. * @param parentScheme the label encoding scheme used by the parent node
  259. * @param routeParentToChild the cannonicalLabel for the link from parent to child
  260. * @param previousLinkEncoding the encoding used for the parent's interface back to the grandparent
  261. * @return a converted/spliced label or extendRoute_INVALID if it happens that the parent
  262. * or extendRoute_TOOLONG if the label is too long to represent.
  263. */
  264. #define extendRoute_INVALID (((uint64_t)~0)-1)
  265. #define extendRoute_TOOLONG (((uint64_t)~0))
  266. static uint64_t extendRoute(uint64_t routeToParent,
  267. struct EncodingScheme* parentScheme,
  268. uint64_t routeParentToChild,
  269. int previousLinkEncoding)
  270. {
  271. Assert_true(routeParentToChild != EncodingScheme_convertLabel_INVALID);
  272. // Make sure they didn't send us a 'silly' route.
  273. int nextLinkEncoding = EncodingScheme_getFormNum(parentScheme, routeParentToChild);
  274. if (nextLinkEncoding == EncodingScheme_getFormNum_INVALID) { return extendRoute_INVALID; }
  275. // If the encoding to get to the parent uses more bits than the encoding to get from parent
  276. // to child, we need to change the encoding...
  277. if (previousLinkEncoding > nextLinkEncoding) {
  278. routeParentToChild =
  279. EncodingScheme_convertLabel(parentScheme, routeParentToChild, previousLinkEncoding);
  280. Assert_true(routeParentToChild != EncodingScheme_convertLabel_INVALID);
  281. }
  282. return LabelSplicer_splice(routeParentToChild, routeToParent);
  283. }
  284. static void update(struct Node_Link* link,
  285. int64_t linkCostDiff,
  286. struct NodeStore_pvt* store)
  287. {
  288. if (linkCostDiff + link->linkCost > UINT32_MAX) {
  289. link->linkCost = UINT32_MAX;
  290. logLink(store, link, "link cost set to maximum");
  291. } else if (linkCostDiff + link->linkCost < 1024) {
  292. link->linkCost = 1024;
  293. //logLink(store, link, "link cost set to zero");
  294. } else {
  295. link->linkCost += linkCostDiff;
  296. }
  297. uint32_t minMultiHopCost = (uint32_t)1 << 20;
  298. if (!Node_isOneHopLink(link) && link->linkCost < minMultiHopCost) {
  299. // Give multi-hop links some minimum cost
  300. link->linkCost = minMultiHopCost;
  301. }
  302. }
  303. static bool isPeer(struct Node_Two* node, struct NodeStore_pvt* store)
  304. {
  305. struct Node_Link* bp = Node_getBestParent(node);
  306. return bp && bp->parent == store->pub.selfNode && Node_isOneHopLink(bp);
  307. }
  308. static void setParentCostAndPath(struct Node_Two* node,
  309. struct Node_Link* parent,
  310. uint64_t cost,
  311. uint64_t path,
  312. struct NodeStore_pvt* store)
  313. {
  314. uint64_t oldPath = node->address.path;
  315. Node_setParentCostAndPath(node, parent, cost, path);
  316. if (oldPath != path && store->pub.onBestPathChange) {
  317. store->pub.onBestPathChange(store->pub.onBestPathChangeCtx, node);
  318. }
  319. }
  320. /**
  321. * This is called when we have no idea what the cost should be for the next hop
  322. * because the path we previously used to get to it is broken and we need to use
  323. * a different one. Take a somewhat educated guess as to what it might be in a way
  324. * that will make the cost non-zero and finite.
  325. */
  326. static uint64_t guessCostOfChild(struct Node_Link* link)
  327. {
  328. // Educated guess, parent's cost + link's cost (neither of which is known perfectly).
  329. uint64_t guess = Node_getCost(link->parent) + link->linkCost;
  330. if (guess < Node_getCost(link->parent)) {
  331. // We wrapped around
  332. guess = UINT64_MAX;
  333. }
  334. Assert_true(guess >= Node_getCost(link->parent));
  335. return guess;
  336. }
  337. /**
  338. * We have reason to believe that cost and/or path to this node should be changed.
  339. * This occurs whenever the cost of one of the links to this node changes, or when the
  340. * cost of link->parent changes (since that would affect the total cost of the path).
  341. * We check each link for which node is the link->child, and calculate the cost of the
  342. * path through this link (using the best path to link->parent). If we find that the best
  343. * path has changed (or the cost of the best path has changed) we update that info for
  344. * this node and recursively call findBestParent on the link->child for each of this node's
  345. * outgoing links (in case those nodes can update their paths too).
  346. */
  347. static bool findBestParent0(struct Node_Two* node, struct NodeStore_pvt* store)
  348. {
  349. node->marked = 0;
  350. if (node == store->pub.selfNode) { return false; }
  351. struct Node_Link* bestLink = NULL;
  352. uint64_t bestCost = UINT64_MAX;
  353. uint64_t bestPath = UINT64_MAX;
  354. for (struct Node_Link* link = node->reversePeers; link; link = link->nextPeer) {
  355. if (link->linkCost == UINT32_MAX) { continue; }
  356. uint64_t cost = guessCostOfChild(link);
  357. if (bestCost <= cost) { continue; }
  358. if (bestLink && Node_isOneHopLink(bestLink) && !Node_isOneHopLink(link)) { continue; }
  359. if (!Node_getBestParent(link->parent)) { continue; }
  360. if (Node_isAncestorOf(node, link->parent)) { continue; }
  361. uint64_t path =
  362. extendRoute(link->parent->address.path,
  363. link->parent->encodingScheme,
  364. link->cannonicalLabel,
  365. Node_getBestParent(link->parent)->inverseLinkEncodingFormNumber);
  366. if (path == extendRoute_TOOLONG) { continue; }
  367. if (path == extendRoute_INVALID) { continue; }
  368. Assert_true(LabelSplicer_routesThrough(path, link->parent->address.path));
  369. bestCost = cost;
  370. bestPath = path;
  371. bestLink = link;
  372. }
  373. if (bestCost != Node_getCost(node) || bestPath != node->address.path) {
  374. if (!Node_getBestParent(node)) { store->pub.linkedNodes++; }
  375. if (!bestLink) { store->pub.linkedNodes--; }
  376. struct Node_Link* link = NULL;
  377. RB_FOREACH(link, PeerRBTree, &node->peerTree) {
  378. if (Node_getCost(node) > bestCost || Node_getBestParent(link->child) == link) {
  379. link->child->marked = 1;
  380. }
  381. }
  382. setParentCostAndPath(node, bestLink, bestCost, bestPath, store);
  383. return true;
  384. }
  385. return false;
  386. }
  387. // Returns true if anything was modified
  388. static bool findBestParent(struct Node_Two* node, struct NodeStore_pvt* store)
  389. {
  390. uint64_t time0 = Time_hrtime();
  391. if (!findBestParent0(node, store)) { return false; }
  392. int ret = 0;
  393. int cycle = 0;
  394. do {
  395. Assert_true(cycle++ < 10000);
  396. ret = 0;
  397. for (struct Node_Two* n = NodeStore_getNextNode(&store->pub, NULL);
  398. n;
  399. n = NodeStore_getNextNode(&store->pub, n))
  400. {
  401. if (n->marked) {
  402. ret |= findBestParent0(n, store);
  403. }
  404. }
  405. } while (ret);
  406. uint64_t time1 = Time_hrtime();
  407. if ((int64_t)(time1 - time0) > 1000000) {
  408. Log_warn(store->logger, "\n\nfindBestParent() took [%lld] ms\n\n",
  409. (long long) ((time1 - time0) / 1000000));
  410. }
  411. return true;
  412. }
  413. /**
  414. * This function updates the cost of a link, and triggers the findBestParent step that fixes
  415. * the routing tree in response to the cost change. For node cost and link costs to remain
  416. * conistent, the cost of a link (or a reachable node) must not be changed by any other mechanism.
  417. * (The store is temporarily inconsistent when links are beeing added/removed.)
  418. */
  419. static void handleLinkNews(struct Node_Link* link,
  420. uint32_t newLinkCost,
  421. struct NodeStore_pvt* store)
  422. {
  423. int64_t linkCostDiff = newLinkCost;
  424. linkCostDiff -= link->linkCost;
  425. update(link, linkCostDiff, store);
  426. //check(store);
  427. if (findBestParent(link->child, store)) {
  428. // This is a hot spot here, so we'll only check if the node tree was modified.
  429. check(store);
  430. }
  431. }
  432. void NodeStore_unlinkNodes(struct NodeStore* nodeStore, struct Node_Link* link)
  433. {
  434. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*) nodeStore);
  435. struct Node_Two* child = Identity_check(link->child);
  436. struct Node_Two* parent = Identity_check(link->parent);
  437. check(store);
  438. if (parent == store->pub.selfNode) {
  439. // yuh ok
  440. if (link == store->selfLink) { return; }
  441. Assert_true(Node_isOneHopLink(link));
  442. store->pub.peerCount--;
  443. if (Defined(Log_INFO)) {
  444. uint8_t addr[60];
  445. Address_print(addr, &child->address);
  446. Log_info(store->logger, "Direct peer [%s] has been unlinked", addr);
  447. }
  448. }
  449. handleLinkNews(link, UINT32_MAX, store);
  450. check(store);
  451. // Remove the entry from the reversePeers
  452. struct Node_Link* current = child->reversePeers;
  453. struct Node_Link** currentP = &child->reversePeers;
  454. while (current) {
  455. if (current == link) {
  456. *currentP = current->nextPeer;
  457. break;
  458. }
  459. currentP = &(current->nextPeer);
  460. current = *currentP;
  461. }
  462. Assert_true(current);
  463. // Remove the RBTree entry
  464. Assert_ifParanoid(link == RB_FIND(PeerRBTree, &parent->peerTree, link));
  465. RB_REMOVE(PeerRBTree, &parent->peerTree, link);
  466. link->nextPeer = store->linksToFree;
  467. store->linksToFree = link;
  468. // prevent double-free of link.
  469. link->parent = NULL;
  470. link->child = NULL;
  471. check(store);
  472. }
  473. /**
  474. * Link two nodes in the graph together.
  475. * If a parent of the child node is also a parent of the parent node, they are
  476. * unlinked (the link is split and the child is inserted in the middle).
  477. *
  478. * @param parent the current end of the graph
  479. * @param child the new node to extend the graph
  480. * @param cannonicalLabel the label for getting from the parent to the child.
  481. * @param linkCostDiff how much to change the link cost for this link.
  482. * @param store
  483. */
  484. static struct Node_Link* linkNodes(struct Node_Two* parent,
  485. struct Node_Two* child,
  486. uint64_t cannonicalLabel,
  487. int64_t linkCostDiff,
  488. int inverseLinkEncodingFormNumber,
  489. uint64_t discoveredPath,
  490. struct NodeStore_pvt* store)
  491. {
  492. check(store);
  493. if (Defined(Log_DEBUG)) {
  494. uint8_t parentIp[40];
  495. uint8_t childIp[40];
  496. AddrTools_printIp(parentIp, parent->address.ip6.bytes);
  497. AddrTools_printIp(childIp, child->address.ip6.bytes);
  498. uint8_t printedLabel[20];
  499. AddrTools_printPath(printedLabel, cannonicalLabel);
  500. Log_debug(store->logger, "Linking [%s] with [%s] with label fragment [%s]",
  501. parentIp, childIp, printedLabel);
  502. }
  503. // It's ok to link a node with itself via some loopey route.
  504. // in practice it should never actually be used and it might yield some interesting
  505. // information when the link is split, self-routes are not allowed unless the self
  506. // link is being set up :)
  507. Assert_true(cannonicalLabel != 1 || store->selfLink == NULL);
  508. if (Defined(PARANOIA)) {
  509. uint64_t definitelyCannonical =
  510. EncodingScheme_convertLabel(parent->encodingScheme,
  511. cannonicalLabel,
  512. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  513. Assert_true(definitelyCannonical == cannonicalLabel);
  514. }
  515. {
  516. struct Node_Link* link;
  517. RB_FOREACH_REVERSE(link, PeerRBTree, &parent->peerTree) {
  518. Identity_check(link);
  519. if (link->child == child) {
  520. if (link->cannonicalLabel != cannonicalLabel) {
  521. // multiple paths between A and B are ok because they
  522. // will have divergent paths following the first director.
  523. continue;
  524. } else if (link->inverseLinkEncodingFormNumber != inverseLinkEncodingFormNumber) {
  525. logLink(store, link, "Relinking nodes with different encoding form");
  526. // This can happen when C renumbers but B->C is the same because B did
  527. // not renumber, EG: if C restarts.
  528. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  529. }
  530. handleLinkNews(link, linkCostDiff+link->linkCost, store);
  531. return link;
  532. }
  533. }
  534. }
  535. if (Defined(PARANOIA)) {
  536. struct Node_Link dummy = { .cannonicalLabel = cannonicalLabel };
  537. struct Node_Link* link = Identity_ncheck(RB_FIND(PeerRBTree, &parent->peerTree, &dummy));
  538. if (link) {
  539. logLink(store, link, "Attempted to create alternate link with same label!");
  540. Assert_true(0);
  541. return link;
  542. }
  543. }
  544. Assert_true(cannonicalLabel <= discoveredPath);
  545. struct Node_Link* link = getLink(store);
  546. // set it up
  547. link->cannonicalLabel = cannonicalLabel;
  548. link->inverseLinkEncodingFormNumber = inverseLinkEncodingFormNumber;
  549. link->child = child;
  550. link->parent = parent;
  551. link->discoveredPath = discoveredPath;
  552. link->linkCost = 0;
  553. link->timeLastSeen = Time_currentTimeMilliseconds();
  554. Identity_set(link);
  555. // reverse link
  556. link->nextPeer = child->reversePeers;
  557. child->reversePeers = link;
  558. // forward link
  559. Assert_ifParanoid(!RB_FIND(PeerRBTree, &parent->peerTree, link));
  560. RB_INSERT(PeerRBTree, &parent->peerTree, link);
  561. // store entry
  562. if (!RB_FIND(NodeRBTree, &store->nodeTree, child)) {
  563. if (child == parent) {
  564. Assert_true(cannonicalLabel == 1);
  565. Assert_true(!store->pub.nodeCount);
  566. Assert_true(!store->selfLink);
  567. store->selfLink = link;
  568. Node_setParentCostAndPath(child, link, 0, 1);
  569. store->pub.linkedNodes++;
  570. }
  571. RB_INSERT(NodeRBTree, &store->nodeTree, child);
  572. store->pub.nodeCount++;
  573. }
  574. handleLinkNews(link, linkCostDiff+link->linkCost, store);
  575. if (parent == store->pub.selfNode && child != store->pub.selfNode) {
  576. Assert_true(Node_isOneHopLink(link));
  577. store->pub.peerCount++;
  578. if (Defined(Log_DEBUG)) {
  579. uint8_t addr[60];
  580. Address_print(addr, &child->address);
  581. Log_info(store->logger, "Direct peer [%s] has been linked", addr);
  582. }
  583. }
  584. check(store);
  585. return link;
  586. }
  587. #define removeLinkFromLabel_IMPOSSIBLE UINT64_MAX
  588. #define removeLinkFromLabel_OVERSIZE (UINT64_MAX-1)
  589. #define removeLinkFromLabel_ERR(x) (((uint64_t)x) >> 63)
  590. // TODO(cjd): This does not depend on nodeStore or alter the link, consider moving to Node.c
  591. static uint64_t removeLinkFromLabel(struct Node_Link* link, uint64_t label)
  592. {
  593. // First we splice off the parent's Director leaving the child's Director.
  594. uint64_t unspliced = LabelSplicer_unsplice(label, link->cannonicalLabel);
  595. int formNum = EncodingScheme_getFormNum(link->child->encodingScheme, unspliced);
  596. if (formNum < link->inverseLinkEncodingFormNumber) {
  597. // Can't get there from here.
  598. return removeLinkFromLabel_IMPOSSIBLE;
  599. }
  600. uint64_t cannonical =
  601. EncodingScheme_convertLabel(link->child->encodingScheme,
  602. unspliced,
  603. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  604. // Check that they didn't waste space by sending an oversize encoding form.
  605. if (formNum > link->inverseLinkEncodingFormNumber && cannonical != unspliced) {
  606. return removeLinkFromLabel_OVERSIZE;
  607. }
  608. Assert_true(cannonical != EncodingScheme_convertLabel_INVALID);
  609. return cannonical;
  610. }
  611. /**
  612. * Find the next hop on a given path.
  613. * Given a label representing a path from parentLink to some destination, set
  614. * outLink to the first link along that journey and return the path from outLink
  615. * to the original destination.
  616. * Feeding outLink back in to parentLink and the return value back into label argument
  617. * will allow you to iteratively walk a path.
  618. *
  619. * @param label the path from parentLink to some unspecified destination.
  620. * @param outLink a pointer to a location which will receive the first link in the path.
  621. * @param parentLink the link where to begin the trek.
  622. * @param store
  623. * @return a label which would take you from the node in memory location outLink to the
  624. * destination provided by the label argument. OR: firstHopInPath_INVALID if the
  625. * label argument traverces a node whose encoding scheme is inconsistent with
  626. * the label. OR: firstHopInPath_NO_NEXT_LINK if there are no *known* further
  627. * links along the path. If the result is firstHopInPath_INVALID, outLink will
  628. * still be set to the node. Use firstHopInPath_ERR() to check if the return
  629. * is an error code.
  630. */
  631. #define firstHopInPath_INVALID UINT64_MAX
  632. #define firstHopInPath_NO_NEXT_LINK (UINT64_MAX-1)
  633. #define firstHopInPath_ERR(path) (path >= firstHopInPath_NO_NEXT_LINK)
  634. static uint64_t firstHopInPath(uint64_t label,
  635. struct Node_Link** outLink,
  636. struct Node_Link* parentLink,
  637. struct NodeStore_pvt* store)
  638. {
  639. // Then we search for the next peer in the path
  640. // RB_NFIND will find a link for which we know that no link before it is in the path.
  641. // Unfortunately I have not found a way to store links in a tree where the search time
  642. // is less than O(n) where n = peers of a given node.
  643. struct Node_Link tmpl = { .cannonicalLabel = label };
  644. struct Node_Link* nextLink =
  645. Identity_ncheck(RB_NFIND(PeerRBTree, &parentLink->child->peerTree, &tmpl));
  646. // Now we walk back through the potential candidates looking for a path which it routes though.
  647. while (nextLink && !LabelSplicer_routesThrough(label, nextLink->cannonicalLabel)) {
  648. nextLink = Identity_ncheck(RB_NEXT(PeerRBTree, NULL, nextLink));
  649. }
  650. // This node has no peers, if it's us then it always has a peer (which is the selfLink)
  651. if (!nextLink || nextLink == store->selfLink) {
  652. return firstHopInPath_NO_NEXT_LINK;
  653. }
  654. // Old:
  655. // check for a looping link, this should never happen but adding the assert helps me
  656. // refactor this function a little more agressively.
  657. //Assert_true(nextLink != parentLink);
  658. //
  659. // New:
  660. // There *can* be loopy links because they are kept around in the hope that they'll be
  661. // split later as more information comes in. For example we can discover A->A->D->E
  662. // and later on we discover A->B->C->A->D->E because the B->C part was hidden, we saw
  663. // it as A->A. If we encounter one of these loopey links, what we probably *should*
  664. // do is skip the loop and resolve the next part of the path, but returning NO_NEXT_LINK
  665. // is ok because we don't claim to have a full knowledge of the network and that is
  666. // much easier. Update stops a rare assertion failure.
  667. if (nextLink == parentLink) {
  668. return firstHopInPath_NO_NEXT_LINK;
  669. }
  670. if (label == nextLink->cannonicalLabel) {
  671. //logLink(store, nextLink, "Exact match");
  672. *outLink = nextLink;
  673. return 1;
  674. }
  675. if (!LabelSplicer_routesThrough(label, nextLink->cannonicalLabel)) {
  676. // child of next link is not in the path, we reached the end.
  677. return firstHopInPath_NO_NEXT_LINK;
  678. }
  679. *outLink = nextLink;
  680. // Cannoicalize the child's Director
  681. label = removeLinkFromLabel(nextLink, label);
  682. if (removeLinkFromLabel_ERR(label)) {
  683. return firstHopInPath_INVALID;
  684. }
  685. return label;
  686. }
  687. #define findClosest_INVALID (~((uint64_t)0))
  688. static uint64_t findClosest(uint64_t path,
  689. struct Node_Link** output,
  690. struct Node_Link* parentLink,
  691. struct NodeStore_pvt* store)
  692. {
  693. for (;;) {
  694. struct Node_Link* nextLink = NULL;
  695. uint64_t nextPath = firstHopInPath(path, &nextLink, parentLink, store);
  696. if (nextPath == firstHopInPath_NO_NEXT_LINK) {
  697. *output = parentLink;
  698. return path;
  699. }
  700. if (firstHopInPath_INVALID == nextPath) {
  701. return findClosest_INVALID;
  702. }
  703. Assert_true(nextLink);
  704. path = nextPath;
  705. parentLink = nextLink;
  706. }
  707. }
  708. static struct Node_Two* nodeForIp(struct NodeStore_pvt* store, uint8_t ip[16])
  709. {
  710. struct Node_Two fakeNode;
  711. Identity_set(&fakeNode);
  712. Bits_memcpy(fakeNode.address.ip6.bytes, ip, 16);
  713. return Identity_ncheck(RB_FIND(NodeRBTree, &store->nodeTree, &fakeNode));
  714. }
  715. static void freePendingLinks(struct NodeStore_pvt* store)
  716. {
  717. struct Node_Link* link;
  718. while ((link = store->linksToFree)) {
  719. store->linksToFree = link->nextPeer;
  720. freeLink(link, store);
  721. }
  722. }
  723. static struct Node_Link* discoverLinkC(struct NodeStore_pvt* store,
  724. struct Node_Link* closestKnown,
  725. uint64_t pathKnownParentChild,
  726. struct Node_Two* child,
  727. uint64_t discoveredPath,
  728. int inverseLinkEncodingFormNumber)
  729. {
  730. // Make sure this link cannot be split before beginning.
  731. struct Node_Link* closest = NULL;
  732. uint64_t pathParentChild = findClosest(pathKnownParentChild, &closest, closestKnown, store);
  733. if (pathParentChild == findClosest_INVALID) {
  734. return NULL;
  735. }
  736. if (!EncodingScheme_isOneHop(closest->child->encodingScheme, pathParentChild)) {
  737. Log_debug(store->logger, "Not discovering link because it's multi-hop");
  738. return NULL;
  739. }
  740. struct Node_Two* parent = closest->child;
  741. if (Defined(Log_DEBUG)) {
  742. uint8_t parentStr[40];
  743. uint8_t childStr[40];
  744. uint8_t pathStr[20];
  745. AddrTools_printIp(parentStr, parent->address.ip6.bytes);
  746. AddrTools_printIp(childStr, child->address.ip6.bytes);
  747. AddrTools_printPath(pathStr, pathParentChild);
  748. Log_debug(store->logger, "discoverLinkC( [%s]->[%s] [%s] )", parentStr, childStr, pathStr);
  749. }
  750. if (closest == store->selfLink &&
  751. !EncodingScheme_isOneHop(parent->encodingScheme, pathParentChild))
  752. {
  753. Log_debug(store->logger, "Attempting to create a link with no parent peer");
  754. return NULL;
  755. }
  756. if (parent == child) {
  757. if (pathParentChild == 1) {
  758. // Link is already known.
  759. //update(closest, 0, store);
  760. //Log_debug(store->logger, "Already known");
  761. return closest;
  762. }
  763. Log_debug(store->logger, "Loopey route");
  764. // lets not bother storing this link, a link with the same parent and child is
  765. // invalid according to verify() and it's just going to take up space in the store
  766. // we'll return closest which is a perfectly valid path to the same node.
  767. // We could reasonably return the closest since it is the same node but it causes
  768. // problems with an assertion in discoverLink.
  769. return NULL;
  770. }
  771. if (EncodingScheme_isSelfRoute(parent->encodingScheme, pathParentChild)) {
  772. // This should never happen for a direct peer or for a direct decendent in a split link.
  773. // This sometimes triggers because a link is split which contains an invalid encoding
  774. // somewhere in the middle.
  775. // It is not harmful to remove it becaue the route is not re-added.
  776. Assert_ifTesting(closestKnown != closest);
  777. // If the packet came in along a path which is not the best path we know, it might be
  778. // that an evil switch modified the path in transit, in this case lets send out a ping
  779. // along the best path and it should return to us, confirming that we need to relink
  780. // the node.
  781. if (discoveredPath == parent->address.path) {
  782. logLink(store, closest, "Double-checking path node change");
  783. // Ping child's key w/ parent's path
  784. uint64_t oldPath = child->address.path;
  785. child->address.path = parent->address.path;
  786. RumorMill_addNode(store->renumberMill, &child->address);
  787. child->address.path = oldPath;
  788. check(store);
  789. return NULL;
  790. } else {
  791. logLink(store, closest, "Unlinking node for path change");
  792. struct Node_Link* nextClosest = Node_getBestParent(closest->parent);
  793. uint64_t nextPPC = closest->cannonicalLabel;
  794. NodeStore_unlinkNodes(&store->pub, closest);
  795. closest = nextClosest;
  796. pathParentChild = nextPPC;
  797. parent = closest->child;
  798. }
  799. }
  800. // link parent to child
  801. //
  802. // ACKTUNG: From this point forward, the nodeStore is in an invalid state, calls to _verify()
  803. // will fail (calls to _check() will still succeed). We have linked parent with child
  804. // but we have not split all of the splitLinks from parent.
  805. //
  806. // FIXME(arceliar,cjd): linking every node with 0 link cost, this can't be right.
  807. struct Node_Link* parentLink = linkNodes(parent,
  808. child,
  809. pathParentChild,
  810. 0,
  811. inverseLinkEncodingFormNumber,
  812. discoveredPath,
  813. store);
  814. check(store);
  815. return parentLink;
  816. }
  817. static void fixLink(struct Node_Link* parentLink,
  818. struct Node_Link** outLinks,
  819. struct NodeStore_pvt* store)
  820. {
  821. int verifyOrder = 0;
  822. // Check whether the parent is already linked with a node which is "behind" the child.
  823. // splitLink appears to be a "sibling link" to the closest->node link but in reality the
  824. // splitLink link should be split and node should be inserted in the middle.
  825. struct Node_Link* splitLink = RB_MIN(PeerRBTree, &parentLink->parent->peerTree);
  826. while (splitLink) {
  827. if (splitLink == parentLink) {
  828. if (Defined(PARANOIA)) {
  829. verifyOrder = 1;
  830. splitLink = PeerRBTree_RB_NEXT(splitLink);
  831. continue;
  832. } else {
  833. // Since they're in order, definitely not found.
  834. break;
  835. }
  836. }
  837. if (!LabelSplicer_routesThrough(splitLink->cannonicalLabel, parentLink->cannonicalLabel)) {
  838. splitLink = PeerRBTree_RB_NEXT(splitLink);
  839. continue;
  840. }
  841. if (Defined(PARANOIA)) {
  842. Assert_true(!verifyOrder);
  843. }
  844. struct Node_Two* grandChild = splitLink->child;
  845. if (parentLink->child == grandChild) {
  846. // loopey route, kill it and let the bestParent pivit over to parentLink
  847. } else {
  848. logLink(store, splitLink, "Splitting link");
  849. // unsplice and cannonicalize so we now have a path from child to grandchild
  850. uint64_t childToGrandchild =
  851. LabelSplicer_unsplice(splitLink->cannonicalLabel, parentLink->cannonicalLabel);
  852. Assert_true(parentLink->child);
  853. childToGrandchild =
  854. EncodingScheme_convertLabel(parentLink->child->encodingScheme,
  855. childToGrandchild,
  856. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  857. Assert_true(childToGrandchild < UINT64_MAX);
  858. Assert_true(childToGrandchild != 1);
  859. Assert_true(splitLink->cannonicalLabel != parentLink->cannonicalLabel);
  860. // We forgot what was the discovered path for the link when we split (destroyed)
  861. // it so we'll just assume the worst among these two possibilities.
  862. // There is an assertion that discoveredPath is never < cannonicalLabel so we must.
  863. uint64_t discoveredPath = parentLink->discoveredPath;
  864. if (childToGrandchild > discoveredPath) { discoveredPath = childToGrandchild; }
  865. struct Node_Link* childLink =
  866. discoverLinkC(store, parentLink, childToGrandchild, grandChild,
  867. discoveredPath, splitLink->inverseLinkEncodingFormNumber);
  868. // Three possibilities:
  869. // 1. discoverLinkC returned NULL for whatever reason, skip this routine.
  870. // 2. discoverLinkC determined that childLink already exists and returned it, this
  871. // routine added it in a previous iteration so
  872. // childLink->nextInSplitList is not NULL so we should skip this
  873. // routine as splitLinks will already attempt to split childLink.
  874. // 3. childLink is new or has existed since before this discoverNode, we will add it
  875. // to the splitList so that splitLinks will attempt to split it.
  876. if (childLink && !childLink->nextInSplitList) {
  877. // Order the list so that the next set of links will be split from
  878. // smallest to largest and nothing will ever be split twice.
  879. for (struct Node_Link** x = outLinks;; x = &(*x)->nextInSplitList) {
  880. if (*x == childLink) { break; }
  881. if (*x && (*x)->cannonicalLabel <= childLink->cannonicalLabel) { continue; }
  882. childLink->nextInSplitList = *x;
  883. *x = childLink;
  884. break;
  885. }
  886. }
  887. }
  888. check(store);
  889. struct Node_Link* next = PeerRBTree_RB_NEXT(splitLink);
  890. NodeStore_unlinkNodes(&store->pub, splitLink);
  891. splitLink = next;
  892. }
  893. }
  894. static void fixLinks(struct Node_Link* parentLinkList,
  895. struct Node_Link** outLinks,
  896. struct NodeStore_pvt* store)
  897. {
  898. while (parentLinkList) {
  899. struct Node_Link* next = parentLinkList->nextInSplitList;
  900. parentLinkList->nextInSplitList = NULL;
  901. // else the parent link has been trashed by splitting another link.
  902. if (parentLinkList->child) {
  903. fixLink(parentLinkList, outLinks, store);
  904. }
  905. parentLinkList = next;
  906. }
  907. }
  908. static struct Node_Link* discoverLink(struct NodeStore_pvt* store,
  909. uint64_t path,
  910. struct Node_Two* child,
  911. int inverseLinkEncodingFormNumber)
  912. {
  913. struct Node_Link* link =
  914. discoverLinkC(store, store->selfLink, path, child, path, inverseLinkEncodingFormNumber);
  915. if (!link) { return NULL; }
  916. // Dropped because v21 triggers this...
  917. //uint64_t pathParentChild = findClosest(path, &link, store->selfLink, store);
  918. // This should always be 1 because the link is gone only because it was just split!
  919. //Assert_true(pathParentChild == 1);
  920. struct Node_Link* ol = NULL;
  921. struct Node_Link* nl = NULL;
  922. fixLinks(link, &ol, store);
  923. for (;;) {
  924. if (ol) {
  925. fixLinks(ol, &nl, store);
  926. ol = NULL;
  927. } else if (nl) {
  928. fixLinks(nl, &ol, store);
  929. nl = NULL;
  930. } else {
  931. break;
  932. }
  933. }
  934. verify(store);
  935. return link;
  936. }
  937. static struct Node_Two* whichIsWorse(struct Node_Two* one,
  938. struct Node_Two* two,
  939. struct NodeStore_pvt* store)
  940. {
  941. // a peer is nevar worse
  942. int worse = isPeer(one, store) - isPeer(two, store);
  943. if (worse) {
  944. return (worse > 0) ? two : one;
  945. }
  946. worse = (one->address.path == UINT64_MAX) - (two->address.path == UINT64_MAX);
  947. if (worse) {
  948. return (worse > 0) ? one : two;
  949. }
  950. if (one->address.protocolVersion != two->address.protocolVersion) {
  951. if (one->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  952. if (two->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  953. return one;
  954. }
  955. } else if (two->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  956. if (one->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  957. return two;
  958. }
  959. }
  960. }
  961. uint32_t selfPrefix = Address_getPrefix(&store->pub.selfNode->address);
  962. uint64_t distOne = Address_getPrefix(&one->address) ^ selfPrefix;
  963. uint64_t distTwo = Address_getPrefix(&two->address) ^ selfPrefix;
  964. distOne += Node_getCost(one);
  965. distTwo += Node_getCost(two);
  966. if (Defined(NodeStore_whichIsWorse_PATHCOUNTS)) {
  967. distOne += Bits_log2x64(one->address.path) << 26;
  968. distTwo += Bits_log2x64(two->address.path) << 26;
  969. }
  970. if (distOne < distTwo) { return two; }
  971. return one;
  972. }
  973. struct NodeList* NodeStore_getNodesForBucket(struct NodeStore* nodeStore,
  974. struct Allocator* allocator,
  975. uint16_t bucket,
  976. const uint32_t count)
  977. {
  978. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  979. struct NodeList* nodeList = Allocator_malloc(allocator, sizeof(struct NodeList));
  980. nodeList->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  981. nodeList->size = 0;
  982. struct Node_Two* nn = NULL;
  983. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  984. if (Node_getCost(nn) == UINT64_MAX) { continue; }
  985. if (NodeStore_bucketForAddr(store->pub.selfAddress, &nn->address) == bucket) {
  986. struct Node_Two* newNode = nn;
  987. struct Node_Two* tempNode = NULL;
  988. for (uint32_t i = 0 ; i < count ; i++) {
  989. if (nodeList->size < i+1) {
  990. // The list isn't full yet, so insert at the end.
  991. nodeList->size = i+1;
  992. nodeList->nodes[i] = newNode;
  993. break;
  994. }
  995. if ( (newNode->marked && !nodeList->nodes[i]->marked) ||
  996. whichIsWorse(nodeList->nodes[i], newNode, store) == nodeList->nodes[i] ) {
  997. // If we've already marked nodes because they're a bestParent,
  998. // lets give them priority in the bucket since we need to keep
  999. // them either way.
  1000. // Otherwise, decide based on whichIsWorse().
  1001. // Insertion sorted list.
  1002. tempNode = nodeList->nodes[i];
  1003. nodeList->nodes[i] = newNode;
  1004. newNode = tempNode;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. return nodeList;
  1010. }
  1011. static bool markNodesForBucket(struct NodeStore_pvt* store,
  1012. uint16_t bucket,
  1013. const uint32_t count)
  1014. {
  1015. struct Allocator* nodeListAlloc = Allocator_child(store->alloc);
  1016. struct NodeList* nodeList = NodeStore_getNodesForBucket(&store->pub,
  1017. nodeListAlloc,
  1018. bucket,
  1019. count);
  1020. bool retVal = false;
  1021. if (nodeList->size > 0) { retVal = true; }
  1022. for (uint32_t i = 0; i < nodeList->size; i++) {
  1023. // Now mark the nodes in the list to protect them.
  1024. Identity_check(nodeList->nodes[i]);
  1025. nodeList->nodes[i]->marked = 1;
  1026. }
  1027. // Cleanup
  1028. Allocator_free(nodeListAlloc);
  1029. return retVal;
  1030. }
  1031. static void markKeyspaceNodes(struct NodeStore_pvt* store)
  1032. {
  1033. for (uint16_t bucket = 0; bucket < NodeStore_bucketNumber ; bucket++) {
  1034. markNodesForBucket(store, bucket, NodeStore_bucketSize);
  1035. }
  1036. }
  1037. /**
  1038. * We define the worst node the node with the highest cost, excluding nodes which are required for
  1039. * the DHT, and nodes which are somebody's bestParent (only relevant if they're the bestParent of
  1040. * a DHT-required node, as otherwise their child would always be higher cost).
  1041. * If two nodes tie (e.g. two unreachable nodes with maximum cost) then the node which is
  1042. * further from us in keyspace is worse.
  1043. */
  1044. static struct Node_Two* getWorstNode(struct NodeStore_pvt* store)
  1045. {
  1046. struct Node_Two* worst = NULL;
  1047. struct Node_Two* nn = NULL;
  1048. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1049. // first cycle we set markings so markings remain if they are behind us
  1050. struct Node_Link* parentLink = Node_getBestParent(nn);
  1051. if (parentLink) {
  1052. parentLink->parent->marked = 1;
  1053. } else if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1054. // this time around we're only addressing nodes which are unreachable.
  1055. worst = nn;
  1056. }
  1057. }
  1058. if (worst) {
  1059. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1060. if (nn->marked) { nn->marked = false; }
  1061. }
  1062. return worst;
  1063. }
  1064. // Mark the nodes that we need to protect for keyspace reasons.
  1065. markKeyspaceNodes(store);
  1066. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1067. if (nn->marked) {
  1068. nn->marked = false;
  1069. } else if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1070. worst = nn;
  1071. }
  1072. }
  1073. if (worst) { return worst; }
  1074. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1075. // third cycle, every node is apparently important but we need to get rid of someone
  1076. // get whoever is worst if we ignore markings
  1077. // by definition, this shouldn't be a bestParent, because their children have higher cost
  1078. // so we're potentially creating a keyspace hole (routing blackhole) when we do this.
  1079. // TODO(arceliar): protect keyspace, evict the worst bestParent instead?
  1080. // Would require something like a forgetNode() to splice links together between
  1081. // that node's bestParent and all its children, before we kill it.
  1082. if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1083. worst = nn;
  1084. }
  1085. }
  1086. // somebody has to be at the end of the line, not *everyone* can be someone's best parent!
  1087. Assert_true(worst);
  1088. return worst;
  1089. }
  1090. static void destroyNode(struct Node_Two* node, struct NodeStore_pvt* store)
  1091. {
  1092. // careful, undefined unless debug is enabled...
  1093. uint8_t address_debug[60];
  1094. if (Defined(Log_DEBUG)) {
  1095. Address_print(address_debug, &node->address);
  1096. }
  1097. struct Node_Link* link;
  1098. RB_FOREACH(link, PeerRBTree, &node->peerTree) {
  1099. Identity_check(link);
  1100. NodeStore_unlinkNodes(&store->pub, link);
  1101. }
  1102. // If the node has a bestParent, it will be changed a number
  1103. // of times as we kill off all of it's parent links.
  1104. // This is an optimization:
  1105. if (!Defined(PARANOIA)) {
  1106. store->pub.linkedNodes--;
  1107. setParentCostAndPath(node, NULL, UINT64_MAX, UINT64_MAX, store);
  1108. }
  1109. link = node->reversePeers;
  1110. while (link) {
  1111. struct Node_Link* nextLink = link->nextPeer;
  1112. NodeStore_unlinkNodes(&store->pub, link);
  1113. link = nextLink;
  1114. }
  1115. Assert_true(!Node_getBestParent(node));
  1116. Assert_ifParanoid(node == RB_FIND(NodeRBTree, &store->nodeTree, node));
  1117. RB_REMOVE(NodeRBTree, &store->nodeTree, node);
  1118. store->pub.nodeCount--;
  1119. Allocator_free(node->alloc);
  1120. }
  1121. // Must be at least 2 to avoid multiplying by 0.
  1122. // If too large, path choice may become unstable due to a guess we make in calcNextCost.
  1123. // This is fixable by storing cost based on links. A lot of work.
  1124. // In the mean time, just don't use a large value.
  1125. #define NodeStore_latencyWindow 8
  1126. static uint32_t costAfterDecay(const uint32_t oldCost)
  1127. {
  1128. // Increase the cost by 1/Xth where X = NodeStore_latencyWindow
  1129. // This is used to keep a weighted rolling average
  1130. int64_t newCost = oldCost - oldCost/NodeStore_latencyWindow;
  1131. if (newCost < 1024) {
  1132. // Set some minimum cost
  1133. newCost = 1024;
  1134. }
  1135. return newCost;
  1136. }
  1137. static uint32_t costAfterTimeout(const uint64_t oldCost)
  1138. {
  1139. int64_t newCost = oldCost;
  1140. newCost *= NodeStore_latencyWindow;
  1141. newCost /= NodeStore_latencyWindow - 1;
  1142. if (newCost > UINT32_MAX) { newCost = UINT32_MAX; }
  1143. return newCost;
  1144. }
  1145. // Returns new cost of a link
  1146. static uint32_t calcNextCost(const uint64_t oldCost)
  1147. {
  1148. // TODO(arceliar) the 1023 here is pretty arbitrary...
  1149. uint64_t out = costAfterDecay(oldCost);
  1150. // TODO(arceliar): is this safe?
  1151. Assert_true(out >= 1024 && out != UINT64_MAX);
  1152. return out;
  1153. }
  1154. static struct Node_Link* discoverNode(struct NodeStore_pvt* store,
  1155. struct Address* addr,
  1156. struct EncodingScheme* scheme,
  1157. int inverseLinkEncodingFormNumber,
  1158. uint64_t milliseconds)
  1159. {
  1160. struct Node_Two* child = nodeForIp(store, addr->ip6.bytes);
  1161. if (Defined(Log_DEBUG)) {
  1162. uint8_t printedAddr[60];
  1163. Address_print(printedAddr, addr);
  1164. Log_debug(store->logger, "Discover node [%s]", printedAddr);
  1165. }
  1166. if (child && child == store->selfLink->child) {
  1167. return NULL;
  1168. }
  1169. if (child && EncodingScheme_compare(child->encodingScheme, scheme)) {
  1170. // Shit.
  1171. // Box reset *and* they just updated and changed their encoding scheme.
  1172. RumorMill_addNode(store->renumberMill, &child->address);
  1173. if (addr->path > (child->address.path | (child->address.path << 3))) {
  1174. Log_debug(store->logger, "Node appears to have changed it's encoding scheme "
  1175. "but the message came from far away and we will not trust it");
  1176. return NULL;
  1177. } else {
  1178. Log_debug(store->logger, "Node appears to have changed it's encoding scheme "
  1179. "dropping him from the table and re-inserting");
  1180. destroyNode(child, store);
  1181. child = NULL;
  1182. }
  1183. } else if (child && child->address.protocolVersion != addr->protocolVersion) {
  1184. child->address.protocolVersion = addr->protocolVersion;
  1185. }
  1186. struct Allocator* alloc = NULL;
  1187. if (!child) {
  1188. alloc = Allocator_child(store->alloc);
  1189. child = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1190. child->alloc = alloc;
  1191. Bits_memcpy(&child->address, addr, sizeof(struct Address));
  1192. child->encodingScheme = EncodingScheme_clone(scheme, child->alloc);
  1193. child->timeLastPinged = Time_currentTimeMilliseconds();
  1194. Identity_set(child);
  1195. }
  1196. struct Node_Link* link = NULL;
  1197. for (;;) {
  1198. link = discoverLink(store,
  1199. addr->path,
  1200. child,
  1201. inverseLinkEncodingFormNumber);
  1202. if (link) { break; }
  1203. // We might have a broken link in the store which is causing new links to be rejected.
  1204. // On the other hand, this path might actually be garbage :)
  1205. // There's a DoS risk in that someone might use garbage paths to evict all of the
  1206. // existing good paths.
  1207. // While an attacker can send in a packet, it will necessarily follow a ridiculous path
  1208. // in order that the path contains one of their nodes.
  1209. // To resolve this, we'll walk the path looking for the "bad" link, then we'll check that
  1210. // node to see if the path we took to reach it is actually the *best* path to that node.
  1211. uint64_t path = addr->path;
  1212. struct Node_Link* lastLink = store->selfLink;
  1213. do {
  1214. struct Node_Link* nextLink = NULL;
  1215. path = firstHopInPath(path, &nextLink, lastLink, store);
  1216. lastLink = nextLink;
  1217. if (path == firstHopInPath_NO_NEXT_LINK) {
  1218. // discoverNode() failed for some other reason.
  1219. lastLink = NULL;
  1220. break;
  1221. }
  1222. } while (firstHopInPath_INVALID != path);
  1223. if (lastLink && LabelSplicer_routesThrough(addr->path, lastLink->child->address.path)) {
  1224. // checking for silliness...
  1225. Assert_true(lastLink != store->selfLink);
  1226. NodeStore_unlinkNodes(&store->pub, lastLink);
  1227. continue;
  1228. }
  1229. if (alloc) {
  1230. Allocator_free(alloc);
  1231. }
  1232. verify(store);
  1233. Log_debug(store->logger, "Invalid path");
  1234. return NULL;
  1235. }
  1236. Assert_true(link->child);
  1237. #ifdef PARANOIA
  1238. struct Node_Two* parent = link->parent;
  1239. #endif
  1240. //handleNews(link->child, cost, store);
  1241. verify(store);
  1242. handleLinkNews(link, calcNextCost(link->linkCost), store);
  1243. verify(store);
  1244. freePendingLinks(store);
  1245. while ((store->pub.nodeCount - store->pub.peerCount) >
  1246. store->pub.nodeCapacity
  1247. || store->pub.linkCount > store->pub.linkCapacity)
  1248. {
  1249. struct Node_Two* worst = getWorstNode(store);
  1250. if (Defined(Log_DEBUG)) {
  1251. uint8_t worstAddr[60];
  1252. Address_print(worstAddr, &worst->address);
  1253. Log_debug(store->logger, "store full, removing worst node: [%s] nodes [%d] links [%d]",
  1254. worstAddr, store->pub.nodeCount, store->pub.linkCount);
  1255. }
  1256. Assert_true(!isPeer(worst, store));
  1257. if (link && (worst == link->parent || worst == link->child)) { link = NULL; }
  1258. destroyNode(worst, store);
  1259. freePendingLinks(store);
  1260. }
  1261. verify(store);
  1262. // This should test that link == NodeStore_linkForPath(path) but that is not guaranteed
  1263. // to work because links are not healed up when a node is removed from the store
  1264. Assert_ifParanoid(!link || RB_FIND(PeerRBTree, &parent->peerTree, link) == link);
  1265. return link;
  1266. }
  1267. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  1268. struct Address* addr,
  1269. struct EncodingScheme* scheme,
  1270. int inverseLinkEncodingFormNumber,
  1271. uint64_t milliseconds)
  1272. {
  1273. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1274. store->disarmCheck++;
  1275. struct Node_Link* out =
  1276. discoverNode(store, addr, scheme, inverseLinkEncodingFormNumber, milliseconds);
  1277. store->disarmCheck--;
  1278. verify(store);
  1279. return out;
  1280. }
  1281. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16])
  1282. {
  1283. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1284. struct Node_Two* n = nodeForIp(store, addr);
  1285. if (n && n->address.path == UINT64_MAX) {
  1286. if (Defined(Log_DEBUG)) {
  1287. uint8_t addrStr[40];
  1288. AddrTools_printIp(addrStr, n->address.ip6.bytes);
  1289. Log_debug(store->logger, "No way to represent path to [%s]", addrStr);
  1290. }
  1291. return NULL;
  1292. }
  1293. return n;
  1294. }
  1295. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path)
  1296. {
  1297. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1298. struct Node_Link* out = NULL;
  1299. findClosest(path, &out, store->selfLink, store);
  1300. if (!out) { return NULL; }
  1301. return Identity_check(out->child);
  1302. }
  1303. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path)
  1304. {
  1305. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1306. struct Node_Link* out = NULL;
  1307. uint64_t pathParentChild = findClosest(path, &out, store->selfLink, store);
  1308. if (pathParentChild != 1) { return NULL; }
  1309. return Identity_check(out);
  1310. }
  1311. struct Node_Link* NodeStore_firstHopInPath(struct NodeStore* nodeStore,
  1312. uint64_t path,
  1313. uint64_t* correctedPath,
  1314. struct Node_Link* startingPoint)
  1315. {
  1316. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1317. if (!startingPoint) { startingPoint = store->selfLink; }
  1318. if (!Node_getBestParent(startingPoint->parent)) { return NULL; }
  1319. struct Node_Link* out = NULL;
  1320. path = firstHopInPath(path, &out, startingPoint, store);
  1321. if (firstHopInPath_ERR(path)) { return NULL; }
  1322. if (correctedPath) { *correctedPath = path; }
  1323. return out;
  1324. }
  1325. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal)
  1326. {
  1327. switch (returnVal) {
  1328. case NodeStore_getRouteLabel_PARENT_NOT_FOUND:
  1329. return "NodeStore_getRouteLabel_PARENT_NOT_FOUND";
  1330. case NodeStore_getRouteLabel_CHILD_NOT_FOUND:
  1331. return "NodeStore_getRouteLabel_CHILD_NOT_FOUND";
  1332. default: return NULL;
  1333. }
  1334. }
  1335. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  1336. uint64_t pathToParent,
  1337. uint64_t pathParentToChild)
  1338. {
  1339. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1340. struct Node_Link* linkToParent;
  1341. if (findClosest(pathToParent, &linkToParent, store->selfLink, store) != 1) {
  1342. return NodeStore_getRouteLabel_PARENT_NOT_FOUND;
  1343. }
  1344. //logLink(store, linkToParent, "NodeStore_getRouteLabel() PARENT");
  1345. struct Node_Link* linkToChild = NULL;
  1346. RB_FOREACH_REVERSE(linkToChild, PeerRBTree, &linkToParent->child->peerTree) {
  1347. if (pathParentToChild == linkToChild->cannonicalLabel) {
  1348. if (linkToParent == store->selfLink) {
  1349. return linkToChild->cannonicalLabel;
  1350. }
  1351. // TODO(cjd): this could return ~0
  1352. return extendRoute(pathToParent,
  1353. linkToChild->parent->encodingScheme,
  1354. linkToChild->cannonicalLabel,
  1355. linkToParent->inverseLinkEncodingFormNumber);
  1356. }
  1357. }
  1358. return NodeStore_getRouteLabel_CHILD_NOT_FOUND;
  1359. }
  1360. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path)
  1361. {
  1362. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1363. struct Node_Link* linkToParent;
  1364. uint64_t next = findClosest(path, &linkToParent, store->selfLink, store);
  1365. if (next == findClosest_INVALID) {
  1366. return NodeStore_optimizePath_INVALID;
  1367. }
  1368. if (EncodingScheme_isSelfRoute(linkToParent->child->encodingScheme, next)) {
  1369. // cannoicalize all the other wild ways that they can represent self routes.
  1370. // TODO(cjd): this has been the source of assert failures and we might be sweeping
  1371. // a significant bug under the carpet.
  1372. next = 1;
  1373. }
  1374. if (linkToParent == store->selfLink) {
  1375. if (next == 1) { return 1; }
  1376. return path;
  1377. }
  1378. if (next == 1) { return linkToParent->child->address.path; }
  1379. struct Node_Link* childBestParent = Node_getBestParent(linkToParent->child);
  1380. if (childBestParent) {
  1381. linkToParent = childBestParent;
  1382. }
  1383. uint64_t optimized = extendRoute(linkToParent->child->address.path,
  1384. linkToParent->child->encodingScheme,
  1385. next,
  1386. linkToParent->inverseLinkEncodingFormNumber);
  1387. if (optimized != extendRoute_INVALID && optimized != extendRoute_TOOLONG) {
  1388. return optimized;
  1389. }
  1390. if (Defined(Log_DEBUG)) {
  1391. do {
  1392. uint8_t pathStr[20];
  1393. uint8_t nextStr[20];
  1394. uint8_t bestPathStr[20];
  1395. AddrTools_printPath(pathStr, path);
  1396. AddrTools_printPath(nextStr, next);
  1397. AddrTools_printPath(bestPathStr, linkToParent->child->address.path);
  1398. Log_debug(store->logger, "Failed to optimize path [%s] with closest known [%s] and "
  1399. "best path to closest known [%s]",
  1400. pathStr, nextStr, bestPathStr);
  1401. } while (0);
  1402. }
  1403. return path;
  1404. }
  1405. struct Node_Link* NodeStore_nextLink(struct Node_Two* parent, struct Node_Link* startLink)
  1406. {
  1407. if (!startLink) {
  1408. return RB_MIN(PeerRBTree, &parent->peerTree);
  1409. }
  1410. return PeerRBTree_RB_NEXT(startLink);
  1411. }
  1412. /** See: NodeStore.h */
  1413. struct NodeStore* NodeStore_new(struct Address* myAddress,
  1414. struct Allocator* allocator,
  1415. struct EventBase* eventBase,
  1416. struct Log* logger,
  1417. struct RumorMill* renumberMill)
  1418. {
  1419. struct Allocator* alloc = Allocator_child(allocator);
  1420. struct NodeStore_pvt* out = Allocator_clone(alloc, (&(struct NodeStore_pvt) {
  1421. .pub = {
  1422. .nodeCapacity = NodeStore_DEFAULT_NODE_CAPACITY,
  1423. .linkCapacity = NodeStore_DEFAULT_LINK_CAPACITY
  1424. },
  1425. .renumberMill = renumberMill,
  1426. .logger = logger,
  1427. .eventBase = eventBase,
  1428. .alloc = alloc
  1429. }));
  1430. Identity_set(out);
  1431. // Create the self node
  1432. struct Node_Two* selfNode = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1433. Assert_true(selfNode);
  1434. Assert_true(myAddress);
  1435. Bits_memcpy(&selfNode->address, myAddress, sizeof(struct Address));
  1436. selfNode->encodingScheme = NumberCompress_defineScheme(alloc);
  1437. selfNode->alloc = alloc;
  1438. Identity_set(selfNode);
  1439. out->pub.selfNode = selfNode;
  1440. linkNodes(selfNode, selfNode, 1, 0, 0, 1, out);
  1441. selfNode->timeLastPinged = Time_currentTimeMilliseconds();
  1442. out->pub.selfAddress = &out->selfLink->child->address;
  1443. out->pub.selfAddress->protocolVersion = Version_CURRENT_PROTOCOL;
  1444. return &out->pub;
  1445. }
  1446. //////////////////////////////////////////////////////////////////////////////////////////////
  1447. //////////////////////////////////////////////////////////////////////////////////////////////
  1448. //////////////////////////////////////////////////////////////////////////////////////////////
  1449. /**
  1450. * Dump the table, one node at a time.
  1451. */
  1452. struct Node_Two* NodeStore_dumpTable(struct NodeStore* nodeStore, uint32_t index)
  1453. {
  1454. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1455. // TODO(cjd): Schlameil the painter
  1456. uint32_t i = 0;
  1457. struct Node_Two* nn = NULL;
  1458. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1459. if (i++ == index) { return nn; }
  1460. }
  1461. return NULL;
  1462. }
  1463. struct Node_Link* NodeStore_getNextLink(struct NodeStore* nodeStore, struct Node_Link* last)
  1464. {
  1465. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1466. struct Node_Two* nn;
  1467. struct Node_Link* next;
  1468. // NULL input, take first link of first node in store
  1469. if (!last) {
  1470. nn = Identity_ncheck(RB_MIN(NodeRBTree, &store->nodeTree));
  1471. next = NULL;
  1472. } else {
  1473. next = Identity_ncheck(PeerRBTree_RB_NEXT(last));
  1474. if (next) { return next; }
  1475. nn = Identity_ncheck(NodeRBTree_RB_NEXT(last->parent));
  1476. }
  1477. while (!next) {
  1478. if (!nn) { return NULL; }
  1479. next = Identity_ncheck(RB_MIN(PeerRBTree, &nn->peerTree));
  1480. nn = Identity_ncheck(NodeRBTree_RB_NEXT(nn));
  1481. }
  1482. return next;
  1483. }
  1484. struct Node_Two* NodeStore_getNextNode(struct NodeStore* nodeStore, struct Node_Two* lastNode)
  1485. {
  1486. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1487. if (!lastNode) {
  1488. return Identity_ncheck(RB_MIN(NodeRBTree, &store->nodeTree));
  1489. }
  1490. return Identity_ncheck(NodeRBTree_RB_NEXT(lastNode));
  1491. }
  1492. static struct Node_Two* getBestCycleB(struct Node_Two* node,
  1493. uint8_t target[16],
  1494. struct NodeStore_pvt* store)
  1495. {
  1496. uint32_t targetPfx = Address_prefixForIp6(target);
  1497. uint32_t ourDistance = Address_getPrefix(store->pub.selfAddress) ^ targetPfx;
  1498. struct Node_Link* next = NULL;
  1499. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1500. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1501. if (next->child->address.path == UINT64_MAX) { continue; }
  1502. if ((Address_getPrefix(&next->child->address) ^ targetPfx) >= ourDistance) { continue; }
  1503. return next->child;
  1504. }
  1505. return NULL;
  1506. }
  1507. static int getBestCycle(struct Node_Two* node,
  1508. uint8_t target[16],
  1509. struct Node_Two** output,
  1510. int limit,
  1511. int cycle,
  1512. struct NodeStore_pvt* store)
  1513. {
  1514. Assert_true(cycle < 1000);
  1515. if (cycle < limit) {
  1516. int total = 0;
  1517. struct Node_Link* next = NULL;
  1518. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1519. if (*output) { return total; }
  1520. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1521. total += getBestCycle(next->child, target, output, limit, cycle+1, store);
  1522. }
  1523. return total;
  1524. }
  1525. *output = getBestCycleB(node, target, store);
  1526. return 1;
  1527. }
  1528. struct Node_Two* NodeStore_getBest(struct NodeStore* nodeStore, uint8_t targetAddress[16])
  1529. {
  1530. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1531. // First try to find the node directly
  1532. struct Node_Two* n = NodeStore_nodeForAddr(nodeStore, targetAddress);
  1533. if (n && Node_getBestParent(n)) { return n; }
  1534. // Try to find the best node that is a valid next hop (closer in keyspace)
  1535. for (int i = 0; i < 10000; i++) {
  1536. int ret = getBestCycle(store->pub.selfNode, targetAddress, &n, i, 0, store);
  1537. if (n || !ret) {
  1538. if (n) { Assert_true(Node_getBestParent(n)); }
  1539. return n;
  1540. }
  1541. }
  1542. // Apparently there are no valid next hops
  1543. return NULL;
  1544. }
  1545. struct NodeList* NodeStore_getPeers(uint64_t label,
  1546. const uint32_t max,
  1547. struct Allocator* allocator,
  1548. struct NodeStore* nodeStore)
  1549. {
  1550. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1551. Log_debug(store->logger, "getPeers request for [%llx]", (unsigned long long) label);
  1552. // truncate the label to the part which this node uses PLUS
  1553. // the self-interface bit for the next hop
  1554. if (label > 1) {
  1555. int bitsUsed = NumberCompress_bitsUsedForLabel(label);
  1556. label = (label & Bits_maxBits64(bitsUsed)) | 1 << bitsUsed;
  1557. }
  1558. struct NodeList* out = Allocator_calloc(allocator, sizeof(struct NodeList), 1);
  1559. out->nodes = Allocator_calloc(allocator, sizeof(char*), max);
  1560. struct Node_Link* next = NULL;
  1561. RB_FOREACH(next, PeerRBTree, &store->pub.selfNode->peerTree) {
  1562. uint64_t p = next->cannonicalLabel;
  1563. if (!Node_isOneHopLink(next) && p != 1) { continue; }
  1564. if (p == UINT64_MAX) { continue; }
  1565. if (p < label) { continue; }
  1566. if (next->child->address.path != p) { continue; }
  1567. int j;
  1568. for (j = 0; j < (int)max; j++) {
  1569. if (!out->nodes[j]) { continue; }
  1570. if ((out->nodes[j]->address.path - label) > (p - label)) { continue; }
  1571. break;
  1572. }
  1573. switch (j) {
  1574. default: Bits_memmove(out->nodes, &out->nodes[1], (j - 1) * sizeof(char*));
  1575. Gcc_FALLTHRU;
  1576. case 1: out->nodes[j - 1] = next->child;
  1577. Gcc_FALLTHRU;
  1578. case 0:;
  1579. }
  1580. }
  1581. out->size = 0;
  1582. for (int i = 0; i < (int)max; i++) {
  1583. if (out->nodes[i]) {
  1584. out->nodes = &out->nodes[i];
  1585. out->size = max - i;
  1586. break;
  1587. }
  1588. }
  1589. for (int i = 0; i < (int)out->size; i++) {
  1590. Identity_check(out->nodes[i]);
  1591. checkNode(out->nodes[i], store);
  1592. Assert_true(out->nodes[i]->address.path);
  1593. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1594. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1595. }
  1596. return out;
  1597. }
  1598. static bool isOkAnswer(struct Node_Two* node,
  1599. uint32_t compatVer,
  1600. struct NodeStore_pvt* store)
  1601. {
  1602. if (node->address.path == UINT64_MAX) {
  1603. // (very) unreachable
  1604. return false;
  1605. }
  1606. if (!Version_isCompatible(compatVer, node->address.protocolVersion)) {
  1607. return false;
  1608. }
  1609. if (node == store->pub.selfNode) {
  1610. return false;
  1611. }
  1612. return true;
  1613. }
  1614. /** See: NodeStore.h */
  1615. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* nodeStore,
  1616. struct Address* targetAddress,
  1617. const uint32_t count,
  1618. uint32_t compatVer,
  1619. struct Allocator* allocator)
  1620. {
  1621. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1622. struct NodeList* out = Allocator_malloc(allocator, sizeof(struct NodeList));
  1623. out->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  1624. out->size = count;
  1625. struct Node_Two fakeNode = { .marked = 0 };
  1626. Bits_memcpy(&fakeNode.address, targetAddress, sizeof(struct Address));
  1627. struct Node_Two* next = Identity_ncheck(RB_NFIND(NodeRBTree, &store->nodeTree, &fakeNode));
  1628. if (!next) {
  1629. next = Identity_ncheck(RB_MAX(NodeRBTree, &store->nodeTree));
  1630. }
  1631. if (!next) {
  1632. out->size = 0;
  1633. return out;
  1634. }
  1635. struct Node_Two* prev = Identity_ncheck(NodeRBTree_RB_PREV(next));
  1636. int idx = out->size-1;
  1637. while (idx > -1) {
  1638. if (prev && (!next || Address_closest(targetAddress, &next->address, &prev->address) > 0)) {
  1639. if (isOkAnswer(prev, compatVer, store)) { out->nodes[idx--] = prev; }
  1640. prev = Identity_ncheck(NodeRBTree_RB_PREV(prev));
  1641. continue;
  1642. }
  1643. if (next && (!prev || Address_closest(targetAddress, &next->address, &prev->address) < 0)) {
  1644. if (isOkAnswer(next, compatVer, store)) { out->nodes[idx--] = next; }
  1645. next = Identity_ncheck(NodeRBTree_RB_NEXT(next));
  1646. continue;
  1647. }
  1648. break;
  1649. }
  1650. out->nodes = &out->nodes[idx+1];
  1651. out->size -= idx+1;
  1652. for (int i = 0; i < (int)out->size; i++) {
  1653. Identity_check(out->nodes[i]);
  1654. Assert_true(out->nodes[i]->address.path);
  1655. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1656. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1657. }
  1658. return out;
  1659. }
  1660. void NodeStore_disconnectedPeer(struct NodeStore* nodeStore, uint64_t path)
  1661. {
  1662. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1663. struct Node_Link* nl = NodeStore_linkForPath(nodeStore, path);
  1664. if (!nl) { return; }
  1665. if (Defined(Log_DEBUG)) {
  1666. uint8_t pathStr[20];
  1667. AddrTools_printPath(pathStr, path);
  1668. Log_debug(store->logger, "NodeStore_disconnectedPeer(%s)", pathStr);
  1669. }
  1670. NodeStore_unlinkNodes(&store->pub, nl);
  1671. }
  1672. static void brokenLink(struct NodeStore_pvt* store, struct Node_Link* brokenLink)
  1673. {
  1674. NodeStore_unlinkNodes(&store->pub, brokenLink);
  1675. }
  1676. static void addLinkToMill(struct NodeStore_pvt* store, struct Node_Link* link)
  1677. {
  1678. struct Address addr;
  1679. Bits_memcpy(&addr, &link->child->address, sizeof(struct Address));
  1680. addr.path =
  1681. NodeStore_getRouteLabel(&store->pub, link->parent->address.path, link->cannonicalLabel);
  1682. Assert_true(!NodeStore_getRouteLabel_ERR(addr.path));
  1683. RumorMill_addNode(store->renumberMill, &addr);
  1684. }
  1685. void NodeStore_brokenLink(struct NodeStore* nodeStore, uint64_t path, uint64_t pathAtErrorHop)
  1686. {
  1687. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1688. if (Defined(Log_DEBUG)) {
  1689. uint8_t pathStr[20];
  1690. uint8_t pathAtErrorHopStr[20];
  1691. AddrTools_printPath(pathStr, path);
  1692. AddrTools_printPath(pathAtErrorHopStr, pathAtErrorHop);
  1693. Log_debug(store->logger, "NodeStore_brokenLink(%s, %s)", pathStr, pathAtErrorHopStr);
  1694. }
  1695. struct Node_Link* link = store->selfLink;
  1696. uint64_t thisPath = path;
  1697. for (;;) {
  1698. uint64_t nextPath = firstHopInPath(thisPath, &link, link, store);
  1699. uint64_t mask = (((uint64_t)1) << (Bits_log2x64(thisPath) + 1)) - 1;
  1700. if (Defined(Log_DEBUG)) {
  1701. uint8_t maskStr[20];
  1702. uint8_t pathStr[20];
  1703. AddrTools_printPath(pathStr, nextPath);
  1704. AddrTools_printPath(maskStr, mask);
  1705. Log_debug(store->logger, "NodeStore_brokenLink() nextPath = [%s] mask = [%s]",
  1706. pathStr, maskStr);
  1707. }
  1708. uint64_t cannonicalPath =
  1709. NodeStore_getRouteLabel(&store->pub, link->parent->address.path, link->cannonicalLabel);
  1710. Assert_true(!NodeStore_getRouteLabel_ERR(cannonicalPath) ||
  1711. cannonicalPath == UINT64_MAX ||
  1712. link->parent->address.path == UINT64_MAX);
  1713. if ((pathAtErrorHop & mask) >= nextPath) {
  1714. uint64_t cannPathAtErrorHop =
  1715. EncodingScheme_convertLabel(link->child->encodingScheme,
  1716. (pathAtErrorHop & mask),
  1717. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  1718. uint8_t cannPathAtErrorHopStr[20];
  1719. AddrTools_printPath(cannPathAtErrorHopStr, cannPathAtErrorHop);
  1720. Log_debug(store->logger, "NodeStore_brokenLink() converted pathAtErrorHop to [%s]",
  1721. cannPathAtErrorHopStr);
  1722. if (cannPathAtErrorHop == UINT64_MAX) {
  1723. // error
  1724. } else if ((cannPathAtErrorHop & mask) != thisPath) {
  1725. // wrong path
  1726. } else if (path != cannonicalPath && !NodeStore_getRouteLabel_ERR(cannonicalPath)) {
  1727. logLink(store, link, "NodeStore_brokenLink() not cannonucal, sending ping");
  1728. addLinkToMill(store, link);
  1729. return;
  1730. } else {
  1731. logLink(store, link, "NodeStore_brokenLink() removing");
  1732. brokenLink(store, link);
  1733. return;
  1734. }
  1735. } else if (firstHopInPath_NO_NEXT_LINK == nextPath && thisPath == 1) {
  1736. Assert_ifParanoid(NodeStore_linkForPath(nodeStore, path) == link);
  1737. if (path >> 56) {
  1738. logLink(store, link, "NodeStore_brokenLink() probably caused by long path");
  1739. } else if (path != cannonicalPath && !NodeStore_getRouteLabel_ERR(cannonicalPath)) {
  1740. logLink(store, link, "NodeStore_brokenLink() not cannonical, sending ping (1link)");
  1741. addLinkToMill(store, link);
  1742. return;
  1743. } else {
  1744. logLink(store, link, "NodeStore_brokenLink() removing (1link)");
  1745. brokenLink(store, link);
  1746. }
  1747. return;
  1748. }
  1749. if (firstHopInPath_NO_NEXT_LINK == nextPath) {
  1750. Log_debug(store->logger, "NodeStore_brokenLink() firstHopInPath_NO_NEXT_LINK");
  1751. // fails if pathAtErrorHop is garbage.
  1752. Assert_ifTesting(!NodeStore_linkForPath(nodeStore, path));
  1753. return;
  1754. }
  1755. if (firstHopInPath_INVALID == nextPath) {
  1756. Log_debug(store->logger, "NodeStore_brokenLink() firstHopInPath_INVALID");
  1757. return;
  1758. }
  1759. Assert_true(link);
  1760. thisPath = nextPath;
  1761. }
  1762. }
  1763. // When a response comes in, we need to pay attention to the path used.
  1764. // NOTE(cjd): newCost was unused, I'm removing it now because this code is mostly deprecated anyway
  1765. // and I don't want to enable it right now and reason how how it should behave.
  1766. static void updatePathCost(struct NodeStore_pvt* store, const uint64_t path/*, uint64_t newCost*/)
  1767. {
  1768. struct Node_Link* link = store->selfLink;
  1769. uint64_t pathFrag = path;
  1770. uint64_t now = Time_currentTimeMilliseconds();
  1771. for (;;) {
  1772. struct Node_Link* nextLink = NULL;
  1773. uint64_t nextPath = firstHopInPath(pathFrag, &nextLink, link, store);
  1774. if (firstHopInPath_ERR(nextPath)) {
  1775. break;
  1776. }
  1777. // expecting behavior of nextLinkOnPath()
  1778. Assert_ifParanoid(nextLink->parent == link->child);
  1779. // Update linkCost.
  1780. int64_t newLinkCost = calcNextCost(nextLink->linkCost);
  1781. verify(store);
  1782. handleLinkNews(nextLink, newLinkCost, store);
  1783. verify(store);
  1784. nextLink->timeLastSeen = now;
  1785. pathFrag = nextPath;
  1786. link = nextLink;
  1787. // newCost++;
  1788. }
  1789. link->child->timeLastPinged = now;
  1790. }
  1791. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds)
  1792. {
  1793. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1794. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1795. if (!link || link == store->selfLink) { return; }
  1796. // NOTE(cjd): This code is now commented because it was not in fact being used.
  1797. // struct Node_Two* node = link->child;
  1798. // uint64_t newCost;
  1799. // if (node->address.path == path) {
  1800. // // Use old cost value to calculate new cost.
  1801. // newCost = calcNextCost(Node_getCost(node));
  1802. // }
  1803. // else {
  1804. // // Old cost value doesn't relate to this path, so we should do something different
  1805. // // FIXME(arceliar): calcNextCost is guessing what the cost would stabilize to
  1806. // // I think actually fixing this would require storing cost (or latency?) per link,
  1807. // // so we can calculate the expected cost for an arbitrary path
  1808. // newCost = calcNextCost(UINT64_MAX);
  1809. // }
  1810. store->disarmCheck++;
  1811. updatePathCost(store, path/*, newCost*/);
  1812. store->disarmCheck--;
  1813. verify(store);
  1814. }
  1815. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path)
  1816. {
  1817. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1818. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1819. if (!link) { return; }
  1820. struct Node_Two* node = link->child;
  1821. // TODO(cjd): What we really should be doing here is storing this link in a
  1822. // potentially-down-list, after pinging the parent, if the parent does not respond
  1823. // and then we replace the link with the parent's link and walk backwards up
  1824. // the tree. If the parent does respond then we keep pinging the child of the path
  1825. // hoping it will respond or die and as it's link-state is destroyed by subsequent
  1826. // lost packets, children will be re-parented to other paths.
  1827. // We probably did not ping it along the node's best path.
  1828. // Keep checking until we're sure it's either OK or down.
  1829. RumorMill_addNode(store->renumberMill, &node->address);
  1830. if (!link || link->child->address.path != path) { return; }
  1831. if (link->parent != store->pub.selfNode) {
  1832. // Nevermind, we did use the best path.
  1833. // All we know for sure is that link->child didn't respond.
  1834. // That could be because an earlier link is down.
  1835. // Ping it, we should eventually backtrack to the correct link.
  1836. RumorMill_addNode(store->renumberMill, &link->parent->address);
  1837. }
  1838. uint64_t oldCost = Node_getCost(node);
  1839. int64_t newLinkCost = costAfterTimeout(link->linkCost);
  1840. verify(store);
  1841. handleLinkNews(link, newLinkCost, store);
  1842. verify(store);
  1843. if (Defined(Log_DEBUG)) {
  1844. uint8_t addr[60];
  1845. Address_print(addr, &node->address);
  1846. Log_debug(store->logger,
  1847. "Ping timeout for %s. changing cost from %llu to %llu\n",
  1848. addr,
  1849. (unsigned long long)oldCost,
  1850. (unsigned long long)Node_getCost(node));
  1851. }
  1852. }
  1853. struct Address NodeStore_addrForBucket(struct Address* source, uint16_t bucket)
  1854. {
  1855. Assert_compileTime(NodeStore_bucketNumber == 128);
  1856. struct Address addr = *source;
  1857. uint64_t* addrPart = (bucket < 64) ? &addr.ip6.longs.one_be : &addr.ip6.longs.two_be;
  1858. uint64_t bitmask = (uint64_t)1 << (63 - (bucket % 64));
  1859. *addrPart ^= Endian_hostToBigEndian64(bitmask);
  1860. Assert_ifParanoid(bucket == NodeStore_bucketForAddr(source, &addr));
  1861. return addr;
  1862. }
  1863. uint16_t NodeStore_bucketForAddr(struct Address* source, struct Address* dest)
  1864. {
  1865. Assert_compileTime(NodeStore_bucketNumber == 128);
  1866. uint16_t bucket = 0;
  1867. uint64_t addrPart = source->ip6.longs.one_be ^ dest->ip6.longs.one_be;
  1868. if (!addrPart) {
  1869. addrPart = source->ip6.longs.two_be ^ dest->ip6.longs.two_be;
  1870. bucket += 64;
  1871. }
  1872. addrPart = Endian_bigEndianToHost64(addrPart);
  1873. bucket += 63 - Bits_log2x64(addrPart);
  1874. return bucket;
  1875. }
  1876. uint64_t NodeStore_timeSinceLastPing(struct NodeStore* nodeStore, struct Node_Two* node)
  1877. {
  1878. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1879. uint64_t now = Time_currentTimeMilliseconds();
  1880. uint64_t lastSeen = node->timeLastPinged;
  1881. struct Node_Link* link = Node_getBestParent(node);
  1882. while (link && link != store->selfLink) {
  1883. lastSeen = (link->timeLastSeen < lastSeen) ? link->timeLastSeen : lastSeen;
  1884. link = Node_getBestParent(link->parent);
  1885. }
  1886. return now - lastSeen;
  1887. }
  1888. bool NodeStore_getFullVerify(struct NodeStore* nodeStore)
  1889. {
  1890. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1891. return store->fullVerify != 0;
  1892. }
  1893. void NodeStore_setFullVerify(struct NodeStore* nodeStore, bool fullVerify)
  1894. {
  1895. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1896. store->fullVerify = (fullVerify != 0);
  1897. }