ReplyModule.c 2.9 KB

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