123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /* vim: set expandtab ts=4 sw=4: */
- /*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "benc/String.h"
- #include "net/SwitchPinger.h"
- #include "dht/Address.h"
- #include "util/Bits.h"
- #include "util/Checksum.h"
- #include "util/Endian.h"
- #include "util/Pinger.h"
- #include "util/version/Version.h"
- #include "util/Identity.h"
- #include "wire/SwitchHeader.h"
- #include "wire/Control.h"
- #include "wire/Error.h"
- struct SwitchPinger
- {
- struct Interface* iface;
- struct Pinger* pinger;
- struct Admin* admin;
- struct Log* logger;
- struct Allocator* allocator;
- struct Address* myAddr;
- /**
- * The label is stored here while the message is sent through the pinger
- * and it decides which ping the incoming message belongs to.
- */
- uint64_t incomingLabel;
- /** The version of the node which sent the message. */
- uint32_t incomingVersion;
- uint8_t incomingKey[32];
- /** The error code if an error has been returned (see Error.h) */
- int error;
- /** Pings which are currently waiting for responses. */
- int outstandingPings;
- /** Maximum number of pings which can be outstanding at one time. */
- int maxConcurrentPings;
- Identity
- };
- struct Ping
- {
- struct SwitchPinger_Ping pub;
- uint64_t label;
- String* data;
- struct SwitchPinger* context;
- SwitchPinger_ResponseCallback onResponse;
- void* onResponseContext;
- struct Pinger_Ping* pingerPing;
- Identity
- };
- // incoming message from network, pointing to the beginning of the switch header.
- static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
- {
- struct SwitchPinger* ctx = Identity_check((struct SwitchPinger*) iface->receiverContext);
- struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
- ctx->incomingLabel = Endian_bigEndianToHost64(switchHeader->label_be);
- ctx->incomingVersion = 0;
- Message_shift(msg, -SwitchHeader_SIZE, NULL);
- uint32_t handle = Message_pop32(msg, NULL);
- #ifdef Version_7_COMPAT
- if (handle != 0xffffffff) {
- Message_push32(msg, handle, NULL);
- handle = 0xffffffff;
- Assert_true(SwitchHeader_isV7Ctrl(switchHeader));
- }
- #endif
- Assert_true(handle == 0xffffffff);
- struct Control* ctrl = (struct Control*) msg->bytes;
- if (ctrl->type_be == Control_PONG_be) {
- Message_shift(msg, -Control_HEADER_SIZE, NULL);
- ctx->error = Error_NONE;
- if (msg->length >= Control_Pong_MIN_SIZE) {
- struct Control_Ping* pongHeader = (struct Control_Ping*) msg->bytes;
- ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
- if (pongHeader->magic != Control_Pong_MAGIC) {
- Log_debug(ctx->logger, "dropped invalid switch pong");
- return Error_INVALID;
- }
- Message_shift(msg, -Control_Pong_HEADER_SIZE, NULL);
- } else {
- Log_debug(ctx->logger, "got runt pong message, length: [%d]", msg->length);
- return Error_INVALID;
- }
- } else if (ctrl->type_be == Control_KEYPONG_be) {
- Message_shift(msg, -Control_HEADER_SIZE, NULL);
- ctx->error = Error_NONE;
- if (msg->length >= Control_KeyPong_HEADER_SIZE && msg->length <= Control_KeyPong_MAX_SIZE) {
- struct Control_KeyPing* pongHeader = (struct Control_KeyPing*) msg->bytes;
- ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
- if (pongHeader->magic != Control_KeyPong_MAGIC) {
- Log_debug(ctx->logger, "dropped invalid switch key-pong");
- return Error_INVALID;
- }
- Bits_memcpyConst(ctx->incomingKey, pongHeader->key, 32);
- Message_shift(msg, -Control_KeyPong_HEADER_SIZE, NULL);
- } else if (msg->length > Control_KeyPong_MAX_SIZE) {
- Log_debug(ctx->logger, "got overlong key-pong message, length: [%d]", msg->length);
- return Error_INVALID;
- } else {
- Log_debug(ctx->logger, "got runt key-pong message, length: [%d]", msg->length);
- return Error_INVALID;
- }
- } else if (ctrl->type_be == Control_ERROR_be) {
- Message_shift(msg, -Control_HEADER_SIZE, NULL);
- Assert_true((uint8_t*)&ctrl->content.error.errorType_be == msg->bytes);
- if (msg->length < (Control_Error_HEADER_SIZE + SwitchHeader_SIZE + Control_HEADER_SIZE)) {
- Log_debug(ctx->logger, "runt error packet");
- return Error_NONE;
- }
- ctx->error = Message_pop32(msg, NULL);
- Message_push32(msg, 0, NULL);
- Message_shift(msg, -(Control_Error_HEADER_SIZE + SwitchHeader_SIZE), NULL);
- struct Control* origCtrl = (struct Control*) msg->bytes;
- Log_debug(ctx->logger, "error [%s] was caused by our [%s]",
- Error_strerror(ctx->error),
- Control_typeString(origCtrl->type_be));
- int shift;
- if (origCtrl->type_be == Control_PING_be) {
- shift = -(Control_HEADER_SIZE + Control_Ping_HEADER_SIZE);
- } else if (origCtrl->type_be == Control_KEYPING_be) {
- shift = -(Control_HEADER_SIZE + Control_KeyPing_HEADER_SIZE);
- } else {
- Assert_failure("problem in Ducttape.c");
- }
- if (msg->length < -shift) {
- Log_debug(ctx->logger, "runt error packet");
- }
- Message_shift(msg, shift, NULL);
- } else {
- // If it gets here then Ducttape.c is failing.
- Assert_true(false);
- }
- String* msgStr = &(String) { .bytes = (char*) msg->bytes, .len = msg->length };
- Pinger_pongReceived(msgStr, ctx->pinger);
- Bits_memset(ctx->incomingKey, 0, 32);
- return Error_NONE;
- }
- static void onPingResponse(String* data, uint32_t milliseconds, void* vping)
- {
- struct Ping* p = Identity_check((struct Ping*) vping);
- enum SwitchPinger_Result err = SwitchPinger_Result_OK;
- uint64_t label = p->context->incomingLabel;
- if (data) {
- if (label != p->label) {
- err = SwitchPinger_Result_LABEL_MISMATCH;
- } else if ((p->data || data->len > 0) && !String_equals(data, p->data)) {
- err = SwitchPinger_Result_WRONG_DATA;
- } else if (p->context->error == Error_LOOP_ROUTE) {
- err = SwitchPinger_Result_LOOP_ROUTE;
- } else if (p->context->error) {
- err = SwitchPinger_Result_ERROR_RESPONSE;
- }
- } else {
- err = SwitchPinger_Result_TIMEOUT;
- }
- uint32_t version = p->context->incomingVersion;
- struct SwitchPinger_Response* resp =
- Allocator_calloc(p->pub.pingAlloc, sizeof(struct SwitchPinger_Response), 1);
- resp->version = p->context->incomingVersion;
- resp->res = err;
- resp->label = label;
- resp->data = data;
- resp->milliseconds = milliseconds;
- resp->version = version;
- Bits_memcpyConst(resp->key, p->context->incomingKey, 32);
- resp->ping = &p->pub;
- p->onResponse(resp, p->pub.onResponseContext);
- }
- static void sendPing(String* data, void* sendPingContext)
- {
- struct Ping* p = Identity_check((struct Ping*) sendPingContext);
- struct Message* msg = Message_new(0, data->len + 512, p->pub.pingAlloc);
- while ((data->len + msg->length) % 8) {
- Message_push8(msg, 0, NULL);
- }
- msg->length = 0;
- Message_push(msg, data->bytes, data->len, NULL);
- if (p->pub.keyPing) {
- Message_shift(msg, Control_KeyPing_HEADER_SIZE, NULL);
- struct Control_KeyPing* keyPingHeader = (struct Control_KeyPing*) msg->bytes;
- keyPingHeader->magic = Control_KeyPing_MAGIC;
- keyPingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
- Bits_memcpyConst(keyPingHeader->key, p->context->myAddr->key, 32);
- } else {
- Message_shift(msg, Control_Ping_HEADER_SIZE, NULL);
- struct Control_Ping* pingHeader = (struct Control_Ping*) msg->bytes;
- pingHeader->magic = Control_Ping_MAGIC;
- pingHeader->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
- }
- Message_shift(msg, Control_HEADER_SIZE, NULL);
- struct Control* ctrl = (struct Control*) msg->bytes;
- ctrl->checksum_be = 0;
- ctrl->type_be = (p->pub.keyPing) ? Control_KEYPING_be : Control_PING_be;
- ctrl->checksum_be = Checksum_engine(msg->bytes, msg->length);
- #ifdef Version_7_COMPAT
- if (0) {
- #endif
- Message_push32(msg, 0xffffffff, NULL);
- #ifdef Version_7_COMPAT
- }
- #endif
- Message_shift(msg, SwitchHeader_SIZE, NULL);
- struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
- switchHeader->label_be = Endian_hostToBigEndian64(p->label);
- SwitchHeader_setVersion(switchHeader, SwitchHeader_CURRENT_VERSION);
- SwitchHeader_setPenalty(switchHeader, 0);
- SwitchHeader_setCongestion(switchHeader, 0);
- #ifdef Version_7_COMPAT
- // v7 detects ctrl packets by the bit which has been
- // re-appropriated for suppression of errors.
- switchHeader->congestAndSuppressErrors = 1;
- SwitchHeader_setVersion(switchHeader, 0);
- #endif
- p->context->iface->sendMessage(msg, p->context->iface);
- }
- static String* RESULT_STRING_OK = String_CONST_SO("pong");
- static String* RESULT_STRING_LABEL_MISMATCH = String_CONST_SO("diff_label");
- static String* RESULT_STRING_WRONG_DATA = String_CONST_SO("diff_data");
- static String* RESULT_STRING_ERROR_RESPONSE = String_CONST_SO("err_switch");
- static String* RESULT_STRING_TIMEOUT = String_CONST_SO("timeout");
- static String* RESULT_STRING_UNKNOWN = String_CONST_SO("err_unknown");
- static String* RESULT_STRING_LOOP = String_CONST_SO("err_loop");
- String* SwitchPinger_resultString(enum SwitchPinger_Result result)
- {
- switch (result) {
- case SwitchPinger_Result_OK:
- return RESULT_STRING_OK;
- case SwitchPinger_Result_LABEL_MISMATCH:
- return RESULT_STRING_LABEL_MISMATCH;
- case SwitchPinger_Result_WRONG_DATA:
- return RESULT_STRING_WRONG_DATA;
- case SwitchPinger_Result_ERROR_RESPONSE:
- return RESULT_STRING_ERROR_RESPONSE;
- case SwitchPinger_Result_TIMEOUT:
- return RESULT_STRING_TIMEOUT;
- case SwitchPinger_Result_LOOP_ROUTE:
- return RESULT_STRING_LOOP;
- default:
- return RESULT_STRING_UNKNOWN;
- };
- }
- static int onPingFree(struct Allocator_OnFreeJob* job)
- {
- struct Ping* ping = Identity_check((struct Ping*)job->userData);
- struct SwitchPinger* ctx = Identity_check(ping->context);
- ctx->outstandingPings--;
- Assert_true(ctx->outstandingPings >= 0);
- return 0;
- }
- struct SwitchPinger_Ping* SwitchPinger_newPing(uint64_t label,
- String* data,
- uint32_t timeoutMilliseconds,
- SwitchPinger_ResponseCallback onResponse,
- struct Allocator* alloc,
- struct SwitchPinger* ctx)
- {
- if (data && data->len > Control_Ping_MAX_SIZE) {
- return NULL;
- }
- if (ctx->outstandingPings > ctx->maxConcurrentPings) {
- Log_debug(ctx->logger, "Skipping switch ping because there are already [%d] outstanding",
- ctx->outstandingPings);
- return NULL;
- }
- struct Pinger_Ping* pp =
- Pinger_newPing(data, onPingResponse, sendPing, timeoutMilliseconds, alloc, ctx->pinger);
- struct Ping* ping = Allocator_clone(pp->pingAlloc, (&(struct Ping) {
- .pub = {
- .pingAlloc = pp->pingAlloc
- },
- .label = label,
- .data = String_clone(data, pp->pingAlloc),
- .context = ctx,
- .onResponse = onResponse,
- .pingerPing = pp
- }));
- Identity_set(ping);
- Allocator_onFree(pp->pingAlloc, onPingFree, ping);
- pp->context = ping;
- ctx->outstandingPings++;
- return &ping->pub;
- }
- struct SwitchPinger* SwitchPinger_new(struct Interface* iface,
- struct EventBase* eventBase,
- struct Random* rand,
- struct Log* logger,
- struct Address* myAddr,
- struct Allocator* alloc)
- {
- struct SwitchPinger* sp = Allocator_malloc(alloc, sizeof(struct SwitchPinger));
- Bits_memcpyConst(sp, (&(struct SwitchPinger) {
- .iface = iface,
- .pinger = Pinger_new(eventBase, rand, logger, alloc),
- .logger = logger,
- .allocator = alloc,
- .myAddr = myAddr,
- .maxConcurrentPings = SwitchPinger_DEFAULT_MAX_CONCURRENT_PINGS,
- }), sizeof(struct SwitchPinger));
- iface->receiveMessage = receiveMessage;
- iface->receiverContext = sp;
- Identity_set(sp);
- return sp;
- }
|