Skip to content

Commit

Permalink
starts up and runs but doesn't populate the routing table
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Feb 23, 2015
1 parent 58dac30 commit fa2f9d6
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 158 deletions.
13 changes: 3 additions & 10 deletions admin/angel/AngelInit.c
Expand Up @@ -58,14 +58,6 @@ static void initCore(char* coreBinaryPath,
}
}

static void sendTo(struct Iface* iface, struct Message* msg)
{
struct Iface myIface = { .send = NULL };
Iface_plumb(&myIface, iface);
Iface_send(&myIface, msg);
Iface_unplumb(&myIface, iface);
}

static void setUser(char* user, struct Log* logger, struct Except* eh)
{
int res = 0;
Expand Down Expand Up @@ -197,11 +189,11 @@ int AngelInit_main(int argc, char** argv)
struct Message* msg = Message_new(0, 1024, tempAlloc);
BencMessageWriter_write(config, msg, eh);
Log_keys(logger, "Sent [%d] bytes to core", msg->length);
sendTo(coreIface, msg);
Iface_CALL(coreIface->send, msg, coreIface);

struct Message* coreResponse = InterfaceWaiter_waitForData(coreIface, eventBase, tempAlloc, eh);

sendTo(&clientPipe->iface, coreResponse);
Iface_CALL(clientPipe->iface.send, coreResponse, &clientPipe->iface);

#ifdef Log_KEYS
uint8_t lastChar = coreResponse->bytes[coreResponse->length-1];
Expand All @@ -215,6 +207,7 @@ int AngelInit_main(int argc, char** argv)
}

Allocator_free(tempAlloc);
Log_debug(logger, "Angel_start()");
Angel_start(coreIface, eventBase, logger, alloc);
return 0;
}
145 changes: 61 additions & 84 deletions admin/angel/Core.c
Expand Up @@ -28,6 +28,7 @@
#include "exception/Jmp.h"
#include "interface/Iface.h"
#include "util/events/UDPAddrIface.h"
#include "interface/tuntap/TUNInterface.h"
#include "interface/UDPInterface_admin.h"
#ifdef HAS_ETH_INTERFACE
#include "interface/ETHInterface_admin.h"
Expand Down Expand Up @@ -56,6 +57,7 @@
#include "net/SessionManager_admin.h"
#include "wire/SwitchHeader.h"
#include "wire/CryptoHeader.h"
#include "wire/Headers.h"
#include "net/NetCore.h"

#include <crypto_scalarmult_curve25519.h>
Expand Down Expand Up @@ -107,41 +109,44 @@ static void adminPid(Dict* input, void* vadmin, String* txid, struct Allocator*

struct Context
{
struct Allocator* allocator;
struct Allocator* alloc;
struct Admin* admin;
struct Log* logger;
struct Hermes* hermes;
struct EventBase* base;
struct NetCore* nc;
struct IpTunnel* ipTunnel;
String* exitTxid;
Identity
};

static void shutdown(void* vcontext)
{
struct Context* context = vcontext;
Allocator_free(context->allocator);
struct Context* context = Identity_check((struct Context*) vcontext);
Allocator_free(context->alloc);
}

static void onAngelExitResponse(Dict* message, void* vcontext)
{
struct Context* context = vcontext;
struct Context* context = Identity_check((struct Context*) vcontext);
Log_info(context->logger, "Angel stopped");
Log_info(context->logger, "Exiting");
Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("none")), NULL);
Admin_sendMessage(&d, context->exitTxid, context->admin);
Timeout_setTimeout(shutdown, context, 1, context->base, context->allocator);
Timeout_setTimeout(shutdown, context, 1, context->base, context->alloc);
}

static void adminExit(Dict* input, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
struct Context* context = vcontext;
struct Context* context = Identity_check((struct Context*) vcontext);
Log_info(context->logger, "Got request to exit");
Log_info(context->logger, "Stopping angel");
context->exitTxid = String_clone(txid, context->allocator);
context->exitTxid = String_clone(txid, context->alloc);
Dict angelExit = Dict_CONST(String_CONST("q"), String_OBJ(String_CONST("Angel_exit")), NULL);
Hermes_callAngel(&angelExit,
onAngelExitResponse,
context,
context->allocator,
context->alloc,
NULL,
context->hermes);
}
Expand All @@ -151,16 +156,6 @@ static void angelDied(struct Pipe* p, int status)
exit(1);
}

