]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Rename trusts_cidr2str to CIDRtostr() and move it to lib/irc_ipv6.c.
authorGunnar Beutner <redacted>
Tue, 30 Jul 2013 15:02:23 +0000 (17:02 +0200)
committerGunnar Beutner <redacted>
Tue, 30 Jul 2013 15:02:23 +0000 (17:02 +0200)
--HG--
branch : shroudtrusts

glines/glines_buf.c
glines/glines_commands.c
glines/glines_util.c
lib/irc_ipv6.c
lib/irc_ipv6.h
trusts/formats.c
trusts/trusts.h
trusts/trusts_commands.c
trusts/trusts_db.c
trusts/trusts_management.c
trusts/trusts_policy.c

index 675711e0e4e5488ca6b7d5ae33edc69988f3e4f5..63f048914889fb99437890c93e2a5e446ff72ce0 100644 (file)
@@ -79,7 +79,7 @@ void glinebufaddbyip(glinebuf *gbuf, const char *user, struct irc_in_addr *ip, u
   if (nodebits < bits)
     bits = nodebits;
 
-  snprintf(mask, sizeof(mask), "%s@%s", user, trusts_cidr2str(ip, bits));
+  snprintf(mask, sizeof(mask), "%s@%s", user, CIDRtostr(*ip, bits));
 
   glinebufadd(gbuf, mask, creator, reason, expire, lastmod, lifetime);
 }
index 4b943dc3eb8725dd86caa529aeaad3f00bd0bc65..0c085741dfaec1d5a874cffe548e1655f329a590 100644 (file)
@@ -554,7 +554,7 @@ static int glines_cmdtrustgline(void *source, int cargc, char **cargv) {
   glinebufcommentv(&gbuf, "TRUSTGLINE", cargc + coff - 1, cargv);
 
   for(th = tg->hosts; th; th = th->next) {
-    snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], trusts_cidr2str(&th->ip, th->bits));
+    snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], CIDRtostr(th->ip, th->bits));
     glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
   }
 
@@ -604,7 +604,7 @@ static int glines_cmdtrustungline(void *source, int cargc, char **cargv) {
   count = 0;
 
   for (th = tg->hosts; th; th = th->next) {
-    snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], trusts_cidr2str(&th->ip, th->bits));
+    snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], CIDRtostr(th->ip, th->bits));
 
     gl = findgline(mask);
 
index da34935e28216bbc8b1a93458a930262d0bf7078..160e5b2cfa8f40a1faef10888eb7b4da14df6e17 100644 (file)
@@ -7,7 +7,7 @@ int glinebyip(const char *user, struct irc_in_addr *ip, unsigned char bits, int
   int hits;
 
   glinebufinit(&gbuf, 0);
-  glinebufcommentf(&gbuf, "on IP mask %s@%s, set by %s", user, trusts_cidr2str(ip, bits), creator);
+  glinebufcommentf(&gbuf, "on IP mask %s@%s, set by %s", user, CIDRtostr(*ip, bits), creator);
   glinebufaddbyip(&gbuf, user, ip, bits, flags, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
 
   glinebufcounthits(&gbuf, &hits, NULL);
index 577491b8d1143776c87ddeaa0ca31b72eb066093..ba6817e1a1ceec63a00eb4513a8b53de618f8ea3 100644 (file)
@@ -77,6 +77,48 @@ const char* ircd_ntoa_r(char* buf, const struct irc_in_addr* in)
     }
 }
 
