1
0

SwitchPinger.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "benc/String.h"
  16. #include "net/SwitchPinger.h"
  17. #include "dht/Address.h"
  18. #include "util/Bits.h"
  19. #include "util/Checksum.h"
  20. #include "util/Endian.h"
  21. #include "util/Pinger.h"
  22. #include "util/version/Version.h"
  23. #include "util/Identity.h"
  24. #include "wire/Headers.h"
  25. #include "wire/Control.h"
  26. #include "wire/Error.h"
  27. struct SwitchPinger
  28. {
  29. struct Interface* iface;
  30. struct Pinger* pinger;
  31. struct Admin* admin;
  32. struct Log* logger;
  33. struct Allocator* allocator;
  34. /**
  35. * The label is stored here while the message is sent through the pinger
  36. * and it decides which ping the incoming message belongs to.
  37. */
  38. uint64_t incomingLabel;
  39. /** The version of the node which sent the message. */
  40. uint32_t incomingVersion;
  41. bool isError;
  42. /** Pings which are currently waiting for responses. */
  43. int outstandingPings;
  44. /** Maximum number of pings which can be outstanding at one time. */
  45. int maxConcurrentPings;
  46. Identity
  47. };
  48. struct Ping
  49. {
  50. struct SwitchPinger_Ping public;
  51. uint64_t label;
  52. String* data;
  53. struct SwitchPinger* context;
  54. SwitchPinger_ResponseCallback onResponse;
  55. void* onResponseContext;
  56. struct Pinger_Ping* pingerPing;
  57. Identity
  58. };
  59. // incoming message from network, pointing to the beginning of the switch header.
  60. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  61. {
  62. struct SwitchPinger* ctx = Identity_cast((struct SwitchPinger*) iface->receiverContext);
  63. struct Headers_SwitchHeader* switchHeader = (struct Headers_SwitchHeader*) msg->bytes;
  64. ctx->incomingLabel = Endian_bigEndianToHost64(switchHeader->label_be);
  65. ctx->incomingVersion = 0;
  66. Assert_true(Headers_getMessageType(switchHeader) == Headers_SwitchHeader_TYPE_CONTROL);
  67. Message_shift(msg, -Headers_SwitchHeader_SIZE, NULL);
  68. struct Control* ctrl = (struct Control*) msg->bytes;
  69. if (ctrl->type_be == Control_PONG_be) {
  70. Message_shift(msg, -Control_HEADER_SIZE, NULL);
  71. ctx->isError = false;
  72. struct Control_Ping* pongHeader = (struct Control_Ping*) msg->bytes;
  73. if (msg->length >= Control_Pong_MIN_SIZE) {
  74. ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
  75. Message_shift(msg, -Control_Pong_HEADER_SIZE, NULL);
  76. if (pongHeader->magic != Control_Pong_MAGIC) {
  77. Log_debug(ctx->logger, "dropped invalid switch pong");
  78. return Error_INVALID;
  79. }
  80. } else {
  81. Log_debug(ctx->logger, "got runt pong message, length: [%d]", msg->length);
  82. return Error_INVALID;
  83. }
  84. } else if (ctrl->type_be == Control_ERROR_be) {
  85. Log_debug(ctx->logger, "error was caused by our ping.");
  86. Message_shift(msg, -(
  87. Control_HEADER_SIZE
  88. + Control_Error_HEADER_SIZE
  89. + Headers_SwitchHeader_SIZE
  90. + Control_HEADER_SIZE
  91. + Control_Ping_HEADER_SIZE
  92. ), NULL);
  93. ctx->isError = true;
  94. } else {
  95. // If it gets here then Ducttape.c is failing.
  96. Assert_always(false);
  97. }
  98. String* msgStr = &(String) { .bytes = (char*) msg->bytes, .len = msg->length };
  99. Pinger_pongReceived(msgStr, ctx->pinger);
  100. return Error_NONE;
  101. }
  102. static void onPingResponse(String* data, uint32_t milliseconds, void* vping)
  103. {
  104. struct Ping* p = Identity_cast((struct Ping*) vping);
  105. enum SwitchPinger_Result err = SwitchPinger_Result_OK;
  106. uint64_t label = p->context->incomingLabel;
  107. if (data) {
  108. if (label != p->label) {
  109. err = SwitchPinger_Result_LABEL_MISMATCH;
  110. } else if ((p->data || data->len > 0) && !String_equals(data, p->data)) {
  111. err = SwitchPinger_Result_WRONG_DATA;
  112. } else if (p->context->isError) {
  113. err = SwitchPinger_Result_ERROR_RESPONSE;
  114. }
  115. } else {
  116. err = SwitchPinger_Result_TIMEOUT;
  117. }
  118. uint32_t version = p->context->incomingVersion;
  119. p->onResponse(err, label, data, milliseconds, version, p->public.onResponseContext);
  120. }
  121. static void sendPing(String* data, void* sendPingContext)
  122. {
  123. struct Ping* p = Identity_cast((struct Ping*) sendPingContext);
  124. #define BUFFER_SZ 4096
  125. uint8_t buffer[BUFFER_SZ];
  126. struct Message msg = {
  127. .length = data->len,
  128. // Make it aligned along an 8 byte boundry (assuming the buffer is)
  129. .bytes = &buffer[BUFFER_SZ - (data->len + 8 - (data->len % 8))]
  130. };
  131. msg.padding = msg.bytes - buffer;
  132. Assert_true(data->len < (BUFFER_SZ / 2));
  133. Bits_memcpy(msg.bytes, data->bytes, data->len);
  134. Message_shift(&msg, Control_Ping_HEADER_SIZE, NULL);
  135. struct Control_Ping* pingHeader = (struct Control_Ping*) msg.bytes;
  136. pingHeader->magic = Control_Ping_MAGIC;
  137. pingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  138. Message_shift(&msg, Control_HEADER_SIZE, NULL);
  139. struct Control* ctrl = (struct Control*) msg.bytes;
  140. ctrl->checksum_be = 0;
  141. ctrl->type_be = Control_PING_be;
  142. ctrl->checksum_be = Checksum_engine(msg.bytes, msg.length);
  143. Message_shift(&msg, Headers_SwitchHeader_SIZE, NULL);
  144. struct Headers_SwitchHeader* switchHeader = (struct Headers_SwitchHeader*) msg.bytes;
  145. switchHeader->label_be = Endian_hostToBigEndian64(p->label);
  146. Headers_setPriorityAndMessageType(switchHeader, 0, Headers_SwitchHeader_TYPE_CONTROL);
  147. p->context->iface->sendMessage(&msg, p->context->iface);
  148. }
  149. #define BSTR_SIZEOF(x) &(String) { .bytes = x, .len = sizeof(x) - 1 }
  150. static String* RESULT_STRING_OK = BSTR_SIZEOF("pong");
  151. static String* RESULT_STRING_LABEL_MISMATCH = BSTR_SIZEOF("pong has different label");
  152. static String* RESULT_STRING_WRONG_DATA = BSTR_SIZEOF("data is different");
  153. static String* RESULT_STRING_ERROR_RESPONSE = BSTR_SIZEOF("ping message caused switch error");
  154. static String* RESULT_STRING_TIMEOUT = BSTR_SIZEOF("timeout");
  155. static String* RESULT_STRING_UNKNOWN = BSTR_SIZEOF("unknown error");
  156. String* SwitchPinger_resultString(enum SwitchPinger_Result result)
  157. {
  158. switch (result) {
  159. case SwitchPinger_Result_OK:
  160. return RESULT_STRING_OK;
  161. case SwitchPinger_Result_LABEL_MISMATCH:
  162. return RESULT_STRING_LABEL_MISMATCH;
  163. case SwitchPinger_Result_WRONG_DATA:
  164. return RESULT_STRING_WRONG_DATA;
  165. case SwitchPinger_Result_ERROR_RESPONSE:
  166. return RESULT_STRING_ERROR_RESPONSE;
  167. case SwitchPinger_Result_TIMEOUT:
  168. return RESULT_STRING_TIMEOUT;
  169. default:
  170. return RESULT_STRING_UNKNOWN;
  171. };
  172. }
  173. static int onPingFree(struct Allocator_OnFreeJob* job)
  174. {
  175. struct Ping* ping = Identity_cast((struct Ping*)job->userData);
  176. struct SwitchPinger* ctx = Identity_cast(ping->context);
  177. ctx->outstandingPings--;
  178. Assert_true(ctx->outstandingPings >= 0);
  179. return 0;
  180. }
  181. struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
  182. String* data,
  183. uint32_t timeoutMilliseconds,
  184. SwitchPinger_ResponseCallback onResponse,
  185. struct Allocator* alloc,
  186. struct SwitchPinger* ctx)
  187. {
  188. if (data && data->len > Control_Ping_MAX_SIZE) {
  189. return NULL;
  190. }
  191. if (ctx->outstandingPings > ctx->maxConcurrentPings) {
  192. Log_debug(ctx->logger, "Skipping switch ping because there are already [%d] outstanding",
  193. ctx->outstandingPings);
  194. return NULL;
  195. }
  196. struct Pinger_Ping* pp =
  197. Pinger_newPing(data, onPingResponse, sendPing, timeoutMilliseconds, alloc, ctx->pinger);
  198. struct Ping* ping = Allocator_clone(pp->pingAlloc, (&(struct Ping) {
  199. .public = {
  200. .pingAlloc = pp->pingAlloc
  201. },
  202. .label = label,
  203. .data = String_clone(data, pp->pingAlloc),
  204. .context = ctx,
  205. .onResponse = onResponse,
  206. .pingerPing = pp
  207. }));
  208. Identity_set(ping);
  209. Allocator_onFree(pp->pingAlloc, onPingFree, ping);
  210. pp->context = ping;
  211. ctx->outstandingPings++;
  212. return &ping->public;
  213. }
  214. void SwitchPinger_sendPing(struct SwitchPinger_Ping* ping)
  215. {
  216. struct Ping* p = Identity_cast((struct Ping*) ping);
  217. Pinger_sendPing(p->pingerPing);
  218. }
  219. struct SwitchPinger* SwitchPinger_new(struct Interface* iface,
  220. struct EventBase* eventBase,
  221. struct Random* rand,
  222. struct Log* logger,
  223. struct Allocator* alloc)
  224. {
  225. struct SwitchPinger* sp = Allocator_malloc(alloc, sizeof(struct SwitchPinger));
  226. Bits_memcpyConst(sp, (&(struct SwitchPinger) {
  227. .iface = iface,
  228. .pinger = Pinger_new(eventBase, rand, logger, alloc),
  229. .logger = logger,
  230. .allocator = alloc,
  231. .maxConcurrentPings = SwitchPinger_DEFAULT_MAX_CONCURRENT_PINGS,
  232. }), sizeof(struct SwitchPinger));
  233. iface->receiveMessage = receiveMessage;
  234. iface->receiverContext = sp;
  235. Identity_set(sp);
  236. return sp;
  237. }