NodeStore.c 89 KB

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