From fbbff772ebfa2082783a203b762684ee2ac650c3 Mon Sep 17 00:00:00 2001 From: cjdelisle Date: Wed, 21 Dec 2011 10:02:23 -0500 Subject: [PATCH] more debugging --- cjdroute.c | 2 +- dht/Ducttape.c | 18 +++++++++--------- dht/dhtcore/NodeStore.c | 17 +++++++++++++++++ dht/dhtcore/RouterModule.c | 12 +++++------- switch/SwitchCore.c | 13 ++++++------- wire/Headers.h | 32 ++++++++------------------------ wire/MessageType.h | 9 ++------- 7 files changed, 48 insertions(+), 55 deletions(-) diff --git a/cjdroute.c b/cjdroute.c index 1b8770ace..789dbaaf2 100644 --- a/cjdroute.c +++ b/cjdroute.c @@ -338,7 +338,7 @@ static void serverFirstIncoming(struct Message* msg, struct Interface* iface) struct User* u = CryptoAuth_getUser(iface); assert(u); - // Add it to the switch. + // Add it to the switch, this will change the receiveMessage for this interface. uint64_t discard; SwitchCore_addInterface(iface, u->trust, &discard, uictx->context->switchCore); diff --git a/dht/Ducttape.c b/dht/Ducttape.c index 70f1b72a6..c03d6a7d7 100644 --- a/dht/Ducttape.c +++ b/dht/Ducttape.c @@ -105,7 +105,7 @@ static inline void incomingDHT(struct Message* message, struct Context* context) { -printf(">>> "); +printf(">.> "); //printMessage(message); printf("\n"); @@ -133,7 +133,7 @@ static int handleOutgoing(struct DHTMessage* dmessage, struct Message message = { .length = dmessage->length, .bytes = (uint8_t*) dmessage->bytes, .padding = 512 }; -printf("<<< "); +printf("<.< "); //printMessage(&message); printf("\n"); @@ -212,7 +212,7 @@ static void incomingForMe(struct Message* message, struct Interface* iface) char* magic = " !\"#$%&'()*+,-./01234567"; char* ptr = memmem(message->bytes, message->length, magic, strlen(magic)); if (ptr) { - printf("It looks like this packet contains a ping! offset=%u\n", (uint32_t)(ptr - (char*)message->bytes)); + printf("Incoming packet contains a ping! offset=%u\n", (uint32_t)(ptr - (char*)message->bytes)); } context->ip6Header->payloadLength_be = Endian_hostToBigEndian16( @@ -242,6 +242,7 @@ static inline uint8_t sendToSwitch(struct Message* message, if (destinationSwitchHeader != switchHeaderLocation) { memmove(message->bytes, destinationSwitchHeader, Headers_SwitchHeader_SIZE); } + context->switchInterface.receiveMessage(message, &context->switchInterface); return 0; @@ -384,7 +385,7 @@ static inline void ip6FromTun(struct Message* message, char* magic = " !\"#$%&'()*+,-./01234567"; char* ptr = memmem(message->bytes, message->length, magic, strlen(magic)); if (ptr) { - printf("It looks like this packet contains a ping! offset=%u\n", (uint32_t)(ptr - (char*)message->bytes)); + printf("Outgoing packet contains a ping! offset=%u\n", (uint32_t)(ptr - (char*)message->bytes)); } struct Context* context = (struct Context*) interface->receiverContext; @@ -519,6 +520,10 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi (uint32_t*) herKey, (uint32_t*) context->myAddr.key)); //printf("nonce %u\n", nonce); + + // The address is extracted from the switch header later. + context->switchHeader = switchHeader; + if (nonce > 4 && node && node->session.exists) { Message_shift(message, -4); decrypt(nonce, message, node->session.sharedSecret, node->session.isInitiator); @@ -536,11 +541,6 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi // it's nolonger null then the message is valid :/ context->messageFromCryptoAuth = NULL; - // If the message causes CryptoAuth to want to send a response, it will call sendToSwitch - // and sendToSwitch (as well as a bunch of other stuff) relies on the switchHeader being in - // the context. - context->switchHeader = switchHeader; - iface->receiveMessage(message, iface); if (context->messageFromCryptoAuth) { diff --git a/dht/dhtcore/NodeStore.c b/dht/dhtcore/NodeStore.c index 8a4422c31..de077f59e 100644 --- a/dht/dhtcore/NodeStore.c +++ b/dht/dhtcore/NodeStore.c @@ -79,6 +79,20 @@ void NodeStore_addNode(struct NodeStore* store, struct Address* addr, const int64_t reachDifference) { +Address_getPrefix(addr); +if (memcmp(addr->ip6.bytes, store->thisNodeAddress, 16) == 0) { + printf("got introduced to ourselves\n"); + return; +} + +uint8_t nodeAddr[40]; +Address_printIp(nodeAddr, addr); +uint8_t netAddr[20]; +Address_printNetworkAddress(netAddr, addr); +if (netAddr[0] != '0') { + printf("This address is probably bogus!\n"); +} + // TODO: maintain a sorted list. uint32_t pfx = Address_getPrefix(addr); @@ -92,6 +106,9 @@ void NodeStore_addNode(struct NodeStore* store, return; } } + +printf("Discovered node: %s at addr %s\n", nodeAddr, netAddr); + // Free space, regular insert. replaceNode(&store->nodes[store->size], &store->headers[store->size], addr); adjustReach(&store->headers[store->size], reachDifference); diff --git a/dht/dhtcore/RouterModule.c b/dht/dhtcore/RouterModule.c index e473e3580..2a0dd3ef9 100644 --- a/dht/dhtcore/RouterModule.c +++ b/dht/dhtcore/RouterModule.c @@ -620,15 +620,13 @@ static inline int handleReply(struct DHTMessage* message, struct RouterModule* m if ((thisNodePrefix ^ targetPrefix) >= parentDistance && xorCompare(&scc->targetAddress, &addr, parent->address) >= 0) { -uint8_t nodeAddr[40]; -Address_printIp(nodeAddr, parent->address); -printf("dropped answer pointing to %s because it is further from the target " - "than the node who gave it to us\n", nodeAddr); +printf("dropped answer because it is further from the target " + "than the node who gave it to us\n"); } else if (thisNodePrefix == ourAddressPrefix - && memcmp(module->address.key, addr.key, Address_NETWORK_ADDR_SIZE) == 0) + && memcmp(module->address.ip6.bytes, addr.ip6.bytes, Address_SEARCH_TARGET_SIZE) == 0) { //printf("Dropping answer because it is our own node.\n"); - // They just told us about ourselves :/ + // They just told us about ourselves. } else { SearchStore_addNodeToSearch(parent, &addr, evictTime, search); } @@ -674,7 +672,7 @@ static inline int handleQuery(struct DHTMessage* message, struct DHTMessage* query = message->replyTo; // We got a query, the reach should be set to 1 in the new node. - NodeStore_addNode(module->nodeStore, message->address, 1); + NodeStore_addNode(module->nodeStore, query->address, 1); // get the target String* target = benc_lookupString(query->asDict, CJDHTConstants_TARGET); diff --git a/switch/SwitchCore.c b/switch/SwitchCore.c index 9038d6e5a..1e6ed74b3 100644 --- a/switch/SwitchCore.c +++ b/switch/SwitchCore.c @@ -82,7 +82,7 @@ static inline void sendError(struct SwitchInterface* interface, uint16_t code) { struct Headers_SwitchHeader* header = (struct Headers_SwitchHeader*) cause->bytes; - if (Headers_getMessageType(header) == MessageType_ERROR) { + if (Headers_getMessageType(header) == MessageType_CONTROL) { // Errors never cause other errors to be sent. return; } @@ -94,18 +94,17 @@ static inline void sendError(struct SwitchInterface* interface, memcpy(err->error.cause.bytes, cause->bytes, errLength); err->switchHeader.label_be = Bits_bitReverse64(header->label_be); - Headers_setPriorityFragmentNumAndMessageType(&err->switchHeader, + Headers_setPriorityAndMessageType(&err->switchHeader, Headers_getPriority(header), - 0, - MessageType_ERROR); + MessageType_CONTROL); err->error.errorType_be = Endian_hostToBigEndian16(code); err->error.length = errLength; cause->length = sizeof(struct ErrorPacket) - (255 - errLength); sendMessage(interface, cause); } - - +#include +#include "dht/Address.h" void receiveMessage(struct Message* message, struct Interface* iface) { struct SwitchInterface* sourceIf = (struct SwitchInterface*) iface->receiverContext; @@ -142,7 +141,7 @@ void receiveMessage(struct Message* message, struct Interface* iface) // If this happens to be an Error_FLOOD packet, we will react by // increasing the congestion for the source interface to make flooding harder. - if (Headers_getMessageType(header) == MessageType_ERROR + if (Headers_getMessageType(header) == MessageType_CONTROL && ((struct ErrorPacket*) header)->error.errorType_be == ntohs(Error_FLOOD)) { sourceIf->congestion += Headers_getPriority(header); diff --git a/wire/Headers.h b/wire/Headers.h index ecf18d111..a2616ab3b 100644 --- a/wire/Headers.h +++ b/wire/Headers.h @@ -5,7 +5,6 @@ #include "util/Endian.h" #include -#include /** * The header which switches use to decide where to route traffic. @@ -17,7 +16,7 @@ * + Switch Label + * 4 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * 8 | Type | Frag | Priority | + * 8 | Type | Priority | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ #pragma pack(4) @@ -27,15 +26,9 @@ struct Headers_SwitchHeader uint64_t label_be; /** - * Top 4 bits: messageType + * Top 8 bits: messageType * See: MessageType.h * - * Next 4 bits: fragmentNumber - * 0 of the message is not fragmented, 1-15 if it is. - * Full headers for the packet along with enough user data to gather - * encryption nonces must be in the first fragment, if not, the router - * may drop the packet. - * * Bottom 24 bits: priority * Anti-flooding, this is a big endian uint32_t with the high 8 bits cut off. * @@ -49,12 +42,7 @@ Assert_assertTrue(sizeof(struct Headers_SwitchHeader) == Headers_SwitchHeader_SI static inline uint32_t Headers_getMessageType(const struct Headers_SwitchHeader* header) { - return ntohl(header->lowBits_be) >> 28; -} - -static inline uint32_t Headers_getFragmentNumber(const struct Headers_SwitchHeader* header) -{ - return (ntohl(header->lowBits_be) >> 24) & ((1 << 4) - 1); + return ntohl(header->lowBits_be) >> 24; } static inline uint32_t Headers_getPriority(const struct Headers_SwitchHeader* header) @@ -62,16 +50,12 @@ static inline uint32_t Headers_getPriority(const struct Headers_SwitchHeader* he return ntohl(header->lowBits_be) & ((1 << 24) - 1); } -static inline void Headers_setPriorityFragmentNumAndMessageType(struct Headers_SwitchHeader* header, - const uint32_t payment, - const uint32_t fragmentNum, - const uint32_t messageType) +static inline void Headers_setPriorityAndMessageType(struct Headers_SwitchHeader* header, + const uint32_t priority, + const uint32_t messageType) { - header->lowBits_be = htonl( - (payment & ((1 << 24) - 1)) - | ((fragmentNum & ((1 << 4) - 1)) << 24) - | messageType << 28 - ); + header->lowBits_be = + Endian_hostToBigEndian32( (priority & ((1 << 24) - 1)) | messageType << 24 ); } /** diff --git a/wire/MessageType.h b/wire/MessageType.h index 5e97e30e4..1fa453462 100644 --- a/wire/MessageType.h +++ b/wire/MessageType.h @@ -3,12 +3,7 @@ #include "wire/Headers.h" -#define MessageType_ROUTER_HANDSHAKE_1 0 -#define MessageType_ROUTER_HANDSHAKE_2 1 -#define MessageType_ROUTER_TRAFFIC 2 -#define MessageType_USER_HANDSHAKE_1 3 -#define MessageType_USER_HANDSHAKE_2 4 -#define MessageType_USER_TRAFFIC 5 -#define MessageType_ERROR 6 +#define MessageType_TRAFFIC 0 +#define MessageType_CONTROL 1 #endif