PingResponder.c 3.4 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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/Dict.h"
  16. #include "subnode/PingResponder.h"
  17. #include "util/Identity.h"
  18. #include "rust/cjdns_sys/Rffi.h"
  19. struct PingResponder_pvt
  20. {
  21. struct PingResponder pub;
  22. struct MsgCore_Handler* handler;
  23. struct AddrSet* peers;
  24. struct Log* log;
  25. struct Allocator* alloc;
  26. struct MsgCore* msgCore;
  27. struct BoilerplateResponder* br;
  28. struct EncodingScheme* myScheme;
  29. Identity
  30. };
  31. static struct RTypes_Error_t* onPing(Dict* msg,
  32. struct Address* src,
  33. struct Allocator* tmpAlloc,
  34. struct MsgCore_Handler* handler)
  35. {
  36. struct PingResponder_pvt* prp = Identity_check((struct PingResponder_pvt*) handler->userData);
  37. Log_debug(prp->log, "Received ping req from [%s]", Address_toString(src, tmpAlloc)->bytes);
  38. String* txid = Dict_getStringC(msg, "txid");
  39. if (!txid) {
  40. Log_debug(prp->log, "ping missing txid");
  41. return Rffi_error("INVALID", tmpAlloc);
  42. }
  43. Dict* responseDict = Dict_new(tmpAlloc);
  44. String* q = Dict_getStringC(msg, "q");
  45. if (!q || !String_equals(q, String_CONST("pn"))) {
  46. Dict_putStringCC(responseDict, "error", "unsupported query type", tmpAlloc);
  47. }
  48. if (!src->protocolVersion) {
  49. } else if (src->protocolVersion >= 21) {
  50. } else if (EncodingScheme_isOneHop(prp->myScheme, src->path)) {
  51. // Always reply to peers
  52. } else {
  53. Log_debug(prp->log, "DROP query from [%s] because their version is [%d] and they won't "
  54. "respect do-not-disturb",
  55. Address_toString(src, tmpAlloc)->bytes,
  56. src->protocolVersion);
  57. return Rffi_error("UNHANDLED", tmpAlloc);
  58. }
  59. Dict_putStringC(responseDict, "txid", txid, tmpAlloc);
  60. Dict_putIntC(msg, "dnd", 1, tmpAlloc); // do not disturb
  61. BoilerplateResponder_addBoilerplate(prp->br, responseDict, src, tmpAlloc);
  62. MsgCore_sendResponse(prp->msgCore, responseDict, src, tmpAlloc);
  63. return NULL;
  64. }
  65. struct PingResponder* PingResponder_new(struct Allocator* allocator,
  66. struct Log* log,
  67. struct MsgCore* msgCore,
  68. struct BoilerplateResponder* br,
  69. struct EncodingScheme* myScheme)
  70. {
  71. struct Allocator* alloc = Allocator_child(allocator);
  72. struct PingResponder_pvt* prp =
  73. Allocator_calloc(alloc, sizeof(struct PingResponder_pvt), 1);
  74. Identity_set(prp);
  75. prp->log = log;
  76. prp->alloc = alloc;
  77. prp->msgCore = msgCore;
  78. prp->handler = MsgCore_onQuery(msgCore, "pn", alloc);
  79. prp->handler->userData = prp;
  80. prp->handler->cb = onPing;
  81. prp->br = br;
  82. prp->myScheme = myScheme;
  83. return &prp->pub;
  84. }