1
0

Pinger.c 5.9 KB

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