SwitchPinger.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/SwitchHeader.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. struct Address* myAddr;
  35. /**
  36. * The label is stored here while the message is sent through the pinger
  37. * and it decides which ping the incoming message belongs to.
  38. */
  39. uint64_t incomingLabel;
  40. /** The version of the node which sent the message. */
  41. uint32_t incomingVersion;
  42. uint8_t incomingKey[32];
  43. /** The error code if an error has been returned (see Error.h) */
  44. int error;
  45. /** Pings which are currently waiting for responses. */
  46. int outstandingPings;
  47. /** Maximum number of pings which can be outstanding at one time. */
  48. int maxConcurrentPings;
  49. Identity
  50. };
  51. struct Ping
  52. {
  53. struct SwitchPinger_Ping pub;
  54. uint64_t label;
  55. String* data;
  56. struct SwitchPinger* context;
  57. SwitchPinger_ResponseCallback onResponse;
  58. void* onResponseContext;
  59. struct Pinger_Ping* pingerPing;
  60. Identity
  61. };
  62. // incoming message from network, pointing to the beginning of the switch header.
  63. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  64. {
  65. struct SwitchPinger* ctx = Identity_check((struct SwitchPinger*) iface->receiverContext);
  66. struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
  67. ctx->incomingLabel = Endian_bigEndianToHost64(switchHeader->label_be);
  68. ctx->incomingVersion = 0;
  69. Message_shift(msg, -SwitchHeader_SIZE, NULL);
  70. uint32_t handle = Message_pop32(msg, NULL);
  71. #ifdef Version_7_COMPAT
  72. if (handle != 0xffffffff) {
  73. Message_push32(msg, handle, NULL);
  74. handle = 0xffffffff;
  75. Assert_true(SwitchHeader_isV7Ctrl(switchHeader));
  76. }
  77. #endif
  78. Assert_true(handle == 0xffffffff);
  79. struct Control* ctrl = (struct Control*) msg->bytes;
  80. if (ctrl->type_be == Control_PONG_be) {
  81. Message_shift(msg, -Control_HEADER_SIZE, NULL);
  82. ctx->error = Error_NONE;
  83. if (msg->length >= Control_Pong_MIN_SIZE) {
  84. struct Control_Ping* pongHeader = (struct Control_Ping*) msg->bytes;
  85. ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
  86. if (pongHeader->magic != Control_Pong_MAGIC) {
  87. Log_debug(ctx->logger, "dropped invalid switch pong");
  88. return Error_INVALID;
  89. }
  90. Message_shift(msg, -Control_Pong_HEADER_SIZE, NULL);
  91. } else {
  92. Log_debug(ctx->logger, "got runt pong message, length: [%d]", msg->length);
  93. return Error_INVALID;
  94. }
  95. } else if (ctrl->type_be == Control_KEYPONG_be) {
  96. Message_shift(msg, -Control_HEADER_SIZE, NULL);
  97. ctx->error = Error_NONE;
  98. if (msg->length >= Control_KeyPong_HEADER_SIZE && msg->length <= Control_KeyPong_MAX_SIZE) {
  99. struct Control_KeyPing* pongHeader = (struct Control_KeyPing*) msg->bytes;
  100. ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
  101. if (pongHeader->magic != Control_KeyPong_MAGIC) {
  102. Log_debug(ctx->logger, "dropped invalid switch key-pong");
  103. return Error_INVALID;
  104. }
  105. Bits_memcpyConst(ctx->incomingKey, pongHeader->key, 32);
  106. Message_shift(msg, -Control_KeyPong_HEADER_SIZE, NULL);
  107. } else if (msg->length > Control_KeyPong_MAX_SIZE) {
  108. Log_debug(ctx->logger, "got overlong key-pong message, length: [%d]", msg->length);
  109. return Error_INVALID;
  110. } else {
  111. Log_debug(ctx->logger, "got runt key-pong message, length: [%d]", msg->length);
  112. return Error_INVALID;
  113. }
  114. } else if (ctrl->type_be == Control_ERROR_be) {
  115. Message_shift(msg, -Control_HEADER_SIZE, NULL);
  116. Assert_true((uint8_t*)&ctrl->content.error.errorType_be == msg->bytes);
  117. if (msg->length < (Control_Error_HEADER_SIZE + SwitchHeader_SIZE + Control_HEADER_SIZE)) {
  118. Log_debug(ctx->logger, "runt error packet");
  119. return Error_NONE;
  120. }
  121. ctx->error = Message_pop32(msg, NULL);
  122. Message_push32(msg, 0, NULL);
  123. Message_shift(msg, -(Control_Error_HEADER_SIZE + SwitchHeader_SIZE), NULL);
  124. struct Control* origCtrl = (struct Control*) msg->bytes;
  125. Log_debug(ctx->logger, "error [%s] was caused by our [%s]",
  126. Error_strerror(ctx->error),
  127. Control_typeString(origCtrl->type_be));
  128. int shift;
  129. if (origCtrl->type_be == Control_PING_be) {
  130. shift = -(Control_HEADER_SIZE + Control_Ping_HEADER_SIZE);
  131. } else if (origCtrl->type_be == Control_KEYPING_be) {
  132. shift = -(Control_HEADER_SIZE + Control_KeyPing_HEADER_SIZE);
  133. } else {
  134. Assert_failure("problem in Ducttape.c");
  135. }
  136. if (msg->length < -shift) {
  137. Log_debug(ctx->logger, "runt error packet");
  138. }
  139. Message_shift(msg, shift, NULL);
  140. } else {
  141. // If it gets here then Ducttape.c is failing.
  142. Assert_true(false);
  143. }
  144. String* msgStr = &(String) { .bytes = (char*) msg->bytes, .len = msg->length };
  145. Pinger_pongReceived(msgStr, ctx->pinger);
  146. Bits_memset(ctx->incomingKey, 0, 32);
  147. return Error_NONE;
  148. }
  149. static void onPingResponse(String* data, uint32_t milliseconds, void* vping)
  150. {
  151. struct Ping* p = Identity_check((struct Ping*) vping);
  152. enum SwitchPinger_Result err = SwitchPinger_Result_OK;
  153. uint64_t label = p->context->incomingLabel;
  154. if (data) {
  155. if (label != p->label) {
  156. err = SwitchPinger_Result_LABEL_MISMATCH;
  157. } else if ((p->data || data->len > 0) && !String_equals(data, p->data)) {
  158. err = SwitchPinger_Result_WRONG_DATA;
  159. } else if (p->context->error == Error_LOOP_ROUTE) {
  160. err = SwitchPinger_Result_LOOP_ROUTE;
  161. } else if (p->context->error) {
  162. err = SwitchPinger_Result_ERROR_RESPONSE;
  163. }
  164. } else {
  165. err = SwitchPinger_Result_TIMEOUT;
  166. }
  167. uint32_t version = p->context->incomingVersion;
  168. struct SwitchPinger_Response* resp =
  169. Allocator_calloc(p->pub.pingAlloc, sizeof(struct SwitchPinger_Response), 1);
  170. resp->version = p->context->incomingVersion;
  171. resp->res = err;
  172. resp->label = label;
  173. resp->data = data;
  174. resp->milliseconds = milliseconds;
  175. resp->version = version;
  176. Bits_memcpyConst(resp->key, p->context->incomingKey, 32);
  177. resp->ping = &p->pub;
  178. p->onResponse(resp, p->pub.onResponseContext);
  179. }
  180. static void sendPing(String* data, void* sendPingContext)
  181. {
  182. struct Ping* p = Identity_check((struct Ping*) sendPingContext);
  183. struct Message* msg = Message_new(0, data->len + 512, p->pub.pingAlloc);
  184. while ((data->len + msg->length) % 8) {
  185. Message_push8(msg, 0, NULL);
  186. }
  187. msg->length = 0;
  188. Message_push(msg, data->bytes, data->len, NULL);
  189. if (p->pub.keyPing) {
  190. Message_shift(msg, Control_KeyPing_HEADER_SIZE, NULL);
  191. struct Control_KeyPing* keyPingHeader = (struct Control_KeyPing*) msg->bytes;
  192. keyPingHeader->magic = Control_KeyPing_MAGIC;
  193. keyPingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  194. Bits_memcpyConst(keyPingHeader->key, p->context->myAddr->key, 32);
  195. } else {
  196. Message_shift(msg, Control_Ping_HEADER_SIZE, NULL);
  197. struct Control_Ping* pingHeader = (struct Control_Ping*) msg->bytes;
  198. pingHeader->magic = Control_Ping_MAGIC;
  199. pingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  200. }
  201. Message_shift(msg, Control_HEADER_SIZE, NULL);
  202. struct Control* ctrl = (struct Control*) msg->bytes;
  203. ctrl->checksum_be = 0;
  204. ctrl->type_be = (p->pub.keyPing) ? Control_KEYPING_be : Control_PING_be;
  205. ctrl->checksum_be = Checksum_engine(msg->bytes, msg->length);
  206. #ifdef Version_7_COMPAT
  207. if (0) {
  208. #endif
  209. Message_push32(msg, 0xffffffff, NULL);
  210. #ifdef Version_7_COMPAT
  211. }
  212. #endif
  213. Message_shift(msg, SwitchHeader_SIZE, NULL);
  214. struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
  215. switchHeader->label_be = Endian_hostToBigEndian64(p->label);
  216. SwitchHeader_setVersion(switchHeader, SwitchHeader_CURRENT_VERSION);
  217. SwitchHeader_setPenalty(switchHeader, 0);
  218. SwitchHeader_setCongestion(switchHeader, 0);
  219. #ifdef Version_7_COMPAT
  220. // v7 detects ctrl packets by the bit which has been
  221. // re-appropriated for suppression of errors.
  222. switchHeader->congestAndSuppressErrors = 1;
  223. SwitchHeader_setVersion(switchHeader, 0);
  224. #endif
  225. p->context->iface->sendMessage(msg, p->context->iface);
  226. }
  227. static String* RESULT_STRING_OK = String_CONST_SO("pong");
  228. static String* RESULT_STRING_LABEL_MISMATCH = String_CONST_SO("diff_label");
  229. static String* RESULT_STRING_WRONG_DATA = String_CONST_SO("diff_data");
  230. static String* RESULT_STRING_ERROR_RESPONSE = String_CONST_SO("err_switch");
  231. static String* RESULT_STRING_TIMEOUT = String_CONST_SO("timeout");
  232. static String* RESULT_STRING_UNKNOWN = String_CONST_SO("err_unknown");
  233. static String* RESULT_STRING_LOOP = String_CONST_SO("err_loop");
  234. String* SwitchPinger_resultString(enum SwitchPinger_Result result)
  235. {
  236. switch (result) {
  237. case SwitchPinger_Result_OK:
  238. return RESULT_STRING_OK;
  239. case SwitchPinger_Result_LABEL_MISMATCH:
  240. return RESULT_STRING_LABEL_MISMATCH;
  241. case SwitchPinger_Result_WRONG_DATA:
  242. return RESULT_STRING_WRONG_DATA;
  243. case SwitchPinger_Result_ERROR_RESPONSE:
  244. return RESULT_STRING_ERROR_RESPONSE;
  245. case SwitchPinger_Result_TIMEOUT:
  246. return RESULT_STRING_TIMEOUT;
  247. case SwitchPinger_Result_LOOP_ROUTE:
  248. return RESULT_STRING_LOOP;
  249. default:
  250. return RESULT_STRING_UNKNOWN;
  251. };
  252. }
  253. static int onPingFree(struct Allocator_OnFreeJob* job)
  254. {
  255. struct Ping* ping = Identity_check((struct Ping*)job->userData);
  256. struct SwitchPinger* ctx = Identity_check(ping->context);
  257. ctx->outstandingPings--;
  258. Assert_true(ctx->outstandingPings >= 0);
  259. return 0;
  260. }
  261. struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
  262. String* data,
  263. uint32_t timeoutMilliseconds,
  264. SwitchPinger_ResponseCallback onResponse,
  265. struct Allocator* alloc,
  266. struct SwitchPinger* ctx)
  267. {
  268. if (data && data->len > Control_Ping_MAX_SIZE) {
  269. return NULL;
  270. }
  271. if (ctx->outstandingPings > ctx->maxConcurrentPings) {
  272. Log_debug(ctx->logger, "Skipping switch ping because there are already [%d] outstanding",
  273. ctx->outstandingPings);
  274. return NULL;
  275. }
  276. struct Pinger_Ping* pp =
  277. Pinger_newPing(data, onPingResponse, sendPing, timeoutMilliseconds, alloc, ctx->pinger);
  278. struct Ping* ping = Allocator_clone(pp->pingAlloc, (&(struct Ping) {
  279. .pub = {
  280. .pingAlloc = pp->pingAlloc
  281. },
  282. .label = label,
  283. .data = String_clone(data, pp->pingAlloc),
  284. .context = ctx,
  285. .onResponse = onResponse,
  286. .pingerPing = pp
  287. }));
  288. Identity_set(ping);
  289. Allocator_onFree(pp->pingAlloc, onPingFree, ping);
  290. pp->context = ping;
  291. ctx->outstandingPings++;
  292. return &ping->pub;
  293. }
  294. struct SwitchPinger* SwitchPinger_new(struct Interface* iface,
  295. struct EventBase* eventBase,
  296. struct Random* rand,
  297. struct Log* logger,
  298. struct Address* myAddr,
  299. struct Allocator* alloc)
  300. {
  301. struct SwitchPinger* sp = Allocator_malloc(alloc, sizeof(struct SwitchPinger));
  302. Bits_memcpyConst(sp, (&(struct SwitchPinger) {
  303. .iface = iface,
  304. .pinger = Pinger_new(eventBase, rand, logger, alloc),
  305. .logger = logger,
  306. .allocator = alloc,
  307. .myAddr = myAddr,
  308. .maxConcurrentPings = SwitchPinger_DEFAULT_MAX_CONCURRENT_PINGS,
  309. }), sizeof(struct SwitchPinger));
  310. iface->receiveMessage = receiveMessage;
  311. iface->receiverContext = sp;
  312. Identity_set(sp);
  313. return sp;
  314. }