ReplyModule.c 3.1 KB

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