NodeStore.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  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. static void updatePathCost(struct NodeStore_pvt* store, const uint64_t path, uint64_t newCost)
  1765. {
  1766. struct Node_Link* link = store->selfLink;
  1767. uint64_t pathFrag = path;
  1768. uint64_t now = Time_currentTimeMilliseconds();
  1769. for (;;) {
  1770. struct Node_Link* nextLink = NULL;
  1771. uint64_t nextPath = firstHopInPath(pathFrag, &nextLink, link, store);
  1772. if (firstHopInPath_ERR(nextPath)) {
  1773. break;
  1774. }
  1775. // expecting behavior of nextLinkOnPath()
  1776. Assert_ifParanoid(nextLink->parent == link->child);
  1777. // Update linkCost.
  1778. int64_t newLinkCost = calcNextCost(nextLink->linkCost);
  1779. verify(store);
  1780. handleLinkNews(nextLink, newLinkCost, store);
  1781. verify(store);
  1782. nextLink->timeLastSeen = now;
  1783. pathFrag = nextPath;
  1784. link = nextLink;
  1785. newCost++;
  1786. }
  1787. link->child->timeLastPinged = now;
  1788. }
  1789. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds)
  1790. {
  1791. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1792. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1793. if (!link || link == store->selfLink) { return; }
  1794. struct Node_Two* node = link->child;
  1795. uint64_t newCost;
  1796. if (node->address.path == path) {
  1797. // Use old cost value to calculate new cost.
  1798. newCost = calcNextCost(Node_getCost(node));
  1799. }
  1800. else {
  1801. // Old cost value doesn't relate to this path, so we should do something different
  1802. // FIXME(arceliar): calcNextCost is guessing what the cost would stabilize to
  1803. // I think actually fixing this would require storing cost (or latency?) per link,
  1804. // so we can calculate the expected cost for an arbitrary path
  1805. newCost = calcNextCost(UINT64_MAX);
  1806. }
  1807. store->disarmCheck++;
  1808. updatePathCost(store, path, newCost);
  1809. store->disarmCheck--;
  1810. verify(store);
  1811. }
  1812. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path)
  1813. {
  1814. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1815. struct Node_Link* link = NodeStore_linkForPath(nodeStore, path);
  1816. if (!link) { return; }
  1817. struct Node_Two* node = link->child;
  1818. // TODO(cjd): What we really should be doing here is storing this link in a
  1819. // potentially-down-list, after pinging the parent, if the parent does not respond
  1820. // and then we replace the link with the parent's link and walk backwards up
  1821. // the tree. If the parent does respond then we keep pinging the child of the path
  1822. // hoping it will respond or die and as it's link-state is destroyed by subsequent
  1823. // lost packets, children will be re-parented to other paths.
  1824. // We probably did not ping it along the node's best path.
  1825. // Keep checking until we're sure it's either OK or down.
  1826. RumorMill_addNode(store->renumberMill, &node->address);
  1827. if (!link || link->child->address.path != path) { return; }
  1828. if (link->parent != store->pub.selfNode) {
  1829. // Nevermind, we did use the best path.
  1830. // All we know for sure is that link->child didn't respond.
  1831. // That could be because an earlier link is down.
  1832. // Ping it, we should eventually backtrack to the correct link.
  1833. RumorMill_addNode(store->renumberMill, &link->parent->address);
  1834. }
  1835. uint64_t oldCost = Node_getCost(node);
  1836. int64_t newLinkCost = costAfterTimeout(link->linkCost);
  1837. verify(store);
  1838. handleLinkNews(link, newLinkCost, store);
  1839. verify(store);
  1840. if (Defined(Log_DEBUG)) {
  1841. uint8_t addr[60];
  1842. Address_print(addr, &node->address);
  1843. Log_debug(store->logger,
  1844. "Ping timeout for %s. changing cost from %llu to %llu\n",
  1845. addr,
  1846. (unsigned long long)oldCost,
  1847. (unsigned long long)Node_getCost(node));
  1848. }
  1849. }
  1850. struct Address NodeStore_addrForBucket(struct Address* source, uint16_t bucket)
  1851. {
  1852. Assert_compileTime(NodeStore_bucketNumber == 128);
  1853. struct Address addr = *source;
  1854. uint64_t* addrPart = (bucket < 64) ? &addr.ip6.longs.one_be : &addr.ip6.longs.two_be;
  1855. uint64_t bitmask = (uint64_t)1 << (63 - (bucket % 64));
  1856. *addrPart ^= Endian_hostToBigEndian64(bitmask);
  1857. Assert_ifParanoid(bucket == NodeStore_bucketForAddr(source, &addr));
  1858. return addr;
  1859. }
  1860. uint16_t NodeStore_bucketForAddr(struct Address* source, struct Address* dest)
  1861. {
  1862. Assert_compileTime(NodeStore_bucketNumber == 128);
  1863. uint16_t bucket = 0;
  1864. uint64_t addrPart = source->ip6.longs.one_be ^ dest->ip6.longs.one_be;
  1865. if (!addrPart) {
  1866. addrPart = source->ip6.longs.two_be ^ dest->ip6.longs.two_be;
  1867. bucket += 64;
  1868. }
  1869. addrPart = Endian_bigEndianToHost64(addrPart);
  1870. bucket += 63 - Bits_log2x64(addrPart);
  1871. return bucket;
  1872. }
  1873. uint64_t NodeStore_timeSinceLastPing(struct NodeStore* nodeStore, struct Node_Two* node)
  1874. {
  1875. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1876. uint64_t now = Time_currentTimeMilliseconds();
  1877. uint64_t lastSeen = node->timeLastPinged;
  1878. struct Node_Link* link = Node_getBestParent(node);
  1879. while (link && link != store->selfLink) {
  1880. lastSeen = (link->timeLastSeen < lastSeen) ? link->timeLastSeen : lastSeen;
  1881. link = Node_getBestParent(link->parent);
  1882. }
  1883. return now - lastSeen;
  1884. }
  1885. bool NodeStore_getFullVerify(struct NodeStore* nodeStore)
  1886. {
  1887. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1888. return store->fullVerify != 0;
  1889. }
  1890. void NodeStore_setFullVerify(struct NodeStore* nodeStore, bool fullVerify)
  1891. {
  1892. struct NodeStore_pvt* store = Identity_check((struct NodeStore_pvt*)nodeStore);
  1893. store->fullVerify = (fullVerify != 0);
  1894. }