Browse Source

Moved BalingWire to SessionManager

Caleb James DeLisle 9 years ago
parent
commit
8431914ac4

+ 4 - 4
admin/angel/Core.c

@@ -59,7 +59,7 @@
 #include "net/SwitchPinger_admin.h"
 #include "net/ControlHandler.h"
 #include "net/EventEmitter.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "net/SwitchAdapter.h"
 #include "switch/SwitchCore.h"
 #include "tunnel/IpTunnel.h"
@@ -422,11 +422,11 @@ void Core_init(struct Allocator* alloc,
 
     struct Router* router = Router_new(routerModule, nodeStore, searchRunner, alloc);
 
-    struct BalingWire* balingWire =
-        BalingWire_new(alloc, eventBase, cryptoAuth, rand, logger, eventEmitter);
+    struct SessionManager* sessionManager =
+        SessionManager_new(alloc, eventBase, cryptoAuth, rand, logger, eventEmitter);
 
     struct SwitchAdapter* switchAdapter = SwitchAdapter_new(alloc, logger);
-    Interface_plumb(&balingWire->switchIf, &switchAdapter->balingWireIf);
+    Interface_plumb(&sessionManager->switchIf, &switchAdapter->sessionManagerIf);
     SwitchCore_setRouterInterface(&switchAdapter->switchIf, switchCore);
 
     struct ControlHandler* controlHandler = ControlHandler_new(alloc, logger, router, addr);

+ 8 - 7
dht/DHTCoreInterface.c

@@ -15,8 +15,9 @@
 #include "dht/DHTCoreInterface.h"
 #include "dht/DHTModule.h"
 #include "dht/Address.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "wire/DataHeader.h"
+#include "wire/RouteHeader.h"
 
 ///////////////////// [ Address ][ content... ]
 
@@ -39,8 +40,8 @@ static int incomingFromDHT(struct DHTMessage* dmessage, void* vcim)
     // Sanity check (make sure the addr was actually calculated)
     Assert_true(addr->ip6.bytes[0] == 0xfc && addr->padding == 0);
 
-    Message_shift(msg, BalingWire_InsideHeader_SIZE + DataHeader_SIZE, NULL);
-    struct BalingWire_InsideHeader* hdr = (struct BalingWire_InsideHeader*) msg->bytes;
+    Message_shift(msg, RouteHeader_SIZE + DataHeader_SIZE, NULL);
+    struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
     struct DataHeader* dh = (struct DataHeader*) &hdr[1];
 
     Bits_memset(dh, 0, DataHeader_SIZE);
@@ -48,7 +49,7 @@ static int incomingFromDHT(struct DHTMessage* dmessage, void* vcim)
     DataHeader_setContentType(dh, ContentType_CJDHT);
 
     Bits_memcpyConst(hdr->ip6, addr->ip6.bytes, 16);
-    hdr->version = addr->protocolVersion;
+    hdr->version_be = Endian_hostToBigEndian32(addr->protocolVersion);
     Bits_memset(&hdr->sh, 0, SwitchHeader_SIZE);
     hdr->sh.label_be = Endian_hostToBigEndian64(addr->path);
     Bits_memcpyConst(hdr->publicKey, addr->key, 32);
@@ -63,11 +64,11 @@ static int incomingFromCore(struct Interface_Two* coreIf, struct Message* msg)
     struct DHTCoreInterface_pvt* cim = Identity_check((struct DHTCoreInterface_pvt*) coreIf);
 
     struct Address addr;
-    struct BalingWire_InsideHeader* hdr = (struct BalingWire_InsideHeader*) msg->bytes;
-    Message_shift(msg, -(BalingWire_InsideHeader_SIZE + DataHeader_SIZE), NULL);
+    struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
+    Message_shift(msg, -(RouteHeader_SIZE + DataHeader_SIZE), NULL);
     Bits_memcpyConst(addr.ip6.bytes, hdr->ip6, 16);
     Bits_memcpyConst(addr.key, hdr->publicKey, 32);
-    addr.protocolVersion = hdr->version;
+    addr.protocolVersion = Endian_bigEndianToHost32(hdr->version_be);
     addr.padding = 0;
     addr.path = Endian_bigEndianToHost64(hdr->sh.label_be);
 

+ 40 - 35
net/ConverterV15.c

@@ -18,12 +18,13 @@
 #include "util/Identity.h"
 #include "wire/SwitchHeader.h"
 #include "wire/DataHeader.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "wire/ContentType.h"
 #include "wire/Headers.h"
 #include "util/Checksum.h"
+#include "wire/RouteHeader.h"
 
-#include "util/Hex.h"
+//#include "util/Hex.h"
 
 struct ConverterV15_pvt
 {
@@ -35,7 +36,7 @@ struct ConverterV15_pvt
 };
 
 /**
- * Incoming packet with a BalingWire header followed by a ContentHeader and then whatever
+ * Incoming packet with a SessionManager header followed by a ContentHeader and then whatever
  * content.
  */
 static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributorIf,
@@ -44,13 +45,17 @@ static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributor
     struct ConverterV15_pvt* conv =
         Identity_containerOf(upperDistributorIf, struct ConverterV15_pvt, pub.upperDistributorIf);
 
-    Assert_true(msg->length >= DataHeader_SIZE + BalingWire_InsideHeader_SIZE);
+    Assert_true(msg->length >= DataHeader_SIZE + RouteHeader_SIZE);
 
-    struct BalingWire_InsideHeader* hdr = (struct BalingWire_InsideHeader*) msg->bytes;
+    struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
     struct SessionTable_Session* sess = SessionTable_sessionForIp6(hdr->ip6, conv->sm);
-    if (!hdr->version && (!sess->version || sess->version > 15)) {
-        // If nothing is known about a node, fuckit, assume it's new !
-        return Interface_send(&conv->pub.balingWireIf, msg);
+    if (hdr->version_be && Endian_bigEndianToHost32(hdr->version_be) < 16) {
+        // definitely old
+    } else if (!hdr->version_be && sess->version && sess->version < 16) {
+        // session thinks it's old
+    } else {
+        // nothing is known about a node, fuckit, assume it's new !
+        return Interface_send(&conv->pub.sessionManagerIf, msg);
     }
 
     struct DataHeader* dh = (struct DataHeader*) &hdr[1];
@@ -64,7 +69,7 @@ static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributor
     // My fears, come alive, in this place where I once died
     // daemons dreamin', goin' eyed, I just needed to
     // RE-ALIGN
-    Message_shift(msg, -(DataHeader_SIZE + BalingWire_InsideHeader_SIZE), NULL);
+    Message_shift(msg, -(DataHeader_SIZE + RouteHeader_SIZE), NULL);
     if (type == ContentType_CJDHT) {
         // push a udp header and then an ip header and then checksum the udp
         Message_shift(msg, Headers_UDPHeader_SIZE, NULL);
@@ -72,13 +77,13 @@ static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributor
     if (type != ContentType_IPTUN) {
         Message_shift(msg, Headers_IP6Header_SIZE, NULL);
     }
-    Message_shift(msg, BalingWire_InsideHeader_SIZE, NULL);
-    Bits_memmove(msg->bytes, hdr, BalingWire_InsideHeader_SIZE);
-    hdr = (struct BalingWire_InsideHeader*) msg->bytes;
+    Message_shift(msg, RouteHeader_SIZE, NULL);
+    Bits_memmove(msg->bytes, hdr, RouteHeader_SIZE);
+    hdr = (struct RouteHeader*) msg->bytes;
 
 
     if (type == ContentType_IPTUN) {
-        return Interface_send(&conv->pub.balingWireIf, msg);
+        return Interface_send(&conv->pub.sessionManagerIf, msg);
     }
 
     struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) &hdr[1];
@@ -87,7 +92,7 @@ static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributor
     ip6->hopLimit = 42;
     ip6->nextHeader = type;
     ip6->payloadLength_be = Endian_hostToBigEndian16(
-        msg->length - BalingWire_InsideHeader_SIZE - Headers_IP6Header_SIZE);
+        msg->length - RouteHeader_SIZE - Headers_IP6Header_SIZE);
     Bits_memcpyConst(ip6->destinationAddr, hdr->ip6, 16);
     Bits_memcpyConst(ip6->sourceAddr, conv->myIp6, 16);
 
@@ -97,31 +102,31 @@ static int incomingFromUpperDistributorIf(struct Interface_Two* upperDistributor
         udp->srcPort_be = 0;
         udp->destPort_be = 0;
         udp->length_be = Endian_hostToBigEndian16(msg->length -
-                                                  BalingWire_InsideHeader_SIZE -
+                                                  RouteHeader_SIZE -
                                                   Headers_IP6Header_SIZE -
                                                   Headers_UDPHeader_SIZE);
         udp->checksum_be = 0;
         udp->checksum_be =
             Checksum_udpIp6(ip6->sourceAddr,
                             (uint8_t*) udp,
-                            msg->length - BalingWire_InsideHeader_SIZE - Headers_IP6Header_SIZE);
+                            msg->length - RouteHeader_SIZE - Headers_IP6Header_SIZE);
 
         Log_debug(conv->log, "Converted CJDHT->v15");
     }
 
     //Log_debug(conv->log, "send [%s]", Hex_print(ip6, 32, msg->alloc));
 
