ControlHandler.c 11 KB

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