struct NetCore* NetCore_new(uint8_t* privateKey, struct Allocator* allocator, struct EventBase* base, struct Random* rand, struct Log* log) { struct Allocator* alloc = Allocator_child(allocator); struct NetCore* nc = Allocator_calloc(alloc, sizeof(struct NetCore), 1); nc->alloc = alloc; nc->base = base; nc->rand = rand; nc->log = log; struct CryptoAuth* ca = nc->ca = CryptoAuth_new(alloc, privateKey, base, log, rand); struct EventEmitter* ee = nc->ee = EventEmitter_new(alloc, log, ca->publicKey); struct Address* myAddress = nc->myAddress = Allocator_calloc(alloc, sizeof(struct Address), 1); Bits_memcpy(myAddress->key, ca->publicKey, 32); Address_getPrefix(myAddress); myAddress->protocolVersion = Version_CURRENT_PROTOCOL; myAddress->path = 1; // lower half struct SwitchCore* switchCore = nc->switchCore = SwitchCore_new(log, alloc, base); struct SwitchAdapter* switchAdapter = nc->switchAdapter = SwitchAdapter_new(alloc, log); Iface_plumb(&switchAdapter->switchIf, switchCore->routerIf); struct ControlHandler* controlHandler = nc->controlHandler = ControlHandler_new(alloc, log, ee, ca->publicKey); Iface_plumb(&controlHandler->coreIf, &switchAdapter->controlIf); struct SwitchPinger* sp = nc->sp = SwitchPinger_new(base, rand, log, myAddress, alloc); Iface_plumb(&controlHandler->switchPingerIf, &sp->controlHandlerIf); nc->ifController = InterfaceController_new(ca, switchCore, log, base, sp, rand, alloc, ee); // session manager struct SessionManager* sm = nc->sm = SessionManager_new(alloc, base, ca, rand, log, ee); Iface_plumb(&switchAdapter->sessionManagerIf, &sm->switchIf); // upper half struct UpperDistributor* upper = nc->upper = UpperDistributor_new(alloc, log, ee, myAddress); Iface_plumb(&sm->insideIf, &upper->sessionManagerIf); struct TUNAdapter* tunAdapt = nc->tunAdapt = TUNAdapter_new(alloc, log, myAddress->ip6.bytes); Iface_plumb(&tunAdapt->upperDistributorIf, &upper->tunAdapterIf); return nc; }
struct Ducttape* Ducttape_register(uint8_t privateKey[32], struct DHTModuleRegistry* registry, struct RouterModule* routerModule, struct SwitchCore* switchCore, struct EventBase* eventBase, struct Allocator* allocator, struct Log* logger, struct IpTunnel* ipTun, struct Random* rand) { struct Ducttape_pvt* context = Allocator_calloc(allocator, sizeof(struct Ducttape_pvt), 1); context->registry = registry; context->routerModule = routerModule; context->logger = logger; context->eventBase = eventBase; context->alloc = allocator; Identity_set(context); context->ipTunnel = ipTun; ipTun->nodeInterface.receiveMessage = sendToNode; ipTun->nodeInterface.receiverContext = context; ipTun->tunInterface.receiveMessage = sendToTun; ipTun->tunInterface.receiverContext = context; struct CryptoAuth* cryptoAuth = CryptoAuth_new(allocator, privateKey, eventBase, logger, rand); Bits_memcpyConst(context->myAddr.key, cryptoAuth->publicKey, 32); Address_getPrefix(&context->myAddr); context->sm = SessionManager_new(incomingFromCryptoAuth, outgoingFromCryptoAuth, context, eventBase, cryptoAuth, allocator); context->pub.sessionManager = context->sm; Bits_memcpyConst(&context->module, (&(struct DHTModule) { .name = "Ducttape", .context = context, .handleOutgoing = handleOutgoing }), sizeof(struct DHTModule));
struct Ducttape* Ducttape_register(Dict* config, uint8_t privateKey[32], struct DHTModuleRegistry* registry, struct RouterModule* routerModule, struct Interface* routerIf, struct SwitchCore* switchCore, struct event_base* eventBase, struct Allocator* allocator, struct Log* logger, struct Admin* admin) { struct Ducttape* context = allocator->calloc(sizeof(struct Ducttape), 1, allocator); context->registry = registry; context->routerModule = routerModule; context->logger = logger; context->forwardTo = NULL; AddressMapper_init(&context->addrMap); struct CryptoAuth* cryptoAuth = CryptoAuth_new(config, allocator, privateKey, eventBase, logger); CryptoAuth_getPublicKey(context->myAddr.key, cryptoAuth); Address_getPrefix(&context->myAddr); context->sm = SessionManager_new(16, incomingFromCryptoAuth, outgoingFromCryptoAuth, context, eventBase, cryptoAuth, allocator); if (routerIf) { context->routerIf = routerIf; routerIf->receiveMessage = ip6FromTun; routerIf->receiverContext = context; } Bits_memcpyConst(&context->module, (&(struct DHTModule) { .name = "Ducttape", .context = context, .handleOutgoing = handleOutgoing }), sizeof(struct DHTModule));