SwitchPinger.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. Identity
  43. };
  44. struct Ping
  45. {
  46. struct SwitchPinger_Ping public;
  47. uint64_t label;
  48. String* data;
  49. struct SwitchPinger* context;
  50. SwitchPinger_ResponseCallback onResponse;
  51. void* onResponseContext;
  52. struct Pinger_Ping* pingerPing;
  53. Identity
  54. };
  55. // incoming message from network, pointing to the beginning of the switch header.
  56. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  57. {
  58. struct SwitchPinger* ctx = Identity_cast((struct SwitchPinger*) iface->receiverContext);
  59. struct Headers_SwitchHeader* switchHeader = (struct Headers_SwitchHeader*) msg->bytes;
  60. ctx->incomingLabel = Endian_bigEndianToHost64(switchHeader->label_be);
  61. ctx->incomingVersion = 0;
  62. Assert_true(Headers_getMessageType(switchHeader) == Headers_SwitchHeader_TYPE_CONTROL);
  63. Message_shift(msg, -Headers_SwitchHeader_SIZE);
  64. struct Control* ctrl = (struct Control*) msg->bytes;
  65. if (ctrl->type_be == Control_PONG_be) {
  66. Message_shift(msg, -Control_HEADER_SIZE);
  67. ctx->isError = false;
  68. struct Control_Ping* pongHeader = (struct Control_Ping*) msg->bytes;
  69. if (msg->length >= Control_Pong_MIN_SIZE) {
  70. ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
  71. Message_shift(msg, -Control_Pong_HEADER_SIZE);
  72. if (pongHeader->magic != Control_Pong_MAGIC) {
  73. Log_debug(ctx->logger, "dropped invalid switch pong");
  74. return Error_INVALID;
  75. }
  76. } else {
  77. Log_debug(ctx->logger, "got runt pong message, length: [%d]", msg->length);
  78. return Error_INVALID;
  79. }
  80. } else if (ctrl->type_be == Control_ERROR_be) {
  81. Log_debug(ctx->logger, "error was caused by our ping.");
  82. Message_shift(msg, -(
  83. Control_HEADER_SIZE
  84. + Control_Error_HEADER_SIZE
  85. + Headers_SwitchHeader_SIZE
  86. + Control_HEADER_SIZE
  87. + Control_Ping_HEADER_SIZE
  88. ));
  89. ctx->isError = true;
  90. } else {
  91. // If it gets here then Ducttape.c is failing.
  92. Assert_always(false);
  93. }
  94. String* msgStr = &(String) { .bytes = (char*) msg->bytes, .len = msg->length };
  95. Pinger_pongReceived(msgStr, ctx->pinger);
  96. return Error_NONE;
  97. }
  98. static void onPingResponse(String* data, uint32_t milliseconds, void* vping)
  99. {
  100. struct Ping* p = Identity_cast((struct Ping*) vping);
  101. enum SwitchPinger_Result err = SwitchPinger_Result_OK;
  102. uint64_t label = p->context->incomingLabel;
  103. if (data) {
  104. if (label != p->label) {
  105. err = SwitchPinger_Result_LABEL_MISMATCH;
  106. } else if ((p->data || data->len > 0) && !String_equals(data, p->data)) {
  107. err = SwitchPinger_Result_WRONG_DATA;
  108. } else if (p->context->isError) {
  109. err = SwitchPinger_Result_ERROR_RESPONSE;
  110. }
  111. } else {
  112. err = SwitchPinger_Result_TIMEOUT;
  113. }
  114. uint32_t version = p->context->incomingVersion;
  115. p->onResponse(err, label, data, milliseconds, version, p->public.onResponseContext);
  116. }
  117. static void sendPing(String* data, void* sendPingContext)
  118. {
  119. struct Ping* p = Identity_cast((struct Ping*) sendPingContext);
  120. #define BUFFER_SZ 4096
  121. uint8_t buffer[BUFFER_SZ];
  122. struct Message msg = {
  123. .length = data->len,
  124. // Make it aligned along an 8 byte boundry (assuming the buffer is)
  125. .bytes = &buffer[BUFFER_SZ - (data->len + 8 - (data->len % 8))]
  126. };
  127. msg.padding = msg.bytes - buffer;
  128. Assert_true(data->len < (BUFFER_SZ / 2));
  129. Bits_memcpy(msg.bytes, data->bytes, data->len);
  130. Message_shift(&msg, Control_Ping_HEADER_SIZE);
  131. struct Control_Ping* pingHeader = (struct Control_Ping*) msg.bytes;
  132. pingHeader->magic = Control_Ping_MAGIC;
  133. pingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  134. Message_shift(&msg, Control_HEADER_SIZE);
  135. struct Control* ctrl = (struct Control*) msg.bytes;
  136. ctrl->checksum_be = 0;
  137. ctrl->type_be = Control_PING_be;
  138. ctrl->checksum_be = Checksum_engine(msg.bytes, msg.length);
  139. Message_shift(&msg, Headers_SwitchHeader_SIZE);
  140. struct Headers_SwitchHeader* switchHeader = (struct Headers_SwitchHeader*) msg.bytes;
  141. switchHeader->label_be = Endian_hostToBigEndian64(p->label);
  142. Headers_setPriorityAndMessageType(switchHeader, 0, Headers_SwitchHeader_TYPE_CONTROL);
  143. p->context->iface->sendMessage(&msg, p->context->iface);
  144. }
  145. #define BSTR_SIZEOF(x) &(String) { .bytes = x, .len = sizeof(x) - 1 }
  146. static String* RESULT_STRING_OK = BSTR_SIZEOF("pong");
  147. static String* RESULT_STRING_LABEL_MISMATCH = BSTR_SIZEOF("pong has different label");
  148. static String* RESULT_STRING_WRONG_DATA = BSTR_SIZEOF("data is different");
  149. static String* RESULT_STRING_ERROR_RESPONSE = BSTR_SIZEOF("ping message caused switch error");
  150. static String* RESULT_STRING_TIMEOUT = BSTR_SIZEOF("timeout");
  151. static String* RESULT_STRING_UNKNOWN = BSTR_SIZEOF("unknown error");
  152. String* SwitchPinger_resultString(enum SwitchPinger_Result result)
  153. {
  154. switch (result) {
  155. case SwitchPinger_Result_OK:
  156. return RESULT_STRING_OK;
  157. case SwitchPinger_Result_LABEL_MISMATCH:
  158. return RESULT_STRING_LABEL_MISMATCH;
  159. case SwitchPinger_Result_WRONG_DATA:
  160. return RESULT_STRING_WRONG_DATA;
  161. case SwitchPinger_Result_ERROR_RESPONSE:
  162. return RESULT_STRING_ERROR_RESPONSE;
  163. case SwitchPinger_Result_TIMEOUT:
  164. return RESULT_STRING_TIMEOUT;
  165. default:
  166. return RESULT_STRING_UNKNOWN;
  167. };
  168. }
  169. struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
  170. String* data,
  171. uint32_t timeoutMilliseconds,
  172. SwitchPinger_ResponseCallback onResponse,
  173. struct Allocator* alloc,
  174. struct SwitchPinger* ctx)
  175. {
  176. if (data && data->len > Control_Ping_MAX_SIZE) {
  177. return NULL;
  178. }
  179. struct Pinger_Ping* pp =
  180. Pinger_newPing(data, onPingResponse, sendPing, timeoutMilliseconds, alloc, ctx->pinger);
  181. if (!pp) {
  182. return NULL;
  183. }
  184. struct Ping* ping = Allocator_clone(pp->pingAlloc, (&(struct Ping) {
  185. .public = {
  186. .pingAlloc = pp->pingAlloc
  187. },
  188. .label = label,
  189. .data = String_clone(data, pp->pingAlloc),
  190. .context = ctx,
  191. .onResponse = onResponse,
  192. .pingerPing = pp
  193. }));
  194. Identity_set(ping);
  195. pp->context = ping;
  196. return &ping->public;
  197. }
  198. void SwitchPinger_sendPing(struct SwitchPinger_Ping* ping)
  199. {
  200. struct Ping* p = Identity_cast((struct Ping*) ping);
  201. Pinger_sendPing(p->pingerPing);
  202. }
  203. struct SwitchPinger* SwitchPinger_new(struct Interface* iface,
  204. struct EventBase* eventBase,
  205. struct Random* rand,
  206. struct Log* logger,
  207. struct Allocator* alloc)
  208. {
  209. struct SwitchPinger* sp = Allocator_malloc(alloc, sizeof(struct SwitchPinger));
  210. Bits_memcpyConst(sp, (&(struct SwitchPinger) {
  211. .iface = iface,
  212. .pinger = Pinger_new(eventBase, rand, logger, alloc),
  213. .logger = logger,
  214. .allocator = alloc
  215. }), sizeof(struct SwitchPinger));
  216. iface->receiveMessage = receiveMessage;
  217. iface->receiverContext = sp;
  218. Identity_set(sp);
  219. return sp;
  220. }