1
0

PingResponder.c 3.2 KB

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