NodeStore.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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 <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(store->eventBase);
  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. struct Node_Two* parent = closest->child;
  737. if (Defined(Log_DEBUG)) {
  738. uint8_t parentStr[40];
  739. uint8_t childStr[40];
  740. uint8_t pathStr[20];
  741. AddrTools_printIp(parentStr, parent->address.ip6.bytes);
  742. AddrTools_printIp(childStr, child->address.ip6.bytes);
  743. AddrTools_printPath(pathStr, pathParentChild);
  744. Log_debug(store->logger, "discoverLinkC( [%s]->[%s] [%s] )", parentStr, childStr, pathStr);
  745. }
  746. if (closest == store->selfLink &&
  747. !EncodingScheme_isOneHop(parent->encodingScheme, pathParentChild))
  748. {
  749. Log_debug(store->logger, "Attempting to create a link with no parent peer");
  750. return NULL;
  751. }
  752. if (parent == child) {
  753. if (pathParentChild == 1) {
  754. // Link is already known.
  755. //update(closest, 0, store);
  756. //Log_debug(store->logger, "Already known");
  757. return closest;
  758. }
  759. Log_debug(store->logger, "Loopey route");
  760. // lets not bother storing this link, a link with the same parent and child is
  761. // invalid according to verify() and it's just going to take up space in the store
  762. // we'll return closest which is a perfectly valid path to the same node.
  763. // We could reasonably return the closest since it is the same node but it causes
  764. // problems with an assertion in discoverLink.
  765. return NULL;
  766. }
  767. if (EncodingScheme_isSelfRoute(parent->encodingScheme, pathParentChild)) {
  768. // This should never happen for a direct peer or for a direct decendent in a split link.
  769. // This sometimes triggers because a link is split which contains an invalid encoding
  770. // somewhere in the middle.
  771. // It is not harmful to remove it becaue the route is not re-added.
  772. Assert_ifTesting(closestKnown != closest);
  773. // If the packet came in along a path which is not the best path we know, it might be
  774. // that an evil switch modified the path in transit, in this case lets send out a ping
  775. // along the best path and it should return to us, confirming that we need to relink
  776. // the node.
  777. if (discoveredPath == parent->address.path) {
  778. logLink(store, closest, "Double-checking path node change");
  779. // Ping child's key w/ parent's path
  780. uint64_t oldPath = child->address.path;
  781. child->address.path = parent->address.path;
  782. RumorMill_addNode(store->renumberMill, &child->address);
  783. child->address.path = oldPath;
  784. check(store);
  785. return NULL;
  786. } else {
  787. logLink(store, closest, "Unlinking node for path change");
  788. struct Node_Link* nextClosest = Node_getBestParent(closest->parent);
  789. uint64_t nextPPC = closest->cannonicalLabel;
  790. NodeStore_unlinkNodes(&store->pub, closest);
  791. closest = nextClosest;
  792. pathParentChild = nextPPC;
  793. parent = closest->child;
  794. }
  795. }
  796. // link parent to child
  797. //
  798. // ACKTUNG: From this point forward, the nodeStore is in an invalid state, calls to _verify()
  799. // will fail (calls to _check() will still succeed). We have linked parent with child
  800. // but we have not split all of the splitLinks from parent.
  801. //
  802. // FIXME(arceliar,cjd): linking every node with 0 link cost, this can't be right.
  803. struct Node_Link* parentLink = linkNodes(parent,
  804. child,
  805. pathParentChild,
  806. 0,
  807. inverseLinkEncodingFormNumber,
  808. discoveredPath,
  809. store);
  810. check(store);
  811. return parentLink;
  812. }
  813. static void fixLink(struct Node_Link* parentLink,
  814. struct Node_Link** outLinks,
  815. struct NodeStore_pvt* store)
  816. {
  817. int verifyOrder = 0;
  818. // Check whether the parent is already linked with a node which is "behind" the child.
  819. // splitLink appears to be a "sibling link" to the closest->node link but in reality the
  820. // splitLink link should be split and node should be inserted in the middle.
  821. struct Node_Link* splitLink = RB_MIN(PeerRBTree, &parentLink->parent->peerTree);
  822. while (splitLink) {
  823. if (splitLink == parentLink) {
  824. if (Defined(PARANOIA)) {
  825. verifyOrder = 1;
  826. splitLink = PeerRBTree_RB_NEXT(splitLink);
  827. continue;
  828. } else {
  829. // Since they're in order, definitely not found.
  830. break;
  831. }
  832. }
  833. if (!LabelSplicer_routesThrough(splitLink->cannonicalLabel, parentLink->cannonicalLabel)) {
  834. splitLink = PeerRBTree_RB_NEXT(splitLink);
  835. continue;
  836. }
  837. if (Defined(PARANOIA)) {
  838. Assert_true(!verifyOrder);
  839. }
  840. struct Node_Two* grandChild = splitLink->child;
  841. if (parentLink->child == grandChild) {
  842. // loopey route, kill it and let the bestParent pivit over to parentLink
  843. } else {
  844. logLink(store, splitLink, "Splitting link");
  845. // unsplice and cannonicalize so we now have a path from child to grandchild
  846. uint64_t childToGrandchild =
  847. LabelSplicer_unsplice(splitLink->cannonicalLabel, parentLink->cannonicalLabel);
  848. Assert_true(parentLink->child);
  849. childToGrandchild =
  850. EncodingScheme_convertLabel(parentLink->child->encodingScheme,
  851. childToGrandchild,
  852. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  853. Assert_true(childToGrandchild < UINT64_MAX);
  854. Assert_true(childToGrandchild != 1);
  855. Assert_true(splitLink->cannonicalLabel != parentLink->cannonicalLabel);
  856. // We forgot what was the discovered path for the link when we split (destroyed)
  857. // it so we'll just assume the worst among these two possibilities.
  858. // There is an assertion that discoveredPath is never < cannonicalLabel so we must.
  859. uint64_t discoveredPath = parentLink->discoveredPath;
  860. if (childToGrandchild > discoveredPath) { discoveredPath = childToGrandchild; }
  861. struct Node_Link* childLink =
  862. discoverLinkC(store, parentLink, childToGrandchild, grandChild,
  863. discoveredPath, splitLink->inverseLinkEncodingFormNumber);
  864. // Three possibilities:
  865. // 1. discoverLinkC returned NULL for whatever reason, skip this routine.
  866. // 2. discoverLinkC determined that childLink already exists and returned it, this
  867. // routine added it in a previous iteration so
  868. // childLink->nextInSplitList is not NULL so we should skip this
  869. // routine as splitLinks will already attempt to split childLink.
  870. // 3. childLink is new or has existed since before this discoverNode, we will add it
  871. // to the splitList so that splitLinks will attempt to split it.
  872. if (childLink && !childLink->nextInSplitList) {
  873. // Order the list so that the next set of links will be split from
  874. // smallest to largest and nothing will ever be split twice.
  875. for (struct Node_Link** x = outLinks;; x = &(*x)->nextInSplitList) {
  876. if (*x == childLink) { break; }
  877. if (*x && (*x)->cannonicalLabel <= childLink->cannonicalLabel) { continue; }
  878. childLink->nextInSplitList = *x;
  879. *x = childLink;
  880. break;
  881. }
  882. }
  883. }
  884. check(store);
  885. struct Node_Link* next = PeerRBTree_RB_NEXT(splitLink);
  886. NodeStore_unlinkNodes(&store->pub, splitLink);
  887. splitLink = next;
  888. }
  889. }
  890. static void fixLinks(struct Node_Link* parentLinkList,
  891. struct Node_Link** outLinks,
  892. struct NodeStore_pvt* store)
  893. {
  894. while (parentLinkList) {
  895. struct Node_Link* next = parentLinkList->nextInSplitList;
  896. parentLinkList->nextInSplitList = NULL;
  897. // else the parent link has been trashed by splitting another link.
  898. if (parentLinkList->child) {
  899. fixLink(parentLinkList, outLinks, store);
  900. }
  901. parentLinkList = next;
  902. }
  903. }
  904. static struct Node_Link* discoverLink(struct NodeStore_pvt* store,
  905. uint64_t path,
  906. struct Node_Two* child,
  907. int inverseLinkEncodingFormNumber)
  908. {
  909. struct Node_Link* link =
  910. discoverLinkC(store, store->selfLink, path, child, path, inverseLinkEncodingFormNumber);
  911. if (!link) { return NULL; }
  912. uint64_t pathParentChild = findClosest(path, &link, store->selfLink, store);
  913. // This should always be 1 because the link is gone only because it was just split!
  914. Assert_true(pathParentChild == 1);
  915. struct Node_Link* ol = NULL;
  916. struct Node_Link* nl = NULL;
  917. fixLinks(link, &ol, store);
  918. for (;;) {
  919. if (ol) {
  920. fixLinks(ol, &nl, store);
  921. ol = NULL;
  922. } else if (nl) {
  923. fixLinks(nl, &ol, store);
  924. nl = NULL;
  925. } else {
  926. break;
  927. }
  928. }
  929. verify(store);
  930. return link;
  931. }
  932. static struct Node_Two* whichIsWorse(struct Node_Two* one,
  933. struct Node_Two* two,
  934. struct NodeStore_pvt* store)
  935. {
  936. // a peer is nevar worse
  937. int worse = isPeer(one, store) - isPeer(two, store);
  938. if (worse) {
  939. return (worse > 0) ? two : one;
  940. }
  941. worse = (one->address.path == UINT64_MAX) - (two->address.path == UINT64_MAX);
  942. if (worse) {
  943. return (worse > 0) ? one : two;
  944. }
  945. if (one->address.protocolVersion != two->address.protocolVersion) {
  946. if (one->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  947. if (two->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  948. return one;
  949. }
  950. } else if (two->address.protocolVersion < Version_CURRENT_PROTOCOL) {
  951. if (one->address.protocolVersion >= Version_CURRENT_PROTOCOL) {
  952. return two;
  953. }
  954. }
  955. }
  956. uint32_t selfPrefix = Address_getPrefix(&store->pub.selfNode->address);
  957. uint64_t distOne = Address_getPrefix(&one->address) ^ selfPrefix;
  958. uint64_t distTwo = Address_getPrefix(&two->address) ^ selfPrefix;
  959. distOne += Node_getCost(one);
  960. distTwo += Node_getCost(two);
  961. if (Defined(NodeStore_whichIsWorse_PATHCOUNTS)) {
  962. distOne += Bits_log2x64(one->address.path) << 26;
  963. distTwo += Bits_log2x64(two->address.path) << 26;
  964. }
  965. if (distOne < distTwo) { return two; }
  966. return one;
  967. }
  968. struct NodeList* NodeStore_getNodesForBucket(struct NodeStore* nodeStore,
  969. struct Allocator* allocator,
  970. uint16_t bucket,
  971. const uint32_t count)
  972. {
  973. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  974. struct NodeList* nodeList = Allocator_malloc(allocator, sizeof(struct NodeList));
  975. nodeList->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  976. nodeList->size = 0;
  977. struct Node_Two* nn = NULL;
  978. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  979. if (Node_getCost(nn) == UINT64_MAX) { continue; }
  980. if (NodeStore_bucketForAddr(store->pub.selfAddress, &nn->address) == bucket) {
  981. struct Node_Two* newNode = nn;
  982. struct Node_Two* tempNode = NULL;
  983. for (uint32_t i = 0 ; i < count ; i++) {
  984. if (nodeList->size < i+1) {
  985. // The list isn't full yet, so insert at the end.
  986. nodeList->size = i+1;
  987. nodeList->nodes[i] = newNode;
  988. break;
  989. }
  990. if ( (newNode->marked && !nodeList->nodes[i]->marked) ||
  991. whichIsWorse(nodeList->nodes[i], newNode, store) == nodeList->nodes[i] ) {
  992. // If we've already marked nodes because they're a bestParent,
  993. // lets give them priority in the bucket since we need to keep
  994. // them either way.
  995. // Otherwise, decide based on whichIsWorse().
  996. // Insertion sorted list.
  997. tempNode = nodeList->nodes[i];
  998. nodeList->nodes[i] = newNode;
  999. newNode = tempNode;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. return nodeList;
  1005. }
  1006. static bool markNodesForBucket(struct NodeStore_pvt* store,
  1007. uint16_t bucket,
  1008. const uint32_t count)
  1009. {
  1010. struct Allocator* nodeListAlloc = Allocator_child(store->alloc);
  1011. struct NodeList* nodeList = NodeStore_getNodesForBucket(&store->pub,
  1012. nodeListAlloc,
  1013. bucket,
  1014. count);
  1015. bool retVal = false;
  1016. if (nodeList->size > 0) { retVal = true; }
  1017. for (uint32_t i = 0; i < nodeList->size; i++) {
  1018. // Now mark the nodes in the list to protect them.
  1019. Identity_check(nodeList->nodes[i]);
  1020. nodeList->nodes[i]->marked = 1;
  1021. }
  1022. // Cleanup
  1023. Allocator_free(nodeListAlloc);
  1024. return retVal;
  1025. }
  1026. static void markKeyspaceNodes(struct NodeStore_pvt* store)
  1027. {
  1028. for (uint16_t bucket = 0; bucket < NodeStore_bucketNumber ; bucket++) {
  1029. markNodesForBucket(store, bucket, NodeStore_bucketSize);
  1030. }
  1031. }
  1032. /**
  1033. * We define the worst node the node with the highest cost, excluding nodes which are required for
  1034. * the DHT, and nodes which are somebody's bestParent (only relevant if they're the bestParent of
  1035. * a DHT-required node, as otherwise their child would always be higher cost).
  1036. * If two nodes tie (e.g. two unreachable nodes with maximum cost) then the node which is
  1037. * further from us in keyspace is worse.
  1038. */
  1039. static struct Node_Two* getWorstNode(struct NodeStore_pvt* store)
  1040. {
  1041. struct Node_Two* worst = NULL;
  1042. struct Node_Two* nn = NULL;
  1043. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1044. // first cycle we set markings so markings remain if they are behind us
  1045. struct Node_Link* parentLink = Node_getBestParent(nn);
  1046. if (parentLink) {
  1047. parentLink->parent->marked = 1;
  1048. } else if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1049. // this time around we're only addressing nodes which are unreachable.
  1050. worst = nn;
  1051. }
  1052. }
  1053. if (worst) {
  1054. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1055. if (nn->marked) { nn->marked = false; }
  1056. }
  1057. return worst;
  1058. }
  1059. // Mark the nodes that we need to protect for keyspace reasons.
  1060. markKeyspaceNodes(store);
  1061. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1062. if (nn->marked) {
  1063. nn->marked = false;
  1064. } else if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1065. worst = nn;
  1066. }
  1067. }
  1068. if (worst) { return worst; }
  1069. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1070. // third cycle, every node is apparently important but we need to get rid of someone
  1071. // get whoever is worst if we ignore markings
  1072. // by definition, this shouldn't be a bestParent, because their children have higher cost
  1073. // so we're potentially creating a keyspace hole (routing blackhole) when we do this.
  1074. // TODO(arceliar): protect keyspace, evict the worst bestParent instead?
  1075. // Would require something like a forgetNode() to splice links together between
  1076. // that node's bestParent and all its children, before we kill it.
  1077. if (!worst || whichIsWorse(nn, worst, store) == nn) {
  1078. worst = nn;
  1079. }
  1080. }
  1081. // somebody has to be at the end of the line, not *everyone* can be someone's best parent!
  1082. Assert_true(worst);
  1083. return worst;
  1084. }
  1085. static void destroyNode(struct Node_Two* node, struct NodeStore_pvt* store)
  1086. {
  1087. // careful, undefined unless debug is enabled...
  1088. uint8_t address_debug[60];
  1089. if (Defined(Log_DEBUG)) {
  1090. Address_print(address_debug, &node->address);
  1091. }
  1092. struct Node_Link* link;
  1093. RB_FOREACH(link, PeerRBTree, &node->peerTree) {
  1094. Identity_check(link);
  1095. NodeStore_unlinkNodes(&store->pub, link);
  1096. }
  1097. // If the node has a bestParent, it will be changed a number
  1098. // of times as we kill off all of it's parent links.
  1099. // This is an optimization:
  1100. if (!Defined(PARANOIA)) {
  1101. store->pub.linkedNodes--;
  1102. setParentCostAndPath(node, NULL, UINT64_MAX, UINT64_MAX, store);
  1103. }
  1104. link = node->reversePeers;
  1105. while (link) {
  1106. struct Node_Link* nextLink = link->nextPeer;
  1107. NodeStore_unlinkNodes(&store->pub, link);
  1108. link = nextLink;
  1109. }
  1110. Assert_true(!Node_getBestParent(node));
  1111. Assert_ifParanoid(node == RB_FIND(NodeRBTree, &store->nodeTree, node));
  1112. RB_REMOVE(NodeRBTree, &store->nodeTree, node);
  1113. store->pub.nodeCount--;
  1114. Allocator_free(node->alloc);
  1115. }
  1116. // Must be at least 2 to avoid multiplying by 0.
  1117. // If too large, path choice may become unstable due to a guess we make in calcNextCost.
  1118. // This is fixable by storing cost based on links. A lot of work.
  1119. // In the mean time, just don't use a large value.
  1120. #define NodeStore_latencyWindow 8
  1121. static uint32_t costAfterDecay(const uint32_t oldCost)
  1122. {
  1123. // Increase the cost by 1/Xth where X = NodeStore_latencyWindow
  1124. // This is used to keep a weighted rolling average
  1125. int64_t newCost = oldCost - oldCost/NodeStore_latencyWindow;
  1126. if (newCost < 1024) {
  1127. // Set some minimum cost
  1128. newCost = 1024;
  1129. }
  1130. return newCost;
  1131. }
  1132. static uint32_t costAfterTimeout(const uint64_t oldCost)
  1133. {
  1134. int64_t newCost = oldCost;
  1135. newCost *= NodeStore_latencyWindow;
  1136. newCost /= NodeStore_latencyWindow - 1;
  1137. if (newCost > UINT32_MAX) { newCost = UINT32_MAX; }
  1138. return newCost;
  1139. }
  1140. // Returns new cost of a link
  1141. static uint32_t calcNextCost(const uint64_t oldCost)
  1142. {
  1143. // TODO(arceliar) the 1023 here is pretty arbitrary...
  1144. uint64_t out = costAfterDecay(oldCost);
  1145. // TODO(arceliar): is this safe?
  1146. Assert_true(out >= 1024 && out != UINT64_MAX);
  1147. return out;
  1148. }
  1149. static struct Node_Link* discoverNode(struct NodeStore_pvt* store,
  1150. struct Address* addr,
  1151. struct EncodingScheme* scheme,
  1152. int inverseLinkEncodingFormNumber,
  1153. uint64_t milliseconds)
  1154. {
  1155. struct Node_Two* child = nodeForIp(store, addr->ip6.bytes);
  1156. if (Defined(Log_DEBUG)) {
  1157. uint8_t printedAddr[60];
  1158. Address_print(printedAddr, addr);
  1159. Log_debug(store->logger, "Discover node [%s]", printedAddr);
  1160. }
  1161. if (child && child == store->selfLink->child) {
  1162. return NULL;
  1163. }
  1164. if (child && EncodingScheme_compare(child->encodingScheme, scheme)) {
  1165. // Shit.
  1166. // Box reset *and* they just updated and changed their encoding scheme.
  1167. RumorMill_addNode(store->renumberMill, &child->address);
  1168. if (addr->path > (child->address.path | (child->address.path << 3))) {
  1169. Log_debug(store->logger, "Node appears to have changed it's encoding scheme "
  1170. "but the message came from far away and we will not trust it");
  1171. return NULL;
  1172. } else {
  1173. Log_debug(store->logger, "Node appears to have changed it's encoding scheme "
  1174. "dropping him from the table and re-inserting");
  1175. destroyNode(child, store);
  1176. child = NULL;
  1177. }
  1178. } else if (child && child->address.protocolVersion != addr->protocolVersion) {
  1179. child->address.protocolVersion = addr->protocolVersion;
  1180. }
  1181. struct Allocator* alloc = NULL;
  1182. if (!child) {
  1183. alloc = Allocator_child(store->alloc);
  1184. child = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1185. child->alloc = alloc;
  1186. Bits_memcpy(&child->address, addr, sizeof(struct Address));
  1187. child->encodingScheme = EncodingScheme_clone(scheme, child->alloc);
  1188. child->timeLastPinged = Time_currentTimeMilliseconds(store->eventBase);
  1189. Identity_set(child);
  1190. }
  1191. struct Node_Link* link = NULL;
  1192. for (;;) {
  1193. link = discoverLink(store,
  1194. addr->path,
  1195. child,
  1196. inverseLinkEncodingFormNumber);
  1197. if (link) { break; }
  1198. // We might have a broken link in the store which is causing new links to be rejected.
  1199. // On the other hand, this path might actually be garbage :)
  1200. // There's a DoS risk in that someone might use garbage paths to evict all of the
  1201. // existing good paths.
  1202. // While an attacker can send in a packet, it will necessarily follow a ridiculous path
  1203. // in order that the path contains one of their nodes.
  1204. // To resolve this, we'll walk the path looking for the "bad" link, then we'll check that
  1205. // node to see if the path we took to reach it is actually the *best* path to that node.
  1206. uint64_t path = addr->path;
  1207. struct Node_Link* lastLink = store->selfLink;
  1208. do {
  1209. struct Node_Link* nextLink = NULL;
  1210. path = firstHopInPath(path, &nextLink, lastLink, store);
  1211. lastLink = nextLink;
  1212. if (path == firstHopInPath_NO_NEXT_LINK) {
  1213. // discoverNode() failed for some other reason.
  1214. lastLink = NULL;
  1215. break;
  1216. }
  1217. } while (firstHopInPath_INVALID != path);
  1218. if (lastLink && LabelSplicer_routesThrough(addr->path, lastLink->child->address.path)) {
  1219. // checking for sillyness...
  1220. Assert_true(lastLink != store->selfLink);
  1221. NodeStore_unlinkNodes(&store->pub, lastLink);
  1222. continue;
  1223. }
  1224. if (alloc) {
  1225. Allocator_free(alloc);
  1226. }
  1227. verify(store);
  1228. Log_debug(store->logger, "Invalid path");
  1229. return NULL;
  1230. }
  1231. Assert_true(link->child);
  1232. #ifdef PARANOIA
  1233. struct Node_Two* parent = link->parent;
  1234. #endif
  1235. //handleNews(link->child, cost, store);
  1236. verify(store);
  1237. handleLinkNews(link, calcNextCost(link->linkCost), store);
  1238. verify(store);
  1239. freePendingLinks(store);
  1240. while ((store->pub.nodeCount - store->pub.peerCount) >
  1241. store->pub.nodeCapacity
  1242. || store->pub.linkCount > store->pub.linkCapacity)
  1243. {
  1244. struct Node_Two* worst = getWorstNode(store);
  1245. if (Defined(Log_DEBUG)) {
  1246. uint8_t worstAddr[60];
  1247. Address_print(worstAddr, &worst->address);
  1248. Log_debug(store->logger, "store full, removing worst node: [%s] nodes [%d] links [%d]",
  1249. worstAddr, store->pub.nodeCount, store->pub.linkCount);
  1250. }
  1251. Assert_true(!isPeer(worst, store));
  1252. if (link && (worst == link->parent || worst == link->child)) { link = NULL; }
  1253. destroyNode(worst, store);
  1254. freePendingLinks(store);
  1255. }
  1256. verify(store);
  1257. // This should test that link == NodeStore_linkForPath(path) but that is not guaranteed
  1258. // to work because links are not healed up when a node is removed from the store
  1259. Assert_ifParanoid(!link || RB_FIND(PeerRBTree, &parent->peerTree, link) == link);
  1260. return link;
  1261. }
  1262. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  1263. struct Address* addr,
  1264. struct EncodingScheme* scheme,
  1265. int inverseLinkEncodingFormNumber,
  1266. uint64_t milliseconds)
  1267. {
  1268. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1269. store->disarmCheck++;
  1270. struct Node_Link* out =
  1271. discoverNode(store, addr, scheme, inverseLinkEncodingFormNumber, milliseconds);
  1272. store->disarmCheck--;
  1273. verify(store);
  1274. return out;
  1275. }
  1276. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16])
  1277. {
  1278. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1279. struct Node_Two* n = nodeForIp(store, addr);
  1280. if (n && n->address.path == UINT64_MAX) {
  1281. if (Defined(Log_DEBUG)) {
  1282. uint8_t addrStr[40];
  1283. AddrTools_printIp(addrStr, n->address.ip6.bytes);
  1284. Log_debug(store->logger, "No way to represent path to [%s]", addrStr);
  1285. }
  1286. return NULL;
  1287. }
  1288. return n;
  1289. }
  1290. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path)
  1291. {
  1292. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1293. struct Node_Link* out = NULL;
  1294. findClosest(path, &out, store->selfLink, store);
  1295. if (!out) { return NULL; }
  1296. return Identity_check(out->child);
  1297. }
  1298. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path)
  1299. {
  1300. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1301. struct Node_Link* out = NULL;
  1302. uint64_t pathParentChild = findClosest(path, &out, store->selfLink, store);
  1303. if (pathParentChild != 1) { return NULL; }
  1304. return Identity_check(out);
  1305. }
  1306. struct Node_Link* NodeStore_firstHopInPath(struct NodeStore* nodeStore,
  1307. uint64_t path,
  1308. uint64_t* correctedPath,
  1309. struct Node_Link* startingPoint)
  1310. {
  1311. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1312. if (!startingPoint) { startingPoint = store->selfLink; }
  1313. if (!Node_getBestParent(startingPoint->parent)) { return NULL; }
  1314. struct Node_Link* out = NULL;
  1315. path = firstHopInPath(path, &out, startingPoint, store);
  1316. if (firstHopInPath_ERR(path)) { return NULL; }
  1317. if (correctedPath) { *correctedPath = path; }
  1318. return out;
  1319. }
  1320. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal)
  1321. {
  1322. switch (returnVal) {
  1323. case NodeStore_getRouteLabel_PARENT_NOT_FOUND:
  1324. return "NodeStore_getRouteLabel_PARENT_NOT_FOUND";
  1325. case NodeStore_getRouteLabel_CHILD_NOT_FOUND:
  1326. return "NodeStore_getRouteLabel_CHILD_NOT_FOUND";
  1327. default: return NULL;
  1328. }
  1329. }
  1330. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  1331. uint64_t pathToParent,
  1332. uint64_t pathParentToChild)
  1333. {
  1334. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1335. struct Node_Link* linkToParent;
  1336. if (findClosest(pathToParent, &linkToParent, store->selfLink, store) != 1) {
  1337. return NodeStore_getRouteLabel_PARENT_NOT_FOUND;
  1338. }
  1339. //logLink(store, linkToParent, "NodeStore_getRouteLabel() PARENT");
  1340. struct Node_Link* linkToChild = NULL;
  1341. RB_FOREACH_REVERSE(linkToChild, PeerRBTree, &linkToParent->child->peerTree) {
  1342. if (pathParentToChild == linkToChild->cannonicalLabel) {
  1343. if (linkToParent == store->selfLink) {
  1344. return linkToChild->cannonicalLabel;
  1345. }
  1346. // TODO(cjd): this could return ~0
  1347. return extendRoute(pathToParent,
  1348. linkToChild->parent->encodingScheme,
  1349. linkToChild->cannonicalLabel,
  1350. linkToParent->inverseLinkEncodingFormNumber);
  1351. }
  1352. }
  1353. return NodeStore_getRouteLabel_CHILD_NOT_FOUND;
  1354. }
  1355. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path)
  1356. {
  1357. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1358. struct Node_Link* linkToParent;
  1359. uint64_t next = findClosest(path, &linkToParent, store->selfLink, store);
  1360. if (next == findClosest_INVALID) {
  1361. return NodeStore_optimizePath_INVALID;
  1362. }
  1363. if (EncodingScheme_isSelfRoute(linkToParent->child->encodingScheme, next)) {
  1364. // cannoicalize all the other wild ways that they can represent self routes.
  1365. // TODO(cjd): this has been the source of assert failures and we might be sweeping
  1366. // a significant bug under the carpet.
  1367. next = 1;
  1368. }
  1369. if (linkToParent == store->selfLink) {
  1370. if (next == 1) { return 1; }
  1371. return path;
  1372. }
  1373. if (next == 1) { return linkToParent->child->address.path; }
  1374. struct Node_Link* childBestParent = Node_getBestParent(linkToParent->child);
  1375. if (childBestParent) {
  1376. linkToParent = childBestParent;
  1377. }
  1378. uint64_t optimized = extendRoute(linkToParent->child->address.path,
  1379. linkToParent->child->encodingScheme,
  1380. next,
  1381. linkToParent->inverseLinkEncodingFormNumber);
  1382. if (optimized != extendRoute_INVALID && optimized != extendRoute_TOOLONG) {
  1383. return optimized;
  1384. }
  1385. if (Defined(Log_DEBUG)) {
  1386. do {
  1387. uint8_t pathStr[20];
  1388. uint8_t nextStr[20];
  1389. uint8_t bestPathStr[20];
  1390. AddrTools_printPath(pathStr, path);
  1391. AddrTools_printPath(nextStr, next);
  1392. AddrTools_printPath(bestPathStr, linkToParent->child->address.path);
  1393. Log_debug(store->logger, "Failed to optimize path [%s] with closest known [%s] and "
  1394. "best path to closest known [%s]",
  1395. pathStr, nextStr, bestPathStr);
  1396. } while (0);
  1397. }
  1398. return path;
  1399. }
  1400. struct Node_Link* NodeStore_nextLink(struct Node_Two* parent, struct Node_Link* startLink)
  1401. {
  1402. if (!startLink) {
  1403. return RB_MIN(PeerRBTree, &parent->peerTree);
  1404. }
  1405. return PeerRBTree_RB_NEXT(startLink);
  1406. }
  1407. /** See: NodeStore.h */
  1408. struct NodeStore* NodeStore_new(struct Address* myAddress,
  1409. struct Allocator* allocator,
  1410. struct EventBase* eventBase,
  1411. struct Log* logger,
  1412. struct RumorMill* renumberMill)
  1413. {
  1414. struct Allocator* alloc = Allocator_child(allocator);
  1415. struct NodeStore_pvt* out = Allocator_clone(alloc, (&(struct NodeStore_pvt) {
  1416. .pub = {
  1417. .nodeCapacity = NodeStore_DEFAULT_NODE_CAPACITY,
  1418. .linkCapacity = NodeStore_DEFAULT_LINK_CAPACITY
  1419. },
  1420. .renumberMill = renumberMill,
  1421. .logger = logger,
  1422. .eventBase = eventBase,
  1423. .alloc = alloc
  1424. }));
  1425. Identity_set(out);
  1426. // Create the self node
  1427. struct Node_Two* selfNode = Allocator_calloc(alloc, sizeof(struct Node_Two), 1);
  1428. Assert_true(selfNode);
  1429. Assert_true(myAddress);
  1430. Bits_memcpy(&selfNode->address, myAddress, sizeof(struct Address));
  1431. selfNode->encodingScheme = NumberCompress_defineScheme(alloc);
  1432. selfNode->alloc = alloc;
  1433. Identity_set(selfNode);
  1434. out->pub.selfNode = selfNode;
  1435. linkNodes(selfNode, selfNode, 1, 0, 0, 1, out);
  1436. selfNode->timeLastPinged = Time_currentTimeMilliseconds(out->eventBase);
  1437. out->pub.selfAddress = &out->selfLink->child->address;
  1438. out->pub.selfAddress->protocolVersion = Version_CURRENT_PROTOCOL;
  1439. return &out->pub;
  1440. }
  1441. //////////////////////////////////////////////////////////////////////////////////////////////
  1442. //////////////////////////////////////////////////////////////////////////////////////////////
  1443. //////////////////////////////////////////////////////////////////////////////////////////////
  1444. /**
  1445. * Dump the table, one node at a time.
  1446. */
  1447. struct Node_Two* NodeStore_dumpTable(struct NodeStore* nodeStore, uint32_t index)
  1448. {
  1449. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1450. // TODO(cjd): Schlameil the painter
  1451. uint32_t i = 0;
  1452. struct Node_Two* nn = NULL;
  1453. RB_FOREACH(nn, NodeRBTree, &store->nodeTree) {
  1454. if (i++ == index) { return nn; }
  1455. }
  1456. return NULL;
  1457. }
  1458. struct Node_Link* NodeStore_getNextLink(struct NodeStore* nodeStore, struct Node_Link* last)
  1459. {
  1460. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1461. struct Node_Two* nn;
  1462. struct Node_Link* next;
  1463. // NULL input, take first link of first node in store
  1464. if (!last) {
  1465. nn = Identity_ncheck(RB_MIN(NodeRBTree, &store->nodeTree));
  1466. next = NULL;
  1467. } else {
  1468. next = Identity_ncheck(PeerRBTree_RB_NEXT(last));
  1469. if (next) { return next; }
  1470. nn = Identity_ncheck(NodeRBTree_RB_NEXT(last->parent));
  1471. }
  1472. while (!next) {
  1473. if (!nn) { return NULL; }
  1474. next = Identity_ncheck(RB_MIN(PeerRBTree, &nn->peerTree));
  1475. nn = Identity_ncheck(NodeRBTree_RB_NEXT(nn));
  1476. }
  1477. return next;
  1478. }
  1479. struct Node_Two* NodeStore_getNextNode(struct NodeStore* nodeStore, struct Node_Two* lastNode)
  1480. {
  1481. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1482. if (!lastNode) {
  1483. return Identity_ncheck(RB_MIN(NodeRBTree, &store->nodeTree));
  1484. }
  1485. return Identity_ncheck(NodeRBTree_RB_NEXT(lastNode));
  1486. }
  1487. static struct Node_Two* getBestCycleB(struct Node_Two* node,
  1488. uint8_t target[16],
  1489. struct NodeStore_pvt* store)
  1490. {
  1491. uint32_t targetPfx = Address_prefixForIp6(target);
  1492. uint32_t ourDistance = Address_getPrefix(store->pub.selfAddress) ^ targetPfx;
  1493. struct Node_Link* next = NULL;
  1494. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1495. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1496. if (next->child->address.path == UINT64_MAX) { continue; }
  1497. if ((Address_getPrefix(&next->child->address) ^ targetPfx) >= ourDistance) { continue; }
  1498. return next->child;
  1499. }
  1500. return NULL;
  1501. }
  1502. static int getBestCycle(struct Node_Two* node,
  1503. uint8_t target[16],
  1504. struct Node_Two** output,
  1505. int limit,
  1506. int cycle,
  1507. struct NodeStore_pvt* store)
  1508. {
  1509. Assert_true(cycle < 1000);
  1510. if (cycle < limit) {
  1511. int total = 0;
  1512. struct Node_Link* next = NULL;
  1513. RB_FOREACH_REVERSE(next, PeerRBTree, &node->peerTree) {
  1514. if (*output) { return total; }
  1515. if (Node_getBestParent(next->child) != next || next == store->selfLink) { continue; }
  1516. total += getBestCycle(next->child, target, output, limit, cycle+1, store);
  1517. }
  1518. return total;
  1519. }
  1520. *output = getBestCycleB(node, target, store);
  1521. return 1;
  1522. }
  1523. struct Node_Two* NodeStore_getBest(struct NodeStore* nodeStore, uint8_t targetAddress[16])
  1524. {
  1525. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1526. // First try to find the node directly
  1527. struct Node_Two* n = NodeStore_nodeForAddr(nodeStore, targetAddress);
  1528. if (n && Node_getBestParent(n)) { return n; }
  1529. // Try to find the best node that is a valid next hop (closer in keyspace)
  1530. for (int i = 0; i < 10000; i++) {
  1531. int ret = getBestCycle(store->pub.selfNode, targetAddress, &n, i, 0, store);
  1532. if (n || !ret) {
  1533. if (n) { Assert_true(Node_getBestParent(n)); }
  1534. return n;
  1535. }
  1536. }
  1537. // Apparently there are no valid next hops
  1538. return NULL;
  1539. }
  1540. struct NodeList* NodeStore_getPeers(uint64_t label,
  1541. const uint32_t max,
  1542. struct Allocator* allocator,
  1543. struct NodeStore* nodeStore)
  1544. {
  1545. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1546. Log_debug(store->logger, "getPeers request for [%llx]", (unsigned long long) label);
  1547. // truncate the label to the part which this node uses PLUS
  1548. // the self-interface bit for the next hop
  1549. if (label > 1) {
  1550. int bitsUsed = NumberCompress_bitsUsedForLabel(label);
  1551. label = (label & Bits_maxBits64(bitsUsed)) | 1 << bitsUsed;
  1552. }
  1553. struct NodeList* out = Allocator_calloc(allocator, sizeof(struct NodeList), 1);
  1554. out->nodes = Allocator_calloc(allocator, sizeof(char*), max);
  1555. struct Node_Link* next = NULL;
  1556. RB_FOREACH(next, PeerRBTree, &store->pub.selfNode->peerTree) {
  1557. uint64_t p = next->cannonicalLabel;
  1558. if (!Node_isOneHopLink(next) && p != 1) { continue; }
  1559. if (p == UINT64_MAX) { continue; }
  1560. if (p < label) { continue; }
  1561. if (next->child->address.path != p) { continue; }
  1562. int j;
  1563. for (j = 0; j < (int)max; j++) {
  1564. if (!out->nodes[j]) { continue; }
  1565. if ((out->nodes[j]->address.path - label) > (p - label)) { continue; }
  1566. break;
  1567. }
  1568. switch (j) {
  1569. default: Bits_memmove(out->nodes, &out->nodes[1], (j - 1) * sizeof(char*));
  1570. Gcc_FALLTHRU;
  1571. case 1: out->nodes[j - 1] = next->child;
  1572. Gcc_FALLTHRU;
  1573. case 0:;
  1574. }
  1575. }
  1576. out->size = 0;
  1577. for (int i = 0; i < (int)max; i++) {
  1578. if (out->nodes[i]) {
  1579. out->nodes = &out->nodes[i];
  1580. out->size = max - i;
  1581. break;
  1582. }
  1583. }
  1584. for (int i = 0; i < (int)out->size; i++) {
  1585. Identity_check(out->nodes[i]);
  1586. checkNode(out->nodes[i], store);
  1587. Assert_true(out->nodes[i]->address.path);
  1588. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1589. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1590. }
  1591. return out;
  1592. }
  1593. static bool isOkAnswer(struct Node_Two* node,
  1594. uint32_t compatVer,
  1595. struct NodeStore_pvt* store)
  1596. {
  1597. if (node->address.path == UINT64_MAX) {
  1598. // (very) unreachable
  1599. return false;
  1600. }
  1601. if (!Version_isCompatible(compatVer, node->address.protocolVersion)) {
  1602. return false;
  1603. }
  1604. if (node == store->pub.selfNode) {
  1605. return false;
  1606. }
  1607. return true;
  1608. }
  1609. /** See: NodeStore.h */
  1610. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* nodeStore,
  1611. struct Address* targetAddress,
  1612. const uint32_t count,
  1613. uint32_t compatVer,
  1614. struct Allocator* allocator)
  1615. {
  1616. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1617. struct NodeList* out = Allocator_malloc(allocator, sizeof(struct NodeList));
  1618. out->nodes = Allocator_calloc(allocator, count, sizeof(char*));
  1619. out->size = count;
  1620. struct Node_Two fakeNode = { .marked = 0 };
  1621. Bits_memcpy(&fakeNode.address, targetAddress, sizeof(struct Address));
  1622. struct Node_Two* next = Identity_ncheck(RB_NFIND(NodeRBTree, &store->nodeTree, &fakeNode));
  1623. if (!next) {
  1624. next = Identity_ncheck(RB_MAX(NodeRBTree, &store->nodeTree));
  1625. }
  1626. if (!next) {
  1627. out->size = 0;
  1628. return out;
  1629. }
  1630. struct Node_Two* prev = Identity_ncheck(NodeRBTree_RB_PREV(next));
  1631. int idx = out->size-1;
  1632. while (idx > -1) {
  1633. if (prev && (!next || Address_closest(targetAddress, &next->address, &prev->address) > 0)) {
  1634. if (isOkAnswer(prev, compatVer, store)) { out->nodes[idx--] = prev; }
  1635. prev = Identity_ncheck(NodeRBTree_RB_PREV(prev));
  1636. continue;
  1637. }
  1638. if (next && (!prev || Address_closest(targetAddress, &next->address, &prev->address) < 0)) {
  1639. if (isOkAnswer(next, compatVer, store)) { out->nodes[idx--] = next; }
  1640. next = Identity_ncheck(NodeRBTree_RB_NEXT(next));
  1641. continue;
  1642. }
  1643. break;
  1644. }
  1645. out->nodes = &out->nodes[idx+1];
  1646. out->size -= idx+1;
  1647. for (int i = 0; i < (int)out->size; i++) {
  1648. Identity_check(out->nodes[i]);
  1649. Assert_true(out->nodes[i]->address.path);
  1650. Assert_true(out->nodes[i]->address.path < (((uint64_t)1)<<63));
  1651. out->nodes[i] = Allocator_clone(allocator, out->nodes[i]);
  1652. }
  1653. return out;
  1654. }
  1655. void NodeStore_disconnectedPeer(struct NodeStore* nodeStore, uint64_t path)
  1656. {
  1657. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1658. struct Node_Link* nl = NodeStore_linkForPath(nodeStore, path);
  1659. if (!nl) { return; }
  1660. if (Defined(Log_DEBUG)) {
  1661. uint8_t pathStr[20];
  1662. AddrTools_printPath(pathStr, path);
  1663. Log_debug(store->logger, "NodeStore_disconnectedPeer(%s)", pathStr);
  1664. }
  1665. NodeStore_unlinkNodes(&store->pub, nl);
  1666. }
  1667. static void brokenLink(struct NodeStore_pvt* store, struct Node_Link* brokenLink)
  1668. {
  1669. NodeStore_unlinkNodes(&store->pub, brokenLink);
  1670. }
  1671. static void addLinkToMill(struct NodeStore_pvt* store, struct Node_Link* link)
  1672. {
  1673. struct Address addr;
  1674. Bits_memcpy(&addr, &link->child->address, sizeof(struct Address));
  1675. addr.path =
  1676. NodeStore_getRouteLabel(&store->pub, link->parent->address.path, link->cannonicalLabel);
  1677. Assert_true(!NodeStore_getRouteLabel_ERR(addr.path));
  1678. RumorMill_addNode(store->renumberMill, &addr);
  1679. }
  1680. void NodeStore_brokenLink(struct NodeStore* nodeStore, uint64_t path, uint64_t pathAtErrorHop)
  1681. {
  1682. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1683. if (Defined(Log_DEBUG)) {
  1684. uint8_t pathStr[20];
  1685. uint8_t pathAtErrorHopStr[20];
  1686. AddrTools_printPath(pathStr, path);
  1687. AddrTools_printPath(pathAtErrorHopStr, pathAtErrorHop);
  1688. Log_debug(store->logger, "NodeStore_brokenLink(%s, %s)", pathStr, pathAtErrorHopStr);
  1689. }
  1690. struct Node_Link* link = store->selfLink;
  1691. uint64_t thisPath = path;
  1692. for (;;) {
  1693. uint64_t nextPath = firstHopInPath(thisPath, &link, link, store);
  1694. uint64_t mask = (((uint64_t)1) << (Bits_log2x64(thisPath) + 1)) - 1;
  1695. if (Defined(Log_DEBUG)) {
  1696. uint8_t maskStr[20];
  1697. uint8_t pathStr[20];
  1698. AddrTools_printPath(pathStr, nextPath);
  1699. AddrTools_printPath(maskStr, mask);
  1700. Log_debug(store->logger, "NodeStore_brokenLink() nextPath = [%s] mask = [%s]",
  1701. pathStr, maskStr);
  1702. }
  1703. uint64_t cannonicalPath =
  1704. NodeStore_getRouteLabel(&store->pub, link->parent->address.path, link->cannonicalLabel);
  1705. Assert_true(!NodeStore_getRouteLabel_ERR(cannonicalPath) ||
  1706. cannonicalPath == UINT64_MAX ||
  1707. link->parent->address.path == UINT64_MAX);
  1708. if ((pathAtErrorHop & mask) >= nextPath) {
  1709. uint64_t cannPathAtErrorHop =
  1710. EncodingScheme_convertLabel(link->child->encodingScheme,
  1711. (pathAtErrorHop & mask),
  1712. EncodingScheme_convertLabel_convertTo_CANNONICAL);
  1713. uint8_t cannPathAtErrorHopStr[20];
  1714. AddrTools_printPath(cannPathAtErrorHopStr, cannPathAtErrorHop);
  1715. Log_debug(store->logger, "NodeStore_brokenLink() converted pathAtErrorHop to [%s]",
  1716. cannPathAtErrorHopStr);
  1717. if (cannPathAtErrorHop == UINT64_MAX) {
  1718. // error
  1719. } else if ((cannPathAtErrorHop & mask) != thisPath) {
  1720. // wrong path
  1721. } else if (path != cannonicalPath && !NodeStore_getRouteLabel_ERR(cannonicalPath)) {
  1722. logLink(store, link, "NodeStore_brokenLink() not cannonucal, sending ping");
  1723. addLinkToMill(store, link);
  1724. return;
  1725. } else {
  1726. logLink(store, link, "NodeStore_brokenLink() removing");
  1727. brokenLink(store, link);
  1728. return;
  1729. }
  1730. } else if (firstHopInPath_NO_NEXT_LINK == nextPath && thisPath == 1) {
  1731. Assert_ifParanoid(NodeStore_linkForPath(nodeStore, path) == link);
  1732. if (path >> 56) {
  1733. logLink(store, link, "NodeStore_brokenLink() probably caused by long path");
  1734. } else if (path != cannonicalPath && !NodeStore_getRouteLabel_ERR(cannonicalPath)) {
  1735. logLink(store, link, "NodeStore_brokenLink() not cannonical, sending ping (1link)");
  1736. addLinkToMill(store, link);
  1737. return;
  1738. } else {
  1739. logLink(store, link, "NodeStore_brokenLink() removing (1link)");
  1740. brokenLink(store, link);
  1741. }
  1742. return;
  1743. }
  1744. if (firstHopInPath_NO_NEXT_LINK == nextPath) {
  1745. Log_debug(store->logger, "NodeStore_brokenLink() firstHopInPath_NO_NEXT_LINK");
  1746. // fails if pathAtErrorHop is garbage.
  1747. Assert_ifTesting(!NodeStore_linkForPath(nodeStore, path));
  1748. return;
  1749. }
  1750. if (firstHopInPath_INVALID == nextPath) {
  1751. Log_debug(store->logger, "NodeStore_brokenLink() firstHopInPath_INVALID");
  1752. return;
  1753. }
  1754. Assert_true(link);
  1755. thisPath = nextPath;
  1756. }
  1757. }
  1758. // When a response comes in, we need to pay attention to the path used.
  1759. static void updatePathCost(struct NodeStore_pvt* store, const uint64_t path, uint64_t newCost)
  1760. {
  1761. struct Node_Link* link = store->selfLink;
  1762. uint64_t pathFrag = path;
  1763. uint64_t now = Time_currentTimeMilliseconds(store->eventBase);
  1764. for (;;) {
  1765. struct Node_Link* nextLink = NULL;
  1766. uint64_t nextPath = firstHopInPath(pathFrag, &nextLink, link, store);
  1767. if (firstHopInPath_ERR(nextPath)) {
  1768. break;
  1769. }
  1770. // expecting behavior of nextLinkOnPath()
  1771. Assert_ifParanoid(nextLink->parent == link->child);
  1772. // Update linkCost.
  1773. int64_t newLinkCost = calcNextCost(nextLink->linkCost);
  1774. verify(store);
  1775. handleLinkNews(nextLink, newLinkCost, store);
  1776. verify(store);
  1777. nextLink->timeLastSeen = now;
  1778. pathFrag = nextPath;
  1779. link = nextLink;
  1780. newCost++;
  1781. }
  1782. link->child->timeLastPinged = now;
  1783. }
  1784. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds)
  1785. {
  1786. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1787. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1788. if (!link || link == store->selfLink) { return; }
  1789. struct Node_Two* node = link->child;
  1790. uint64_t newCost;
  1791. if (node->address.path == path) {
  1792. // Use old cost value to calculate new cost.
  1793. newCost = calcNextCost(Node_getCost(node));
  1794. }
  1795. else {
  1796. // Old cost value doesn't relate to this path, so we should do something different
  1797. // FIXME(arceliar): calcNextCost is guessing what the cost would stabilize to
  1798. // I think actually fixing this would require storing cost (or latency?) per link,
  1799. // so we can calculate the expected cost for an arbitrary path
  1800. newCost = calcNextCost(UINT64_MAX);
  1801. }
  1802. store->disarmCheck++;
  1803. updatePathCost(store, path, newCost);
  1804. store->disarmCheck--;
  1805. verify(store);
  1806. }
  1807. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path)
  1808. {
  1809. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1810. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1811. if (!link) { return; }
  1812. struct Node_Two* node = link->child;
  1813. // TODO(cjd): What we really should be doing here is storing this link in a
  1814. // potentially-down-list, after pinging the parent, if the parent does not respond
  1815. // and then we replace the link with the parent's link and walk backwards up
  1816. // the tree. If the parent does respond then we keep pinging the child of the path
  1817. // hoping it will respond or die and as it's link-state is destroyed by subsequent
  1818. // lost packets, children will be re-parented to other paths.
  1819. // We probably did not ping it along the node's best path.
  1820. // Keep checking until we're sure it's either OK or down.
  1821. RumorMill_addNode(store->renumberMill, &node->address);
  1822. if (!link || link->child->address.path != path) { return; }
  1823. if (link->parent != store->pub.selfNode) {
  1824. // Nevermind, we did use the best path.
  1825. // All we know for sure is that link->child didn't respond.
  1826. // That could be because an earlier link is down.
  1827. // Ping it, we should eventually backtrack to the correct link.
  1828. RumorMill_addNode(store->renumberMill, &link->parent->address);
  1829. }
  1830. uint64_t oldCost = Node_getCost(node);
  1831. int64_t newLinkCost = costAfterTimeout(link->linkCost);
  1832. verify(store);
  1833. handleLinkNews(link, newLinkCost, store);
  1834. verify(store);
  1835. if (Defined(Log_DEBUG)) {
  1836. uint8_t addr[60];
  1837. Address_print(addr, &node->address);
  1838. Log_debug(store->logger,
  1839. "Ping timeout for %s. changing cost from %llu to %llu\n",
  1840. addr,
  1841. (unsigned long long)oldCost,
  1842. (unsigned long long)Node_getCost(node));
  1843. }
  1844. }
  1845. struct Address NodeStore_addrForBucket(struct Address* source, uint16_t bucket)
  1846. {
  1847. Assert_compileTime(NodeStore_bucketNumber == 128);
  1848. struct Address addr = *source;
  1849. uint64_t* addrPart = (bucket < 64) ? &addr.ip6.longs.one_be : &addr.ip6.longs.two_be;
  1850. uint64_t bitmask = (uint64_t)1 << (63 - (bucket % 64));
  1851. *addrPart ^= Endian_hostToBigEndian64(bitmask);
  1852. Assert_ifParanoid(bucket == NodeStore_bucketForAddr(source, &addr));
  1853. return addr;
  1854. }
  1855. uint16_t NodeStore_bucketForAddr(struct Address* source, struct Address* dest)
  1856. {
  1857. Assert_compileTime(NodeStore_bucketNumber == 128);
  1858. uint16_t bucket = 0;
  1859. uint64_t addrPart = source->ip6.longs.one_be ^ dest->ip6.longs.one_be;
  1860. if (!addrPart) {
  1861. addrPart = source->ip6.longs.two_be ^ dest->ip6.longs.two_be;
  1862. bucket += 64;
  1863. }
  1864. addrPart = Endian_bigEndianToHost64(addrPart);
  1865. bucket += 63 - Bits_log2x64(addrPart);
  1866. return bucket;
  1867. }
  1868. uint64_t NodeStore_timeSinceLastPing(struct NodeStore* nodeStore, struct Node_Two* node)
  1869. {
  1870. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1871. uint64_t now = Time_currentTimeMilliseconds(store->eventBase);
  1872. uint64_t lastSeen = node->timeLastPinged;
  1873. struct Node_Link* link = Node_getBestParent(node);
  1874. while (link && link != store->selfLink) {
  1875. lastSeen = (link->timeLastSeen < lastSeen) ? link->timeLastSeen : lastSeen;
  1876. link = Node_getBestParent(link->parent);
  1877. }
  1878. return now - lastSeen;
  1879. }
  1880. bool NodeStore_getFullVerify(struct NodeStore* nodeStore)
  1881. {
  1882. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1883. return store->fullVerify != 0;
  1884. }
  1885. void NodeStore_setFullVerify(struct NodeStore* nodeStore, bool fullVerify)
  1886. {
  1887. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1888. store->fullVerify = (fullVerify != 0);
  1889. }