struct Core_Context
{
struct Sockaddr* ipAddr;
struct Log* logger;
struct Allocator* alloc;
struct Admin* admin;
struct EventBase* eventBase;
struct IpTunnel* ipTunnel;
};

static void sendResponse(String* error,
struct Admin* admin,
String* txid,
Expand All @@ -171,20 +166,38 @@ static void sendResponse(String* error,
Admin_sendMessage(output, txid, admin);
}

static void initTunnel2(String* desiredDeviceName,
struct Context* ctx,
uint8_t addressPrefix,
struct Except* eh)
{
Log_debug(ctx->logger, "Initializing TUN device [%s]",
(desiredDeviceName) ? desiredDeviceName->bytes : "<auto>");

char assignedTunName[TUNInterface_IFNAMSIZ];
char* desiredName = (desiredDeviceName) ? desiredDeviceName->bytes : NULL;

struct Iface* tun = TUNInterface_new(
desiredName, assignedTunName, 0, ctx->base, ctx->logger, eh, ctx->alloc);

Iface_plumb(tun, &ctx->nc->tunAdapt->tunIf);

IpTunnel_setTunName(assignedTunName, ctx->ipTunnel);

struct Sockaddr* myAddr =
Sockaddr_fromBytes(ctx->nc->myAddress->ip6.bytes, Sockaddr_AF_INET6, ctx->alloc);
NetDev_addAddress(assignedTunName, myAddr, addressPrefix, ctx->logger, eh);
NetDev_setMTU(assignedTunName, DEFAULT_MTU, ctx->logger, eh);
}

static void initTunnel(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
struct Core_Context* const ctx = (struct Core_Context*) vcontext;
struct Context* const ctx = Identity_check((struct Context*) vcontext);

struct Jmp jmp;
Jmp_try(jmp) {
Core_initTunnel(Dict_getString(args, String_CONST("desiredTunName")),
ctx->ipAddr,
8,
ctx->logger,
ctx->ipTunnel,
ctx->eventBase,
ctx->alloc,
&jmp.handler);
String* desiredName = Dict_getString(args, String_CONST("desiredTunName"));
initTunnel2(desiredName, ctx, 8, &jmp.handler);
} Jmp_catch {
String* error = String_printf(requestAlloc, "Failed to configure tunnel [%s]", jmp.message);
sendResponse(error, ctx->admin, txid, requestAlloc);
Expand All @@ -194,28 +207,6 @@ static void initTunnel(Dict* args, void* vcontext, String* txid, struct Allocato
sendResponse(String_CONST("none"), ctx->admin, txid, requestAlloc);
}

void Core_admin_register(struct Sockaddr* ipAddr,
struct Log* logger,
struct IpTunnel* ipTunnel,
struct Allocator* alloc,
struct Admin* admin,
struct EventBase* eventBase)
{
struct Core_Context* ctx = Allocator_malloc(alloc, sizeof(struct Core_Context));
ctx->ipAddr = ipAddr;
ctx->logger = logger;
ctx->alloc = alloc;
ctx->admin = admin;
ctx->eventBase = eventBase;
ctx->ipTunnel = ipTunnel;

struct Admin_FunctionArg args[] = {
{ .name = "desiredTunName", .required = 0, .type = "String" }
};
Admin_registerFunction("Core_initTunnel", initTunnel, ctx, true, args, admin);
}


static Dict* getInitialConfig(struct Iface* iface,
struct EventBase* eventBase,
struct Allocator* alloc,
Expand All @@ -225,32 +216,6 @@ static Dict* getInitialConfig(struct Iface* iface,
return BencMessageReader_read(m, alloc, eh);
}

void Core_initTunnel(String* desiredDeviceName,
struct Sockaddr* addr,
uint8_t addressPrefix,
struct Log* logger,
struct IpTunnel* ipTunnel,
struct EventBase* eventBase,
struct Allocator* alloc,
struct Except* eh)
{
Log_debug(logger, "Initializing TUN device [%s]",
(desiredDeviceName) ? desiredDeviceName->bytes : "<auto>");
Assert_true(0);
/*
char assignedTunName[TUNInterface_IFNAMSIZ];
char* desiredName = (desiredDeviceName) ? desiredDeviceName->bytes : NULL;
struct Iface* tun =
TUNInterface_new(desiredName, assignedTunName, 0, eventBase, logger, eh, alloc);
IpTunnel_setTunName(assignedTunName, ipTunnel);
NetDev_addAddress(assignedTunName, addr, addressPrefix, logger, eh);
NetDev_setMTU(assignedTunName, DEFAULT_MTU, logger, eh);
*/
}

/** This is a response from a call which is intended only to send information to the angel. */
static void angelResponse(Dict* resp, void* vNULL)
{
Expand Down Expand Up @@ -325,6 +290,10 @@ void Core_init(struct Allocator* alloc,

struct NetCore* nc = NetCore_new(privateKey, alloc, eventBase, rand, logger);

struct IpTunnel* ipTunnel = IpTunnel_new(logger, eventBase, alloc, rand, hermes);
Iface_plumb(&nc->tunAdapt->ipTunnelIf, &ipTunnel->tunInterface);
Iface_plumb(&nc->upper->ipTunnelIf, &ipTunnel->nodeInterface);

Pathfinder_register(alloc, logger, eventBase, rand, admin, nc->ee);

// ------------------- DNS -------------------------//
Expand Down Expand Up @@ -352,23 +321,31 @@ void Core_init(struct Allocator* alloc,

AuthorizedPasswords_init(admin, nc->ca, alloc);
Admin_registerFunction("ping", adminPing, admin, false, NULL, admin);
// Core_admin_register(myAddr, dtAAAAAAAAAAAAAA, logger, ipTun, alloc, admin, eventBase);
// Core_admin_register(myAddr, logger, ipTun, alloc, admin, eventBase);
Security_admin_register(alloc, logger, admin);
IpTunnel_admin_register(nc->ipTunnel, admin, alloc);
IpTunnel_admin_register(ipTunnel, admin, alloc);
SessionManager_admin_register(nc->sm, admin, alloc);
RainflyClient_admin_register(rainfly, admin, alloc);
Allocator_admin_register(alloc, admin);

struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
.allocator = alloc,
.admin = admin,
.logger = logger,
.hermes = hermes,
.base = eventBase,
}));
struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
Identity_set(ctx);
ctx->alloc = alloc;
ctx->admin = admin;
ctx->logger = logger;
ctx->hermes = hermes;
ctx->base = eventBase;
ctx->ipTunnel = ipTunnel;
ctx->nc = nc;

Admin_registerFunction("Core_exit", adminExit, ctx, true, NULL, admin);

Admin_registerFunction("Core_pid", adminPid, admin, false, NULL, admin);

Admin_registerFunction("Core_initTunnel", initTunnel, ctx, true,
((struct Admin_FunctionArg[]) {
{ .name = "desiredTunName", .required = 0, .type = "String" }
}), admin);
}

int Core_main(int argc, char** argv)
Expand Down
1 change: 1 addition & 0 deletions admin/angel/InterfaceWaiter.c
Expand Up @@ -77,6 +77,7 @@ struct Message* InterfaceWaiter_waitForData(struct Iface* iface,
Except_throw(eh, "InterfaceWaiter Timed out waiting for data.");
}

Assert_true(!iface->connectedIf);
Assert_true(ctx.message);
return ctx.message;
}
Expand Down
3 changes: 2 additions & 1 deletion admin/angel/cjdroute2.c
Expand Up @@ -333,6 +333,7 @@ static int usage(struct Allocator* alloc, char* appName)

static int benchmark()
{
// TODO(cjd):reimplement bench
Assert_failure("unimplemented");
/*
struct Allocator* alloc = MallocAllocator_new(1<<22);
Expand Down Expand Up @@ -585,7 +586,7 @@ int main(int argc, char** argv)

struct Message* toAngelMsg = Message_new(0, 1024, allocator);
BencMessageWriter_write(preConf, toAngelMsg, eh);
Iface_send(&angelPipe->iface, toAngelMsg);
Iface_CALL(angelPipe->iface.send, toAngelMsg, &angelPipe->iface);

Log_debug(logger, "Sent [%d] bytes to angel process", toAngelMsg->length);

Expand Down
2 changes: 1 addition & 1 deletion interface/addressable/AddrIfaceAdapter.c
Expand Up @@ -50,7 +50,7 @@ static Iface_DEFUN incomingFromInputIf(struct Message* msg, struct Iface* inputI
struct AddrIfaceAdapter* AddrIfaceAdapter_new(struct Allocator* alloc)
{
struct AddrIfaceAdapter_pvt* context =
Allocator_malloc(alloc, sizeof(struct AddrIfaceAdapter_pvt));
Allocator_calloc(alloc, sizeof(struct AddrIfaceAdapter_pvt), 1);
context->pub.generic.addr = Sockaddr_clone(Sockaddr_LOOPBACK, alloc);
context->pub.generic.alloc = alloc;
context->pub.generic.iface.send = incomingFromAddrIf;
Expand Down
5 changes: 3 additions & 2 deletions interface/addressable/PacketHeaderToUDPAddrIface.c
Expand Up @@ -103,15 +103,16 @@ static Iface_DEFUN incomingFromHeaderIf(struct Message* message, struct Iface* i
}

struct PacketHeaderToUDPAddrIface* PacketHeaderToUDPAddrIface_new(struct Allocator* alloc,
struct Sockaddr* addr)
struct Sockaddr* addr)
{
struct PacketHeaderToUDPAddrIface_pvt* context =
Allocator_malloc(alloc, sizeof(struct PacketHeaderToUDPAddrIface_pvt));
Allocator_calloc(alloc, sizeof(struct PacketHeaderToUDPAddrIface_pvt), 1);
Identity_set(context);

context->pub.udpIf.addr = Sockaddr_clone(addr, alloc);
context->pub.udpIf.iface.send = incomingFromUdpIf;
context->pub.headerIf.send = incomingFromHeaderIf;
context->pub.udpIf.alloc = alloc;

return &context->pub;
}
2 changes: 1 addition & 1 deletion net/ConverterV15.c
Expand Up @@ -50,7 +50,7 @@ static Iface_DEFUN incomingFromUpperDistributorIf(struct Message* msg,
struct SessionManager_Session* sess = SessionManager_sessionForIp6(hdr->ip6, conv->sm);
if (hdr->version_be && Endian_bigEndianToHost32(hdr->version_be) < 16) {
// definitely old
} else if (!hdr->version_be && sess->version && sess->version < 16) {
} else if (!hdr->version_be && sess && sess->version && sess->version < 16) {
// session thinks it's old
} else {
// nothing is known about a node, fuckit, assume it's new !
Expand Down
2 changes: 2 additions & 0 deletions net/IfController.c
Expand Up @@ -597,6 +597,7 @@ static Iface_DEFUN handleUnexpectedIncoming(struct Message* msg, struct IfContro

struct Peer* ep = Allocator_calloc(epAlloc, sizeof(struct Peer), 1);
Identity_set(ep);
ep->alloc = epAlloc;
ep->ici = ici;
ep->lladdr = lladdr;
ep->alloc = epAlloc;
Expand Down Expand Up @@ -806,6 +807,7 @@ int IfController_bootstrapPeer(struct IfController* ifc,
struct Peer* ep = Allocator_calloc(epAlloc, sizeof(struct Peer), 1);
int index = Map_EndpointsBySockaddr_put(&lladdr, &ep, &ici->peerMap);
Assert_true(index >= 0);
ep->alloc = epAlloc;
ep->handle = ici->peerMap.handles[index];
ep->lladdr = lladdr;
ep->ici = ici;
Expand Down
4 changes: 0 additions & 4 deletions net/NetCore.c
Expand Up @@ -82,9 +82,5 @@ struct NetCore* NetCore_new(uint8_t* privateKey,
struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes);
Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf);

struct IpTunnel* ipTunnel = nc->ipTunnel = IpTunnel_new(log, base, alloc, rand, NULL);
Iface_plumb(&tunAdapt->ipTunnelIf, &ipTunnel->tunInterface);
Iface_plumb(&upper->ipTunnelIf, &ipTunnel->nodeInterface);

return nc;
}
1 change: 0 additions & 1 deletion net/NetCore.h
Expand Up @@ -52,7 +52,6 @@ struct NetCore
struct SessionManager* sm;
struct UpperDistributor* upper;
struct TUNAdapter* tunAdapt;
struct IpTunnel* ipTunnel;
};

struct NetCore* NetCore_new(uint8_t* privateKey,
Expand Down

0 comments on commit fa2f9d6

Please sign in to comment.