+/** Convert a CIDR mask to printable ASCII form.
+ * This is generally deprecated in favor of ircd_ntoa_masked_r().
+ * @param[in] in Address to convert.
+ * @param[in] bits Mask bits.
+ * @return Pointer to a static buffer containing the readable form.
+ */
+const char* ircd_ntoa_masked(const struct irc_in_addr* in, unsigned char bits)
+{
+  static char buf[CIDRLEN];
+  return ircd_ntoa_masked_r(buf, in, bits);
+}
+
+/** Convert a CIDR mask to printable ASCII form.
+ * @param[out] buf Output buffer to write to.
+ * @param[in] in Address to format.
+ * @param[in] bits Mask bits.
+ * @return Pointer to the output buffer \a buf.
+ */
+const char* ircd_ntoa_masked_r(char* buf, const struct irc_in_addr* in, unsigned char bits)
+{
+  char inname[SOCKIPLEN];
+  struct irc_in_addr intemp;
+  int i;
+
+  for(i=0;i<8;i++) {
+    int curbits = bits - i * 16;
+
+    if (curbits<0)
+      curbits = 0;
+    else if (curbits>16)
+      curbits = 16;
+
+    uint16_t mask = 0xffff & ~((1 << (16 - curbits)) - 1);
+    intemp.in6_16[i] = htons(ntohs(in->in6_16[i]) & mask);
+  }
+
+  ircd_ntoa_r(inname, &intemp);
+  sprintf(buf, "%s/%u", inname, irc_bitlen(in, bits));
+
+  return buf;
+}
+
 /** Attempt to parse an IPv4 address into a network-endian form.
  * @param[in] input Input string.
  * @param[out] output Network-endian representation of the address.
index 66b10cbcb13515cc6df6b7a4de5de069f85806a9..584385787ed04f24cb9432591a7de9880da5d4c4 100644 (file)
@@ -56,16 +56,21 @@ struct irc_sockaddr
  */
 #define SOCKIPLEN 45
 
+#define CIDRLEN SOCKIPLEN + 4
+
 /* from ircd_string.h */
 
 extern const char* ircd_ntoa(const struct irc_in_addr* addr);
 extern const char* ircd_ntoa_r(char* buf, const struct irc_in_addr* addr);
+extern const char * ircd_ntoa_masked(const struct irc_in_addr* addr, unsigned char bits);
+extern const char* ircd_ntoa_masked_r(char* buf, const struct irc_in_addr* in, unsigned char bits);
 #define ircd_aton(ADDR, STR) ipmask_parse((STR), (ADDR), NULL)
 extern int ipmask_parse(const char *in, struct irc_in_addr *mask, unsigned char *bits_ptr);
 extern int ipmask_check(const struct irc_in_addr *, const struct irc_in_addr *, unsigned char);
 void ip_canonicalize_tunnel(struct irc_in_addr *out, const struct irc_in_addr *in);
 
 #define IPtostr(ipaddr) ircd_ntoa(&(ipaddr))
+#define CIDRtostr(ipaddr, bits) ircd_ntoa_masked(&(ipaddr), bits)
 #define irc_in_addr_v4_to_int(ADDR) ((ntohs((ADDR)->in6_16[6]) << 16) | ntohs((ADDR)->in6_16[7]))
 
 /* from numnicks.h */
index 46709e8b4543a91c8175e2621d224e49cbd01837..5a6914d32d2fd6cc4906a2ac837ae50ebd674c10 100644 (file)
@@ -7,28 +7,6 @@
 #include "../irc/irc.h"
 #include "trusts.h"
 
-char *trusts_cidr2str(struct irc_in_addr *ip, unsigned char bits) {
-  static char buf[100];
-  struct irc_in_addr iptemp;
-  int i;
-
-  for(i=0;i<8;i++) {
-    int curbits = bits - i * 16;
-
-    if (curbits<0)
-      curbits = 0;
-    else if (curbits>16)
-      curbits = 16;
-
-    uint16_t mask = 0xffff & ~((1 << (16 - curbits)) - 1);
-    iptemp.in6_16[i] = htons(ntohs(ip->in6_16[i]) & mask);
-  }
-
-  snprintf(buf, sizeof(buf), "%s/%u", IPtostr(iptemp), (irc_in_addr_is_ipv4(&iptemp))?bits-96:bits);
-
-  return buf;
-}
-
 char *trusts_timetostr(time_t t) {
   static char buf[100];
 
@@ -41,9 +19,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->bits), th->count, th->maxusage, (intmax_t)th->lastseen);
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, CIDRtostr(th->ip, th->bits), th->count, th->maxusage, (intmax_t)th->lastseen);
   } else {
-    snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, trusts_cidr2str(&th->ip, th->bits), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits);
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, CIDRtostr(th->ip, th->bits), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits);
   }
 
   return buf;
index 5d8e547ca79c8b3b4f0a67b5ccfc998f4d43965c..542bfc8f5acf0a029d2ac47f4a55eeafe8a2397c 100644 (file)
@@ -115,7 +115,6 @@ int trusts_fullyonline(void);
 
 /* formats.c */
 char *trusts_timetostr(time_t);
