SerializationModule.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "benc/serialization/standard/BencMessageReader.h"
  21. #include "benc/serialization/standard/BencMessageWriter.h"
  22. #include "util/Bits.h"
  23. #include "util/log/Log.h"
  24. #include "wire/Message.h"
  25. #define SERIALIZER StandardBencSerializer_get()
  26. struct SerializationModule_context {
  27. struct DHTModule module;
  28. struct Log* logger;
  29. };
  30. /*--------------------Prototypes--------------------*/
  31. static int handleOutgoing(struct DHTMessage* message,
  32. void* vcontext);
  33. static int handleIncoming(struct DHTMessage* message,
  34. void* vcontext);
  35. /*--------------------Interface--------------------*/
  36. void SerializationModule_register(struct DHTModuleRegistry* registry,
  37. struct Log* logger,
  38. struct Allocator* allocator)
  39. {
  40. struct SerializationModule_context* context =
  41. Allocator_malloc(allocator, sizeof(struct SerializationModule_context));
  42. Bits_memcpy(context, (&(struct SerializationModule_context) {
  43. .module = {
  44. .name = "SerializationModule",
  45. .context = context,
  46. .handleIncoming = handleIncoming,
  47. .handleOutgoing = handleOutgoing
  48. },
  49. .logger = logger
  50. }), sizeof(struct SerializationModule_context));
  51. DHTModuleRegistry_register(&(context->module), registry);
  52. }
  53. /*--------------------Internals--------------------*/
  54. /**
  55. * Take an outgoing message and serialize the bencoded message.
  56. *
  57. * @see DHTModule->handleOutgoing in DHTModules.h
  58. */
  59. static int handleOutgoing(struct DHTMessage* message,
  60. void* vcontext)
  61. {
  62. // This is always at the end of the message.
  63. Assert_true(!message->binMessage->length);
  64. Assert_true(!((uintptr_t)message->binMessage->bytes % 4) || !"alignment fault0");
  65. BencMessageWriter_write(message->asDict, message->binMessage, NULL);
  66. Assert_true(!((uintptr_t)message->binMessage->bytes % 4) || !"alignment fault");
  67. return 0;
  68. }
  69. /**
  70. * Take an incoming message and deserialize the bencoded message.
  71. *
  72. * @see DHTModule->handleIncoming in DHTModules.h
  73. */
  74. static int handleIncoming(struct DHTMessage* message,
  75. void* vcontext)
  76. {
  77. struct SerializationModule_context* context = vcontext;
  78. char* err =
  79. BencMessageReader_readNoExcept(message->binMessage, message->allocator, &message->asDict);
  80. if (err) {
  81. Log_info(context->logger, "Failed to parse message [%s]", err);
  82. return -2;
  83. }
  84. if (message->binMessage->length) {
  85. Log_info(context->logger, "Message contains [%d] bytes of crap at the end",
  86. (int)message->binMessage->length);
  87. }
  88. return 0;
  89. }