]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/formats.c
IPv6 support for trusts.
[irc/quakenet/newserv.git] / trusts / formats.c
index aa427760a3e8bd14bb5b727e2fd136af09be8026..8486bca830e058c1d1c839d2fac7ef0e0d9f8b48 100644 (file)
@@ -6,61 +6,10 @@
 #include "../lib/strlfunc.h"
 #include "trusts.h"
 
-int trusts_parsecidr(const char *host, uint32_t *ip, short *mask) {
-  unsigned int octet1 = 0, octet2 = 0, octet3 = 0, octet4 = 0, umask = 32;
-
-  if(sscanf(host, "%u.%u.%u.%u/%u", &octet1, &octet2, &octet3, &octet4, &umask) != 5)
-    if(sscanf(host, "%u.%u.%u/%u", &octet1, &octet2, &octet3, &umask) != 4)
-      if(sscanf(host, "%u.%u/%u", &octet1, &octet2, &umask) != 3)
-        if(sscanf(host, "%u/%u", &octet1, &umask) != 2)
-          if(sscanf(host, "%u.%u.%u.%u", &octet1, &octet2, &octet3, &octet4) != 4)
-            return 0;
-
-  if(octet1 > 255 || octet2 > 255 || octet3 > 255 || octet4 > 255 || umask > 32)
-    return 0;
-
-  *ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4;
-  *mask = umask;
-
-  return 1;
-}
-
-/* returns mask pre-anded */
-int trusts_str2cidr(const char *host, uint32_t *ip, uint32_t *mask) {
-  uint32_t result;
-  short smask;
-
-  if(!trusts_parsecidr(host, &result, &smask))
-    return 0;
-
-  if(smask == 0) {
-    *mask = 0;
-  } else {
-    *mask = 0xffffffff << (32 - smask);
-  }
-  *ip = result & *mask;
-
-  return 1;
-}
-
-char *trusts_cidr2str(uint32_t ip, uint32_t mask) {
+char *trusts_cidr2str(struct irc_in_addr *ip, unsigned char bits) {
   static char buf[100];
-  char maskbuf[10];
-
-  if(mask != 0) {
-    /* count number of trailing zeros */
-    float f = (float)(mask & -mask);
-
-    mask = 32 - ((*(unsigned int *)&f >> 23) - 0x7f);
-  }
-
-  if(mask < 32) {
-    snprintf(maskbuf, sizeof(maskbuf), "/%u", mask);
-  } else {
-    maskbuf[0] = '\0';
-  }
 
-  snprintf(buf, sizeof(buf), "%u.%u.%u.%u%s", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, maskbuf);
+  snprintf(buf, sizeof(buf), "%s/%u", IPtostr(*ip), (irc_in_addr_is_ipv4(ip))?bits-96:bits);
 
   return buf;
 }
@@ -77,9 +26,9 @@ char *dumpth(trusthost *th, int oformat) {
   static char buf[512];
 
   if(oformat) {
-    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (intmax_t)th->lastseen);
+    snprintf(buf, sizeof(buf), "#%u,%s/%u,%u,%u,%jd", th->group->id, IPtostr(th->ip), (unsigned int)th->bits, th->count, th->maxusage, (intmax_t)th->lastseen);
   } else {
-    snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->id, th->maxusage, (intmax_t)th->lastseen);
+    snprintf(buf, sizeof(buf), "%u,%s/%u,%u,%u,%jd,%jd,%u,%u", th->group->id, IPtostr(th->ip), (unsigned int)th->bits, th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits);
   }
 
   return buf;
@@ -89,9 +38,9 @@ char *dumptg(trustgroup *tg, int oformat) {
   static char buf[512];
 
   if(oformat) {
-    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxuserreset, tg->createdby->content, tg->contact->content, tg->comment->content);
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
   } else {
-    snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxuserreset, tg->createdby->content, tg->contact->content, tg->comment->content);
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
   }
 
   return buf;
@@ -132,6 +81,9 @@ int parsetg(char *buf, trustgroup *tg, int oformat) {
     return 0;
   *line++ = '\0';
 
+  if(oformat && (id[0] == '#'))
+    id++;
+
   tg->id = strtoul(id, NULL, 10);
   if(!tg->id)
     return 0;
@@ -156,7 +108,7 @@ int parsetg(char *buf, trustgroup *tg, int oformat) {
 
   tg->expires = (time_t)expires;
   tg->lastseen = (time_t)lastseen;
-  tg->lastmaxuserreset = (time_t)lastmaxusereset;
+  tg->lastmaxusereset = (time_t)lastmaxusereset;
 
   createdby = &line[pos];
   contact = strchr(createdby, ',');
@@ -170,7 +122,7 @@ int parsetg(char *buf, trustgroup *tg, int oformat) {
   *comment++ = '\0';
 
   tg->name = getsstring(name, TRUSTNAMELEN);
-  tg->createdby = getsstring(createdby, NICKLEN);
+  tg->createdby = getsstring(createdby, CREATEDBYLEN);
   tg->comment = getsstring(comment, COMMENTLEN);
   tg->contact = getsstring(contact, CONTACTLEN);
   if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
@@ -185,7 +137,8 @@ int parsetg(char *buf, trustgroup *tg, int oformat) {
 }
 
 int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
-  unsigned long lastseen;
+  unsigned long lastseen, created;
+  int maxpernode, nodebits;
   char *ip, xbuf[1024], *id;
 
 /* #id,213.230.192.128/26,20,23,1222732944
@@ -199,6 +152,9 @@ int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
     return 0;
   *line++ = '\0';
 
+  if(oformat && (id[0] == '#'))
+    id++;
+
   *tgid = strtoul(id, NULL, 10);
   if(!*tgid)
     return 0;
@@ -209,18 +165,22 @@ int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
     return 0;
   *line++ = '\0';
 
-  if(!trusts_str2cidr(ip, &th->ip, &th->mask))
+  if(!ipmask_parse(ip, &th->ip, &th->bits))
     return 0;
 
   if(oformat) {
     if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
       return 0;
+    created = time(NULL);
   } else {
-    if(sscanf(line, "%u,%u,%lu", &th->id, &th->maxusage, &lastseen) != 3)
+    if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6)
       return 0;
   }
 
   th->lastseen = (time_t)lastseen;
+  th->created = (time_t)created;
+  th->maxpernode = maxpernode;
+  th->nodebits = nodebits;
 
   return 1;
 }
@@ -230,7 +190,7 @@ char *rtrim(char *buf) {
   size_t len = strlcpy(obuf, buf, sizeof(obuf));
 
   if((len < sizeof(obuf)) && (len > 0)) {
-    size_t i;
+    int i;
     for(i=len-1;i>=0;i--) {
       if(obuf[i] != ' ')
         break;