-    return Interface_send(&conv->pub.balingWireIf, msg);
+    return Interface_send(&conv->pub.sessionManagerIf, msg);
 }
 
 //// --------------- Incoming, convert v15 to v16 --------------- ////
 
 #define tryConvertDHT_OVERHEAD \
-    (BalingWire_InsideHeader_SIZE + Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE)
+    (RouteHeader_SIZE + Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE)
 static inline bool tryConvertDHT(struct Message* msg, struct Headers_IP6Header* ip6)
 {
     if (msg->length < tryConvertDHT_OVERHEAD) { return false; }
-    struct BalingWire_InsideHeader* bih = (struct BalingWire_InsideHeader*) msg->bytes;
+    struct RouteHeader* bih = (struct RouteHeader*) msg->bytes;
     struct Headers_UDPHeader* udp = (struct Headers_UDPHeader*) &ip6[1];
     if (udp->srcPort_be || udp->destPort_be) { return false; }
     Message_shift(msg, -tryConvertDHT_OVERHEAD, NULL);
@@ -130,32 +135,32 @@ static inline bool tryConvertDHT(struct Message* msg, struct Headers_IP6Header*
         .versionAndFlags = DataHeader_CURRENT_VERSION << 4
     };
     Message_push(msg, &dh, DataHeader_SIZE, NULL);
-    Message_shift(msg, BalingWire_InsideHeader_SIZE, NULL);
-    Bits_memmoveConst(msg->bytes, bih, BalingWire_InsideHeader_SIZE);
+    Message_shift(msg, RouteHeader_SIZE, NULL);
+    Bits_memmoveConst(msg->bytes, bih, RouteHeader_SIZE);
     return true;
 }
 
 /**
- * Incoming packet with a BalingWire header and under that either an ipv6 or ipv4 header
+ * Incoming packet with a SessionManager header and under that either an ipv6 or ipv4 header
  * depending on whether it's destine for TUN/DHT or IpTunnel.
  */
