Bladeren bron

Not crashing anymore, just not linking

Caleb James DeLisle 9 jaren geleden
bovenliggende
commit
2eb79d491e
6 gewijzigde bestanden met toevoegingen van 76 en 59 verwijderingen
  1. 8 7
      dht/Pathfinder.c
  2. 4 1
      interface/Iface.h
  3. 1 1
      net/ControlHandler.c
  4. 32 25
      net/EventEmitter.c
  5. 1 1
      net/EventEmitter.h
  6. 30 24
      wire/PFChan.h

+ 8 - 7
dht/Pathfinder.c

@@ -106,6 +106,8 @@ static int incomingFromDHT(struct DHTMessage* dmessage, void* vpf)
 
 static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)
 {
+    Log_debug(pf->log, "INIT");
+
     struct PFChan_Core_Connect conn;
     Message_pop(msg, &conn, PFChan_Core_Connect_SIZE, NULL);
     Assert_true(!msg->length);
@@ -122,8 +124,7 @@ static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)
     struct RumorMill* rumorMill =
         RumorMill_new(pf->alloc, &pf->myAddr, RUMORMILL_CAPACITY, pf->log, "extern");
 
-    struct NodeStore* nodeStore =
-        NodeStore_new(&pf->myAddr, pf->alloc, pf->base, pf->log, rumorMill);
+    pf->nodeStore = NodeStore_new(&pf->myAddr, pf->alloc, pf->base, pf->log, rumorMill);
 
     struct RouterModule* routerModule = RouterModule_register(pf->registry,
                                                               pf->alloc,
@@ -131,9 +132,9 @@ static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)
                                                               pf->base,
                                                               pf->log,
                                                               pf->rand,
-                                                              nodeStore);
+                                                              pf->nodeStore);
 
-    struct SearchRunner* searchRunner = SearchRunner_new(nodeStore,
+    struct SearchRunner* searchRunner = SearchRunner_new(pf->nodeStore,
                                                          pf->log,
                                                          pf->base,
                                                          routerModule,
@@ -144,7 +145,7 @@ static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)
     Janitor_new(LOCAL_MAINTENANCE_SEARCH_MILLISECONDS,
                 GLOBAL_MAINTENANCE_SEARCH_MILLISECONDS,
                 routerModule,
-                nodeStore,
+                pf->nodeStore,
                 searchRunner,
                 rumorMill,
                 pf->log,
@@ -160,8 +161,8 @@ static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)
 
     // Now the admin stuff...
     if (pf->admin) {
-        struct Router* router = Router_new(routerModule, nodeStore, searchRunner, pf->alloc);
-        NodeStore_admin_register(nodeStore, pf->admin, pf->alloc);
+        struct Router* router = Router_new(routerModule, pf->nodeStore, searchRunner, pf->alloc);
+        NodeStore_admin_register(pf->nodeStore, pf->admin, pf->alloc);
         RouterModule_admin_register(routerModule, router, pf->admin, pf->alloc);
         SearchRunner_admin_register(searchRunner, pf->admin, pf->alloc);
     }

+ 4 - 1
interface/Iface.h

@@ -57,11 +57,14 @@ static inline void Iface_send(struct Iface* iface, struct Message* msg)
         #ifdef PARANOIA
             struct Iface* ifaceNext = conn->send(conn, msg);
             msg->currentIface = NULL;
-            iface = ifaceNext;
             iface->currentMsg = NULL;
+            iface = ifaceNext;
         #else
             iface = conn->send(conn, msg);
         #endif
+        #ifndef STUPID_OPTIMIZATIONS
+            Assert_true(!iface);
+        #endif
     } while (iface);
 }
 

+ 1 - 1
net/ControlHandler.c

@@ -205,7 +205,7 @@ struct ControlHandler* ControlHandler_new(struct Allocator* alloc,
     Bits_memcpyConst(ch->myPublicKey, myPublicKey, 32);
     ch->pub.coreIf.send = incomingFromCore;
     ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
-    EventEmitter_regCore(ee, &ch->eventIf, PFChan_Pathfinder_INVALID);
+    EventEmitter_regCore(ee, &ch->eventIf, 0);
     Identity_set(ch);
     return &ch->pub;
 }

