1
0

Pinger.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "util/Bits.h"
  16. #include "util/Pinger.h"
  17. #include "crypto/random/Random.h"
  18. #include "util/events/Time.h"
  19. #include "util/events/Timeout.h"
  20. #include "util/Hex.h"
  21. #include "util/Identity.h"
  22. struct Ping
  23. {
  24. struct Pinger_Ping pub;
  25. struct Pinger* pinger;
  26. struct Timeout* timeout;
  27. String* data;
  28. int64_t timeSent;
  29. uint64_t cookie;
  30. Pinger_SEND_PING(sendPing);
  31. Pinger_ON_RESPONSE(onResponse);
  32. Identity
  33. };
  34. #define Map_ENABLE_HANDLES
  35. #define Map_VALUE_TYPE struct Ping*
  36. #define Map_NAME OutstandingPings
  37. #include "util/Map.h"
  38. struct Pinger
  39. {
  40. struct Map_OutstandingPings outstandingPings;
  41. struct EventBase* eventBase;
  42. struct Random* rand;
  43. struct Log* logger;
  44. struct Allocator* allocator;
  45. /** Make all handles for different pingers wildly different to simplify debugging. */
  46. uint32_t baseHandle;
  47. };
  48. static void callback(String* data, struct Ping* ping)
  49. {
  50. uint32_t now = Time_currentTimeMilliseconds();
  51. ping->onResponse(data, now - ping->timeSent, ping->pub.context);
  52. // Flag the freePing function to tell it that the ping was not terminated by the user...
  53. ping->timeSent = 0;
  54. Allocator_free(ping->pub.pingAlloc);
  55. }
  56. static void timeoutCallback(void* vping)
  57. {
  58. struct Ping* p = Identity_check((struct Ping*) vping);
  59. if (!p->timeSent) {
  60. // The ping came in at the same time that the timeout was scheduled to happen.
  61. return;
  62. }
  63. int64_t now = Time_currentTimeMilliseconds();
  64. long long diff = ((long long) now) - ((long long)p->timeSent);
  65. Assert_true(diff < 1000000000);
  66. Log_debug(p->pinger->logger, "Ping timeout for [%u] in [%lld] ms", p->pub.handle, diff);
  67. callback(NULL, p);
  68. }
  69. static int freePing(struct Allocator_OnFreeJob* job)
  70. {
  71. struct Ping* p = Identity_check((struct Ping*) job->userData);
  72. if (p->timeSent) {
  73. //Log_debug(p->pinger->logger, "Ping cancelled [%u]", p->pub.handle);
  74. }
  75. int index = Map_OutstandingPings_indexForHandle(p->pub.handle - p->pinger->baseHandle,
  76. &p->pinger->outstandingPings);
  77. Assert_true(index > -1);
  78. Map_OutstandingPings_remove(index, &p->pinger->outstandingPings);
  79. return 0;
  80. }
  81. static void asyncSendPing(void* vping)
  82. {
  83. struct Ping* p = Identity_check((struct Ping*) vping);
  84. //Log_debug(p->pinger->logger, "Sending ping [%u]", p->pub.handle);
  85. p->sendPing(p->data, p->pub.context);
  86. }
  87. struct Pinger_Ping* Pinger_newPing(String* data,
  88. Pinger_ON_RESPONSE(onResponse),
  89. Pinger_SEND_PING(sendPing),
  90. uint32_t timeoutMilliseconds,
  91. struct Allocator* allocator,
  92. struct Pinger* pinger)
  93. {
  94. struct Allocator* alloc = Allocator_child(allocator);
  95. struct Ping* ping = Allocator_clone(alloc, (&(struct Ping) {
  96. .pub = {
  97. .pingAlloc = alloc,
  98. },
  99. .sendPing = sendPing,
  100. .pinger = pinger,
  101. .timeSent = Time_currentTimeMilliseconds(),
  102. .onResponse = onResponse
  103. }));
  104. Identity_set(ping);
  105. int pingIndex = Map_OutstandingPings_put(&ping, &pinger->outstandingPings);
  106. ping->pub.handle = pinger->outstandingPings.handles[pingIndex] + pinger->baseHandle;
  107. ping->cookie = Random_uint64(pinger->rand);
  108. // Prefix the data with the handle and cookie
  109. String* toSend = String_newBinary(NULL, ((data) ? data->len : 0) + 12, alloc);
  110. Bits_memcpy(toSend->bytes, &ping->pub.handle, 4);
  111. Bits_memcpy(&toSend->bytes[4], &ping->cookie, 8);
  112. if (data) {
  113. Bits_memcpy(toSend->bytes + 12, data->bytes, data->len);
  114. }
  115. ping->data = toSend;
  116. Allocator_onFree(alloc, freePing, ping);
  117. ping->timeout =
  118. Timeout_setTimeout(timeoutCallback, ping, timeoutMilliseconds, pinger->eventBase, alloc);
  119. Timeout_setTimeout(asyncSendPing, ping, 0, pinger->eventBase, alloc);
  120. return &ping->pub;
  121. }
  122. void Pinger_pongReceived(String* data, struct Pinger* pinger)
  123. {
  124. if (data->len < 12) {
  125. Log_debug(pinger->logger, "Invalid ping response, too short");
  126. return;
  127. }
  128. uint32_t handle;
  129. Bits_memcpy(&handle, data->bytes, 4);
  130. int index = Map_OutstandingPings_indexForHandle(handle - pinger->baseHandle,
  131. &pinger->outstandingPings);
  132. if (index < 0) {
  133. struct Allocator* alloc = Allocator_child(pinger->allocator);
  134. char* encoded = Hex_print(data->bytes, data->len, alloc);
  135. Log_debug(pinger->logger, "Invalid ping response handle [%u] txid = [%s]",
  136. handle, encoded);
  137. Allocator_free(alloc);
  138. } else {
  139. data->len -= 4;
  140. data->bytes += 4;
  141. uint64_t cookie;
  142. Bits_memcpy(&cookie, data->bytes, 8);
  143. struct Ping* p = Identity_check((struct Ping*) pinger->outstandingPings.values[index]);
  144. if (cookie != p->cookie) {
  145. Log_debug(pinger->logger, "Ping response with invalid cookie");
  146. return;
  147. }
  148. data->len -= 8;
  149. data->bytes += 8;
  150. if (!p->timeSent) {
  151. Log_debug(pinger->logger, "Duplicate response");
  152. return;
  153. }
  154. callback(data, p);
  155. }
  156. }
  157. struct Pinger* Pinger_new(struct EventBase* eventBase,
  158. struct Random* rand,
  159. struct Log* logger,
  160. struct Allocator* alloc)
  161. {
  162. struct Pinger* out = Allocator_clone(alloc, (&(struct Pinger) {
  163. .outstandingPings = {
  164. .allocator = alloc
  165. },
  166. .rand = rand,
  167. .eventBase = eventBase,
  168. .logger = logger,
  169. .allocator = alloc
  170. }));
  171. out->baseHandle = Random_uint32(rand);
  172. return out;
  173. }