ControlHandler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "util/Identity.h"
  18. #include "util/AddrTools.h"
  19. #include "util/Checksum.h"
  20. #include "wire/Control.h"
  21. #include "wire/Error.h"
  22. #define NumberCompress_OLD_CODE
  23. #include "switch/NumberCompress.h"
  24. struct ControlHandler_pvt
  25. {
  26. struct ControlHandler pub;
  27. struct Log* log;
  28. struct Allocator* alloc;
  29. uint8_t myPublicKey[32];
  30. struct Iface eventIf;
  31. struct Address activeSnode;
  32. Identity
  33. };
  34. /**
  35. * Expects [ Ctrl ][ Error ][ cause SwitchHeader ][ cause handle ][ cause etc.... ]
  36. */
  37. #define handleError_MIN_SIZE (Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE + 4)
  38. static Iface_DEFUN handleError(struct Message* msg,
  39. struct ControlHandler_pvt* ch,
  40. uint64_t label,
  41. uint8_t* labelStr,
  42. struct RouteHeader* rh)
  43. {
  44. if (msg->length < handleError_MIN_SIZE) {
  45. Log_info(ch->log, "DROP runt error packet from [%s]", labelStr);
  46. return NULL;
  47. }
  48. msg->length = handleError_MIN_SIZE;
  49. Message_push(msg, &rh->sh, SwitchHeader_SIZE, NULL);
  50. Message_push32(msg, 0xffffffff, NULL);
  51. Message_push32(msg, PFChan_Core_SWITCH_ERR, NULL);
  52. return Iface_next(&ch->eventIf, msg);
  53. }
  54. /**
  55. * Expects [ SwitchHeader ][ Ctrl ][ (key)Ping ][ data etc.... ]
  56. */
  57. #define handlePing_MIN_SIZE (Control_Header_SIZE + Control_Ping_MIN_SIZE)
  58. static Iface_DEFUN handlePing(struct Message* msg,
  59. struct ControlHandler_pvt* ch,
  60. uint64_t label,
  61. uint8_t* labelStr,
  62. uint16_t messageType_be)
  63. {
  64. if (msg->length < handlePing_MIN_SIZE) {
  65. Log_info(ch->log, "DROP runt ping");
  66. return NULL;
  67. }
  68. struct Control* ctrl = (struct Control*) msg->bytes;
  69. Message_shift(msg, -Control_Header_SIZE, NULL);
  70. // Ping and keyPing share version location
  71. struct Control_Ping* ping = (struct Control_Ping*) msg->bytes;
  72. uint32_t herVersion = Endian_bigEndianToHost32(ping->version_be);
  73. if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
  74. Log_debug(ch->log, "DROP ping from incompatible version [%d]", herVersion);
  75. return NULL;
  76. }
  77. if (messageType_be == Control_KEYPING_be) {
  78. Log_debug(ch->log, "got switch keyPing from [%s]", labelStr);
  79. if (msg->length < Control_KeyPing_HEADER_SIZE) {
  80. // min keyPing size is longer
  81. Log_debug(ch->log, "DROP runt keyPing");
  82. return NULL;
  83. }
  84. if (msg->length > Control_KeyPing_MAX_SIZE) {
  85. Log_debug(ch->log, "DROP long keyPing");
  86. return NULL;
  87. }
  88. if (ping->magic != Control_KeyPing_MAGIC) {
  89. Log_debug(ch->log, "DROP keyPing (bad magic)");
  90. return NULL;
  91. }
  92. struct Control_KeyPing* keyPing = (struct Control_KeyPing*) msg->bytes;
  93. keyPing->magic = Control_KeyPong_MAGIC;
  94. ctrl->header.type_be = Control_KEYPONG_be;
  95. Bits_memcpy(keyPing->key, ch->myPublicKey, 32);
  96. } else if (messageType_be == Control_PING_be) {
  97. // Happens in benchmark.
  98. //Log_debug(ch->log, "got switch ping from [%s]", labelStr);
  99. if (ping->magic != Control_Ping_MAGIC) {
  100. Log_debug(ch->log, "DROP ping (bad magic)");
  101. return NULL;
  102. }
  103. ping->magic = Control_Pong_MAGIC;
  104. ctrl->header.type_be = Control_PONG_be;
  105. } else {
  106. Assert_failure("2+2=5");
  107. }
  108. ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  109. Message_shift(msg, Control_Header_SIZE, NULL);
  110. ctrl->header.checksum_be = 0;
  111. ctrl->header.checksum_be = Checksum_engine(msg->bytes, msg->length);
  112. Message_shift(msg, RouteHeader_SIZE, NULL);
  113. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  114. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  115. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  116. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  117. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  118. return Iface_next(&ch->pub.coreIf, msg);
  119. }
  120. /**
  121. * Expects [ SwitchHeader ][ Ctrl ][ SupernodeQuery ][ data etc.... ]
  122. */
  123. #define handleGetSnodeQuery_MIN_SIZE (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  124. static Iface_DEFUN handleGetSnodeQuery(struct Message* msg,
  125. struct ControlHandler_pvt* ch,
  126. uint64_t label,
  127. uint8_t* labelStr)
  128. {
  129. Log_debug(ch->log, "incoming getSupernode query");
  130. if (msg->length < handleGetSnodeQuery_MIN_SIZE) {
  131. Log_info(ch->log, "DROP runt getSupernode query");
  132. return NULL;
  133. }
  134. struct Control* ctrl = (struct Control*) msg->bytes;
  135. struct Control_GetSnode* snq = &ctrl->content.getSnode;
  136. if (snq->magic != Control_GETSNODE_QUERY_MAGIC) {
  137. Log_debug(ch->log, "DROP getSupernode query (bad magic)");
  138. return NULL;
  139. }
  140. uint32_t herVersion = Endian_bigEndianToHost32(snq->version_be);
  141. if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
  142. Log_debug(ch->log, "DROP getSupernode query from incompatible version [%d]", herVersion);
  143. return NULL;
  144. }
  145. ctrl->header.type_be = Control_GETSNODE_REPLY_be;
  146. snq->kbps_be = 0xffffffff;
  147. snq->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  148. snq->magic = Control_GETSNODE_REPLY_MAGIC;
  149. if (ch->activeSnode.path) {
  150. uint64_t fixedLabel = NumberCompress_getLabelFor(ch->activeSnode.path, label);
  151. uint64_t fixedLabel_be = Endian_hostToBigEndian64(fixedLabel);
  152. Bits_memcpy(snq->pathToSnode_be, &fixedLabel_be, 8);
  153. Bits_memcpy(&snq->snodeKey, ch->activeSnode.key, 32);
  154. snq->snodeVersion_be = Endian_hostToBigEndian32(ch->activeSnode.protocolVersion);
  155. } else {
  156. snq->snodeVersion_be = 0;
  157. Bits_memset(snq->pathToSnode_be, 0, 8);
  158. Bits_memcpy(&snq->snodeKey, ch->activeSnode.key, 32);
  159. }
  160. ctrl->header.checksum_be = 0;
  161. ctrl->header.checksum_be = Checksum_engine(msg->bytes, msg->length);
  162. Message_shift(msg, RouteHeader_SIZE, NULL);
  163. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  164. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  165. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  166. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  167. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  168. return Iface_next(&ch->pub.coreIf, msg);
  169. }
  170. /**
  171. * Handle an incoming control message from a switch.
  172. *
  173. * @param context the ducttape context.
  174. * @param message the control message, this should be alligned on the beginning of the content,
  175. * that is to say, after the end of the switch header.
  176. * @param switchHeader the header.
  177. * @param switchIf the interface which leads to the switch.
  178. * @param isFormV8 true if the control message is in the form specified by protocol version 8+
  179. */
  180. static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* coreIf)
  181. {
  182. struct ControlHandler_pvt* ch = Identity_check((struct ControlHandler_pvt*) coreIf);
  183. struct RouteHeader routeHdr;
  184. Message_pop(msg, &routeHdr, RouteHeader_SIZE, NULL);
  185. uint8_t labelStr[20];
  186. uint64_t label = Endian_bigEndianToHost64(routeHdr.sh.label_be);
  187. AddrTools_printPath(labelStr, label);
  188. // happens in benchmark
  189. // Log_debug(ch->log, "ctrl packet from [%s]", labelStr);
  190. if (msg->length < 4 + Control_Header_SIZE) {
  191. Log_info(ch->log, "DROP runt ctrl packet from [%s]", labelStr);
  192. return NULL;
  193. }
  194. Assert_true(routeHdr.flags & RouteHeader_flags_CTRLMSG);
  195. if (Checksum_engine(msg->bytes, msg->length)) {
  196. Log_info(ch->log, "DROP ctrl packet from [%s] with invalid checksum", labelStr);
  197. return NULL;
  198. }
  199. struct Control* ctrl = (struct Control*) msg->bytes;
  200. if (ctrl->header.type_be == Control_ERROR_be) {
  201. return handleError(msg, ch, label, labelStr, &routeHdr);
  202. } else if (ctrl->header.type_be == Control_KEYPING_be
  203. || ctrl->header.type_be == Control_PING_be)
  204. {
  205. return handlePing(msg, ch, label, labelStr, ctrl->header.type_be);
  206. } else if (ctrl->header.type_be == Control_KEYPONG_be
  207. || ctrl->header.type_be == Control_PONG_be)
  208. {
  209. Log_debug(ch->log, "got switch pong from [%s]", labelStr);
  210. Message_push(msg, &routeHdr, RouteHeader_SIZE, NULL);
  211. return Iface_next(&ch->pub.switchPingerIf, msg);
  212. } else if (ctrl->header.type_be == Control_GETSNODE_QUERY_be) {
  213. return handleGetSnodeQuery(msg, ch, label, labelStr);
  214. } else if (ctrl->header.type_be == Control_GETSNODE_REPLY_be) {
  215. Log_debug(ch->log, "got GETSNODE_REPLY from [%s]", labelStr);
  216. Message_push(msg, &routeHdr, RouteHeader_SIZE, NULL);
  217. Message_push32(msg, 0xffffffff, NULL);
  218. Message_push32(msg, PFChan_Core_CTRL_MSG, NULL);
  219. return Iface_next(&ch->eventIf, msg);
  220. }
  221. Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
  222. labelStr, Endian_bigEndianToHost16(ctrl->header.type_be));
  223. return NULL;
  224. }
  225. // Forward from switch pinger directly to core.
  226. static Iface_DEFUN incomingFromSwitchPinger(struct Message* msg, struct Iface* switchPingerIf)
  227. {
  228. struct ControlHandler_pvt* ch =
  229. Identity_containerOf(switchPingerIf, struct ControlHandler_pvt, pub.switchPingerIf);
  230. return Iface_next(&ch->pub.coreIf, msg);
  231. }
  232. static Iface_DEFUN changeSnode(struct Message* msg, struct Iface* eventIf)
  233. {
  234. struct ControlHandler_pvt* ch =
  235. Identity_containerOf(eventIf, struct ControlHandler_pvt, eventIf);
  236. enum PFChan_Pathfinder ev = Message_pop32(msg, NULL);
  237. Assert_true(ev == PFChan_Pathfinder_SNODE);
  238. Message_pop32(msg, NULL);
  239. struct PFChan_Node node;
  240. Message_pop(msg, &node, PFChan_Node_SIZE, NULL);
  241. Assert_true(!msg->length);
  242. Bits_memcpy(ch->activeSnode.key, node.publicKey, 32);
  243. ch->activeSnode.path = Endian_bigEndianToHost64(node.path_be);
  244. ch->activeSnode.protocolVersion = Endian_bigEndianToHost32(node.version_be);
  245. return NULL;
  246. }
  247. struct ControlHandler* ControlHandler_new(struct Allocator* allocator,
  248. struct Log* logger,
  249. struct EventEmitter* ee,
  250. uint8_t myPublicKey[32])
  251. {
  252. struct Allocator* alloc = Allocator_child(allocator);
  253. struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
  254. ch->alloc = alloc;
  255. ch->log = logger;
  256. Bits_memcpy(ch->myPublicKey, myPublicKey, 32);
  257. ch->pub.coreIf.send = incomingFromCore;
  258. ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
  259. ch->eventIf.send = changeSnode;
  260. EventEmitter_regCore(ee, &ch->eventIf, PFChan_Pathfinder_SNODE);
  261. Identity_set(ch);
  262. return &ch->pub;
  263. }