1
0

SwitchPinger.h 4.2 KB

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