SwitchPinger.h 4.2 KB

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