Quellcode durchsuchen

export iface info

Caleb James DeLisle vor 5 Jahren
Ursprung
Commit
71b657f944
3 geänderte Dateien mit 94 neuen und 13 gelöschten Zeilen
  1. 26 13
      net/InterfaceController.c
  2. 29 0
      net/InterfaceController.h
  3. 39 0
      net/InterfaceController_admin.c

+ 26 - 13
net/InterfaceController.c

@@ -87,8 +87,6 @@ struct InterfaceController_pvt;
 struct InterfaceController_Iface_pvt
 {
     struct InterfaceController_Iface pub;
-    String* name;
-    int beaconState;
     struct Map_EndpointsBySockaddr peerMap;
     /** The number of the next peer to try pinging, this iterates through the list of peers. */
     uint32_t lastPeerPinged;
@@ -525,22 +523,22 @@ static int closeInterface(struct Allocator_OnFreeJob* job)
 static Iface_DEFUN handleBeacon(struct Message* msg, struct InterfaceController_Iface_pvt* ici)
 {
     struct InterfaceController_pvt* ic = ici->ic;
-    if (!ici->beaconState) {
+    if (!ici->pub.beaconState) {
         // accepting beacons disabled.
         Log_debug(ic->logger, "[%s] Dropping beacon because beaconing is disabled",
-                  ici->name->bytes);
+                  ici->pub.name->bytes);
         return NULL;
     }
 
     if (msg->length < Sockaddr_OVERHEAD) {
-        Log_debug(ic->logger, "[%s] Dropping runt beacon", ici->name->bytes);
+        Log_debug(ic->logger, "[%s] Dropping runt beacon", ici->pub.name->bytes);
         return NULL;
     }
 
     struct Sockaddr* lladdrInmsg = (struct Sockaddr*) msg->bytes;
 
     if (msg->length < lladdrInmsg->addrLen + Headers_Beacon_SIZE) {
-        Log_debug(ic->logger, "[%s] Dropping runt beacon", ici->name->bytes);
+        Log_debug(ic->logger, "[%s] Dropping runt beacon", ici->pub.name->bytes);
         return NULL;
     }
 
@@ -579,7 +577,7 @@ static Iface_DEFUN handleBeacon(struct Message* msg, struct InterfaceController_
     if (!Version_isCompatible(addr.protocolVersion, Version_CURRENT_PROTOCOL)) {
         if (Defined(Log_DEBUG)) {
             Log_debug(ic->logger, "[%s] DROP beacon from [%s] which was version [%d] "
-                      "our version is [%d] making them incompatable", ici->name->bytes,
+                      "our version is [%d] making them incompatable", ici->pub.name->bytes,
                       printedAddr->bytes, addr.protocolVersion, Version_CURRENT_PROTOCOL);
         }
         return NULL;
@@ -741,6 +739,20 @@ static Iface_DEFUN handleIncomingFromWire(struct Message* msg, struct Iface* add
     return receivedPostCryptoAuth(msg, ep, ici->ic);
 }
 
+int InterfaceController_ifaceCount(struct InterfaceController* ifc)
+{
+    struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
+    return ic->icis->length;
+}
+
+struct InterfaceController_Iface* InterfaceController_getIface(struct InterfaceController* ifc,
+                                                               int ifNum)
+{
+    struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
+    struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, ifNum);
+    return (ici) ? &ici->pub : NULL;
+}
+
 struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceController* ifc,
                                                                String* name,
                                                                struct Allocator* alloc)
@@ -749,7 +761,7 @@ struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceC
 
     struct InterfaceController_Iface_pvt* ici =
         Allocator_calloc(alloc, sizeof(struct InterfaceController_Iface_pvt), 1);
-    ici->name = String_clone(name, alloc);
+    ici->pub.name = String_clone(name, alloc);
     ici->peerMap.allocator = alloc;
     ici->ic = ic;
     ici->alloc = alloc;
@@ -770,12 +782,12 @@ static int freeAlloc(struct Allocator_OnFreeJob* job)
 
 static void sendBeacon(struct InterfaceController_Iface_pvt* ici, struct Allocator* tempAlloc)
 {
-    if (ici->beaconState < InterfaceController_beaconState_newState_SEND) {
-        Log_debug(ici->ic->logger, "sendBeacon(%s) -> beaconing disabled", ici->name->bytes);
+    if (ici->pub.beaconState < InterfaceController_beaconState_newState_SEND) {
+        Log_debug(ici->ic->logger, "sendBeacon(%s) -> beaconing disabled", ici->pub.name->bytes);
         return;
     }
 
-    Log_debug(ici->ic->logger, "sendBeacon(%s)", ici->name->bytes);
+    Log_debug(ici->ic->logger, "sendBeacon(%s)", ici->pub.name->bytes);
 
     struct Message* msg = Message_new(0, 128, tempAlloc);
     Message_push(msg, &ici->ic->beacon, Headers_Beacon_SIZE, NULL);
@@ -830,8 +842,8 @@ int InterfaceController_beaconState(struct InterfaceController* ifc,
         case InterfaceController_beaconState_newState_ACCEPT: val = "ACCEPT"; break;
         case InterfaceController_beaconState_newState_SEND: val = "SEND"; break;
     }
-    Log_debug(ic->logger, "InterfaceController_beaconState(%s, %s)", ici->name->bytes, val);
-    ici->beaconState = newState;
+    Log_debug(ic->logger, "InterfaceController_beaconState(%s, %s)", ici->pub.name->bytes, val);
+    ici->pub.beaconState = newState;
     if (newState == InterfaceController_beaconState_newState_SEND) {
         // Send out a beacon right away so we don't have to wait.
         struct Allocator* alloc = Allocator_child(ici->alloc);
@@ -947,6 +959,7 @@ int InterfaceController_getPeerStats(struct InterfaceController* ifController,
             struct Peer* peer = Identity_check((struct Peer*) ici->peerMap.values[i]);
             struct InterfaceController_PeerStats* s = &stats[xcount];
             xcount++;
+            s->ifNum = ici->pub.ifNum;
             s->lladdr = Sockaddr_clone(peer->lladdr, alloc);
             Bits_memcpy(&s->addr, &peer->addr, sizeof(struct Address));
             s->bytesOut = peer->bytesOut;

+ 29 - 0
net/InterfaceController.h

@@ -72,6 +72,23 @@ static inline char* InterfaceController_stateString(enum InterfaceController_Pee
     }
 }
 
+enum InterfaceController_BeaconState
+{
+    InterfaceController_BeaconState_DISABLED,
+    InterfaceController_BeaconState_ACCEPTING,
+    InterfaceController_BeaconState_SENDING
+};
+
+static inline char* InterfaceController_beaconStateString(enum InterfaceController_BeaconState bs)
+{
+    switch (bs) {
+        case InterfaceController_BeaconState_DISABLED:  return "DISABLED";
+        case InterfaceController_BeaconState_ACCEPTING: return "ACCEPTING";
+        case InterfaceController_BeaconState_SENDING:   return "SENDING";
+        default: return "INVALID";
+    }
+}
+
 /**
  * Stats about a peer
  */
@@ -80,6 +97,7 @@ struct InterfaceController_PeerStats
     struct Address addr;
     struct Sockaddr* lladdr;
     int state;
+    int ifNum;
     uint64_t timeOfLastMessage;
     uint64_t bytesOut;
     uint64_t bytesIn;
@@ -106,6 +124,10 @@ struct InterfaceController_Iface
 
     /** Interface number within InterfaceController. */
     int ifNum;
+
+    enum InterfaceController_BeaconState beaconState;
+
+    String* name;
 };
 
 /**
@@ -121,6 +143,13 @@ struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceC
                                                  String* name,
                                                  struct Allocator* alloc);
 
+/** Get the number of interfaces registered with the controller. */
+int InterfaceController_ifaceCount(struct InterfaceController* ifc);
+
+/** Get an interface from the InterfaceController. */
+struct InterfaceController_Iface* InterfaceController_getIface(struct InterfaceController* ifc,
+                                                               int ifNum);
+
 /**
  * Add a new peer.
  * Called from the network interface when it is asked to make a connection or it autoconnects.

+ 39 - 0
net/InterfaceController_admin.c

@@ -36,6 +36,38 @@ struct Context
 
 // typical peer record is around 140 benc chars, so can't have very many in 1023
 #define ENTRIES_PER_PAGE 6
+
+static void adminInterfaces(Dict* args,
+                            void* vcontext,
+                            String* txid,
+                            struct Allocator* alloc)
+{
+    struct Context* context = Identity_check((struct Context*)vcontext);
+
+    int64_t* page = Dict_getIntC(args, "page");
+    int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
+
+    int count = InterfaceController_ifaceCount(context->ic);
+    //int count = InterfaceController_getIface(context->ic, alloc, &stats);
+
+    List* list = List_new(alloc);
+    for (int counter = 0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
+        struct InterfaceController_Iface* iface = InterfaceController_getIface(context->ic, i);
+        Dict* d = Dict_new(alloc);
+        Dict_putIntC(d, "ifNum", iface->ifNum, alloc);
+        Dict_putStringC(d, "name", iface->name, alloc);
+        char* bs = InterfaceController_beaconStateString(iface->beaconState);
+        Dict_putStringCC(d, "beaconState", bs, alloc);
+        List_addDict(list, d, alloc);
+    }
+
+    Dict* resp = Dict_new(alloc);
+    Dict_putListC(resp, "ifaces", list, alloc);
+    Dict_putIntC(resp, "total", count, alloc);
+    if (i < count) { Dict_putIntC(resp, "more", 1, alloc); }
+    Admin_sendMessage(resp, txid, context->admin);
+}
+
 static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
 {
     struct Context* context = Identity_check((struct Context*)vcontext);
@@ -82,6 +114,8 @@ static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allo
         Dict_putIntC(d, "lostPackets", stats[i].lostPackets, alloc);
         Dict_putIntC(d, "receivedOutOfRange", stats[i].receivedOutOfRange, alloc);
 
+        Dict_putIntC(d, "ifNum", stats[i].ifNum, alloc);
+
         if (stats[i].user) {
             Dict_putStringC(d, "user", stats[i].user, alloc);
         }
@@ -212,6 +246,11 @@ void InterfaceController_admin_register(struct InterfaceController* ic,
     }));
     Identity_set(ctx);
 
+    Admin_registerFunction("InterfaceController_interfaces", adminInterfaces, ctx, true,
+        ((struct Admin_FunctionArg[]) {
+            { .name = "page", .required = 0, .type = "Int" }
+        }), admin);
+
     Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
         ((struct Admin_FunctionArg[]) {
             { .name = "page", .required = 0, .type = "Int" }