Pinger.c 6.4 KB

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