SerializationModule.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/Object.h"
  16. #include "dht/DHTMessage.h"
  17. #include "dht/DHTModule.h"
  18. #include "dht/DHTModuleRegistry.h"
  19. #include "memory/Allocator.h"
  20. #include "io/Reader.h"
  21. #include "io/ArrayReader.h"
  22. #include "io/Writer.h"
  23. #include "io/ArrayWriter.h"
  24. #include "benc/serialization/BencSerializer.h"
  25. #include "benc/serialization/standard/StandardBencSerializer.h"
  26. #include "util/Bits.h"
  27. #include "util/log/Log.h"
  28. #include "wire/Message.h"
  29. #define SERIALIZER StandardBencSerializer_get()
  30. struct SerializationModule_context {
  31. struct DHTModule module;
  32. struct Log* logger;
  33. };
  34. /*--------------------Prototypes--------------------*/
  35. static int handleOutgoing(struct DHTMessage* message,
  36. void* vcontext);
  37. static int handleIncoming(struct DHTMessage* message,
  38. void* vcontext);
  39. /*--------------------Interface--------------------*/
  40. void SerializationModule_register(struct DHTModuleRegistry* registry,
  41. struct Log* logger,
  42. struct Allocator* allocator)
  43. {
  44. struct SerializationModule_context* context =
  45. Allocator_malloc(allocator, sizeof(struct SerializationModule_context));
  46. Bits_memcpyConst(context, (&(struct SerializationModule_context) {
  47. .module = {
  48. .name = "SerializationModule",
  49. .context = context,
  50. .handleIncoming = handleIncoming,
  51. .handleOutgoing = handleOutgoing
  52. },
  53. .logger = logger
  54. }), sizeof(struct SerializationModule_context));
  55. DHTModuleRegistry_register(&(context->module), registry);
  56. }
  57. /*--------------------Internals--------------------*/
  58. /**
  59. * Take an outgoing message and serialize the bencoded message.
  60. *
  61. * @see DHTModule->handleOutgoing in DHTModules.h
  62. */
  63. static int handleOutgoing(struct DHTMessage* message,
  64. void* vcontext)
  65. {
  66. uint8_t buff[DHTMessage_MAX_SIZE];
  67. struct Writer* writer = ArrayWriter_new(buff, DHTMessage_MAX_SIZE, message->allocator);
  68. SERIALIZER->serializeDictionary(writer, message->asDict);
  69. while ((writer->bytesWritten + message->binMessage->length) % 8) {
  70. Message_push8(message->binMessage, 0, NULL);
  71. }
  72. message->binMessage->length = 0;
  73. Message_push(message->binMessage, buff, writer->bytesWritten, NULL);
  74. return 0;
  75. }
  76. /**
  77. * Take an incoming message and deserialize the bencoded message.
  78. *
  79. * @see DHTModule->handleIncoming in DHTModules.h
  80. */
  81. static int handleIncoming(struct DHTMessage* message,
  82. void* vcontext)
  83. {
  84. message->asDict = Dict_new(message->allocator);
  85. struct Reader* reader = ArrayReader_new(message->binMessage->bytes,
  86. message->binMessage->length,
  87. message->allocator);
  88. int ret = SERIALIZER->parseDictionary(reader, message->allocator, message->asDict);
  89. if (ret != 0) {
  90. #ifdef Log_INFO
  91. struct SerializationModule_context* context = vcontext;
  92. Log_info(context->logger, "Failed to parse message [%d]", ret);
  93. #endif
  94. return -2;
  95. }
  96. if (message->binMessage->length != (int)reader->bytesRead) {
  97. #ifdef Log_INFO
  98. struct SerializationModule_context* context = vcontext;
  99. Log_info(context->logger, "Message contains [%d] bytes of crap at the end",
  100. (int)(message->binMessage->length - (int)reader->bytesRead));
  101. #endif
  102. }
  103. Message_reset(message->binMessage);
  104. return 0;
  105. }