]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Undo 2044:0116b2bbbb39.
authorGunnar Beutner <redacted>
Mon, 17 Jun 2013 08:07:43 +0000 (10:07 +0200)
committerGunnar Beutner <redacted>
Mon, 17 Jun 2013 08:07:43 +0000 (10:07 +0200)
--HG--
branch : shroudtrusts

trusts/data.c
trusts/trusts.h
trusts/trusts_commands.c
trusts/trusts_db.c

index e81c1898c9b27d43a8da57c5f6a67ac8310a486a..f4c49a6a316894c446bf18f29ab9861b91882c9e 100644 (file)
@@ -13,6 +13,8 @@ trustgroup *tglist;
 void th_dbupdatecounts(trusthost *);
 void tg_dbupdatecounts(trustgroup *);
 
+static trusthost *th_getnextchildbyhost(trusthost *, trusthost *);
+
 void trusts_freeall(void) {
   trustgroup *tg, *ntg;
   trusthost *th, *nth;
@@ -47,6 +49,36 @@ void th_free(trusthost *th) {
   nsfree(POOL_TRUSTS, th);
 }
 
+static void th_updatechildren(trusthost *th) {
+  trusthost *nth = NULL;
+
+  th->children = NULL;
+
+  for(;;) {
+    nth = th_getnextchildbyhost(th, nth);
+    if(!nth)
+      break;
+
+    nth->nextbychild = th->children;
+    th->children = nth;
+  }
+}
+
+void th_linktree(void) {
+  trustgroup *tg;
+  trusthost *th;
+
+  /* ugh */
+  for(tg=tglist;tg;tg=tg->next)
+    for(th=tg->hosts;th;th=th->next)
+      th->parent = th_getsmallestsupersetbyhost(th->ip, th->mask);
+
+  for(tg=tglist;tg;tg=tg->next)
+    for(th=tg->hosts;th;th=th->next)
+      if(th->parent)
+        th_updatechildren(th->parent);
+}
+
 trusthost *th_add(trusthost *ith) {
   trusthost *th;
 
@@ -59,6 +91,11 @@ trusthost *th_add(trusthost *ith) {
   th->users = NULL;
   th->count = 0;
 
+  th->parent = NULL;
+  th->children = NULL;
+
+  th->marker = 0;
+
   th->next = th->group->hosts;
   th->group->hosts = th;
 
@@ -93,6 +130,7 @@ trustgroup *tg_add(trustgroup *itg) {
   }
 
   tg->hosts = NULL;
+  tg->marker = 0;
   tg->count = 0;
 
   memset(tg->exts, 0, sizeof(tg->exts));
@@ -170,6 +208,40 @@ trusthost *th_getsubsetbyhost(uint32_t ip, uint32_t mask) {
   return NULL;
 }
 
+/* NOT reentrant obviously */
+static trusthost *th_getnextchildbyhost(trusthost *orig, trusthost *th) {
+  if(!th) {
+    trustgroup *tg;
+
+    tg = tglist;
+    for(tg=tglist;tg;tg=tg->next) {
+      th = tg->hosts;
+      if(th)
+        break;
+    }
+
+    /* INVARIANT: tg => th */
+    if(!tg)
+      return NULL;
+
+    if(th->parent == orig)
+      return th;
+  }
+
+  for(;;) {
+    if(th->next) {
+      th = th->next;
+    } else {
+      if(!th->group->next)
+        return NULL;
+      th = th->group->next->hosts;
+    }
+
+    if(th->parent == orig)
+      return th;
+  }
+}
+
 void th_getsuperandsubsets(uint32_t ip, uint32_t mask, trusthost **superset, trusthost **subset) {
   *superset = th_getsmallestsupersetbyhost(ip, mask);
   *subset = th_getsubsetbyhost(ip, mask);
@@ -300,6 +372,40 @@ void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
   }
 }
 
+unsigned int nexttgmarker(void) {
+  static unsigned int tgmarker = 0;
+  trustgroup *tg;
+
+  tgmarker++;
+  if(!tgmarker) {
+    /* If we wrapped to zero, zap the marker on all groups */
+    for(tg=tglist;tg;tg=tg->next)
+      tg->marker=0;
+
+    tgmarker++;
+  }
+
+  return tgmarker;
+}
+
+unsigned int nextthmarker(void) {
+  static unsigned int thmarker = 0;
+  trustgroup *tg;
+  trusthost *th;
+
+  thmarker++;
+  if(!thmarker) {
+    /* If we wrapped to zero, zap the marker on all hosts */
+    for(tg=tglist;tg;tg=tg->next)
+      for(th=tg->hosts;th;th=th->next)
+        th->marker=0;
+
+    thmarker++;
+  }
+
+  return thmarker;
+}
+
 trusthost *th_getbyid(unsigned int id) {
   trustgroup *tg;
   trusthost *th;
index 17f9adbf1fb5d5b0d947827dd337230e8cc3e4ec..3a55300a8721a42562ecf32fd4e6d71e5a578aa1 100644 (file)
@@ -46,6 +46,10 @@ typedef struct trusthost {
 
   unsigned int count;
 
+  struct trusthost *parent, *children;
+  unsigned int marker;
+
+  struct trusthost *nextbychild;
   struct trusthost *next;
 } trusthost;
 
@@ -65,6 +69,8 @@ typedef struct trustgroup {
   trusthost *hosts;
   unsigned int count;
 
+  unsigned int marker;
+
   struct trustgroup *next;
 
   void *exts[MAXTGEXTS];
@@ -108,6 +114,9 @@ void th_adjusthosts(trusthost *th, trusthost *, trusthost *);
 void th_getsuperandsubsets(uint32_t, uint32_t, trusthost **, trusthost **);
 trusthost *th_getsubsetbyhost(uint32_t ip, uint32_t mask);
 trusthost *th_getnextsubsetbyhost(trusthost *th, uint32_t ip, uint32_t mask);
+void th_linktree(void);
+unsigned int nexttgmarker(void);
+unsigned int nextthmarker(void);
 trusthost *th_getbyid(unsigned int);
 int tg_modify(trustgroup *, trustgroup *);
 
index fd7238f22106fcc35c3ff440c643d912c610ff5e..7a52145b2c7f2c73e26c7b721b00b00ef870f630 100644 (file)
@@ -9,9 +9,100 @@
 static void registercommands(int, void *);
 static void deregistercommands(int, void *);
 
+void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
+  static char prebuf[512], postbuf[512];
+  int spacelen;
+
+  if(spaces + 5 >= sizeof(prebuf)) {
+    prebuf[0] = prebuf[1] = '\0';
+  } else {
+    memset(prebuf, ' ', spaces);
+    prebuf[spaces] = '\0';
+  }
+
+  spacelen = width - (strlen(str) + spaces);
+  if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) {
+    postbuf[0] = postbuf[1] = '\0';
+  } else {
+    memset(postbuf, ' ', spacelen);
+    postbuf[spacelen] = '\0';
+  }
+
+  *_prebuf = prebuf;
+  *_postbuf = postbuf;
+}
+
+static void traverseandmark(unsigned int marker, trusthost *th) {
+  th->marker = marker;
+
+  for(th=th->children;th;th=th->nextbychild) {
+    th->marker = marker;
+    traverseandmark(marker, th);
+  }
+}
+
+static void insertth(array *parents, trusthost *th) {
+  int i;
+  trusthost **p2 = (trusthost **)(parents->content);
+
+  /* this eliminates common subtrees */
+  for(i=0;i<parents->cursi;i++)
+    if(p2[i] == th)
+      break;
+
+  if(i == parents->cursi) {
+    int pos = array_getfreeslot(parents);
+    ((trusthost **)(parents->content))[pos] = th;
+  }
+}
+
+static void marktree(array *parents, unsigned int marker, trusthost *th) {
+  trusthost *pth;
+  int parentcount = 0;
+
+  for(pth=th->parent;pth;pth=pth->next) {
+    insertth(parents, pth);
+
+    pth->marker = marker;
+  }
+
+  if(parentcount == 0)
+    insertth(parents, th);
+
+  /* sadly we need to recurse down */
+  traverseandmark(marker, th);
+}
+
+static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
+  char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
+
+  if(th->marker != marker)
+    return;
+
+  cidrstr = trusts_cidr2str(th->ip, th->mask);
+  calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf);
+
+  if(th->group == originalgroup) {
+    prespacebuf[0] = '>';
+
+    parentbuf[0] = '\0';
+  } else {
+    /* show the ids of other groups */
+
+    snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
+  }
+
+  controlreply(np, "%s%s%s %-10d %-10d %-21s%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), trusts_timetostr(th->created), parentbuf);  
+
+  for(th=th->children;th;th=th->nextbychild)
+    outputtree(np, marker, originalgroup, th, depth + 1);
+}
+
 static void displaygroup(nick *sender, trustgroup *tg) {
-  trusthost *th;
-  char *cidrstr;
+  trusthost *th, **p2;
+  unsigned int marker;
+  array parents;
+  int i;
   time_t t = time(NULL);
 
   /* abusing the ternary operator a bit :( */
@@ -28,13 +119,19 @@ static void displaygroup(nick *sender, trustgroup *tg) {
   controlreply(sender, "Max usage        : %d", tg->maxusage);
   controlreply(sender, "Last max reset   : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
 
-  controlreply(sender, "Host                 Current    Max        Last seen            Created");
+  controlreply(sender, "Host                 Current    Max        Last seen            Created              Group ID   Group name");
 
-  for(th=tg->hosts;th;th=th->next) {
-    cidrstr = trusts_cidr2str(th->ip, th->mask);
+  marker = nextthmarker();
+  array_init(&parents, sizeof(trusthost *));
 
-    controlreply(sender, "%-20s %-10d %-10d %-21s%s", cidrstr, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), trusts_timetostr(th->created));
-  }
+  for(th=tg->hosts;th;th=th->next)
+    marktree(&parents, marker, th);
+
+  p2 = (trusthost **)(parents.content);
+  for(i=0;i<parents.cursi;i++)
+    outputtree(sender, marker, tg, p2[i], 0);
+
+  array_free(&parents);
 
   controlreply(sender, "End of list.");
 }
index 5b3fe3f2f0007b90a49217dacee5a8145b2e06ea..41f9ab7a6704f5f52a7cb153266074458e9dfa37 100644 (file)
@@ -59,6 +59,7 @@ static void loadcomplete(void) {
   if(loaderror)
     return;
 
+  th_linktree();
   trustsdbloaded = 1;
   flushschedule = schedulerecurring(time(NULL) + 300, 0, 300, flushdatabase, NULL);
 
@@ -257,6 +258,7 @@ trusthost *th_copy(trusthost *ith) {
 
   th_getsuperandsubsets(ith->ip, ith->mask, &superset, &subset);
   th_adjusthosts(th, subset, superset);
+  th_linktree();
 
   return th;
 }
@@ -373,6 +375,8 @@ void th_delete(trusthost *th) {
 
   trustsdb_deleteth("hosts", th);
   th_free(th);
+
+  th_linktree();
 }
 
 void trustlog(trustgroup *tg, const char *user, const char *format, ...) {