1
0

SerializationModule.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "memory/BufferAllocator.h"
  21. #include "io/Reader.h"
  22. #include "io/ArrayReader.h"
  23. #include "io/Writer.h"
  24. #include "io/ArrayWriter.h"
  25. #include "benc/serialization/BencSerializer.h"
  26. #include "benc/serialization/standard/StandardBencSerializer.h"
  27. #include "util/Bits.h"
  28. #include "util/log/Log.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. struct Writer* writer =
  68. ArrayWriter_new(message->bytes, DHTMessage_MAX_SIZE, message->allocator);
  69. SERIALIZER->serializeDictionary(writer, message->asDict);
  70. message->length = writer->bytesWritten;
  71. return 0;
  72. }
  73. /**
  74. * Take an incoming message and deserialize the bencoded message.
  75. *
  76. * @see DHTModule->handleIncoming in DHTModules.h
  77. */
  78. static int handleIncoming(struct DHTMessage* message,
  79. void* vcontext)
  80. {
  81. message->asDict = Allocator_malloc(message->allocator, sizeof(Dict));
  82. struct Reader* reader =
  83. ArrayReader_new(message->bytes, DHTMessage_MAX_SIZE, message->allocator);
  84. int ret = SERIALIZER->parseDictionary(reader, message->allocator, message->asDict);
  85. if (ret != 0) {
  86. #ifdef Log_INFO
  87. struct SerializationModule_context* context = vcontext;
  88. Log_info(context->logger, "Failed to parse message [%d]", ret);
  89. #endif
  90. return -2;
  91. }
  92. return 0;
  93. }