* This file should be edited in a window with a width of 141 characters
* ick
*/
+#include "config.h"
+
#include "IPcheck.h"
#include "client.h"
#include "ircd.h"
#include "msg.h"
#include "numnicks.h" /* NumNick, NumServ (GODMODE) */
#include "ircd_alloc.h"
+#include "ircd_events.h"
+#include "ircd_features.h"
#include "s_debug.h" /* Debug */
#include "s_user.h" /* TARGET_DELAY */
#include "send.h"
#include <assert.h>
-
+#include <string.h>
struct IPTargetEntry {
int count;
struct IPRegistryEntry {
struct IPRegistryEntry* next;
struct IPTargetEntry* target;
- unsigned int addr;
+ struct irc_in_addr addr;
int last_connect;
unsigned short connected;
unsigned char attempts;
#define NOW ((unsigned short)(CurrentTime & MASK_16))
#define CONNECTED_SINCE(x) (NOW - (x))
-#define IPCHECK_CLONE_LIMIT 4
-#define IPCHECK_CLONE_PERIOD 40
-#define IPCHECK_CLONE_DELAY 600
+#define IPCHECK_CLONE_LIMIT feature_int(FEAT_IPCHECK_CLONE_LIMIT)
+#define IPCHECK_CLONE_PERIOD feature_int(FEAT_IPCHECK_CLONE_PERIOD)
+#define IPCHECK_CLONE_DELAY feature_int(FEAT_IPCHECK_CLONE_DELAY)
static struct IPRegistryEntry* hashTable[IP_REGISTRY_TABLE_SIZE];
static struct IPRegistryEntry* freeList = 0;
-static unsigned int ip_registry_hash(unsigned int ip)
+static struct Timer expireTimer;
+
+static unsigned int ip_registry_hash(const struct irc_in_addr *ip)
{
- return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
+ unsigned int res;
+
+ if (ip->in6_16[0] || ip->in6_16[1] || ip->in6_16[2] || ip->in6_16[3] || ip->in6_16[4]) {
+ /* Only use the first 64 bits of address, since the last 64 bits
+ * tend to be under user control. */
+ res = ip->in6_16[0] ^ ip->in6_16[1] ^ ip->in6_16[2] ^ ip->in6_16[3];
+ } else {
+ /* Looks like an IPv4 address. */
+ res = ip->in6_16[6] ^ ip->in6_16[7];
+ }
+ return res & (IP_REGISTRY_TABLE_SIZE - 1);
}
-static struct IPRegistryEntry* ip_registry_find(unsigned int ip)
+static struct IPRegistryEntry* ip_registry_find(const struct irc_in_addr *ip)
{
struct IPRegistryEntry* entry = hashTable[ip_registry_hash(ip)];
- for ( ; entry; entry = entry->next) {
- if (entry->addr == ip)
+ for ( ; entry; entry = entry->next)
+ if (!irc_in_addr_cmp(ip, &entry->addr))
break;
- }
return entry;
}
static void ip_registry_add(struct IPRegistryEntry* entry)
{
- unsigned int bucket = ip_registry_hash(entry->addr);
+ unsigned int bucket = ip_registry_hash(&entry->addr);
entry->next = hashTable[bucket];
hashTable[bucket] = entry;
}
-
+
static void ip_registry_remove(struct IPRegistryEntry* entry)
{
- unsigned int bucket = ip_registry_hash(entry->addr);
+ unsigned int bucket = ip_registry_hash(&entry->addr);
if (hashTable[bucket] == entry)
hashTable[bucket] = entry->next;
else {
}
}
}
-
+
static struct IPRegistryEntry* ip_registry_new_entry()
{
struct IPRegistryEntry* entry = freeList;
}
}
-/*
- * ip_registry_expire
- */
-static void ip_registry_expire()
+/* Callback to run an expiry of the IPcheck registry */
+static void ip_registry_expire(struct Event* ev)
{
int i;
struct IPRegistryEntry* entry;
struct IPRegistryEntry* entry_next;
+ assert(ET_EXPIRE == ev_type(ev));
+ assert(0 != ev_timer(ev));
+
for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
for (entry = hashTable[i]; entry; entry = entry_next) {
entry_next = entry->next;
}
}
+/*
+ * IPcheck_init()
+ *
+ * Initializes the registry timer
+ */
+void IPcheck_init(void)
+{
+ timer_add(timer_init(&expireTimer), ip_registry_expire, 0, TT_PERIODIC, 60);
+}
+
/*
* IPcheck_local_connect
*
* cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
* where FREE_TARGETS may range from 0 till STARTTARGETS.
*/
-int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
+int ip_registry_check_local(const struct irc_in_addr *addr, time_t* next_target_out)
{
struct IPRegistryEntry* entry = ip_registry_find(addr);
unsigned int free_targets = STARTTARGETS;
-
+
if (0 == entry) {
entry = ip_registry_new_entry();
- entry->addr = addr; /* The IP number of registry entry */
+ memcpy(&entry->addr, addr, sizeof(entry->addr));
ip_registry_add(entry);
return 1;
}
* Don't allow more then 255 connects from one IP number, ever
*/
if (0 == ++entry->connected)
+ {
+ entry->connected--;
return 0;
+ }
if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
entry->attempts = 0;
#ifdef NOTHROTTLE
return 1;
#else
+ assert(entry->connected > 0);
--entry->connected;
return 0;
#endif
*/
int ip_registry_check_remote(struct Client* cptr, int is_burst)
{
- struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+ struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
/*
* Mark that we did add/update an IPregistry entry
SetIPChecked(cptr);
if (0 == entry) {
entry = ip_registry_new_entry();
- entry->addr = (cli_ip(cptr)).s_addr;
+ memcpy(&entry->addr, &cli_ip(cptr), sizeof(entry->addr));
if (is_burst)
entry->attempts = 0;
ip_registry_add(entry);
entry->last_connect = NOW;
}
}
- SetIPChecked(cptr);
return 1;
}
* a way that the client won't be penalized when trying to reconnect
* again.
*/
-void ip_registry_connect_fail(unsigned int addr)
+void ip_registry_connect_fail(const struct irc_in_addr *addr)
{
struct IPRegistryEntry* entry = ip_registry_find(addr);
if (entry)
- --entry->attempts;
+ {
+ if (0 == --entry->attempts)
+ ++entry->attempts;
+ }
}
/*
{
const char* tr = "";
unsigned int free_targets = STARTTARGETS;
- struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+ struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
if (!entry) {
Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
free_targets = entry->target->count;
tr = " tr";
}
- sendcmdto_one(&me, CMD_NOTICE, cptr, ":on %u ca %u(%u) ft %u(%u)%s",
- entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
+ sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
+ cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
free_targets, STARTTARGETS, tr);
}
*/
void ip_registry_disconnect(struct Client *cptr)
{
- struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+ struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
if (0 == entry) {
/*
* trying to find an entry for a server causes this to happen,
- * servers should never have FLAGS_IPCHECK set
+ * servers should never have FLAG_IPCHECK set
*/
return;
}
/*
* If this was the last one, set `last_connect' to disconnect time (used for expiration)
*/
+ /* assert(entry->connected > 0); */
if (0 == --entry->connected) {
if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD) {
/*
*
* Returns number of clients with the same IP number
*/
-int ip_registry_count(unsigned int addr)
+int ip_registry_count(const struct irc_in_addr *addr)
{
struct IPRegistryEntry* entry = ip_registry_find(addr);
return (entry) ? entry->connected : 0;
* cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
* where FREE_TARGETS may range from 0 till STARTTARGETS.
*/
-int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
+int IPcheck_local_connect(const struct irc_in_addr *a, time_t* next_target_out)
{
assert(0 != next_target_out);
- return ip_registry_check_local(a.s_addr, next_target_out);
+ return ip_registry_check_local(a, next_target_out);
}
/*
* a way that the client won't be penalized when trying to reconnect
* again.
*/
-void IPcheck_connect_fail(struct in_addr a)
+void IPcheck_connect_fail(const struct irc_in_addr *a)
{
- ip_registry_connect_fail(a.s_addr);
+ ip_registry_connect_fail(a);
}
/*
unsigned short IPcheck_nr(struct Client *cptr)
{
assert(0 != cptr);
- return ip_registry_count(cptr->ip.s_addr);
+ return ip_registry_count(&cli_ip(cptr));
}
-
-void IPcheck_expire()
-{
- static time_t next_expire = 0;
- if (next_expire < CurrentTime) {
- ip_registry_expire();
- next_expire = CurrentTime + 60;
- }
-}
-
-