SwitchPinger.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #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/Iface.h"
  21. #include "util/events/EventBase.h"
  22. #include "util/log/Log.h"
  23. #include "util/Linker.h"
  24. #include "wire/Control.h"
  25. Linker_require("net/SwitchPinger.c")
  26. #include <stdint.h>
  27. #define SwitchPinger_DEFAULT_MAX_CONCURRENT_PINGS 50
  28. enum SwitchPinger_Result
  29. {
  30. /** Ping responded to ok. */
  31. SwitchPinger_Result_OK,
  32. /** Response label differs from sent label. */
  33. SwitchPinger_Result_LABEL_MISMATCH,
  34. /** Response contains different data than what was sent. */
  35. SwitchPinger_Result_WRONG_DATA,
  36. /** Instead of a normal response, got an error control packet. */
  37. SwitchPinger_Result_ERROR_RESPONSE,
  38. /** A sub-set of ERROR_RESPONSE where the route contains a loop. */
  39. SwitchPinger_Result_LOOP_ROUTE,
  40. /** Ping timeout. */
  41. SwitchPinger_Result_TIMEOUT
  42. };
  43. struct SwitchPinger_Ping;
  44. struct SwitchPinger_Response
  45. {
  46. /** SwitchPinger_Result_OK if all went well, otherwise SwitchPinger_Result_* */
  47. enum SwitchPinger_Result res;
  48. /** the label as of the responding node in host order. */
  49. uint64_t label;
  50. /** the content of the ping response. */
  51. String* data;
  52. /** the number of milliseconds since the original ping was sent. */
  53. uint64_t milliseconds;
  54. /** the version of the node which was pinged. */
  55. uint32_t version;
  56. /** The key for the node which was pinged, if not a keyPing then this is set to 0. */
  57. uint8_t key[32];
  58. // relevant to messages of type GETSNODE
  59. uint32_t kbpsLimit;
  60. struct Address snode;
  61. // relevant only to messages of type RPATH
  62. uint64_t rpath;
  63. // If it's an lladdr response, the address
  64. struct Control_LlAddr lladdr;
  65. struct SwitchPinger_Ping* ping;
  66. };
  67. /** Callback which will be called when the ping response comes back. */
  68. typedef void (* SwitchPinger_ResponseCallback)(struct SwitchPinger_Response* resp, void* userData);
  69. enum SwitchPinger_Type
  70. {
  71. SwitchPinger_Type_PING,
  72. SwitchPinger_Type_KEYPING,
  73. SwitchPinger_Type_GETSNODE,
  74. SwitchPinger_Type_RPATH,
  75. SwitchPinger_Type_LLADDR,
  76. };
  77. struct SwitchPinger_Ping
  78. {
  79. /**
  80. * The allocator which is set by SwitchPinger_ping() and can be used to
  81. * allocate space which will be freed when the ping completes.
  82. */
  83. struct Allocator* pingAlloc;
  84. enum SwitchPinger_Type type;
  85. // relevant only for pings of type GETSNODE
  86. uint32_t kbpsLimit;
  87. struct Address snode;
  88. /**
  89. * This is NULL by default and is set by the caller of Pinger_ping(),
  90. * when onResponse() is called, whatever this is, will be given to it.
  91. */
  92. void* onResponseContext;
  93. };
  94. struct SwitchPinger
  95. {
  96. struct Iface controlHandlerIf;
  97. };
  98. String* SwitchPinger_resultString(enum SwitchPinger_Result result);
  99. /**
  100. * Allocate a ping message.
  101. *
  102. * @param label the HOST ORDER label of the node to send the ping message to.
  103. * @param data the content of the ping to send, if NULL, an empty string will be
  104. * returned in the response.
  105. * @param timeoutMilliseconds how long to wait before failing the ping.
  106. * @param onResponse the callback after the on pong or timeout.
  107. * @param alloc free this to cancel the ping.
  108. * @param ctx the pinger
  109. */
  110. struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
  111. String* data,
  112. uint32_t timeoutMilliseconds,
  113. SwitchPinger_ResponseCallback onResponse,
  114. struct Allocator* alloc,
  115. struct SwitchPinger* ctx);
  116. struct SwitchPinger* SwitchPinger_new(EventBase_t* eventBase,
  117. struct Random* rand,
  118. struct Log* logger,
  119. struct Address* myAddr,
  120. struct Allocator* alloc);
  121. #endif