+ 32 - 25
net/EventEmitter.c

@@ -21,7 +21,7 @@
 
 #include <stdbool.h>
 
-#define ArrayList_TYPE struct Iface*
+#define ArrayList_TYPE struct Iface
 #define ArrayList_NAME Ifaces
 #include "util/ArrayList.h"
 
@@ -54,7 +54,7 @@ struct Pathfinder
 
     Identity
 };
-#define ArrayList_TYPE struct Pathfinder*
+#define ArrayList_TYPE struct Pathfinder
 #define ArrayList_NAME Pathfinders
 #include "util/ArrayList.h"
 
@@ -64,28 +64,29 @@ struct EventEmitter_pvt
     struct Iface trickIf;
     struct Allocator* alloc;
     struct Log* log;
-    struct ArrayList_Ifaces* listTable[PFChan_Pathfinder_INVALID];
+    struct ArrayList_Ifaces* listTable[PFChan_Pathfinder__TOO_HIGH - PFChan_Pathfinder__TOO_LOW];
     struct ArrayList_Pathfinders* pathfinders;
     uint8_t publicKey[32];
     Identity
 };
 
+#define OFFSET(ev) (ev - PFChan_Pathfinder__TOO_LOW - 1)
+
 static struct ArrayList_Ifaces* getHandlers(struct EventEmitter_pvt* ee,
                                             enum PFChan_Pathfinder ev,
                                             bool create)
 {
-    if (ev < 0 || ev >= PFChan_Pathfinder_INVALID) { return NULL; }
-    struct ArrayList_Ifaces* out = ee->listTable[ev];
+    if (ev <= PFChan_Pathfinder__TOO_LOW || ev >= PFChan_Pathfinder__TOO_HIGH) { return NULL; }
+    struct ArrayList_Ifaces* out = ee->listTable[OFFSET(ev)];
     if (!out) {
-        out = ee->listTable[ev] = ArrayList_Ifaces_new(ee->alloc);
+        out = ee->listTable[OFFSET(ev)] = ArrayList_Ifaces_new(ee->alloc);
     }
     return out;
 }
 
-static void sendToPathfinder(struct Pathfinder* pf, struct Message* msg)
+static Iface_DEFUN sendToPathfinder(struct Message* msg, struct Pathfinder* pf)
 {
-    if (!pf || pf->state != Pathfinder_state_CONNECTED) { return; }
-    Iface_send(&pf->iface, msg);
+    if (!pf || pf->state != Pathfinder_state_CONNECTED) { return NULL; }
     if (pf->bytesSinceLastPing < 8192 && pf->bytesSinceLastPing + msg->length >= 8192) {
         struct Message* ping = Message_new(0, 512, msg->alloc);
         Message_push32(ping, pf->bytesSinceLastPing, NULL);
@@ -94,6 +95,7 @@ static void sendToPathfinder(struct Pathfinder* pf, struct Message* msg)
         Iface_send(&pf->iface, ping);
     }
     pf->bytesSinceLastPing += msg->length;
+    return Iface_next(&pf->iface, msg);
 }
 
 static bool PFChan_Pathfinder_sizeOk(enum PFChan_Pathfinder ev, int size)
@@ -119,7 +121,8 @@ static bool PFChan_Pathfinder_sizeOk(enum PFChan_Pathfinder ev, int size)
     Assert_failure("invalid event [%d]", ev);
 }
 // Forget to add the event here? :)
-Assert_compileTime(PFChan_Pathfinder_INVALID == 9);
+Assert_compileTime(PFChan_Pathfinder__TOO_LOW == 511);
+Assert_compileTime(PFChan_Pathfinder__TOO_HIGH == 521);
 
 static bool PFChan_Core_sizeOk(enum PFChan_Core ev, int size)
 {
@@ -155,7 +158,8 @@ static bool PFChan_Core_sizeOk(enum PFChan_Core ev, int size)
     Assert_failure("invalid event [%d]", ev);
 }
 // Remember to add the event to this function too!
