ReplyModule.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "dht/CJDHTConstants.h"
  16. #include "dht/DHTMessage.h"
  17. #include "dht/DHTModule.h"
  18. #include "dht/DHTModuleRegistry.h"
  19. #include "dht/ReplyModule.h"
  20. #include "dht/Address.h"
  21. #include "benc/Object.h"
  22. #include "wire/Message.h"
  23. #include "util/log/Log.h"
  24. /**
  25. * The reply module replies to all incoming queries.
  26. * It also modifies outgoing replies to make sure that a reply packet has the
  27. * correct transaction id and is labeled as a reply. It adds the "y":"r" and
  28. * the "t":"aa" to the packet.
  29. * It is the core of the cjdns dht engine.
  30. */
  31. /*--------------------Prototypes--------------------*/
  32. static int handleIncoming(struct DHTMessage* message, void* vcontext);
  33. static int handleOutgoing(struct DHTMessage* message, void* vcontext);
  34. /*--------------------Interface--------------------*/
  35. /**
  36. * Register a new ReplyModule.
  37. *
  38. * @param registry the DHT module registry for signal handling.
  39. * @param allocator a means to allocate memory.
  40. */
  41. void ReplyModule_register(struct DHTModuleRegistry* registry, struct Allocator* allocator)
  42. {
  43. struct DHTModule* dm = Allocator_clone(allocator, (&(struct DHTModule) {
  44. .name = "ReplyModule",
  45. // We use the registry itself as the context
  46. .context = registry,
  47. .handleIncoming = handleIncoming,
  48. .handleOutgoing = handleOutgoing
  49. }));
  50. DHTModuleRegistry_register(dm, registry);
  51. }
  52. static int handleIncoming(struct DHTMessage* message, void* vcontext)
  53. {
  54. String* q = Dict_getString(message->asDict, CJDHTConstants_QUERY);
  55. if (Dict_getString(message->asDict, CJDHTConstants_QUERY) == NULL) {
  56. return 0;
  57. }
  58. struct DHTModuleRegistry* registry = (struct DHTModuleRegistry*) vcontext;
  59. Log_debug(registry->log, "Replying to query of type [%s] from [%s]", q->bytes,
  60. Address_toString(message->address, message->allocator)->bytes);
  61. Message_reset(message->binMessage);
  62. Assert_true(!((uintptr_t)Message_bytes(message->binMessage) % 4) || !"alignment fault0");
  63. struct DHTMessage* reply = Allocator_clone(message->allocator, (&(struct DHTMessage) {
  64. .replyTo = message,
  65. .address = message->address,
  66. .allocator = message->allocator,
  67. .binMessage = message->binMessage
  68. }));
  69. DHTModuleRegistry_handleOutgoing(reply, registry);
  70. return 0;
  71. }
  72. static int handleOutgoing(struct DHTMessage* message, void* vcontext)
  73. {
  74. if (message->replyTo != NULL) {
  75. if (message->asDict == NULL) {
  76. message->asDict = Dict_new(message->allocator);
  77. }
  78. // Put the transaction ID
  79. String* tid = Dict_getString(message->replyTo->asDict, CJDHTConstants_TXID);
  80. if (tid != NULL) {
  81. Dict_putString(message->asDict, CJDHTConstants_TXID, tid, message->allocator);
  82. }
  83. }
  84. return 0;
  85. }