-char *trusts_cidr2str(struct irc_in_addr *ip, unsigned char);
 char *dumpth(trusthost *, int);
 char *dumptg(trustgroup *, int);
 int parseth(char *, trusthost *, unsigned int *, int);
index c3c8056aebb07cfd5ffd9df704169c55f9085786..c3d7737cf517e429101079a8be35df4df0095589 100644 (file)
@@ -90,7 +90,7 @@ static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup,
   if(th->marker != marker)
     return;
 
-  cidrstr = trusts_cidr2str(&th->ip, th->bits);
+  cidrstr = CIDRtostr(th->ip, th->bits);
   calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
 
   if(th->group == originalgroup) {
index ed457f18d374d8c94f7fda10c240f4c74d6b80a5..87a9209a0d2ba1e1e6320478c11f038091e25b68 100644 (file)
@@ -322,7 +322,7 @@ trustgroup *tg_new(trustgroup *itg) {
 void trustsdb_insertth(char *table, trusthost *th, unsigned int groupid) {
   trustsdb->squery(trustsdb,
     "INSERT INTO ? (id, groupid, host, maxusage, created, lastseen, maxpernode, nodebits) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
-    "Tuusuutuu", table, th->id, groupid, trusts_cidr2str(&th->ip, th->bits), th->maxusage, th->created, th->lastseen, th->maxpernode, th->nodebits
+    "Tuusuutuu", table, th->id, groupid, CIDRtostr(th->ip, th->bits), th->maxusage, th->created, th->lastseen, th->maxpernode, th->nodebits
   );
 }
 
index 687e45d71d3baccf41cca303455685dc4236b296..a387b21a907ee09891135625a8e0bcd2460dda63 100644 (file)
@@ -644,8 +644,8 @@ static int trusts_cmdtrusthostmodify(void *source, int cargc, char **cargv) {
   th_update(th);
   controlreply(sender, "Host modified.");
 
-  controlwall(NO_OPER, NL_TRUSTS, "%s TRUSTMODIFIED'ed host '%s' in group '%s' (field: %s, value: %s)", controlid(sender), trusts_cidr2str(&ip, bits), tg->name->content, what, to);
-  trustlog(tg, sender->authname, "Modified %s for host '%s': %s", what, trusts_cidr2str(&ip, bits), to);
+  controlwall(NO_OPER, NL_TRUSTS, "%s TRUSTMODIFIED'ed host '%s' in group '%s' (field: %s, value: %s)", controlid(sender), CIDRtostr(ip, bits), tg->name->content, what, to);
+  trustlog(tg, sender->authname, "Modified %s for host '%s': %s", what, CIDRtostr(ip, bits), to);
 
   return CMD_OK;
 }
@@ -842,11 +842,12 @@ static void cleanuptrusts(void *arg) {
 
       th = ((trusthost **)(expiredths.content))[i];
       triggerhook(HOOK_TRUSTS_DELHOST, th);
-      th_delete(th);
 
-      cidrstr = trusts_cidr2str(&th->ip, th->bits);
+      cidrstr = CIDRtostr(th->ip, th->bits);
       trustlog(tg, "cleanuptrusts", "Removed host '%s' because it was unused for %d days.", cidrstr, CLEANUP_TH_INACTIVE);
 
+      th_delete(th);
+
       thcount++;
     }
 
index 4556112b93bbb73f88a859d8af72eee3ab3b5a00..8160a8c870f3ba2ec3d4b9555ecd64cf6141b6cf 100644 (file)
@@ -95,7 +95,7 @@ static int checkconnectionth(const char *username, struct irc_in_addr *ip, trust
     derefnode(iptree, head);
 
     if(th->maxpernode && nodecount + usercountadjustment > th->maxpernode) {
-      controlwall(NO_OPER, NL_CLONING, "Hard connection limit exceeded on subnet: %s (group: %s): %d connected, %d max.", trusts_cidr2str(ip, th->nodebits), tg->name->content, nodecount + usercountadjustment, th->maxpernode);
+      controlwall(NO_OPER, NL_CLONING, "Hard connection limit exceeded on subnet: %s (group: %s): %d connected, %d max.", CIDRtostr(*ip, th->nodebits), tg->name->content, nodecount + usercountadjustment, th->maxpernode);
       snprintf(message, messagelen, "Too many connections from your host (%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip));
       return POLICY_FAILURE_NODECOUNT;
     }