Skip to content

Commit

Permalink
more debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Dec 21, 2011
1 parent ed1ffc6 commit fbbff77
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cjdroute.c
Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions dht/Ducttape.c
Expand Up @@ -105,7 +105,7 @@ static inline void incomingDHT(struct Message* message,
struct Context* context)
{

printf(">>> ");
printf(">.> ");
//printMessage(message);
printf("\n");

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions dht/dhtcore/NodeStore.c
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions dht/dhtcore/RouterModule.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions switch/SwitchCore.c
Expand Up @@ -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;
}
Expand All @@ -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 <stdio.h>
#include "dht/Address.h"
void receiveMessage(struct Message* message, struct Interface* iface)
{
struct SwitchInterface* sourceIf = (struct SwitchInterface*) iface->receiverContext;
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 8 additions & 24 deletions wire/Headers.h
Expand Up @@ -5,7 +5,6 @@
#include "util/Endian.h"

#include <stdint.h>
#include <arpa/inet.h>

/**
* The header which switches use to decide where to route traffic.
Expand All @@ -17,7 +16,7 @@
* + Switch Label +
* 4 | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 8 | Type | Frag | Priority |
* 8 | Type | Priority |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
#pragma pack(4)
Expand All @@ -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.
*
Expand All @@ -49,29 +42,20 @@ 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)
{
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 );
}

/**
Expand Down
9 changes: 2 additions & 7 deletions wire/MessageType.h
Expand Up @@ -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

0 comments on commit fbbff77

Please sign in to comment.