Pinger.c 6.1 KB

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