1
0

ControlHandler.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "dht/Address.h"
  16. #include "net/ControlHandler.h"
  17. #include "exception/Err.h"
  18. #include "memory/Allocator.h"
  19. #include "net/InterfaceController.h"
  20. #include "switch/EncodingScheme.h"
  21. #include "util/Bits.h"
  22. #include "util/Endian.h"
  23. #include "util/Identity.h"
  24. #include "util/AddrTools.h"
  25. #include "util/Checksum.h"
  26. #include "util/platform/Sockaddr.h"
  27. #include "util/version/Version.h"
  28. #include "wire/Control.h"
  29. #include "wire/Error.h"
  30. #include "wire/Message.h"
  31. #define NumberCompress_OLD_CODE
  32. #include "switch/NumberCompress.h"
  33. #include <inttypes.h>
  34. struct ControlHandler_pvt
  35. {
  36. struct ControlHandler pub;
  37. struct EncodingScheme* ourEncodingScheme;
  38. struct Log* log;
  39. struct Allocator* alloc;
  40. struct InterfaceController* ifc;
  41. uint8_t myPublicKey[32];
  42. struct Iface eventIf;
  43. struct Address activeSnode;
  44. Identity
  45. };
  46. /**
  47. * Expects [ Ctrl ][ Error ][ cause SwitchHeader ][ cause handle ][ cause etc.... ]
  48. */
  49. #define handleError_MIN_SIZE (Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE + 4)
  50. static Iface_DEFUN handleError(Message_t* msg,
  51. struct ControlHandler_pvt* ch,
  52. uint64_t label,
  53. uint8_t* labelStr,
  54. struct RouteHeader* rh)
  55. {
  56. if (Message_getLength(msg) < handleError_MIN_SIZE) {
  57. Log_info(ch->log, "DROP runt error packet from [%s]", labelStr);
  58. return Error(msg, "RUNT");
  59. }
  60. Err(Message_truncate(msg, handleError_MIN_SIZE));
  61. Err(Message_epush(msg, &rh->sh, SwitchHeader_SIZE));
  62. Err(Message_epush32be(msg, 0xffffffff));
  63. Err(Message_epush32be(msg, PFChan_Core_SWITCH_ERR));
  64. return Iface_next(&ch->eventIf, msg);
  65. }
  66. /**
  67. * Expects [ SwitchHeader ][ Ctrl ][ (key)Ping ][ data etc.... ]
  68. */
  69. #define handlePing_MIN_SIZE (Control_Header_SIZE + Control_Ping_MIN_SIZE)
  70. static Iface_DEFUN handlePing(Message_t* msg,
  71. struct ControlHandler_pvt* ch,
  72. uint64_t label,
  73. uint8_t* labelStr,
  74. uint16_t messageType_be)
  75. {
  76. if (Message_getLength(msg) < handlePing_MIN_SIZE) {
  77. Log_info(ch->log, "DROP runt ping");
  78. return Error(msg, "RUNT");
  79. }
  80. struct Control* ctrl = (struct Control*) Message_bytes(msg);
  81. Err(Message_eshift(msg, -Control_Header_SIZE));
  82. // Ping and keyPing share version location
  83. struct Control_Ping* ping = (struct Control_Ping*) Message_bytes(msg);
  84. uint32_t herVersion = Endian_bigEndianToHost32(ping->version_be);
  85. if (!Version_compatibleWithCurrent(herVersion)) {
  86. Log_debug(ch->log, "DROP ping from incompatible version [%d]", herVersion);
  87. return Error(msg, "INVALID");
  88. }
  89. if (messageType_be == Control_KEYPING_be) {
  90. Log_debug(ch->log, "got switch keyPing from [%s]", labelStr);
  91. if (Message_getLength(msg) < Control_KeyPing_HEADER_SIZE) {
  92. // min keyPing size is longer
  93. Log_debug(ch->log, "DROP runt keyPing");
  94. return Error(msg, "RUNT");
  95. }
  96. if (Message_getLength(msg) > Control_KeyPing_MAX_SIZE) {
  97. Log_debug(ch->log, "DROP long keyPing");
  98. return Error(msg, "INVALID");
  99. }
  100. if (ping->magic != Control_KeyPing_MAGIC) {
  101. Log_debug(ch->log, "DROP keyPing (bad magic)");
  102. return Error(msg, "INVALID");
  103. }
  104. struct Control_KeyPing* keyPing = (struct Control_KeyPing*) Message_bytes(msg);
  105. keyPing->magic = Control_KeyPong_MAGIC;
  106. ctrl->header.type_be = Control_KEYPONG_be;
  107. Bits_memcpy(keyPing->key, ch->myPublicKey, 32);
  108. } else if (messageType_be == Control_PING_be) {
  109. // Happens in benchmark.
  110. //Log_debug(ch->log, "got switch ping from [%s]", labelStr);
  111. if (ping->magic != Control_Ping_MAGIC) {
  112. Log_debug(ch->log, "DROP ping (bad magic)");
  113. return Error(msg, "INVALID");
  114. }
  115. ping->magic = Control_Pong_MAGIC;
  116. ctrl->header.type_be = Control_PONG_be;
  117. } else {
  118. Assert_failure("2+2=5");
  119. }
  120. ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  121. Err(Message_eshift(msg, Control_Header_SIZE));
  122. ctrl->header.checksum_be = 0;
  123. ctrl->header.checksum_be = Checksum_engine_be(Message_bytes(msg), Message_getLength(msg));
  124. Err(Message_eshift(msg, RouteHeader_SIZE));
  125. struct RouteHeader* routeHeader = (struct RouteHeader*) Message_bytes(msg);
  126. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  127. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  128. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  129. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  130. return Iface_next(&ch->pub.coreIf, msg);
  131. }
  132. /**
  133. * Expects [ SwitchHeader ][ Ctrl ][ RPath ][ data etc.... ]
  134. */
  135. #define handleRPathQuery_MIN_SIZE (Control_Header_SIZE + Control_RPath_HEADER_SIZE)
  136. static Iface_DEFUN handleRPathQuery(Message_t* msg,
  137. struct ControlHandler_pvt* ch,
  138. uint64_t label,
  139. uint8_t* labelStr)
  140. {
  141. Log_debug(ch->log, "Incoming RPATH query");
  142. if (Message_getLength(msg) < handleRPathQuery_MIN_SIZE) {
  143. Log_info(ch->log, "DROP runt RPATH query");
  144. return Error(msg, "RUNT");
  145. }
  146. struct Control* ctrl = (struct Control*) Message_bytes(msg);
  147. struct Control_RPath* rpa = &ctrl->content.rpath;
  148. if (rpa->magic != Control_RPATH_QUERY_MAGIC) {
  149. Log_debug(ch->log, "DROP RPATH query (bad magic)");
  150. return Error(msg, "INVALID");
  151. }
  152. ctrl->header.type_be = Control_RPATH_REPLY_be;
  153. rpa->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  154. rpa->magic = Control_RPATH_REPLY_MAGIC;
  155. uint64_t label_be = Endian_hostToBigEndian64(label);
  156. Bits_memcpy(rpa->rpath_be, &label_be, 8);
  157. ctrl->header.checksum_be = 0;
  158. ctrl->header.checksum_be = Checksum_engine_be(Message_bytes(msg), Message_getLength(msg));
  159. Err(Message_eshift(msg, RouteHeader_SIZE));
  160. struct RouteHeader* routeHeader = (struct RouteHeader*) Message_bytes(msg);
  161. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  162. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  163. routeHeader->sh.label_be = label_be;
  164. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  165. return Iface_next(&ch->pub.coreIf, msg);
  166. }
  167. static Err_DEFUN writeLlAddr(Message_t* msg, Sockaddr_t* sa) {
  168. struct Control_LlAddr out = {
  169. .magic = Control_LlAddr_REPLY_MAGIC,
  170. .version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL),
  171. };
  172. if (sa->type == Sockaddr_PLATFORM) {
  173. int fam = Sockaddr_getFamily(sa);
  174. int port = Sockaddr_getPort(sa);
  175. if (fam == Sockaddr_AF_INET && port > -1) {
  176. out.addr.udp4.type = Control_LlAddr_Udp4_TYPE;
  177. out.addr.udp4.len = sizeof(Control_LlAddr_Udp4_t);
  178. out.addr.udp4.port_be = Endian_hostToBigEndian16(port);
  179. uint8_t* p = NULL;
  180. int len = Sockaddr_getAddress(sa, &p);
  181. Assert_true(len == sizeof out.addr.udp4.addr && p != NULL);
  182. Bits_memcpy(&out.addr.udp4.addr, p, sizeof out.addr.udp4.addr);
  183. return Message_epush(msg, &out, sizeof out);
  184. } else if (fam == Sockaddr_AF_INET6 && port > -1) {
  185. out.addr.udp6.type = Control_LlAddr_Udp6_TYPE;
  186. out.addr.udp6.len = sizeof(Control_LlAddr_Udp6_t);
  187. out.addr.udp6.port_be = Endian_hostToBigEndian16(port);
  188. uint8_t* p = NULL;
  189. int len = Sockaddr_getAddress(sa, &p);
  190. Assert_true(len == sizeof out.addr.udp6.addr && p != NULL);
  191. Bits_memcpy(&out.addr.udp6.addr, p, sizeof out.addr.udp6.addr);
  192. return Message_epush(msg, &out, sizeof out);
  193. }
  194. }
  195. out.addr.other.type = Control_LlAddr_Other_TYPE;
  196. out.addr.other.len = sizeof(Control_LlAddr_Other_t);
  197. unsigned int len = sa->addrLen;
  198. if (len > sizeof out.addr.other.sockaddrHeader) {
  199. len = sizeof out.addr.other.sockaddrHeader;
  200. }
  201. Bits_memcpy(&out.addr.other.sockaddrHeader, &sa, len);
  202. return Message_epush(msg, &out, sizeof out);
  203. }
  204. /**
  205. * Expects [ SwitchHeader ][ Ctrl ][ LlAddr ]
  206. */
  207. #define handleLlAddrQuery_MIN_SIZE (Control_Header_SIZE + Control_LlAddr_HEADER_SIZE)
  208. static Iface_DEFUN handleLlAddrQuery(Message_t* msg,
  209. struct ControlHandler_pvt* ch,
  210. uint64_t label,
  211. uint8_t* labelStr)
  212. {
  213. Log_debug(ch->log, "Incoming LLADDR query");
  214. if (Message_getLength(msg) < handleLlAddrQuery_MIN_SIZE) {
  215. Log_info(ch->log, "DROP runt LLADDR query");
  216. return Error(msg, "RUNT");
  217. }
  218. struct Control_Header hdr;
  219. Err(Message_epop(msg, &hdr, sizeof hdr));
  220. struct Control_LlAddr lla;
  221. Err(Message_epop(msg, &lla, sizeof lla));
  222. if (lla.magic != Control_LlAddr_QUERY_MAGIC) {
  223. Log_debug(ch->log, "DROP LLADDR query (bad magic)");
  224. return Error(msg, "INVALID");
  225. }
  226. // If this is not a one-hop packet, it's invalid
  227. if (!EncodingScheme_isOneHop(ch->ourEncodingScheme, label)) {
  228. Log_debug(ch->log, "DROP LLADDR query from non-peer");
  229. return Error(msg, "INVALID");
  230. }
  231. // Get the lladdr from the peer who sent the message
  232. struct Sockaddr* sa = InterfaceController_getPeerLlAddr(
  233. ch->ifc, Message_getAlloc(msg), label);
  234. if (!sa) {
  235. Log_info(ch->log, "LLADDR query peer not found");
  236. return Error(msg, "INTERNAL");
  237. }
  238. Err(writeLlAddr(msg, sa));
  239. hdr.type_be = Control_LlAddr_REPLY_be;
  240. hdr.checksum_be = 0;
  241. Err(Message_epush(msg, &hdr, sizeof hdr));
  242. hdr.checksum_be = Checksum_engine_be(Message_bytes(msg), Message_getLength(msg));
  243. Err(Message_epop(msg, NULL, sizeof hdr));
  244. Err(Message_epush(msg, &hdr, sizeof hdr));
  245. struct RouteHeader routeHeader = {
  246. .sh.label_be = Endian_hostToBigEndian64(label),
  247. .flags = RouteHeader_flags_CTRLMSG,
  248. };
  249. SwitchHeader_setVersion(&routeHeader.sh, SwitchHeader_CURRENT_VERSION);
  250. Err(Message_epush(msg, &routeHeader, sizeof routeHeader));
  251. return Iface_next(&ch->pub.coreIf, msg);
  252. }
  253. /**
  254. * Expects [ SwitchHeader ][ Ctrl ][ SupernodeQuery ][ data etc.... ]
  255. */
  256. #define handleGetSnodeQuery_MIN_SIZE (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  257. static Iface_DEFUN handleGetSnodeQuery(Message_t* msg,
  258. struct ControlHandler_pvt* ch,
  259. uint64_t label,
  260. uint8_t* labelStr)
  261. {
  262. Log_debug(ch->log, "incoming getSupernode query");
  263. if (Message_getLength(msg) < handleGetSnodeQuery_MIN_SIZE) {
  264. Log_info(ch->log, "DROP runt getSupernode query");
  265. return Error(msg, "RUNT");
  266. }
  267. struct Control* ctrl = (struct Control*) Message_bytes(msg);
  268. struct Control_GetSnode* snq = &ctrl->content.getSnode;
  269. if (snq->magic != Control_GETSNODE_QUERY_MAGIC) {
  270. Log_debug(ch->log, "DROP getSupernode query (bad magic)");
  271. return Error(msg, "INVALID");
  272. }
  273. uint32_t herVersion = Endian_bigEndianToHost32(snq->version_be);
  274. if (!Version_compatibleWithCurrent(herVersion)) {
  275. Log_debug(ch->log, "DROP getSupernode query from incompatible version [%d]", herVersion);
  276. // Nothing wrong with the query but we're just not going to answer it
  277. return NULL;
  278. }
  279. ctrl->header.type_be = Control_GETSNODE_REPLY_be;
  280. snq->kbps_be = 0xffffffff;
  281. snq->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  282. snq->magic = Control_GETSNODE_REPLY_MAGIC;
  283. uint64_t fixedLabel = 0;
  284. if (ch->activeSnode.path) {
  285. fixedLabel = NumberCompress_getLabelFor(ch->activeSnode.path, label);
  286. uint64_t fixedLabel_be = Endian_hostToBigEndian64(fixedLabel);
  287. Bits_memcpy(snq->pathToSnode_be, &fixedLabel_be, 8);
  288. Bits_memcpy(snq->snodeKey, ch->activeSnode.key, 32);
  289. snq->snodeVersion_be = Endian_hostToBigEndian32(ch->activeSnode.protocolVersion);
  290. } else {
  291. Log_debug(ch->log, "getSupernode query and we do not have a known snode");
  292. snq->snodeVersion_be = 0;
  293. Bits_memset(snq->pathToSnode_be, 0, 8);
  294. Bits_memset(snq->snodeKey, 0, 32);
  295. }
  296. ctrl->header.checksum_be = 0;
  297. ctrl->header.checksum_be = Checksum_engine_be(Message_bytes(msg), Message_getLength(msg));
  298. Err(Message_eshift(msg, RouteHeader_SIZE));
  299. struct RouteHeader* routeHeader = (struct RouteHeader*) Message_bytes(msg);
  300. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  301. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  302. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  303. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  304. Log_debug(ch->log, "Replied to [%" PRIx64 "] with snode [%" PRIx64 "] aka [%" PRIx64 "]",
  305. label, fixedLabel, ch->activeSnode.path);
  306. return Iface_next(&ch->pub.coreIf, msg);
  307. }
  308. /**
  309. * Handle an incoming control message from a switch.
  310. *
  311. * @param context the ducttape context.
  312. * @param message the control message, this should be alligned on the beginning of the content,
  313. * that is to say, after the end of the switch header.
  314. * @param switchHeader the header.
  315. * @param switchIf the interface which leads to the switch.
  316. * @param isFormV8 true if the control message is in the form specified by protocol version 8+
  317. */
  318. static Iface_DEFUN incomingFromCore(Message_t* msg, struct Iface* coreIf)
  319. {
  320. struct ControlHandler_pvt* ch = Identity_check((struct ControlHandler_pvt*) coreIf);
  321. struct RouteHeader routeHdr;
  322. Err(Message_epop(msg, &routeHdr, RouteHeader_SIZE));
  323. uint8_t labelStr[20];
  324. uint64_t label = Endian_bigEndianToHost64(routeHdr.sh.label_be);
  325. AddrTools_printPath(labelStr, label);
  326. // happens in benchmark
  327. // Log_debug(ch->log, "ctrl packet from [%s]", labelStr);
  328. if (Message_getLength(msg) < 4 + Control_Header_SIZE) {
  329. Log_info(ch->log, "DROP runt ctrl packet from [%s]", labelStr);
  330. return Error(msg, "RUNT");
  331. }
  332. Assert_true(routeHdr.flags & RouteHeader_flags_CTRLMSG);
  333. if (Checksum_engine_be(Message_bytes(msg), Message_getLength(msg))) {
  334. Log_info(ch->log, "DROP ctrl packet from [%s] with invalid checksum", labelStr);
  335. return Error(msg, "INVALID");
  336. }
  337. struct Control* ctrl = (struct Control*) Message_bytes(msg);
  338. const uint16_t t = ctrl->header.type_be;
  339. if (t == Control_ERROR_be) {
  340. return handleError(msg, ch, label, labelStr, &routeHdr);
  341. } else if (t == Control_KEYPING_be || t == Control_PING_be) {
  342. return handlePing(msg, ch, label, labelStr, t);
  343. } else if (t == Control_GETSNODE_QUERY_be) {
  344. return handleGetSnodeQuery(msg, ch, label, labelStr);
  345. } else if (t == Control_RPATH_QUERY_be) {
  346. return handleRPathQuery(msg, ch, label, labelStr);
  347. } else if (t == Control_LlAddr_QUERY_be) {
  348. return handleLlAddrQuery(msg, ch, label, labelStr);
  349. } else if (
  350. t == Control_PONG_be ||
  351. t == Control_KEYPONG_be ||
  352. t == Control_LlAddr_REPLY_be ||
  353. t == Control_GETSNODE_REPLY_be ||
  354. t == Control_RPATH_REPLY_be)
  355. {
  356. if (t == Control_PONG_be) {
  357. // Special case for IfController which has it's own pinger
  358. int32_t offset = Message_getLength(msg) - 12;
  359. uint8_t* ptr = &Message_bytes(msg)[offset];
  360. if (!Bits_memcmp(ptr, "IFACE_CNTRLR", 12)) {
  361. Log_debug(ch->log, "IFC switch pong from [%s]", labelStr);
  362. Err(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  363. return Iface_next(&ch->pub.ifcSwitchPingerIf, msg);
  364. }
  365. }
  366. Log_debug(ch->log, "Got [%d] REPLY from [%s]", t, labelStr);
  367. Err(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  368. Err(Message_epush32be(msg, 0xffffffff));
  369. Err(Message_epush32be(msg, PFChan_Core_CTRL_MSG));
  370. return Iface_next(&ch->eventIf, msg);
  371. }
  372. Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
  373. labelStr, Endian_bigEndianToHost16(ctrl->header.type_be));
  374. return Error(msg, "INVALID");
  375. }
  376. // Forward from switch pinger directly to core.
  377. static Iface_DEFUN incomingFromSwitchPinger(Message_t* msg, struct Iface* switchPingerIf)
  378. {
  379. struct ControlHandler_pvt* ch =
  380. Identity_containerOf(switchPingerIf, struct ControlHandler_pvt, pub.ifcSwitchPingerIf);
  381. return Iface_next(&ch->pub.coreIf, msg);
  382. }
  383. static Iface_DEFUN changeSnode(Message_t* msg, struct Iface* eventIf)
  384. {
  385. struct ControlHandler_pvt* ch =
  386. Identity_containerOf(eventIf, struct ControlHandler_pvt, eventIf);
  387. enum PFChan_Pathfinder ev = 0;
  388. Err(Message_epop32be(&ev, msg));
  389. Assert_true(ev == PFChan_Pathfinder_SNODE);
  390. uint32_t discard = 0;
  391. Err(Message_epop32be(&discard, msg));
  392. struct PFChan_Node node;
  393. Err(Message_epop(msg, &node, PFChan_Node_SIZE));
  394. Assert_true(!Message_getLength(msg));
  395. uint64_t path = Endian_bigEndianToHost64(node.path_be);
  396. uint32_t protocolVersion = Endian_bigEndianToHost32(node.version_be);
  397. const char* log = NULL;
  398. if (!ch->activeSnode.path) {
  399. if (node.path_be) {
  400. log = "Found snode";
  401. } else {
  402. // didn't know the snode before, still don't
  403. return NULL;
  404. }
  405. } else if (!node.path_be) {
  406. // We had one, now we don't
  407. log = "Removing snode";
  408. } else {
  409. if (Bits_memcmp(ch->activeSnode.key, node.publicKey, 32)) {
  410. log = "Changing snode";
  411. } else if (path != ch->activeSnode.path) {
  412. log = "Changing snode path";
  413. } else if (ch->activeSnode.protocolVersion != protocolVersion) {
  414. log = "Changing snode protocolVersion";
  415. } else {
  416. // Nothing has changed
  417. return NULL;
  418. }
  419. }
  420. struct Address old = {
  421. .protocolVersion = ch->activeSnode.protocolVersion,
  422. .path = ch->activeSnode.path,
  423. };
  424. Bits_memcpy(old.key, ch->activeSnode.key, 32);
  425. Bits_memcpy(ch->activeSnode.key, node.publicKey, 32);
  426. ch->activeSnode.path = path;
  427. ch->activeSnode.protocolVersion = protocolVersion;
  428. struct Address addr = { .protocolVersion = protocolVersion, .path = path, };
  429. Bits_memcpy(addr.key, node.publicKey, 32);
  430. Log_debug(ch->log, "%s [%s] -> [%s]", log,
  431. Address_toStringKey(&old, Message_getAlloc(msg))->bytes,
  432. Address_toStringKey(&addr, Message_getAlloc(msg))->bytes);
  433. return NULL;
  434. }
  435. struct ControlHandler* ControlHandler_new(struct Allocator* allocator,
  436. struct Log* logger,
  437. struct EventEmitter* ee,
  438. uint8_t myPublicKey[32],
  439. struct InterfaceController* ifc)
  440. {
  441. struct Allocator* alloc = Allocator_child(allocator);
  442. struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
  443. ch->ourEncodingScheme = NumberCompress_defineScheme(alloc);
  444. ch->alloc = alloc;
  445. ch->log = logger;
  446. ch->ifc = ifc;
  447. Bits_memcpy(ch->myPublicKey, myPublicKey, 32);
  448. ch->pub.coreIf.send = incomingFromCore;
  449. ch->pub.ifcSwitchPingerIf.send = incomingFromSwitchPinger;
  450. ch->eventIf.send = changeSnode;
  451. EventEmitter_regCore(ee, &ch->eventIf, PFChan_Pathfinder_SNODE);
  452. Identity_set(ch);
  453. return &ch->pub;
  454. }