-static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct Message* msg)
+static int incomingFromSessionManagerIf(struct Interface_Two* sessionManagerIf, struct Message* msg)
 {
     struct ConverterV15_pvt* conv =
-        Identity_containerOf(balingWireIf, struct ConverterV15_pvt, pub.balingWireIf);
+        Identity_containerOf(sessionManagerIf, struct ConverterV15_pvt, pub.sessionManagerIf);
 
-    Log_debug(conv->log, "incomingFromBalingWireIf");
+    Log_debug(conv->log, "incomingFromSessionManagerIf");
 
-    if (msg->length < BalingWire_InsideHeader_SIZE + 8) {
+    if (msg->length < RouteHeader_SIZE + 8) {
         Log_debug(conv->log, "DROP runt");
         return 0;
     }
 
-    struct BalingWire_InsideHeader* bih = (struct BalingWire_InsideHeader*) msg->bytes;
+    struct RouteHeader* bih = (struct RouteHeader*) msg->bytes;
 
     uint8_t* ipPtr = (uint8_t*) &bih[1];
 
-    Log_debug(conv->log, "recv [%s]", Hex_print(ipPtr, 32, msg->alloc));
+    //Log_debug(conv->log, "recv [%s]", Hex_print(ipPtr, 32, msg->alloc));
 
     int ipVer = Headers_getIpVersion(ipPtr);
     if (ipVer == DataHeader_CURRENT_VERSION) {
@@ -164,7 +169,7 @@ static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct M
     }
 
     if (ipVer == 6) {
-        if (msg->length < BalingWire_InsideHeader_SIZE + Headers_IP6Header_SIZE) {
+        if (msg->length < RouteHeader_SIZE + Headers_IP6Header_SIZE) {
             Log_debug(conv->log, "DROP runt");
             return 0;
         }
@@ -175,14 +180,14 @@ static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct M
                 return Interface_send(&conv->pub.upperDistributorIf, msg);
             }
     Log_debug(conv->log, "tryConvertDHT(fail)");
-            Message_pop(msg, NULL, BalingWire_InsideHeader_SIZE + Headers_IP6Header_SIZE, NULL);
+            Message_pop(msg, NULL, RouteHeader_SIZE + Headers_IP6Header_SIZE, NULL);
             struct DataHeader dh = {
                 .contentType_be = Endian_hostToBigEndian16(ip6->nextHeader),
                 .versionAndFlags = DataHeader_CURRENT_VERSION << 4
             };
             Message_push(msg, &dh, DataHeader_SIZE, NULL);
-            Message_shift(msg, BalingWire_InsideHeader_SIZE, NULL);
-            Bits_memmoveConst(msg->bytes, bih, BalingWire_InsideHeader_SIZE);
+            Message_shift(msg, RouteHeader_SIZE, NULL);
+            Bits_memmoveConst(msg->bytes, bih, RouteHeader_SIZE);
             return Interface_send(&conv->pub.upperDistributorIf, msg);
         }
     Log_debug(conv->log, "iptunnel?");
@@ -192,9 +197,9 @@ static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct M
     }
 
     Message_shift(msg, DataHeader_SIZE, NULL);
-    Bits_memmoveConst(msg->bytes, bih, BalingWire_InsideHeader_SIZE);
+    Bits_memmoveConst(msg->bytes, bih, RouteHeader_SIZE);
 
-    bih = (struct BalingWire_InsideHeader*) msg->bytes;
+    bih = (struct RouteHeader*) msg->bytes;
     ipPtr = (uint8_t*) &bih[1];
 
     struct DataHeader* dh = (struct DataHeader*) &bih[1];
@@ -211,7 +216,7 @@ struct ConverterV15* ConverterV15_new(struct Allocator* alloc,
 {
     struct ConverterV15_pvt* out = Allocator_calloc(alloc, sizeof(struct ConverterV15_pvt), 1);
     out->pub.upperDistributorIf.send = incomingFromUpperDistributorIf;
-    out->pub.balingWireIf.send = incomingFromBalingWireIf;
+    out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
     out->log = log;
     out->sm = sm;
     Bits_memcpyConst(out->myIp6, myIp6, 16);

+ 1 - 1
net/ConverterV15.h

@@ -29,7 +29,7 @@ Linker_require("net/ConverterV15.c")
 struct ConverterV15
 {
     /** talks v15 */
-    struct Interface_Two balingWireIf;
+    struct Interface_Two sessionManagerIf;
 
     /** talks v16 */
     struct Interface_Two upperDistributorIf;

+ 27 - 26
net/BalingWire.c → net/SessionManager.c

@@ -15,12 +15,13 @@
 #include "interface/Interface.h"
 #include "memory/Allocator.h"
 #include "net/Event.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "crypto/AddressCalc.h"
 #include "util/AddrTools.h"
 #include "wire/Error.h"
 #include "util/events/Time.h"
 #include "util/Defined.h"
+#include "wire/RouteHeader.h"
 
 struct BufferedMessage
 {
@@ -38,9 +39,9 @@ struct Ip6 {
 #define Map_NAME BufferedMessages
 #include "util/Map.h"
 
-struct BalingWire_pvt
+struct SessionManager_pvt
 {
-    struct BalingWire pub;
+    struct SessionManager pub;
     struct Interface_Two eventIf;
     struct Allocator* alloc;
     struct Map_BufferedMessages bufMap;
@@ -78,7 +79,7 @@ struct BalingWire_pvt
 
 static uint8_t incomingFromSwitchPostCryptoAuth(struct Message* msg, struct Interface* iface)
 {
-    struct BalingWire_pvt* bw = Identity_check((struct BalingWire_pvt*) iface->receiverContext);
+    struct SessionManager_pvt* bw = Identity_check((struct SessionManager_pvt*) iface->receiverContext);
 
     struct SessionTable_Session* session = bw->currentSession;
     struct SwitchHeader* sh = bw->currentSwitchHeader;
@@ -94,8 +95,8 @@ static uint8_t incomingFromSwitchPostCryptoAuth(struct Message* msg, struct Inte
         session->sendHandle = Message_pop32(msg, NULL);
     }
 
-    Message_shift(msg, BalingWire_InsideHeader_SIZE, NULL);
-    struct BalingWire_InsideHeader* header = (struct BalingWire_InsideHeader*) msg->bytes;
+    Message_shift(msg, RouteHeader_SIZE, NULL);
+    struct RouteHeader* header = (struct RouteHeader*) msg->bytes;
 
     if (currentMessageSetup) {
         Bits_memcpyConst(&header->sh, sh, SwitchHeader_SIZE);
@@ -104,7 +105,7 @@ static uint8_t incomingFromSwitchPostCryptoAuth(struct Message* msg, struct Inte
                               Endian_bigEndianToHost64(sh->label_be),
                               "received start message");
     } else {
-        // BalingWire_InsideHeader is laid out such that no copy of switch header should be needed.
+        // RouteHeader is laid out such that no copy of switch header should be needed.
         Assert_true(&header->sh == sh);
         debugHandlesAndLabel0(bw->log,
                               session,
@@ -112,7 +113,7 @@ static uint8_t incomingFromSwitchPostCryptoAuth(struct Message* msg, struct Inte
                               "received run message");
     }
 
-    header->version = session->version;
+    header->version_be = Endian_hostToBigEndian32(session->version);
     Bits_memcpyConst(header->ip6, session->ip6, 16);
     uint8_t* pubKey = CryptoAuth_getHerPublicKey(session->internal);
     Bits_memcpyConst(header->publicKey, pubKey, 32);
@@ -124,7 +125,7 @@ static uint8_t incomingFromSwitchPostCryptoAuth(struct Message* msg, struct Inte
 
 static int incomingFromSwitchIf(struct Interface_Two* iface, struct Message* msg)
 {
-    struct BalingWire_pvt* bw = Identity_containerOf(iface, struct BalingWire_pvt, pub.switchIf);
+    struct SessionManager_pvt* bw = Identity_containerOf(iface, struct SessionManager_pvt, pub.switchIf);
 
     // SwitchHeader, handle, small cryptoAuth header
     if (msg->length < SwitchHeader_SIZE + 4 + 20) {
@@ -200,9 +201,9 @@ static int incomingFromSwitchIf(struct Interface_Two* iface, struct Message* msg
  * the search has somehow gone missing. If the search was not confirmed to have begun,
  * do not keep for more than 1 second.
  */
-static void checkTimedOutBuffers(void* vBalingWire)
+static void checkTimedOutBuffers(void* vSessionManager)
 {
-    struct BalingWire_pvt* bw = Identity_check((struct BalingWire_pvt*) vBalingWire);
+    struct SessionManager_pvt* bw = Identity_check((struct SessionManager_pvt*) vSessionManager);
     for (int i = 0; i < (int)bw->bufMap.count; i++) {
         struct BufferedMessage* buffered = bw->bufMap.values[i];
         uint64_t lag = Time_currentTimeSeconds(bw->eventBase) - buffered->timeSent;
@@ -213,9 +214,9 @@ static void checkTimedOutBuffers(void* vBalingWire)
     }
 }
 
-static int needsLookup(struct BalingWire_pvt* bw, struct Message* msg)
+static int needsLookup(struct SessionManager_pvt* bw, struct Message* msg)
 {
-    struct BalingWire_InsideHeader* header = (struct BalingWire_InsideHeader*) msg->bytes;
+    struct RouteHeader* header = (struct RouteHeader*) msg->bytes;
     if (Defined(Log_DEBUG)) {
         uint8_t ipStr[40];
         AddrTools_printIp(ipStr, header->ip6);
@@ -255,7 +256,7 @@ static int needsLookup(struct BalingWire_pvt* bw, struct Message* msg)
 
 static uint8_t readyToSendPostCryptoAuth(struct Message* msg, struct Interface* iface)
 {
-    struct BalingWire_pvt* bw = Identity_check((struct BalingWire_pvt*) iface->senderContext);
+    struct SessionManager_pvt* bw = Identity_check((struct SessionManager_pvt*) iface->senderContext);
     struct SwitchHeader* sh = bw->currentSwitchHeader;
     struct SessionTable_Session* sess = bw->currentSession;
     bw->currentSession = NULL;
@@ -282,12 +283,12 @@ static uint8_t readyToSendPostCryptoAuth(struct Message* msg, struct Interface*
     return Interface_send(&bw->pub.switchIf, msg);
 }
 
-static int readyToSend(struct BalingWire_pvt* bw,
+static int readyToSend(struct SessionManager_pvt* bw,
                        struct SessionTable_Session* sess,
                        struct Message* msg)
 {
-    struct BalingWire_InsideHeader* header = (struct BalingWire_InsideHeader*) msg->bytes;
-    Message_shift(msg, -BalingWire_InsideHeader_SIZE, NULL);
+    struct RouteHeader* header = (struct RouteHeader*) msg->bytes;
+    Message_shift(msg, -RouteHeader_SIZE, NULL);
     Assert_true(!bw->currentSession);
     Assert_true(!bw->currentSwitchHeader);
     bw->currentSession = sess;
@@ -316,9 +317,9 @@ static int readyToSend(struct BalingWire_pvt* bw,
 
 static int incomingFromInsideIf(struct Interface_Two* iface, struct Message* msg)
 {
-    struct BalingWire_pvt* bw = Identity_containerOf(iface, struct BalingWire_pvt, pub.insideIf);
-    Assert_true(msg->length >= BalingWire_InsideHeader_SIZE);
-    struct BalingWire_InsideHeader* header = (struct BalingWire_InsideHeader*) msg->bytes;
+    struct SessionManager_pvt* bw = Identity_containerOf(iface, struct SessionManager_pvt, pub.insideIf);
+    Assert_true(msg->length >= RouteHeader_SIZE);
+    struct RouteHeader* header = (struct RouteHeader*) msg->bytes;
 
     struct SessionTable_Session* sess =
         SessionTable_sessionForIp6(header->ip6, bw->pub.sessionTable);
@@ -331,7 +332,7 @@ static int incomingFromInsideIf(struct Interface_Two* iface, struct Message* msg
         }
     }
 
-    if (header->version) { sess->version = header->version; }
+    if (header->version_be) { sess->version = Endian_bigEndianToHost32(header->version_be); }
 
     if (header->sh.label_be) {
         // fallthrough
@@ -374,7 +375,7 @@ static uint32_t getEffectiveMetric(uint64_t nowMilliseconds,
 
 static int incomingFromEventIf(struct Interface_Two* iface, struct Message* msg)
 {
-    struct BalingWire_pvt* bw = Identity_containerOf(iface, struct BalingWire_pvt, eventIf);
+    struct SessionManager_pvt* bw = Identity_containerOf(iface, struct SessionManager_pvt, eventIf);
     enum Event ev = Message_pop32(msg, NULL);
 
     struct Ip6 ip6;
@@ -448,14 +449,14 @@ static int incomingFromEventIf(struct Interface_Two* iface, struct Message* msg)
     Assert_failure("2+2=5");
 }
 
-struct BalingWire* BalingWire_new(struct Allocator* alloc,
+struct SessionManager* SessionManager_new(struct Allocator* alloc,
                                   struct EventBase* eventBase,
                                   struct CryptoAuth* cryptoAuth,
                                   struct Random* rand,
                                   struct Log* log,
                                   struct EventEmitter* ee)
 {
-    struct BalingWire_pvt* bw = Allocator_calloc(alloc, sizeof(struct BalingWire_pvt), 1);
+    struct SessionManager_pvt* bw = Allocator_calloc(alloc, sizeof(struct SessionManager_pvt), 1);
     bw->alloc = alloc;
     bw->pub.switchIf.send = incomingFromSwitchIf;
     bw->pub.insideIf.send = incomingFromInsideIf;
@@ -465,8 +466,8 @@ struct BalingWire* BalingWire_new(struct Allocator* alloc,
     bw->ca = cryptoAuth;
     bw->eventBase = eventBase;
 
-    bw->pub.metricHalflifeMilliseconds = BalingWire_METRIC_HALFLIFE_MILLISECONDS_DEFAULT;
-    bw->pub.maxBufferedMessages = BalingWire_MAX_BUFFERED_MESSAGES_DEFAULT;
+    bw->pub.metricHalflifeMilliseconds = SessionManager_METRIC_HALFLIFE_MILLISECONDS_DEFAULT;
+    bw->pub.maxBufferedMessages = SessionManager_MAX_BUFFERED_MESSAGES_DEFAULT;
 
     EventEmitter_regIface(ee, &bw->eventIf, Event_DISCOVERY);
     EventEmitter_regIface(ee, &bw->eventIf, Event_SEARCH_BEGIN);

+ 15 - 42
net/BalingWire.h → net/SessionManager.h

@@ -12,8 +12,8 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-#ifndef BalingWire_H
-#define BalingWire_H
+#ifndef SessionManager_H
+#define SessionManager_H
 
 #include "interface/Interface.h"
 #include "net/SessionTable.h"
@@ -23,10 +23,10 @@
 #include "wire/SwitchHeader.h"
 #include "wire/CryptoHeader.h"
 #include "util/Linker.h"
-Linker_require("net/BalingWire.c")
+Linker_require("net/SessionManager.c")
 
 /**
- * Called BalingWire because I can't think of what this should be called.
+ * Called SessionManager because I can't think of what this should be called.
  * Purpose of this module is to take packets from "the inside" which contain ipv6 address and
  * skeleton switch header and find an appropriate CryptoAuth session for them or begin one.
  * If a key for this node cannot be found then the packet will be blocked and a search will be
@@ -35,14 +35,14 @@ Linker_require("net/BalingWire.c")
  * already buffered, the packet will be dropped instead).
  * Incoming messages from the outside will be decrypted and their key and path will be stored.
  */
-struct BalingWire
+struct SessionManager
 {
     /** Sends and handles packets prepped to/from switch. */
     struct Interface_Two switchIf;
 
     /**
-     * Sends and handles packets with BalingWire_InsideHeader on top.
-     * When sending a packet to BalingWire:
+     * Sends and handles packets with RouteHeader on top.
+     * When sending a packet to SessionManager:
      *     header.sh.label_be may be zero
      *     version may be zero
      *     publicKey may be zero
@@ -56,7 +56,7 @@ struct BalingWire
     /**
      * Maximum number of packets to hold in buffer before summarily dropping...
      */
-    #define BalingWire_MAX_BUFFERED_MESSAGES_DEFAULT 30
+    #define SessionManager_MAX_BUFFERED_MESSAGES_DEFAULT 30
     int maxBufferedMessages;
 
     /**
@@ -64,42 +64,15 @@ struct BalingWire
      * This allows less good routes to supplant better ones if the "better" ones have not been
      * tested in a long time (maybe down).
      */
-    #define BalingWire_METRIC_HALFLIFE_MILLISECONDS_DEFAULT 250000
+    #define SessionManager_METRIC_HALFLIFE_MILLISECONDS_DEFAULT 250000
     uint32_t metricHalflifeMilliseconds;
 };
 
-struct BalingWire_InsideHeader
-{
-    /** public key of peer node, 0 if unknown, always send from BailingWire. */
-    uint8_t publicKey[32];
-
-    /**
-     * The switch header to use.
-     * label_be may be zero if unknown.
-     * version will be automatically set to the node's current version.
-     */
-    struct SwitchHeader sh;
-
-    /** Protocol version of peer node, 0 if unknown, sometimes 0 from BailingWire. */
-    uint32_t version;
-
-    /**
-     * Create a layout which puts the SwitchHeader 24 bytes behind the end of the header
-     * allowing it to be in exactly the right place after encryption.
-     */
-    uint32_t pad;
-
-    /** IPv6 of peer node REQUIRED */
-    uint8_t ip6[16];
-};
-#define BalingWire_InsideHeader_SIZE (56 + SwitchHeader_SIZE)
-Assert_compileTime(BalingWire_InsideHeader_SIZE == sizeof(struct BalingWire_InsideHeader));
-
-struct BalingWire* BalingWire_new(struct Allocator* alloc,
-                                  struct EventBase* eventBase,
-                                  struct CryptoAuth* cryptoAuth,
-                                  struct Random* rand,
-                                  struct Log* log,
-                                  struct EventEmitter* ee);
+struct SessionManager* SessionManager_new(struct Allocator* alloc,
+                                          struct EventBase* eventBase,
+                                          struct CryptoAuth* cryptoAuth,
+                                          struct Random* rand,
+                                          struct Log* log,
+                                          struct EventEmitter* ee);
 
 #endif

+ 4 - 4
net/SwitchAdapter.c

@@ -41,10 +41,10 @@ static int incomingFromControlIf(struct Interface_Two* controlIf, struct Message
     return Interface_receiveMessage(&sa->pub.switchIf, msg);
 }
 
-static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct Message* msg)
+static int incomingFromSessionManagerIf(struct Interface_Two* sessionManagerIf, struct Message* msg)
 {
     struct SwitchAdapter_pvt* sa =
-        Identity_containerOf(balingWireIf, struct SwitchAdapter_pvt, pub.balingWireIf);
+        Identity_containerOf(sessionManagerIf, struct SwitchAdapter_pvt, pub.sessionManagerIf);
     return Interface_receiveMessage(&sa->pub.switchIf, msg);
 }
 
@@ -67,14 +67,14 @@ static uint8_t incomingFromSwitchIf(struct Message* msg, struct Interface* switc
     if (hdr->handle_be == 0xffffffff) {
         return Interface_send(&sa->pub.controlIf, msg);
     }
-    return Interface_send(&sa->pub.balingWireIf, msg);
+    return Interface_send(&sa->pub.sessionManagerIf, msg);
 }
 
 struct SwitchAdapter* SwitchAdapter_new(struct Allocator* alloc, struct Log* log)
 {
     struct SwitchAdapter_pvt* out = Allocator_calloc(alloc, sizeof(struct SwitchAdapter_pvt), 1);
     out->pub.controlIf.send = incomingFromControlIf;
-    out->pub.balingWireIf.send = incomingFromBalingWireIf;
+    out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
     out->pub.switchIf.sendMessage = incomingFromSwitchIf;
     out->pub.switchIf.allocator = alloc;
     out->log = log;

+ 2 - 2
net/SwitchAdapter.h

@@ -22,7 +22,7 @@
 Linker_require("net/SwitchAdapter.c")
 
 /**
- * Connects the actual switch interface to the BalingWire and the ControlHandler.
+ * Connects the actual switch interface to the SessionManager and the ControlHandler.
  */
 struct SwitchAdapter
 {
@@ -39,7 +39,7 @@ struct SwitchAdapter
      * Sends all packets which are not control frames.
      * Directs all input to the switch.
      */
-    struct Interface_Two balingWireIf;
+    struct Interface_Two sessionManagerIf;
 };
 
 struct SwitchAdapter* SwitchAdapter_new(struct Allocator* alloc, struct Log* log);

+ 10 - 10
net/TUNAdapter.c

@@ -17,7 +17,7 @@
 #include "util/Identity.h"
 #include "wire/SwitchHeader.h"
 #include "net/UpperDistributor.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "wire/DataHeader.h"
 
 struct UpperDistributor_pvt
@@ -31,37 +31,37 @@ static int incomingFromDhtIf(struct Interface_Two* dhtIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(dhtIf, struct UpperDistributor_pvt, pub.dhtIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 static int incomingFromTunIf(struct Interface_Two* tunIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(tunIf, struct UpperDistributor_pvt, pub.tunIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 static int incomingFromIpTunnelIf(struct Interface_Two* ipTunnelIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(ipTunnelIf, struct UpperDistributor_pvt, pub.ipTunnelIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 
-static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct Message* msg)
+static int incomingFromSessionManagerIf(struct Interface_Two* sessionManagerIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
-        Identity_containerOf(balingWireIf, struct UpperDistributor_pvt, pub.balingWireIf);
-    Assert_true(msg->length >= BalingWire_InsideHeader_SIZE + DataHeader_SIZE);
-    struct BalingWire_InsideHeader* hdr = (struct BalingWire_InsideHeader*) msg->bytes;
+        Identity_containerOf(sessionManagerIf, struct UpperDistributor_pvt, pub.sessionManagerIf);
+    Assert_true(msg->length >= RouteHeader_SIZE + DataHeader_SIZE);
+    struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
     struct DataHeader* dh = (struct DataHeader*) &hdr[1];
     enum ContentType type = DataHeader_getContentType(dh);
     if (type <= ContentType_IP6_RAW) {
         return Interface_send(&ud->pub.tunIf, msg);
     }
     if (type == ContentType_CJDHT) {
-        Log_debug(ud->log, "UD_incomingFromBalingWireIf");
+        Log_debug(ud->log, "UD_incomingFromSessionManagerIf");
         return Interface_send(&ud->pub.dhtIf, msg);
     }
     if (type == ContentType_IPTUN) {
@@ -78,7 +78,7 @@ struct UpperDistributor* UpperDistributor_new(struct Allocator* alloc, struct Lo
     out->pub.dhtIf.send = incomingFromDhtIf;
     out->pub.tunIf.send = incomingFromTunIf;
     out->pub.ipTunnelIf.send = incomingFromIpTunnelIf;
-    out->pub.balingWireIf.send = incomingFromBalingWireIf;
+    out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
     out->log = log;
     Identity_set(out);
     return &out->pub;

+ 3 - 3
net/TUNAdapter.h

@@ -22,12 +22,12 @@
 Linker_require("net/UpperDistributor.c")
 
 /**
- * Connects the TUN, DHT and IpTunnel (and other?) handlers to the BalingWire.
- * All packets must have BalingWire_UpperHeader on them.
+ * Connects the TUN, DHT and IpTunnel (and other?) handlers to the SessionManager.
+ * All packets must have SessionManager_UpperHeader on them.
  */
 struct UpperDistributor
 {
-    struct Interface_Two balingWireIf;
+    struct Interface_Two sessionManagerIf;
 
     struct Interface_Two tunIf;
 

+ 11 - 10
net/UpperDistributor.c

@@ -17,8 +17,9 @@
 #include "util/Identity.h"
 #include "wire/SwitchHeader.h"
 #include "net/UpperDistributor.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "wire/DataHeader.h"
+#include "wire/RouteHeader.h"
 
 struct UpperDistributor_pvt
 {
@@ -31,37 +32,37 @@ static int incomingFromDhtIf(struct Interface_Two* dhtIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(dhtIf, struct UpperDistributor_pvt, pub.dhtIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 static int incomingFromTunIf(struct Interface_Two* tunIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(tunIf, struct UpperDistributor_pvt, pub.tunIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 static int incomingFromIpTunnelIf(struct Interface_Two* ipTunnelIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
         Identity_containerOf(ipTunnelIf, struct UpperDistributor_pvt, pub.ipTunnelIf);
-    return Interface_send(&ud->pub.balingWireIf, msg);
+    return Interface_send(&ud->pub.sessionManagerIf, msg);
 }
 
 
-static int incomingFromBalingWireIf(struct Interface_Two* balingWireIf, struct Message* msg)
+static int incomingFromSessionManagerIf(struct Interface_Two* sessionManagerIf, struct Message* msg)
 {
     struct UpperDistributor_pvt* ud =
-        Identity_containerOf(balingWireIf, struct UpperDistributor_pvt, pub.balingWireIf);
-    Assert_true(msg->length >= BalingWire_InsideHeader_SIZE + DataHeader_SIZE);
-    struct BalingWire_InsideHeader* hdr = (struct BalingWire_InsideHeader*) msg->bytes;
+        Identity_containerOf(sessionManagerIf, struct UpperDistributor_pvt, pub.sessionManagerIf);
+    Assert_true(msg->length >= RouteHeader_SIZE + DataHeader_SIZE);
+    struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
     struct DataHeader* dh = (struct DataHeader*) &hdr[1];
     enum ContentType type = DataHeader_getContentType(dh);
     if (type <= ContentType_IP6_RAW) {
         return Interface_send(&ud->pub.tunIf, msg);
     }
     if (type == ContentType_CJDHT) {
-        Log_debug(ud->log, "UD_incomingFromBalingWireIf");
+        Log_debug(ud->log, "UD_incomingFromSessionManagerIf");
         return Interface_send(&ud->pub.dhtIf, msg);
     }
     if (type == ContentType_IPTUN) {
@@ -78,7 +79,7 @@ struct UpperDistributor* UpperDistributor_new(struct Allocator* alloc, struct Lo
     out->pub.dhtIf.send = incomingFromDhtIf;
     out->pub.tunIf.send = incomingFromTunIf;
     out->pub.ipTunnelIf.send = incomingFromIpTunnelIf;
-    out->pub.balingWireIf.send = incomingFromBalingWireIf;
+    out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
     out->log = log;
     Identity_set(out);
     return &out->pub;

+ 3 - 3
net/UpperDistributor.h

@@ -22,12 +22,12 @@
 Linker_require("net/UpperDistributor.c")
 
 /**
- * Connects the TUN, DHT and IpTunnel (and other?) handlers to the BalingWire.
- * All packets must have BalingWire_UpperHeader on them.
+ * Connects the TUN, DHT and IpTunnel (and other?) handlers to the SessionManager.
+ * All packets must have SessionManager_UpperHeader on them.
  */
 struct UpperDistributor
 {
-    struct Interface_Two balingWireIf;
+    struct Interface_Two sessionManagerIf;
 
     struct Interface_Two dhtIf;
 

+ 6 - 6
test/TestFramework.c

@@ -36,7 +36,7 @@
 #include "interface/InterfaceController.h"
 #include "tunnel/IpTunnel.h"
 #include "net/EventEmitter.h"
-#include "net/BalingWire.h"
+#include "net/SessionManager.h"
 #include "net/SwitchAdapter.h"
 #include "net/ConverterV15.h"
 #include "net/UpperDistributor.h"
@@ -154,18 +154,18 @@ struct TestFramework* TestFramework_setUp(char* privateKey,
 
     struct EventEmitter* eventEmitter = EventEmitter_new(allocator);
 
-    struct BalingWire* balingWire = BalingWire_new(allocator, base, ca, rand, logger, eventEmitter);
+    struct SessionManager* sessionManager = SessionManager_new(allocator, base, ca, rand, logger, eventEmitter);
     struct SwitchAdapter* switchAdapter = SwitchAdapter_new(allocator, logger);
-    Interface_plumb(&switchAdapter->balingWireIf, &balingWire->switchIf);
+    Interface_plumb(&switchAdapter->sessionManagerIf, &sessionManager->switchIf);
 
     SwitchCore_setRouterInterface(&switchAdapter->switchIf, switchCore);
 
     struct ConverterV15* v15conv =
-        ConverterV15_new(allocator, logger, balingWire->sessionTable, myAddress->ip6.bytes);
-    Interface_plumb(&v15conv->balingWireIf, &balingWire->insideIf);
+        ConverterV15_new(allocator, logger, sessionManager->sessionTable, myAddress->ip6.bytes);
+    Interface_plumb(&v15conv->sessionManagerIf, &sessionManager->insideIf);
 
     struct UpperDistributor* upper = UpperDistributor_new(allocator, logger);
-    Interface_plumb(&v15conv->upperDistributorIf, &upper->balingWireIf);
+    Interface_plumb(&v15conv->upperDistributorIf, &upper->sessionManagerIf);
 
     struct DHTCoreInterface* dhtCore = DHTCoreInterface_register(allocator, logger, registry);
     Interface_plumb(&dhtCore->coreIf, &upper->dhtIf);

+ 65 - 0
wire/RouteHeader.h

@@ -0,0 +1,65 @@
+/* vim: set expandtab ts=4 sw=4: */
+/*
+ * You may redistribute this program and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef RouteHeader_H
+#define RouteHeader_H
+
+#include "util/Assert.h"
+#include "wire/SwitchHeader.h"
+
+/**
+ * This header is never sent on the wire, it is only used for communicating to the SessionManager
+ * which node you wish to communicate with and what path the packet should take.
+ * Everything "below" the SessionManager is encrypted end-to-end, everything "above" has this
+ * header. SessionManager will place this header on top of packets in order to tell the upper
+ * layers the packet's origin. Upper layers must use this header in order to tell the SessionManager
+ * to whom the packet should be sent.
+ * Some of the fields in this header may be left zero when sending to the SessionManager,
+ * for example if the publicKey of the node to communicate with is not known, you may send 32
+ * bytes of zeros and the SessionManager will attempt to find the key in the SessionTable or
+ * it will buffer the packet and trigger a search.
+ * When the SessionManager emits a packet, all fields except possibly the version field will be
+ * properly filled. If the version field is zero, the SessionManager has not determined the version
+ * of the peer.
+ */
+struct RouteHeader
+{
+    /** public key of peer node, 0 if unknown, always sent from SessionManager. */
+    uint8_t publicKey[32];
+
+    /**
+     * The switch header to use.
+     * label_be may be zero if unknown.
+     * version will be automatically set to the node's current version.
+     */
+    struct SwitchHeader sh;
+
+    /** Protocol version of peer node, 0 if unknown, sometimes 0 from SessionManager. */
+    uint32_t version_be;
+
+    /**
+     * Create a layout which puts the SwitchHeader 24 bytes behind the end of the header
+     * allowing it to be in exactly the right place after encryption thus saving SessionManager
+     * the need to copy it after encryption/decryption.
+     */
+    uint32_t pad;
+
+    /** IPv6 of peer node REQUIRED */
+    uint8_t ip6[16];
+};
+#define RouteHeader_SIZE (56 + SwitchHeader_SIZE)
+Assert_compileTime(RouteHeader_SIZE == sizeof(struct RouteHeader));
+
+
+#endif

+ 0 - 5
wire/SwitchHeader.h

@@ -85,11 +85,6 @@ static inline uint32_t SwitchHeader_getVersion(const struct SwitchHeader* header
     return header->versionAndLabelShift >> 6;
 }
 
-static inline bool SwitchHeader_isV7Ctrl(const struct SwitchHeader* header)
-{
-    return !SwitchHeader_getVersion(header) && (header->congestAndSuppressErrors == 1);
-}
-
 static inline void SwitchHeader_setVersion(struct SwitchHeader* header, uint8_t version)
 {
     Assert_true(version < 4);