SerializationModule.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "benc/Object.h"
  16. #include "benc/String.h"
  17. #include "dht/DHTMessage.h"
  18. #include "dht/DHTModule.h"
  19. #include "dht/DHTModuleRegistry.h"
  20. #include "dht/SerializationModule.h"
  21. #include "memory/Allocator.h"
  22. #include "benc/serialization/standard/BencMessageReader.h"
  23. #include "benc/serialization/standard/BencMessageWriter.h"
  24. #include "util/Bits.h"
  25. #include "util/log/Log.h"
  26. #include "wire/Message.h"
  27. #define SERIALIZER StandardBencSerializer_get()
  28. struct SerializationModule_context {
  29. struct DHTModule module;
  30. struct Log* logger;
  31. };
  32. /*--------------------Prototypes--------------------*/
  33. static int handleOutgoing(struct DHTMessage* message,
  34. void* vcontext);
  35. static int handleIncoming(struct DHTMessage* message,
  36. void* vcontext);
  37. /*--------------------Interface--------------------*/
  38. void SerializationModule_register(struct DHTModuleRegistry* registry,
  39. struct Log* logger,
  40. struct Allocator* allocator)
  41. {
  42. struct SerializationModule_context* context =
  43. Allocator_malloc(allocator, sizeof(struct SerializationModule_context));
  44. Bits_memcpy(context, (&(struct SerializationModule_context) {
  45. .module = {
  46. .name = "SerializationModule",
  47. .context = context,
  48. .handleIncoming = handleIncoming,
  49. .handleOutgoing = handleOutgoing
  50. },
  51. .logger = logger
  52. }), sizeof(struct SerializationModule_context));
  53. DHTModuleRegistry_register(&(context->module), registry);
  54. }
  55. /*--------------------Internals--------------------*/
  56. /**
  57. * Take an outgoing message and serialize the bencoded message.
  58. *
  59. * @see DHTModule->handleOutgoing in DHTModules.h
  60. */
  61. static int handleOutgoing(struct DHTMessage* message,
  62. void* vcontext)
  63. {
  64. // This is always at the end of the message.
  65. Assert_true(!message->binMessage->length);
  66. Assert_true(!((uintptr_t)message->binMessage->bytes % 4) || !"alignment fault0");
  67. String* q = Dict_getStringC(message->asDict, "q");
  68. if (q) {
  69. String* txid = Dict_getStringC(message->asDict, "txid");
  70. Assert_true(txid);
  71. String* newTxid = String_newBinary(NULL, txid->len + 1, message->allocator);
  72. newTxid->bytes[0] = '0';
  73. Bits_memcpy(&newTxid->bytes[1], txid->bytes, txid->len);
  74. Dict_putStringC(message->asDict, "txid", newTxid, message->allocator);
  75. }
  76. BencMessageWriter_write(message->asDict, message->binMessage, NULL);
  77. Assert_true(!((uintptr_t)message->binMessage->bytes % 4) || !"alignment fault");
  78. return 0;
  79. }
  80. /**
  81. * Take an incoming message and deserialize the bencoded message.
  82. *
  83. * @see DHTModule->handleIncoming in DHTModules.h
  84. */
  85. static int handleIncoming(struct DHTMessage* message,
  86. void* vcontext)
  87. {
  88. struct SerializationModule_context* context = vcontext;
  89. char* err =
  90. BencMessageReader_readNoExcept(message->binMessage, message->allocator, &message->asDict);
  91. if (err) {
  92. Log_info(context->logger, "Failed to parse message [%s]", err);
  93. return -2;
  94. }
  95. if (message->binMessage->length) {
  96. Log_info(context->logger, "Message contains [%d] bytes of crap at the end",
  97. (int)message->binMessage->length);
  98. }
  99. String* q = Dict_getStringC(message->asDict, "q");
  100. String* txid = Dict_getStringC(message->asDict, "txid");
  101. if (!txid) {
  102. Log_info(context->logger, "query with no txid");
  103. return -2;
  104. }
  105. if (q) {
  106. if (txid->bytes[0] == '1') {
  107. Log_debug(context->logger, "query txid which appears to be meant for subnode");
  108. return -2;
  109. }
  110. } else {
  111. if (txid->bytes[0] != '0') {
  112. Log_debug(context->logger, "reply txid which is not from old pathfinder");
  113. return -2;
  114. }
  115. String* newTxid = String_newBinary(&txid->bytes[1], txid->len - 1, message->allocator);
  116. Dict_putStringC(message->asDict, "txid", newTxid, message->allocator);
  117. }
  118. return 0;
  119. }