NodeStore.c 91 KB

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