-Assert_compileTime(PFChan_Core_INVALID == 13);
+Assert_compileTime(PFChan_Core__TOO_LOW == 1023);
+Assert_compileTime(PFChan_Core__TOO_HIGH == 1037);
 
 static Iface_DEFUN incomingFromCore(struct Iface* trickIf, struct Message* msg)
 {
@@ -166,22 +170,22 @@ static Iface_DEFUN incomingFromCore(struct Iface* trickIf, struct Message* msg)
     uint32_t pathfinderNum = Message_pop32(msg, NULL);
     Message_push32(msg, ev, NULL);
     if (pathfinderNum != 0xffffffff) {
-        struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, pathfinderNum)[0];
+        struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, pathfinderNum);
         Assert_true(pf && pf->state == Pathfinder_state_CONNECTED);
-        sendToPathfinder(pf, msg);
+        return sendToPathfinder(msg, pf);
     } else {
         for (int i = 0; i < ee->pathfinders->length; i++) {
-            struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, i)[0];
+            struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, i);
             if (!pf || pf->state != Pathfinder_state_CONNECTED) { continue; }
             struct Message* messageClone = msg;
             if (ee->pathfinders->length > i+1) {
                 // if only one handler, no need to copy the message...
                 messageClone = Message_clone(msg, msg->alloc);
             }
-            sendToPathfinder(pf, messageClone);
+            Iface_CALL(sendToPathfinder, messageClone, pf);
         }
     }
-    return 0;
+    return NULL;
 }
 
 static struct Message* pathfinderMsg(enum PFChan_Core ev,
@@ -221,7 +225,7 @@ static int handleFromPathfinder(enum PFChan_Pathfinder ev,
             Bits_memcpyConst(resp.publicKey, ee->publicKey, 32);
             Message_push(msg, &resp, PFChan_Core_Connect_SIZE, NULL);
             Message_push32(msg, PFChan_Core_CONNECT, NULL);
-            sendToPathfinder(pf, msg);
+            Iface_CALL(sendToPathfinder, msg, pf);
             break;
         }
         case PFChan_Pathfinder_SUPERIORITY: {
@@ -251,11 +255,11 @@ static int handleFromPathfinder(enum PFChan_Pathfinder ev,
         }
         case PFChan_Pathfinder_PATHFINDERS: {
             for (int i = 0; i < ee->pathfinders->length; i++) {
-                struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i)[0];
+                struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
                 if (!xpf || xpf->state != Pathfinder_state_CONNECTED) { continue; }
                 struct Allocator* alloc = Allocator_child(msg->alloc);
                 struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, alloc);
-                sendToPathfinder(pf, resp);
+                Iface_CALL(sendToPathfinder, resp, pf);
                 Allocator_free(alloc);
             }
             break;
@@ -275,7 +279,7 @@ static Iface_DEFUN incomingFromPathfinder(struct Iface* iface, struct Message* m
     enum PFChan_Pathfinder ev = Message_pop32(msg, NULL);
     Message_push32(msg, pf->pathfinderId, NULL);
     Message_push32(msg, ev, NULL);
-    if (ev < 0 || ev >= PFChan_Pathfinder_INVALID) {
+    if (ev <= PFChan_Pathfinder__TOO_LOW || ev >= PFChan_Pathfinder__TOO_HIGH) {
         Log_debug(ee->log, "DROPPF invalid type [%d]", ev);
         return NULL;
     }
@@ -304,7 +308,7 @@ static Iface_DEFUN incomingFromPathfinder(struct Iface* iface, struct Message* m
             // if only one handler, no need to copy the message...
             messageClone = Message_clone(msg, msg->alloc);
         }
-        struct Iface* iface = ArrayList_Ifaces_get(handlers, i)[0];
+        struct Iface* iface = ArrayList_Ifaces_get(handlers, i);
         // We have to call this manually because we don't have an interface handy which is
         // actually plumbed with this one.
         Assert_true(iface);
@@ -321,8 +325,11 @@ void EventEmitter_regCore(struct EventEmitter* eventEmitter,
     struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) eventEmitter);
     iface->connectedIf = &ee->trickIf;
     struct ArrayList_Ifaces* l = getHandlers(ee, ev, true);
-    if (!l) { return; }
-    ArrayList_Ifaces_add(l, &iface);
+    if (!l) {
+        Assert_true(ev == 0);
+        return;
+    }
+    ArrayList_Ifaces_add(l, iface);
 }
 
 void EventEmitter_regPathfinderIface(struct EventEmitter* emitter, struct Iface* iface)
@@ -337,10 +344,10 @@ void EventEmitter_regPathfinderIface(struct EventEmitter* emitter, struct Iface*
     Identity_set(pf);
     int i = 0;
     for (; i < ee->pathfinders->length; i++) {
-        struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i)[0];
+        struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
         if (!xpf) { break; }
     }
-    pf->pathfinderId = ArrayList_Pathfinders_put(ee->pathfinders, i, &pf);
+    pf->pathfinderId = ArrayList_Pathfinders_put(ee->pathfinders, i, pf);
 }
 
 struct EventEmitter* EventEmitter_new(struct Allocator* alloc, struct Log* log, uint8_t* publicKey)

