Skip to content

Commit

Permalink
More logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Dec 24, 2011
1 parent 63234a2 commit a2b3372
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 73 deletions.
9 changes: 6 additions & 3 deletions cjdroute.c
Expand Up @@ -431,7 +431,7 @@ static void configureUDP(Dict* config, struct Context* ctx)
char* bindAddress = bindStr ? bindStr->bytes : NULL;

struct UDPInterface* udpContext =
UDPInterface_new(ctx->base, bindAddress, ctx->allocator, ctx->eHandler);
UDPInterface_new(ctx->base, bindAddress, ctx->allocator, ctx->eHandler, ctx->logger);

if (bindStr) {
struct Interface* udpDefault = UDPInterface_getDefaultInterface(udpContext);
Expand Down Expand Up @@ -473,8 +473,11 @@ static void registerRouter(Dict* config, uint8_t myPubKey[32], struct Context* c
String* tunPath = benc_lookupString(iface, BSTR("tunDevice"));
context->routerIf = TunInterface_new(tunPath, context->base, context->allocator);
}
context->routerModule =
RouterModule_register(context->registry, context->allocator, myPubKey, context->base);
context->routerModule = RouterModule_register(context->registry,
context->allocator,
myPubKey,
context->base,
context->logger);
}

int main(int argc, char** argv)
Expand Down
4 changes: 2 additions & 2 deletions cjdvpn.c
Expand Up @@ -42,7 +42,7 @@ int startServer(char* bindToAddress,
{
CryptoAuth_addUser(&(String){.bytes=passwd, .len=strlen(passwd)}, 1, (void*)1, ca);
struct UDPInterface* udpContext =
UDPInterface_new(base, bindToAddress, allocator, AbortHandler_INSTANCE);
UDPInterface_new(base, bindToAddress, allocator, AbortHandler_INSTANCE, NULL);
struct Interface* udpDefault = UDPInterface_getDefaultInterface(udpContext);
struct Interface* authedUdp = CryptoAuth_wrapInterface(udpDefault, NULL, true, true, ca);
struct Interface* tun = TunInterface_new(NULL, base, allocator);
Expand Down Expand Up @@ -83,7 +83,7 @@ int startClient(char* connectToAddress,
}

struct UDPInterface* udpContext =
UDPInterface_new(base, NULL, allocator, AbortHandler_INSTANCE);
UDPInterface_new(base, NULL, allocator, AbortHandler_INSTANCE, NULL);
struct Interface* udp =
UDPInterface_addEndpoint(udpContext, connectToAddress, AbortHandler_INSTANCE);
struct Interface* authedUdp = CryptoAuth_wrapInterface(udp, srvrKey, false, true, ca);
Expand Down
36 changes: 21 additions & 15 deletions dht/dhtcore/Janitor.c
Expand Up @@ -7,7 +7,7 @@
#include "dht/dhtcore/NodeStore.h"
#include "dht/dhtcore/NodeStore_struct.h"
#include "dht/dhtcore/RouterModule.h"
#include "dht/dhtcore/RouterModuleInternal.h"
#include "dht/dhtcore/RouterModule_struct.h"
#include "libbenc/benc.h"
#include "memory/MemAllocator.h"
#include "memory/BufferAllocator.h"
Expand All @@ -16,7 +16,6 @@
#include "util/Timeout.h"
#include "util/Time.h"

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
Expand Down Expand Up @@ -113,10 +112,16 @@ static void runSearch(void* vcontext)

// If the best next node doesn't exist or has 0 reach, run a local maintenance search.
if (nodes->size == 0 || nodes->nodes[nodes->size - 1]->reach == 0) {
/*
String* hex = Hex_encode(&(String) { .len = Address_SEARCH_TARGET_SIZE, .bytes = (char*) &targetAddr.ip6.bytes }, tempAllocator);
printf("Running search for %s, node count: %d total reach: %ld\n", hex->bytes, NodeStore_size(janitor->nodeStore), (long) janitor->routerModule->totalReach);
*/
#ifdef Log_DEBUG
uint8_t printable[40];
Address_printIp(printable, &targetAddr);
Log_debug3(janitor->routerModule->logger,
"Running search for %s, node count: %u total reach: %lu\n",
printable,
(unsigned int) NodeStore_size(janitor->nodeStore),
(unsigned long) janitor->routerModule->totalReach);
#endif

RouterModule_beginSearch(targetAddr.ip6.bytes,
searchStepCallback,
janitor,
Expand All @@ -133,15 +138,16 @@ printf("Running search for %s, node count: %d total reach: %ld\n", hex->bytes, N
NodeStore_decreaseReach(amountPerNode, janitor->nodeStore);
janitor->timeOfLastReachDecrease = now;


uint32_t nonZeroNodes = 0;
for (uint32_t i = 0; i < janitor->routerModule->nodeStore->size; i++) {
nonZeroNodes += (janitor->routerModule->nodeStore->headers[i].reach > 0);
}
printf("gmrt %d non-zero nodes %d\n",
(unsigned int) AverageRoller_getAverage(janitor->routerModule->gmrtRoller),
(unsigned int) nonZeroNodes);

#ifdef Log_DEBUG
uint32_t nonZeroNodes = 0;
for (uint32_t i = 0; i < janitor->routerModule->nodeStore->size; i++) {
nonZeroNodes += (janitor->routerModule->nodeStore->headers[i].reach > 0);
}
Log_debug2(janitor->routerModule->logger,
"Global Mean Response Time: %u non-zero nodes: %u\n",
(unsigned int) AverageRoller_getAverage(janitor->routerModule->gmrtRoller),
(unsigned int) nonZeroNodes);
#endif

if (now > janitor->timeOfNextGlobalMaintainence) {
RouterModule_beginSearch(targetAddr.ip6.bytes,
Expand Down
31 changes: 16 additions & 15 deletions dht/dhtcore/NodeStore.c
Expand Up @@ -5,20 +5,23 @@
#include "dht/dhtcore/NodeStore_struct.h"
#include "dht/dhtcore/NodeCollector.h"
#include "dht/dhtcore/NodeList.h"
#include "log/Log.h"

#include <stdbool.h>
#include <stdio.h>

/** See: NodeStore.h */
struct NodeStore* NodeStore_new(struct Address* myAddress,
const uint32_t capacity,
const struct MemAllocator* allocator)
const struct MemAllocator* allocator,
struct Log* logger)
{
struct NodeStore* out = allocator->malloc(sizeof(struct NodeStore), allocator);
out->thisNodeAddress = myAddress;
out->headers = allocator->malloc(sizeof(struct NodeHeader) * capacity, allocator);
out->nodes = allocator->malloc(sizeof(struct Node) * capacity, allocator);
out->capacity = capacity;
out->logger = logger;
out->size = 0;
return out;
}
Expand Down Expand Up @@ -79,19 +82,11 @@ 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");
}
Address_getPrefix(addr);
if (memcmp(addr->ip6.bytes, store->thisNodeAddress, 16) == 0) {
printf("got introduced to ourselves\n");
return;
}

// TODO: maintain a sorted list.

Expand All @@ -107,7 +102,13 @@ if (netAddr[0] != '0') {
}
}

printf("Discovered node: %s at addr %s\n", nodeAddr, netAddr);
#ifdef Log_DEBUG
uint8_t nodeAddr[40];
Address_printIp(nodeAddr, addr);
uint8_t netAddr[20];
Address_printNetworkAddress(netAddr, addr);
Log_debug2(store->logger, "Discovered node: %s at addr %s\n", nodeAddr, netAddr);
#endif

// Free space, regular insert.
replaceNode(&store->nodes[store->size], &store->headers[store->size], addr);
Expand Down
5 changes: 4 additions & 1 deletion dht/dhtcore/NodeStore.h
Expand Up @@ -6,6 +6,7 @@

#include "dht/Address.h"
#include "dht/dhtcore/Node.h"
#include "log/Log.h"
#include "memory/MemAllocator.h"

struct NodeStore;
Expand All @@ -16,10 +17,12 @@ struct NodeStore;
* @param myAddress the address for this DHT node.
* @param capacity the number of nodes which this store can hold.
* @param allocator the allocator to allocate storage space for this NodeStore.
* @param logger the means for this node store to log.
*/
struct NodeStore* NodeStore_new(struct Address* myAddress,
const uint32_t capacity,
const struct MemAllocator* allocator);
const struct MemAllocator* allocator,
struct Log* logger);

/**
* Find a node in the store.
Expand Down
4 changes: 4 additions & 0 deletions dht/dhtcore/NodeStore_struct.h
Expand Up @@ -2,6 +2,7 @@
#define NODE_STORE_STRUCT_H

#include "dht/dhtcore/NodeStore.h"
#include "log/Log.h"

/** A list of DHT nodes. */
struct NodeStore
Expand All @@ -23,6 +24,9 @@ struct NodeStore

/** The number of nodes in the list. */
uint32_t size;

/** The means for this node store to log. */
struct Log* logger;
};

#endif
34 changes: 16 additions & 18 deletions dht/dhtcore/RouterModule.c
@@ -1,14 +1,15 @@
#include "dht/Address.h"
#include "dht/dhtcore/Janitor.h"
#include "dht/dhtcore/RouterModule.h"
#include "dht/dhtcore/RouterModuleInternal.h"
#include "dht/dhtcore/RouterModule_struct.h"
#include "dht/dhtcore/Node.h"
#include "dht/dhtcore/NodeList.h"
#include "dht/dhtcore/NodeStore.h"
#include "dht/dhtcore/SearchStore.h"
#include "dht/CJDHTConstants.h"
#include "dht/DHTModules.h"
#include "libbenc/benc.h"
#include "log/Log.h"
#include "memory/MemAllocator.h"
#include "memory/BufferAllocator.h"
#include "switch/LabelSplicer.h"
Expand Down Expand Up @@ -189,7 +190,8 @@ static int handleOutgoing(struct DHTMessage* message, void* vcontext);
struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry,
struct MemAllocator* allocator,
const uint8_t myAddress[Address_KEY_SIZE],
struct event_base* eventBase)
struct event_base* eventBase,
struct Log* logger)
{
struct RouterModule* const out = allocator->malloc(sizeof(struct RouterModule), allocator);

Expand All @@ -205,9 +207,10 @@ struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry,
out->gmrtRoller = AverageRoller_new(GMRT_SECONDS, allocator);
AverageRoller_update(out->gmrtRoller, GMRT_INITAL_MILLISECONDS);
out->searchStore = SearchStore_new(allocator, out->gmrtRoller);
out->nodeStore = NodeStore_new(&out->address, NODE_STORE_SIZE, allocator);
out->nodeStore = NodeStore_new(&out->address, NODE_STORE_SIZE, allocator, logger);
out->registry = registry;
out->eventBase = eventBase;
out->logger = logger;
out->janitor = Janitor_new(LOCAL_MAINTENANCE_SEARCH_MILLISECONDS,
GLOBAL_MAINTENANCE_SEARCH_MILLISECONDS,
REACH_DECREASE_PER_SECOND,
Expand Down Expand Up @@ -432,9 +435,14 @@ static inline void cleanup(struct SearchStore* store,
parentNode->reach = newReach;
module->totalReach += newReach - oldReach;

uint8_t nodeAddr[40];
Address_printIp(nodeAddr, &parentNode->address);
printf("increasing reach for node (%s) by %d\n", nodeAddr, ((int) (newReach - oldReach)));
#ifdef Log_DEBUG
uint8_t nodeAddr[40];
Address_printIp(nodeAddr, &parentNode->address);
Log_debug2(module->logger,
"increasing reach for node (%s) by %u\n",
nodeAddr,
(unsigned int) (newReach - oldReach));
#endif

NodeStore_updateReach(parentNode, module->nodeStore);
}
Expand Down Expand Up @@ -612,11 +620,11 @@ static inline int handleReply(struct DHTMessage* message, struct RouterModule* m
if ((thisNodePrefix ^ targetPrefix) >= parentDistance
&& xorCompare(&scc->targetAddress, &addr, parent->address) >= 0)
{
// Answer was further from the target than us.
Log_debug(module->logger, "Answer was further from the target than us.\n");
} else if (thisNodePrefix == ourAddressPrefix
&& memcmp(module->address.ip6.bytes, addr.ip6.bytes, Address_SEARCH_TARGET_SIZE) == 0)
{
// They just told us about ourselves.
Log_debug(module->logger, "They just told us about ourselves.\n");
} else {
SearchStore_addNodeToSearch(parent, &addr, evictTime, search);
}
Expand Down Expand Up @@ -820,16 +828,6 @@ void RouterModule_addNode(const uint8_t key[Address_KEY_SIZE],
memset(&address, 0, sizeof(struct Address));
memcpy(&address.key, key, Address_KEY_SIZE);
address.networkAddress_be = networkAddress_be;

#ifdef DEBUGGING
Address_getPrefix(&address);
printf("Adding node: ");
for (int i = 0; i < 16; i++) {
printf("%02x", address.ip6.bytes[i]);
}
printf("\n");
#endif

NodeStore_addNode(module->nodeStore, &address, 0);
}

Expand Down
10 changes: 7 additions & 3 deletions dht/dhtcore/RouterModule.h
Expand Up @@ -8,6 +8,7 @@
#include "dht/Address.h"
#include "dht/DHTModules.h"
#include "libbenc/benc.h"
#include "log/Log.h"

/**
* The router module is the functional part of the DHT engine.
Expand All @@ -27,12 +28,15 @@ struct RouterModule_Search;
*
* @param registry the DHT module registry for signal handling.
* @param allocator a means to allocate memory.
* @param id a 20 byte random id for this DHT node.
* @param myAddress the public key for this node.
* @param eventBase the libevent base.
* @param logger the means of writing logs.
*/
struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry,
struct MemAllocator* allocator,
const uint8_t id[Address_KEY_SIZE],
struct event_base* eventBase);
const uint8_t myAddress[Address_KEY_SIZE],
struct event_base* eventBase,
struct Log* logger);

/**
* Start a search.
Expand Down
@@ -1,3 +1,6 @@
#ifndef ROUTER_MODULE_STRUCT
#define ROUTER_MODULE_STRUCT

/**
* Internal structures which are needed for testing but should not be exposed to the outside world.
*/
Expand Down Expand Up @@ -27,4 +30,8 @@ struct RouterModule
struct event_base* eventBase;

struct Janitor* janitor;

struct Log* logger;
};

#endif
4 changes: 2 additions & 2 deletions dht/dhtcore/test/RouterModule_test.c
Expand Up @@ -10,7 +10,7 @@
#include "dht/dhtcore/Node.h"
#include "dht/dhtcore/NodeStore.h"
#include "dht/dhtcore/RouterModule.h"
#include "dht/dhtcore/RouterModuleInternal.h"
#include "dht/dhtcore/RouterModule_struct.h"
#include "dht/dhtcore/test/TestFramework.h"
#include "memory/MemAllocator.h"
#include "memory/BufferAllocator.h"
Expand Down Expand Up @@ -185,7 +185,7 @@ int main()

ReplyModule_register(registry, allocator);
struct RouterModule* routerModule =
RouterModule_register(registry, allocator, (uint8_t*) MY_ADDRESS, event_base_new());
RouterModule_register(registry, allocator, (uint8_t*) MY_ADDRESS, event_base_new(), NULL);

SerializationModule_register(registry, allocator);

Expand Down

0 comments on commit a2b3372

Please sign in to comment.