1
0

SwitchPinger.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef SwitchPinger_H
  16. #define SwitchPinger_H
  17. #include "benc/String.h"
  18. #include "crypto/random/Random.h"
  19. #include "interface/Interface.h"
  20. #include "util/events/EventBase.h"
  21. #include "util/log/Log.h"
  22. #include <stdint.h>
  23. enum SwitchPinger_Result
  24. {
  25. /** Ping responded to ok. */
  26. SwitchPinger_Result_OK,
  27. /** Response label differs from sent label. */
  28. SwitchPinger_Result_LABEL_MISMATCH,
  29. /** Response contains different data than what was sent. */
  30. SwitchPinger_Result_WRONG_DATA,
  31. /** Instead of a normal response, got an error control packet. */
  32. SwitchPinger_Result_ERROR_RESPONSE,
  33. /** Ping timeout. */
  34. SwitchPinger_Result_TIMEOUT
  35. };
  36. /**
  37. * Callback which will be called when the ping response comes back.
  38. *
  39. * @param result SwitchPinger_Result_OK if all went well,
  40. * otherwise SwitchPinger_Result_*
  41. * @param label the label as of the responding node in host order.
  42. * @param data the content of the ping response.
  43. * @param millisecondsLag the number of milliseconds since the original ping was sent.
  44. * @param nodeVersion the version of the node which was pinged.
  45. * @param onResponseContext a context which was provided to SwitchPinger_ping().
  46. */
  47. typedef void (* SwitchPinger_ResponseCallback)(enum SwitchPinger_Result result,
  48. uint64_t label,
  49. String* data,
  50. uint32_t millisecondsLag,
  51. uint32_t nodeVersion,
  52. void* onResponseContext);
  53. struct SwitchPinger_Ping
  54. {
  55. /**
  56. * The allocator which is set by SwitchPinger_ping() and can be used to
  57. * allocate space which will be freed when the ping completes.
  58. */
  59. struct Allocator* pingAlloc;
  60. /**
  61. * This is NULL by default and is set by the caller of Pinger_ping(),
  62. * when onResponse() is called, whatever this is, will be given to it.
  63. */
  64. void* onResponseContext;
  65. };
  66. struct SwitchPinger;
  67. String* SwitchPinger_resultString(enum SwitchPinger_Result result);
  68. /**
  69. * Allocate a ping message.
  70. *
  71. * @param label the HOST ORDER label of the node to send the ping message to.
  72. * @param data the content of the ping to send, if NULL, an empty string will be
  73. * returned in the response.
  74. * @param timeoutMilliseconds how long to wait before failing the ping.
  75. * @param onResponse the callback after the on pong or timeout.
  76. * @param alloc free this to cancel the ping.
  77. * @param ctx the pinger
  78. */
  79. struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
  80. String* data,
  81. uint32_t timeoutMilliseconds,
  82. SwitchPinger_ResponseCallback onResponse,
  83. struct Allocator* alloc,
  84. struct SwitchPinger* ctx);
  85. /**
  86. * Send a ping message after allocating a callback structure for it.
  87. *
  88. * @param ping the ping to send.
  89. */
  90. void SwitchPinger_sendPing(struct SwitchPinger_Ping* ping);
  91. struct SwitchPinger* SwitchPinger_new(struct Interface* iface,
  92. struct EventBase* eventBase,
  93. struct Random* rand,
  94. struct Log* logger,
  95. struct Allocator* alloc);
  96. #endif