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);
}
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);
}
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);
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);
}
}
+/** 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.
*/
#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 */
#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];
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;
/* 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);
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) {
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
);
}
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;
}
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++;
}
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;
}