1
0

SerializationModule.c 4.2 KB

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