Skip to content

Commit

Permalink
completed first phase of ducttape refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Feb 4, 2015
1 parent dbafca0 commit 181e9f5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
19 changes: 10 additions & 9 deletions admin/angel/Core.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,24 @@ void Core_init(struct Allocator* alloc,
}

// CryptoAuth
struct Address addr = { .protocolVersion = Version_CURRENT_PROTOCOL };
parsePrivateKey(privateKey, &addr, eh);
struct Address* addr = Allocator_calloc(alloc, sizeof(struct Address), 1);
addr->protocolVersion = Version_CURRENT_PROTOCOL;
parsePrivateKey(privateKey, addr, eh);
struct CryptoAuth* cryptoAuth = CryptoAuth_new(alloc, privateKey, eventBase, logger, rand);

struct Sockaddr* myAddr = Sockaddr_fromBytes(addr.ip6.bytes, Sockaddr_AF_INET6, alloc);
struct Sockaddr* myAddr = Sockaddr_fromBytes(addr->ip6.bytes, Sockaddr_AF_INET6, alloc);

struct SwitchCore* switchCore = SwitchCore_new(logger, alloc, eventBase);
struct DHTModuleRegistry* registry = DHTModuleRegistry_new(alloc);
ReplyModule_register(registry, alloc);

struct RumorMill* rumorMill = RumorMill_new(alloc, &addr, RUMORMILL_CAPACITY, logger, "extern");
struct RumorMill* rumorMill = RumorMill_new(alloc, addr, RUMORMILL_CAPACITY, logger, "extern");

struct NodeStore* nodeStore = NodeStore_new(&addr, alloc, eventBase, logger, rumorMill);
struct NodeStore* nodeStore = NodeStore_new(addr, alloc, eventBase, logger, rumorMill);