+ 1 - 1
net/EventEmitter.h

@@ -30,7 +30,7 @@ struct EventEmitter
 /**
  * Register an interface to listen for and fire events.
  * The same interface may be registered multiple times.
- * If you only intend to fire events, just register with PFChan_Pathfinder_INVALID.
+ * If you only intend to fire events, just register with 0 as the event.
  */
 void EventEmitter_regCore(struct EventEmitter* ee,
                           struct Iface* iface,

+ 30 - 24
wire/PFChan.h

@@ -89,17 +89,20 @@ Assert_compileTime(
 
 enum PFChan_Pathfinder
 {
+    /** Below the lowest valid value. */
+    PFChan_Pathfinder__TOO_LOW = 511,
+
     /**
      * Must be emitted before any other messages.
      * (Received by: EventEmitter.c)
      */
-    PFChan_Pathfinder_CONNECT,
+    PFChan_Pathfinder_CONNECT = 512,
 
     /**
      * See PFChan_Pathfinder_Superiority for more information about this event.
      * (Received by: EventEmitter.c)
      */
-    PFChan_Pathfinder_SUPERIORITY,
+    PFChan_Pathfinder_SUPERIORITY = 513,
 
     /**
      * Emit to indicate the discovery of a node or a new best path to the node.
@@ -107,25 +110,25 @@ enum PFChan_Pathfinder
      * there are active sessions.
      * (Received by: SessionManager.c)
      */
-    PFChan_Pathfinder_NODE,
+    PFChan_Pathfinder_NODE = 514,
 
     /**
      * Send a DHT message to another node.
      * (Received by: UpperDistributor.c)
      */
-    PFChan_Pathfinder_SENDMSG,
+    PFChan_Pathfinder_SENDMSG = 515,
 
     /**
      * PFChan_Pathfinder_PING will elicit an PFChan_Core_PONG
      * (Received by: EventEmitter.c)
      */
-    PFChan_Pathfinder_PING,
+    PFChan_Pathfinder_PING = 516,
 
     /**
      * PFChan_Pathfinder_PONG must be sent if core sends a PFChan_Core_PING
      * (Received by: EventEmitter.c)
      */
-    PFChan_Pathfinder_PONG,
+    PFChan_Pathfinder_PONG = 517,
 
     // The following events have no content.
 
@@ -133,21 +136,21 @@ enum PFChan_Pathfinder
      * Get all sessions.
      * (Received by: SessionManager.c)
      */
-    PFChan_Pathfinder_SESSIONS,
+    PFChan_Pathfinder_SESSIONS = 518,
 
     /**
      * Get all peers.
      * (Received by: InterfaceController.c)
      */
-    PFChan_Pathfinder_PEERS,
+    PFChan_Pathfinder_PEERS = 519,
 
     /**
      * Get all registered pathfinders
      * (Received by: EventEmitter.c)
      */
-    PFChan_Pathfinder_PATHFINDERS,
+    PFChan_Pathfinder_PATHFINDERS = 520,
 
-    PFChan_Pathfinder_INVALID
+    PFChan_Pathfinder__TOO_HIGH = 521,
 };
 
 struct PFChan_FromPathfinder
@@ -172,89 +175,92 @@ struct PFChan_FromPathfinder
 
 enum PFChan_Core
 {
+    /** This is below the lowest valid value */
+    PFChan_Core__TOO_LOW = 1023,
+
     /**
      * This message is sent in response to an PFChan_Pathfinder_CONNECT message and is
      * guaranteed to be sent before any other message.
      * (emitted by: EventEmitter.c)
      */
-    PFChan_Core_CONNECT,
+    PFChan_Core_CONNECT = 1024,
 
     /**
      * Emitted when a pathfinder connects or if PFChan_Pathfinder_PATHFINDERS is sent.
      * (emitted by: EventEmitter.c)
      */
-    PFChan_Core_PATHFINDER,
+    PFChan_Core_PATHFINDER = 1025,
 
     /**
      * Emitted when a pathfinder disconnects from the core
      * (emitted by: EventEmitter.c)
      */
-    PFChan_Core_PATHFINDER_GONE,
+    PFChan_Core_PATHFINDER_GONE = 1026,
 
     /**
      * Emitted if a switch error is received, no matter what type of packet causes it.
      * (emitted by: ControlHandler.c)
      */
-    PFChan_Core_SWITCH_ERR,
+    PFChan_Core_SWITCH_ERR = 1027,
 
     /**
      * Emitted if the core wants the pathfinder to begin searching for a node.
      * (emitted by: SessionManager.c)
      */
-    PFChan_Core_SEARCH_REQ,
+    PFChan_Core_SEARCH_REQ = 1028,
 
     /**
      * Emitted when a peer connects (becomes state ESTABLISHED) or
      * emitted for every peer if PFChan_Pathfinder_PEERS is sent.
      * (emitted by: InterfaceController.c)
      */
-    PFChan_Core_PEER,
+    PFChan_Core_PEER = 1029,
 
     /**
      * Emitted when a peer disconnects (or becomes state UNRESPONSIVE)
      * (emitted by: InterfaceController.c)
      */
-    PFChan_Core_PEER_GONE,
+    PFChan_Core_PEER_GONE = 1030,
 
     /**
      * Emitted if a new session begins, also emitted for every active session of
      * PFChan_Pathfinder_SESSIONS is sent.
      * (emitted by: SessionManager.c)
      */
-    PFChan_Core_SESSION,
+    PFChan_Core_SESSION = 1031,
 
     /**
      * Emitted when a session ends.
      * (emitted by: SessionManager.c)
      */
-    PFChan_Core_SESSION_ENDED,
+    PFChan_Core_SESSION_ENDED = 1032,
 
     /**
      * Emitted when SessionManager sees an incoming packet with a new path.
      * (emitted by: SessionManager.c)
      */
-    PFChan_Core_DISCOVERED_PATH,
+    PFChan_Core_DISCOVERED_PATH = 1033,
 
     /**
      * Emitted for each incoming DHT message.
      * (emitted by: UpperDistributor.c)
      */
-    PFChan_Core_MSG,
+    PFChan_Core_MSG = 1034,
 
     /**
      * Emitted from time to time in order to verify the pathfinder is alive.
      * Must be responded to by an PFChan_Pathfinder_PONG.
      * (emitted by: EventEmitter.c)
      */
-    PFChan_Core_PING,
+    PFChan_Core_PING = 1035,
 
     /**
      * Will be emitted if the pathfinder emits an PFChan_Pathfinder_PING.
      * (emitted by: EventEmitter.c)
      */
-    PFChan_Core_PONG,
+    PFChan_Core_PONG = 1036,
 
-    PFChan_Core_INVALID
+    PFChan_Core__TOO_HIGH = 1037,
 };
 
 struct PFChan_Core_SearchReq