PingResponder.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "subnode/PingResponder.h"
  16. #include "util/Identity.h"
  17. struct PingResponder_pvt
  18. {
  19. struct PingResponder pub;
  20. struct MsgCore_Handler* handler;
  21. struct AddrSet* peers;
  22. struct Log* log;
  23. struct Allocator* alloc;
  24. struct MsgCore* msgCore;
  25. struct BoilerplateResponder* br;
  26. Identity
  27. };
  28. static void onPing(Dict* msg,
  29. struct Address* src,
  30. struct Allocator* tmpAlloc,
  31. struct MsgCore_Handler* handler)
  32. {
  33. struct PingResponder_pvt* prp = Identity_check((struct PingResponder_pvt*) handler->userData);
  34. Log_debug(prp->log, "Received ping req from [%s]", Address_toString(src, tmpAlloc)->bytes);
  35. String* txid = Dict_getStringC(msg, "txid");
  36. if (!txid) {
  37. Log_debug(prp->log, "ping missing txid");
  38. return;
  39. }
  40. Dict* responseDict = Dict_new(tmpAlloc);
  41. Dict_putStringC(responseDict, "txid", txid, tmpAlloc);
  42. BoilerplateResponder_addBoilerplate(prp->br, responseDict, src, tmpAlloc);
  43. MsgCore_sendResponse(prp->msgCore, responseDict, src, tmpAlloc);
  44. }
  45. struct PingResponder* PingResponder_new(struct Allocator* allocator,
  46. struct Log* log,
  47. struct MsgCore* msgCore,
  48. struct BoilerplateResponder* br)
  49. {
  50. struct Allocator* alloc = Allocator_child(allocator);
  51. struct PingResponder_pvt* prp =
  52. Allocator_calloc(alloc, sizeof(struct PingResponder_pvt), 1);
  53. Identity_set(prp);
  54. prp->log = log;
  55. prp->alloc = alloc;
  56. prp->msgCore = msgCore;
  57. prp->handler = MsgCore_onQuery(msgCore, "pn", alloc);
  58. prp->handler->userData = prp;
  59. prp->handler->cb = onPing;
  60. prp->br = br;
  61. return &prp->pub;
  62. }