struct RouterModule* routerModule = RouterModule_register(registry,
alloc,
addr.key,
addr->key,
eventBase,
logger,
rand,
Expand All @@ -396,7 +397,7 @@ void Core_init(struct Allocator* alloc,
logger,
eventBase,
routerModule,
addr.ip6.bytes,
addr->ip6.bytes,
rumorMill,
alloc);

Expand Down Expand Up @@ -431,9 +432,9 @@ void Core_init(struct Allocator* alloc,

SwitchCore_setRouterInterface(&dt->switchIf, switchCore);

struct ControlHandler* controlHandler = ControlHandler_new(alloc, logger, router);
struct ControlHandler* controlHandler = ControlHandler_new(alloc, logger, router, addr);
Interface_plumb(&controlHandler->coreIf, &dt->controlIf);
struct SwitchPinger* sp = SwitchPinger_new(eventBase, rand, logger, &addr, alloc);
struct SwitchPinger* sp = SwitchPinger_new(eventBase, rand, logger, addr, alloc);
Interface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);

// Interfaces.
Expand Down
50 changes: 30 additions & 20 deletions net/ControlHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
*/
#include "net/ControlHandler.h"
#include "util/Identity.h"
#include "util/AddrTools.h"
#include "util/Checksum.h"
#include "wire/Control.h"
#include "wire/Error.h"
#include "switch/NumberCompress.h"

struct ControlHandler_pvt
{
struct ControlHandler pub;
struct Log* log;
struct Allocator* alloc;
struct Router* router;
struct Address* myAddr;
Identity
};

Expand All @@ -39,21 +44,20 @@ static int handleError(struct ControlHandler_pvt* ch,
return 0;
}
struct Control* ctrl = (struct Control*) msg->bytes;
struct Control_Error* err = ctrl->content.error;
struct Control_Error* err = &ctrl->content.error;
struct SwitchHeader* causeHeader = &err->cause;
uint64_t causeLabel = Endian_bigEndianToHost64(causeHeader->label_be);
uint32_t errorType = Endian_bigEndianToHost32(err->errorType_be);

if (!NumberCompress_isOneHop(label)) {
Router_brokenLink(ch->router, label, causeLabel);
} else (errorType == Error_UNDELIVERABLE) {
} else if (errorType == Error_UNDELIVERABLE) {
// this is our own InterfaceController complaining
// because the node isn't responding to pings.
return 0;
}

Log_debug(context->log,
"error packet from [%s] [%s]%s",
Log_debug(ch->log, "error packet from [%s] [%s]%s",
labelStr,
Error_strerror(errorType),
((err->causeHandle == 0xffffffff) ? " caused by ctrl" : ""));
Expand All @@ -80,10 +84,10 @@ static int handlePing(struct ControlHandler_pvt* ch,
Message_shift(msg, -Control_HEADER_SIZE, NULL);

// Ping and keyPing share version location
struct Control_Ping* ping = (struct Control_Ping*) message->bytes;
struct Control_Ping* ping = (struct Control_Ping*) msg->bytes;
uint32_t herVersion = Endian_bigEndianToHost32(ping->version_be);
if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
Log_debug(ch->log, "DROP ping from incompatible version [%s]", herVersion);
Log_debug(ch->log, "DROP ping from incompatible version [%d]", herVersion);
return 0;
}

Expand All @@ -106,10 +110,10 @@ static int handlePing(struct ControlHandler_pvt* ch,
struct Control_KeyPing* keyPing = (struct Control_KeyPing*) msg->bytes;
keyPing->magic = Control_KeyPong_MAGIC;
ctrl->type_be = Control_KEYPONG_be;
Bits_memcpyConst(keyPing->key, ch->myAddr.key, 32);
Bits_memcpyConst(keyPing->key, ch->myAddr->key, 32);

} else if (messageType_be == Control_PING_be) {
Log_debug(ch->log, "got switch keyPing from [%s]", labelStr);
Log_debug(ch->log, "got switch ping from [%s]", labelStr);
if (ping->magic != Control_Ping_MAGIC) {
Log_debug(ch->log, "DROP ping (bad magic)");
return 0;
Expand All @@ -123,19 +127,21 @@ static int handlePing(struct ControlHandler_pvt* ch,

ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);

Message_shift(message, Control_HEADER_SIZE, NULL);
Message_shift(msg, Control_HEADER_SIZE, NULL);

ctrl->checksum_be = 0;
ctrl->checksum_be = Checksum_engine(message->bytes, message->length);
ctrl->checksum_be = Checksum_engine(msg->bytes, msg->length);

Message_push32(msg, 0xffffffff, NULL);

Message_shift(message, SwitchHeader_SIZE, NULL);
Message_shift(msg, SwitchHeader_SIZE, NULL);

struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
Bits_memset(switchHeader, 0, SwitchHeader_SIZE);
SwitchHeader_setVersion(switchHeader, SwitchHeader_CURRENT_VERSION);
switchHeader->label_be = Endian_hostToBigEndian64(label);

Interface_send(ch->coreIf, msg);
Interface_send(&ch->pub.coreIf, msg);
return 0;
}

Expand Down Expand Up @@ -182,13 +188,13 @@ static int incomingFromCore(struct Interface_Two* coreIf, struct Message* msg)

case Control_KEYPONG_be:
case Control_PONG_be: {
Log_debug(context->log, "got switch pong from [%s]", labelStr);
Log_debug(ch->log, "got switch pong from [%s]", labelStr);
// Shift back over the header
Message_shift(message, 4 + SwitchHeader_SIZE, NULL);
return Interface_send(&context->pub.switchPingerIf, message);
Message_shift(msg, 4 + SwitchHeader_SIZE, NULL);
return Interface_send(&ch->pub.switchPingerIf, msg);
}

default:
default:;
}

Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
Expand All @@ -201,18 +207,22 @@ static int incomingFromCore(struct Interface_Two* coreIf, struct Message* msg)
static int incomingFromSwitchPinger(struct Interface_Two* switchPingerIf, struct Message* msg)
{
struct ControlHandler_pvt* ch =
Identity_check(&((struct ControlHandler_pvt*) switchPingerIf)[-1]);
return Interface_send(ch->coreIf, msg);
Identity_check( (struct ControlHandler_pvt*) (&switchPingerIf[-1]) );
return Interface_send(&ch->pub.coreIf, msg);
}

struct ControlHandler* ControlHandler_new(struct Allocator* alloc,
struct Log* logger,
struct Router* router)
struct Router* router,
struct Address* myAddr)
{
struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
ch->alloc = alloc;
ch->log = logger;
ch->router = router;
ch->myAddr = myAddr;
ch->pub.coreIf.send = incomingFromCore;
ch->pub.switchPoingerIf.send = incomingFromSwitchPinger;
ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
Identity_set(ch);
return &ch->pub;
}
3 changes: 2 additions & 1 deletion net/ControlHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct ControlHandler

struct ControlHandler* ControlHandler_new(struct Allocator* alloc,
struct Log* logger,
struct Router* router);
struct Router* router,
struct Address* myAddr);

#endif
11 changes: 5 additions & 6 deletions net/Ducttape.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static inline uint8_t sendToSwitch(struct Message* message,

Assert_true(!((uintptr_t)message->bytes % 4));

return context->switchInterface.receiveMessage(message, &context->switchInterface);
return Interface_receiveMessage(&context->pub.switchIf, message);
}

static inline bool validEncryptedIP6(struct Message* message)
Expand All @@ -398,11 +398,9 @@ static inline bool isForMe(struct Message* message, struct Ducttape_pvt* context
return (Bits_memcmp(header->destinationAddr, context->myAddr.ip6.bytes, 16) == 0);
}

