SubnodePathfinder.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "subnode/SubnodePathfinder.h"
  16. #include "subnode/AddrSet.h"
  17. #include "subnode/MsgCore.h"
  18. #include "subnode/SupernodeHunter.h"
  19. #include "subnode/GetPeersResponder.h"
  20. #include "subnode/PingResponder.h"
  21. #include "subnode/BoilerplateResponder.h"
  22. #include "subnode/ReachabilityCollector.h"
  23. #include "crypto/AddressCalc.h"
  24. #include "dht/Address.h"
  25. #include "wire/DataHeader.h"
  26. #include "wire/RouteHeader.h"
  27. #include "dht/dhtcore/ReplySerializer.h"
  28. #include "util/AddrTools.h"
  29. #include "util/events/Timeout.h"
  30. #include "net/SwitchPinger.h"
  31. #include "switch/LabelSplicer.h"
  32. #include "wire/Error.h"
  33. #include "wire/PFChan.h"
  34. #include "wire/DataHeader.h"
  35. #include "util/CString.h"
  36. #include "subnode/ReachabilityAnnouncer.h"
  37. struct Query {
  38. struct Address target;
  39. uint8_t routeFrom[16];
  40. uint8_t routeTo[16];
  41. };
  42. #define Map_NAME OfPromiseByQuery
  43. #define Map_KEY_TYPE struct Query
  44. #define Map_VALUE_TYPE struct MsgCore_Promise*
  45. #define Map_ENABLE_HANDLES
  46. #include "util/Map.h"
  47. struct SubnodePathfinder_pvt
  48. {
  49. struct SubnodePathfinder pub;
  50. struct Iface msgCoreIf;
  51. struct Allocator* alloc;
  52. struct Log* log;
  53. struct EventBase* base;
  54. struct Random* rand;
  55. #define SubnodePathfinder_pvt_state_INITIALIZING 0
  56. #define SubnodePathfinder_pvt_state_RUNNING 1
  57. int state;
  58. struct Address* myAddress;
  59. struct AddrSet* myPeers;
  60. struct MsgCore* msgCore;
  61. struct Admin* admin;
  62. struct BoilerplateResponder* br;
  63. struct ReachabilityAnnouncer* ra;
  64. struct Map_OfPromiseByQuery queryMap;
  65. struct SwitchPinger* sp;
  66. struct Iface switchPingerIf;
  67. struct EncodingScheme* myScheme;
  68. uint8_t* privateKey;
  69. String* encodingSchemeStr;
  70. Identity
  71. };
  72. static void nodeForAddress(struct PFChan_Node* nodeOut, struct Address* addr, uint32_t metric)
  73. {
  74. Bits_memset(nodeOut, 0, PFChan_Node_SIZE);
  75. nodeOut->version_be = Endian_hostToBigEndian32(addr->protocolVersion);
  76. nodeOut->metric_be = Endian_hostToBigEndian32(metric);
  77. nodeOut->path_be = Endian_hostToBigEndian64(addr->path);
  78. Bits_memcpy(nodeOut->publicKey, addr->key, 32);
  79. Bits_memcpy(nodeOut->ip6, addr->ip6.bytes, 16);
  80. }
  81. static Iface_DEFUN sendNode(struct Message* msg,
  82. struct Address* addr,
  83. uint32_t metric,
  84. enum PFChan_Pathfinder msgType,
  85. struct SubnodePathfinder_pvt* pf)
  86. {
  87. Message_reset(msg);
  88. Message_shift(msg, PFChan_Node_SIZE, NULL);
  89. nodeForAddress((struct PFChan_Node*) msg->bytes, addr, metric);
  90. if (addr->path == UINT64_MAX) {
  91. ((struct PFChan_Node*) msg->bytes)->path_be = 0;
  92. }
  93. Message_push32(msg, msgType, NULL);
  94. return Iface_next(&pf->pub.eventIf, msg);
  95. }
  96. static Iface_DEFUN connected(struct SubnodePathfinder_pvt* pf, struct Message* msg)
  97. {
  98. Log_debug(pf->log, "INIT");
  99. pf->state = SubnodePathfinder_pvt_state_RUNNING;
  100. return NULL;
  101. }
  102. static void addressForNode(struct Address* addrOut, struct Message* msg)
  103. {
  104. struct PFChan_Node node;
  105. Message_pop(msg, &node, PFChan_Node_SIZE, NULL);
  106. Assert_true(!msg->length);
  107. addrOut->protocolVersion = Endian_bigEndianToHost32(node.version_be);
  108. addrOut->path = Endian_bigEndianToHost64(node.path_be);
  109. Bits_memcpy(addrOut->key, node.publicKey, 32);
  110. Bits_memcpy(addrOut->ip6.bytes, node.ip6, 16);
  111. }
  112. static Iface_DEFUN switchErr(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  113. {
  114. struct PFChan_Core_SwitchErr switchErr;
  115. Message_pop(msg, &switchErr, PFChan_Core_SwitchErr_MIN_SIZE, NULL);
  116. uint64_t path = Endian_bigEndianToHost64(switchErr.sh.label_be);
  117. if (pf->pub.snh->snodeAddr.path &&
  118. pf->pub.snh->snodeAddr.path != path &&
  119. LabelSplicer_routesThrough(pf->pub.snh->snodeAddr.path, path)) {
  120. uint8_t pathStr[20];
  121. AddrTools_printPath(pathStr, path);
  122. int err = Endian_bigEndianToHost32(switchErr.ctrlErr.errorType_be);
  123. Log_debug(pf->log, "switch err from active snode [%s] type [%s][%d]",
  124. pathStr, Error_strerror(err), err);
  125. pf->pub.snh->snodeIsReachable = false;
  126. if (pf->pub.snh->onSnodeUnreachable) {
  127. pf->pub.snh->onSnodeUnreachable(pf->pub.snh, 0, 0);
  128. }
  129. }
  130. return NULL;
  131. }
  132. struct SnodeQuery {
  133. struct SubnodePathfinder_pvt* pf;
  134. uint32_t mapHandle;
  135. Identity
  136. };
  137. static void getRouteReply(Dict* msg, struct Address* src, struct MsgCore_Promise* prom)
  138. {
  139. struct SnodeQuery* snq = Identity_check((struct SnodeQuery*) prom->userData);
  140. struct SubnodePathfinder_pvt* pf = Identity_check(snq->pf);
  141. int index = Map_OfPromiseByQuery_indexForHandle(snq->mapHandle, &pf->queryMap);
  142. Assert_true(index > -1);
  143. Map_OfPromiseByQuery_remove(index, &pf->queryMap);
  144. if (!src) {
  145. Log_debug(pf->log, "GetRoute timeout");
  146. return;
  147. }
  148. Log_debug(pf->log, "Search reply!");
  149. struct Address_List* al = ReplySerializer_parse(src, msg, pf->log, false, prom->alloc);
  150. if (!al || al->length == 0) { return; }
  151. Log_debug(pf->log, "reply with[%s]", Address_toString(&al->elems[0], prom->alloc)->bytes);
  152. if (al->elems[0].protocolVersion < 20) {
  153. Log_debug(pf->log, "not sending [%s] because version is old",
  154. Address_toString(&al->elems[0], prom->alloc)->bytes);
  155. return;
  156. }
  157. //NodeCache_discoverNode(pf->nc, &al->elems[0]);
  158. struct Message* msgToCore = Message_new(0, 512, prom->alloc);
  159. Iface_CALL(sendNode, msgToCore, &al->elems[0], 0xfff00033, PFChan_Pathfinder_NODE, pf);
  160. }
  161. static Iface_DEFUN searchReq(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  162. {
  163. uint8_t addr[16];
  164. Message_pop(msg, addr, 16, NULL);
  165. Message_pop32(msg, NULL);
  166. uint32_t version = Message_pop32(msg, NULL);
  167. if (version && version < 20) { return NULL; }
  168. Assert_true(!msg->length);
  169. uint8_t printedAddr[40];
  170. AddrTools_printIp(printedAddr, addr);
  171. Log_debug(pf->log, "Search req [%s]", printedAddr);
  172. for (int i = 0; i < pf->myPeers->length; ++i) {
  173. struct Address* myPeer = AddrSet_get(pf->myPeers, i);
  174. if (!Bits_memcmp(myPeer->ip6.bytes, addr, 16)) {
  175. return sendNode(msg, myPeer, 0xfff00000, PFChan_Pathfinder_NODE, pf);
  176. }
  177. }
  178. if (!pf->pub.snh || !pf->pub.snh->snodeAddr.path) { return NULL; }
  179. if (!Bits_memcmp(pf->pub.snh->snodeAddr.ip6.bytes, addr, 16)) {
  180. return sendNode(msg, &pf->pub.snh->snodeAddr, 0xfff00000, PFChan_Pathfinder_NODE, pf);
  181. }
  182. struct Query q = { .routeFrom = { 0 } };
  183. Bits_memcpy(&q.target, &pf->pub.snh->snodeAddr, sizeof(struct Address));
  184. Bits_memcpy(q.routeFrom, pf->myAddress->ip6.bytes, 16);
  185. Bits_memcpy(q.routeFrom, addr, 16);
  186. if (Map_OfPromiseByQuery_indexForKey(&q, &pf->queryMap) > -1) {
  187. Log_debug(pf->log, "Skipping snode query because one is outstanding");
  188. return NULL;
  189. }
  190. struct MsgCore_Promise* qp = MsgCore_createQuery(pf->msgCore, 0, pf->alloc);
  191. struct SnodeQuery* snq = Allocator_calloc(qp->alloc, sizeof(struct SnodeQuery), 1);
  192. Identity_set(snq);
  193. snq->pf = pf;
  194. Dict* dict = qp->msg = Dict_new(qp->alloc);
  195. qp->cb = getRouteReply;
  196. qp->userData = snq;
  197. Assert_true(AddressCalc_validAddress(pf->pub.snh->snodeAddr.ip6.bytes));
  198. qp->target = &pf->pub.snh->snodeAddr;
  199. Log_debug(pf->log, "Sending getRoute to snode %s",
  200. Address_toString(qp->target, qp->alloc)->bytes);
  201. Dict_putStringCC(dict, "sq", "gr", qp->alloc);
  202. String* src = String_newBinary(pf->myAddress->ip6.bytes, 16, qp->alloc);
  203. Dict_putStringC(dict, "src", src, qp->alloc);
  204. String* target = String_newBinary(addr, 16, qp->alloc);
  205. Dict_putStringC(dict, "tar", target, qp->alloc);
  206. int index = Map_OfPromiseByQuery_put(&q, &qp, &pf->queryMap);
  207. snq->mapHandle = pf->queryMap.handles[index];
  208. return NULL;
  209. }
  210. static void rcChange(struct ReachabilityCollector* rc,
  211. uint8_t nodeIpv6[16],
  212. uint64_t pathThemToUs,
  213. uint64_t pathUsToThem,
  214. uint32_t mtu,
  215. uint16_t drops,
  216. uint16_t latency,
  217. uint16_t penalty)
  218. {
  219. struct SubnodePathfinder_pvt* pf = Identity_check((struct SubnodePathfinder_pvt*) rc->userData);
  220. ReachabilityAnnouncer_updatePeer(
  221. pf->ra, nodeIpv6, pathThemToUs, pathUsToThem, mtu, drops, latency, penalty);
  222. }
  223. static Iface_DEFUN peer(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  224. {
  225. struct Address addr;
  226. addressForNode(&addr, msg);
  227. String* str = Address_toString(&addr, msg->alloc);
  228. Log_debug(pf->log, "Peer [%s]", str->bytes);
  229. int index = AddrSet_indexOf(pf->myPeers, &addr);
  230. if (index > -1) {
  231. struct Address* myPeer = AddrSet_get(pf->myPeers, index);
  232. if (myPeer->path == addr.path && myPeer->protocolVersion == addr.protocolVersion) {
  233. return NULL;
  234. }
  235. AddrSet_remove(pf->myPeers, myPeer);
  236. }
  237. AddrSet_add(pf->myPeers, &addr);
  238. //NodeCache_discoverNode(pf->nc, &addr);
  239. ReachabilityCollector_change(pf->pub.rc, &addr);
  240. return sendNode(msg, &addr, 0xfff00000, PFChan_Pathfinder_NODE, pf);
  241. }
  242. static Iface_DEFUN peerGone(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  243. {
  244. struct Address addr;
  245. addressForNode(&addr, msg);
  246. for (int i = pf->myPeers->length - 1; i >= 0; i--) {
  247. struct Address* myPeer = AddrSet_get(pf->myPeers, i);
  248. if (myPeer->path == addr.path) {
  249. String* str = Address_toString(myPeer, msg->alloc);
  250. AddrSet_remove(pf->myPeers, myPeer);
  251. Log_debug(pf->log, "Peer gone [%s]", str->bytes);
  252. }
  253. }
  254. //NodeCache_forgetNode(pf->nc, &addr);
  255. struct Address zaddr;
  256. Bits_memcpy(&zaddr, &addr, Address_SIZE);
  257. zaddr.path = 0;
  258. ReachabilityCollector_change(pf->pub.rc, &zaddr);
  259. // We notify about the node but with max metric so it will be removed soon.
  260. return sendNode(msg, &addr, 0xffffffff, PFChan_Pathfinder_NODE, pf);
  261. }
  262. static Iface_DEFUN session(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  263. {
  264. struct Address addr;
  265. addressForNode(&addr, msg);
  266. String* str = Address_toString(&addr, msg->alloc);
  267. Log_debug(pf->log, "Session [%s]", str->bytes);
  268. //if (addr.protocolVersion) { NodeCache_discoverNode(pf->nc, &addr); }
  269. return NULL;
  270. }
  271. static Iface_DEFUN sessionEnded(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  272. {
  273. struct Address addr;
  274. addressForNode(&addr, msg);
  275. String* str = Address_toString(&addr, msg->alloc);
  276. Log_debug(pf->log, "Session ended [%s]", str->bytes);
  277. //NodeCache_forgetNode(pf->nc, &addr);
  278. return NULL;
  279. }
  280. static Iface_DEFUN discoveredPath(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  281. {
  282. //struct Address addr;
  283. //addressForNode(&addr, msg);
  284. //Log_debug(pf->log, "discoveredPath(%s)", Address_toString(&addr, msg->alloc)->bytes);
  285. //if (addr.protocolVersion) { NodeCache_discoverNode(pf->nc, &addr); }
  286. return NULL;
  287. }
  288. static Iface_DEFUN handlePing(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  289. {
  290. //Log_debug(pf->log, "Received ping");
  291. Message_push32(msg, PFChan_Pathfinder_PONG, NULL);
  292. return Iface_next(&pf->pub.eventIf, msg);
  293. }
  294. static Iface_DEFUN handlePong(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  295. {
  296. //Log_debug(pf->log, "Received pong");
  297. return NULL;
  298. }
  299. static Iface_DEFUN ctrlMsgFromSwitchPinger(struct Message* msg, struct Iface* iface)
  300. {
  301. struct SubnodePathfinder_pvt* pf =
  302. Identity_containerOf(iface, struct SubnodePathfinder_pvt, switchPingerIf);
  303. Message_push32(msg, PFChan_Pathfinder_CTRL_SENDMSG, NULL);
  304. return Iface_next(&pf->pub.eventIf, msg);
  305. }
  306. static Iface_DEFUN ctrlMsg(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  307. {
  308. return Iface_next(&pf->switchPingerIf, msg);
  309. }
  310. struct UnsetupSessionPing {
  311. struct SubnodePathfinder_pvt* pf;
  312. uint32_t mapHandle;
  313. Identity
  314. };
  315. static void unsetupSessionPingReply(Dict* msg, struct Address* src, struct MsgCore_Promise* prom)
  316. {
  317. struct UnsetupSessionPing* usp =
  318. Identity_check((struct UnsetupSessionPing*) prom->userData);
  319. struct SubnodePathfinder_pvt* pf = Identity_check(usp->pf);
  320. int index = Map_OfPromiseByQuery_indexForHandle(usp->mapHandle, &pf->queryMap);
  321. Assert_true(index > -1);
  322. Map_OfPromiseByQuery_remove(index, &pf->queryMap);
  323. if (!src) {
  324. //Log_debug(pf->log, "Ping timeout");
  325. return;
  326. }
  327. //Log_debug(pf->log, "\n\n\n\nPING reply from [%s]!\n\n\n\n",
  328. // Address_toString(src, prom->alloc)->bytes);
  329. struct Message* msgToCore = Message_new(0, 512, prom->alloc);
  330. Iface_CALL(sendNode, msgToCore, src, 0xfffffff0, PFChan_Pathfinder_NODE, pf);
  331. }
  332. static Iface_DEFUN unsetupSession(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  333. {
  334. struct PFChan_Node node;
  335. Message_pop(msg, &node, PFChan_Node_SIZE, NULL);
  336. Assert_true(!msg->length);
  337. struct Query q = { .routeFrom = { 0 } };
  338. struct Address* addr = &q.target;
  339. Bits_memcpy(addr->ip6.bytes, node.ip6, 16);
  340. Bits_memcpy(addr->key, node.publicKey, 32);
  341. addr->protocolVersion = Endian_bigEndianToHost32(node.version_be);
  342. addr->path = Endian_bigEndianToHost64(node.path_be);
  343. if (Map_OfPromiseByQuery_indexForKey(&q, &pf->queryMap) > -1) {
  344. Log_debug(pf->log, "Skipping ping because one is already outstanding");
  345. return NULL;
  346. }
  347. // We have a path to the node but the session is not setup, lets ping them...
  348. struct MsgCore_Promise* qp = MsgCore_createQuery(pf->msgCore, 0, pf->alloc);
  349. struct UnsetupSessionPing* usp =
  350. Allocator_calloc(qp->alloc, sizeof(struct UnsetupSessionPing), 1);
  351. Identity_set(usp);
  352. usp->pf = pf;
  353. Dict* dict = qp->msg = Dict_new(qp->alloc);
  354. qp->cb = unsetupSessionPingReply;
  355. qp->userData = usp;
  356. Assert_true(AddressCalc_validAddress(addr->ip6.bytes));
  357. Assert_true(addr->path);
  358. qp->target = Address_clone(addr, qp->alloc);
  359. //Log_debug(pf->log, "unsetupSession sending ping to [%s]",
  360. // Address_toString(qp->target, qp->alloc)->bytes);
  361. Dict_putStringCC(dict, "q", "pn", qp->alloc);
  362. BoilerplateResponder_addBoilerplate(pf->br, dict, addr, qp->alloc);
  363. int index = Map_OfPromiseByQuery_put(&q, &qp, &pf->queryMap);
  364. usp->mapHandle = pf->queryMap.handles[index];
  365. return NULL;
  366. }
  367. static Iface_DEFUN incomingMsg(struct Message* msg, struct SubnodePathfinder_pvt* pf)
  368. {
  369. return Iface_next(&pf->msgCoreIf, msg);
  370. }
  371. static Iface_DEFUN incomingFromMsgCore(struct Message* msg, struct Iface* iface)
  372. {
  373. struct SubnodePathfinder_pvt* pf =
  374. Identity_containerOf(iface, struct SubnodePathfinder_pvt, msgCoreIf);
  375. Assert_true(msg->length >= (RouteHeader_SIZE + DataHeader_SIZE));
  376. struct RouteHeader* rh = (struct RouteHeader*) msg->bytes;
  377. struct DataHeader* dh = (struct DataHeader*) &rh[1];
  378. Assert_true(DataHeader_getContentType(dh) == ContentType_CJDHT);
  379. Assert_true(!Bits_isZero(rh->publicKey, 32));
  380. Assert_true(rh->version_be);
  381. Assert_true(rh->sh.label_be);
  382. Message_push32(msg, PFChan_Pathfinder_SENDMSG, NULL);
  383. return Iface_next(&pf->pub.eventIf, msg);
  384. }
  385. static Iface_DEFUN incomingFromEventIf(struct Message* msg, struct Iface* eventIf)
  386. {
  387. struct SubnodePathfinder_pvt* pf =
  388. Identity_containerOf(eventIf, struct SubnodePathfinder_pvt, pub.eventIf);
  389. enum PFChan_Core ev = Message_pop32(msg, NULL);
  390. if (SubnodePathfinder_pvt_state_INITIALIZING == pf->state) {
  391. Assert_true(ev == PFChan_Core_CONNECT);
  392. return connected(pf, msg);
  393. }
  394. switch (ev) {
  395. case PFChan_Core_SWITCH_ERR: return switchErr(msg, pf);
  396. case PFChan_Core_SEARCH_REQ: return searchReq(msg, pf);
  397. case PFChan_Core_PEER: return peer(msg, pf);
  398. case PFChan_Core_PEER_GONE: return peerGone(msg, pf);
  399. case PFChan_Core_SESSION: return session(msg, pf);
  400. case PFChan_Core_SESSION_ENDED: return sessionEnded(msg, pf);
  401. case PFChan_Core_DISCOVERED_PATH: return discoveredPath(msg, pf);
  402. case PFChan_Core_MSG: return incomingMsg(msg, pf);
  403. case PFChan_Core_PING: return handlePing(msg, pf);
  404. case PFChan_Core_PONG: return handlePong(msg, pf);
  405. case PFChan_Core_CTRL_MSG: return ctrlMsg(msg, pf);
  406. case PFChan_Core_UNSETUP_SESSION: return unsetupSession(msg, pf);
  407. default:;
  408. }
  409. Assert_failure("unexpected event [%d]", ev);
  410. }
  411. static void sendEvent(struct SubnodePathfinder_pvt* pf,
  412. enum PFChan_Pathfinder ev,
  413. void* data,
  414. int size)
  415. {
  416. struct Allocator* alloc = Allocator_child(pf->alloc);
  417. struct Message* msg = Message_new(0, 512+size, alloc);
  418. Message_push(msg, data, size, NULL);
  419. Message_push32(msg, ev, NULL);
  420. Iface_send(&pf->pub.eventIf, msg);
  421. Allocator_free(alloc);
  422. }
  423. void SubnodePathfinder_start(struct SubnodePathfinder* sp)
  424. {
  425. struct SubnodePathfinder_pvt* pf = Identity_check((struct SubnodePathfinder_pvt*) sp);
  426. pf->msgCore = MsgCore_new(pf->base, pf->rand, pf->alloc, pf->log, pf->myScheme);
  427. Iface_plumb(&pf->msgCoreIf, &pf->msgCore->interRouterIf);
  428. PingResponder_new(pf->alloc, pf->log, pf->msgCore, pf->br);
  429. GetPeersResponder_new(
  430. pf->alloc, pf->log, pf->myPeers, pf->myAddress, pf->msgCore, pf->br, pf->myScheme);
  431. pf->pub.snh = SupernodeHunter_new(
  432. pf->alloc, pf->log, pf->base, pf->sp, pf->myPeers, pf->msgCore, pf->myAddress);
  433. pf->ra = ReachabilityAnnouncer_new(
  434. pf->alloc, pf->log, pf->base, pf->rand, pf->msgCore, pf->pub.snh, pf->privateKey,
  435. pf->myScheme);
  436. pf->pub.rc = ReachabilityCollector_new(
  437. pf->alloc, pf->msgCore, pf->log, pf->base, pf->br, pf->myAddress);
  438. pf->pub.rc->userData = pf;
  439. pf->pub.rc->onChange = rcChange;
  440. struct PFChan_Pathfinder_Connect conn = {
  441. .superiority_be = Endian_hostToBigEndian32(1),
  442. .version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL)
  443. };
  444. CString_strncpy(conn.userAgent, "Cjdns subnode pathfinder", 64);
  445. sendEvent(pf, PFChan_Pathfinder_CONNECT, &conn, PFChan_Pathfinder_Connect_SIZE);
  446. }
  447. static void sendCurrentSupernode(void* vsp)
  448. {
  449. struct SubnodePathfinder_pvt* pf = Identity_check((struct SubnodePathfinder_pvt*) vsp);
  450. struct Allocator* alloc = Allocator_child(pf->alloc);
  451. struct Message* msgToCore = Message_new(0, 512, alloc);
  452. Iface_CALL(sendNode, msgToCore, &pf->pub.snh->snodeAddr, 0, PFChan_Pathfinder_SNODE, pf);
  453. Allocator_free(alloc);
  454. }
  455. struct SubnodePathfinder* SubnodePathfinder_new(struct Allocator* allocator,
  456. struct Log* log,
  457. struct EventBase* base,
  458. struct Random* rand,
  459. struct Address* myAddress,
  460. uint8_t* privateKey,
  461. struct EncodingScheme* myScheme)
  462. {
  463. struct Allocator* alloc = Allocator_child(allocator);
  464. struct SubnodePathfinder_pvt* pf =
  465. Allocator_calloc(alloc, sizeof(struct SubnodePathfinder_pvt), 1);
  466. Identity_set(pf);
  467. pf->alloc = alloc;
  468. pf->log = log;
  469. pf->base = base;
  470. pf->rand = rand;
  471. pf->myAddress = myAddress;
  472. pf->myPeers = AddrSet_new(alloc);
  473. pf->pub.eventIf.send = incomingFromEventIf;
  474. pf->msgCoreIf.send = incomingFromMsgCore;
  475. pf->privateKey = privateKey;
  476. pf->queryMap.allocator = Allocator_child(alloc);
  477. pf->myScheme = myScheme;
  478. pf->br = BoilerplateResponder_new(myScheme, alloc);
  479. pf->sp = SwitchPinger_new(base, rand, log, myAddress, alloc);
  480. pf->switchPingerIf.send = ctrlMsgFromSwitchPinger;
  481. Iface_plumb(&pf->switchPingerIf, &pf->sp->controlHandlerIf);
  482. Timeout_setInterval(sendCurrentSupernode, pf, 3000, base, alloc);
  483. return &pf->pub;
  484. }