static uint8_t magicInterfaceSendMessage(struct Message* msg, struct Interface* iface)
static uint8_t magicInterfaceSendMessage(struct Message* msg, struct Interface* magicInterface)
{
struct Ducttape_pvt* ctx =
Identity_check((struct Ducttape_pvt*)
&((uint8_t*)iface)[-offsetof(struct Ducttape, magicInterface)]);
struct Ducttape_pvt* ctx = DUCTTAPE_FOR_IFACE(magicInterface);

Assert_ifParanoid(msg->length >= Headers_IP6Header_SIZE);
#ifdef PARANOIA
Expand Down Expand Up @@ -891,6 +889,7 @@ static uint8_t outgoingFromCryptoAuth(struct Message* message, struct Interface*
static uint8_t incomingFromSwitch(struct Message* message, struct Interface* switchIf)
{
struct Ducttape_pvt* context = DUCTTAPE_FOR_IFACE(switchIf);
Log_debug(context->logger, "Got message...");

struct Ducttape_MessageHeader* dtHeader = getDtHeader(message, true);

Expand Down Expand Up @@ -992,6 +991,7 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi
static int incomingFromControlHandler(struct Interface_Two* controlIf, struct Message* message)
{
struct Ducttape_pvt* ctx = DUCTTAPE_FOR_IFACE(controlIf);
Assert_true(ctx->pub.switchIf.receiveMessage);
return Interface_receiveMessage(&ctx->pub.switchIf, message);
}

Expand Down Expand Up @@ -1074,7 +1074,6 @@ struct Ducttape* Ducttape_register(uint8_t privateKey[32],
}

context->pub.switchIf.sendMessage = incomingFromSwitch;
context->pub.switchIf.senderContext = context;
context->pub.switchIf.allocator = allocator;

// setup the control frame interface.
Expand Down
6 changes: 0 additions & 6 deletions net/Ducttape_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,9 @@ struct Ducttape_pvt

struct Router* router;

/** The interface which interacts with the switch core. */
struct Interface switchInterface;

/** The interface which is used by the operator of the node to communicate in the network. */
struct Interface* userIf;

/** An interface which receives messages that are sent to fc00::1 from the TUN. */
struct Interface magicInterface;

struct Address myAddr;

struct SessionManager* sm;
Expand Down
5 changes: 4 additions & 1 deletion test/TestFramework.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ struct TestFramework* TestFramework_setUp(char* privateKey,
Ducttape_register((uint8_t*)privateKey, registry, router,
base, allocator, logger, ipTun, rand, rumorMill);

struct ControlHandler* controlHandler = ControlHandler_new(allocator, logger, router);
SwitchCore_setRouterInterface(&dt->switchIf, switchCore);

struct ControlHandler* controlHandler =
ControlHandler_new(allocator, logger, router, myAddress);
Interface_plumb(&controlHandler->coreIf, &dt->controlIf);
struct SwitchPinger* sp = SwitchPinger_new(base, rand, logger, myAddress, allocator);
Interface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf);
Expand Down
10 changes: 5 additions & 5 deletions test/cjdroute_routerPing_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ int main()

CString_strncpy((char*)(udp + 1), pingBenc, CString_strlen(pingBenc));

dt->switchInterface.receiveMessage = catchResponse;
dt->switchInterface.receiverContext = NULL;
dt->pub.switchIf.receiveMessage = catchResponse;
dt->pub.switchIf.receiverContext = NULL;

// bad checksum
udp->checksum_be = 1;
Expand All @@ -75,7 +75,7 @@ int main()
.alloc = alloc
};
Ducttape_injectIncomingForMe(&m, &dt->pub, herPublicKey);
Assert_true(!dt->switchInterface.receiverContext);
Assert_true(!dt->pub.switchIf.receiverContext);

// zero checksum
udp->checksum_be = 0;
Expand All @@ -86,7 +86,7 @@ int main()
.alloc = alloc
};
Ducttape_injectIncomingForMe(&m2, &dt->pub, herPublicKey);
Assert_true(!dt->switchInterface.receiverContext);
Assert_true(!dt->pub.switchIf.receiverContext);

// good checksum
udp->checksum_be =
Expand All @@ -100,7 +100,7 @@ int main()
.alloc = alloc
};
Ducttape_injectIncomingForMe(&m3, &dt->pub, herPublicKey);
Assert_true(dt->switchInterface.receiverContext);
Assert_true(dt->pub.switchIf.receiverContext);

Allocator_free(alloc);
Allocator_free(allocator);
Expand Down

0 comments on commit 181e9f5

Please sign in to comment.