-#if 0
-struct IPregistry_vector {
- unsigned short length;
- unsigned short allocated_length;
- struct IPregistry *vector;
-};
-
-#define HASHTABSIZE 0x2000 /* Must be power of 2 */
-static struct IPregistry_vector IPregistry_hashtable[HASHTABSIZE];
-
-/*
- * Calculate a `hash' value between 0 and HASHTABSIZE, from the internet address `in_addr'.
- * Apply it immedeately to the table, effectively hiding the table itself.
- */
-#define CALCULATE_HASH(in_addr) \
- struct IPregistry_vector *hash; \
- do { unsigned int ip = (in_addr).s_addr; \
- hash = &IPregistry_hashtable[((ip >> 14) + (ip >> 7) + ip) & (HASHTABSIZE - 1)]; } while(0)
-
-/*
- * Fit `now' in an unsigned short, the advantage is that we use less memory
- * `struct IPregistry::last_connect' can be smaller while the only disadvantage
- * is that if someone reconnects after exactly 18 hours and 12 minutes, and NOBODY with the
- * same _hash_ value for this IP-number did disconnect in the meantime, then the server
- * will think he reconnected immedeately. In other words: No disadvantage at all.
- */
-#define BITMASK 0xffff /* Same number of bits as `struct IPregistry::last_connect' */
-#define HAS_TARGETS_MAGIC 15
-#define HAS_TARGETS(entry) ((entry)->free_targets == HAS_TARGETS_MAGIC)
-
-#if STARTTARGETS >= HAS_TARGETS_MAGIC
-#error "That doesn't fit in 4 bits, does it?"
-#endif
-
-/* IP(entry) returns the `struct in_addr' of the IPregistry. */
-#define IP(entry) (HAS_TARGETS(entry) ? (entry)->ip_targets.ptr->ip : (entry)->ip_targets.ip)
-#define FREE_TARGETS(entry) (HAS_TARGETS(entry) ? (entry)->ip_targets.ptr->free_targets : (entry)->free_targets)
-
-static unsigned short count = 10000, average_length = 4;
-
-static struct IPregistry *IPregistry_add(struct IPregistry_vector *iprv)
-{
- assert(0 != iprv);
- if (iprv->length == iprv->allocated_length)
- {
- iprv->allocated_length += 4;
- if (iprv->vector) {
- iprv->vector =
- (struct IPregistry*) MyRealloc(iprv->vector,
- iprv->allocated_length * sizeof(struct IPregistry));
- }
- else {
- iprv->vector =
- (struct IPregistry*) MyMalloc(
- iprv->allocated_length * sizeof(struct IPregistry));
- }
- }
- return &iprv->vector[iprv->length++];
-}
-
-static struct IPregistry *IPregistry_find(struct IPregistry_vector *iprv,
- struct in_addr ip)
-{
- if (iprv->length > 0)
- {
- struct IPregistry *i, *end = &iprv->vector[iprv->length];
- for (i = &iprv->vector[0]; i < end; ++i)
- if (IP(i).s_addr == ip.s_addr)
- return i;
- }
- return NULL;
-}
-
-static struct IPregistry *IPregistry_find_with_expire(struct IPregistry_vector
- *iprv, struct in_addr ip)
-{
- struct IPregistry *last;
- struct IPregistry *curr;
- struct IPregistry *retval = NULL;
-
- /*
- * if the vector is empty, IPcheck_disconnect will cause the server
- * to core when NDEBUG is defined
- */
- if (iprv->length < 1)
- return retval;
-
- last = &iprv->vector[iprv->length - 1];
-
- for (curr = &iprv->vector[0]; curr < last;)
- {
- if (IP(curr).s_addr == ip.s_addr)
- /* `curr' is element we looked for */
- retval = curr;
- else if (curr->connected == 0)
- {
- if (CONNECTED_SINCE(curr) > 600U) /* Don't touch this number, it has statistical significance */
- {
- /* `curr' expired */
- if (HAS_TARGETS(curr))
- MyFree(curr->ip_targets.ptr);
- *curr = *last--;
- iprv->length--;
- if (--count == 0)
- {
- /* Make ever 10000 disconnects an estimation of the average vector length */
- count = 10000;
- average_length =
- (UserStats.clients + UserStats.unknowns + UserStats.local_servers) / HASHTABSIZE;
- }
- /* Now check the new element (last) that was moved to this position */
- continue;
- }
- else if (CONNECTED_SINCE(curr) > 120U && HAS_TARGETS(curr))
- {
- /* Expire storage of targets */
- struct in_addr ip1 = curr->ip_targets.ptr->ip;
- curr->free_targets = curr->ip_targets.ptr->free_targets;
- MyFree(curr->ip_targets.ptr);
- curr->ip_targets.ip = ip1;
- }
- }
- /* Did not expire, check next element */
- ++curr;
- }
- /* Now check the last element in the list (curr == last) */
- if (IP(curr).s_addr == ip.s_addr)
- /* `curr' is element we looked for */
- retval = curr;
- else if (curr->connected == 0)
- {
- if (CONNECTED_SINCE(curr) > 600U) /* Don't touch this number, it has statistical significance */
- {
- /* `curr' expired */
- if (HAS_TARGETS(curr))
- MyFree(curr->ip_targets.ptr);
- iprv->length--;
- if (--count == 0)
- {
- /* Make ever 10000 disconnects an estimation of the average vector length */
- count = 10000;
- average_length =
- (UserStats.clients + UserStats.unknowns + UserStats.local_servers) / HASHTABSIZE;
- }
- }
- else if (CONNECTED_SINCE(curr) > 120U && HAS_TARGETS(curr))
- {
- /* Expire storage of targets */
- struct in_addr ip1 = curr->ip_targets.ptr->ip;
- curr->free_targets = curr->ip_targets.ptr->free_targets;
- MyFree(curr->ip_targets.ptr);
- curr->ip_targets.ip = ip1;
- }
- }
- /* Do we need to shrink the vector? */
- if (iprv->allocated_length > average_length
- && iprv->allocated_length - iprv->length >= 4)
- {
- struct IPregistry *newpos;
- iprv->allocated_length = iprv->length;
- newpos =
- (struct IPregistry *)MyRealloc(iprv->vector,
- iprv->allocated_length * sizeof(struct IPregistry));
- if (newpos != iprv->vector) /* Is this ever true? */
- {
- retval =
- (struct IPregistry *)((char *)retval + ((char *)newpos -
- (char *)iprv->vector));
- iprv->vector = newpos;
- }
- }
- return retval;
-}
-
-static void reset_connect_time(struct IPregistry *entry)
-{
- unsigned int previous_free_targets;
-
- /* Apply aging */
- previous_free_targets =
- FREE_TARGETS(entry) + CONNECTED_SINCE(entry) / TARGET_DELAY;
- if (previous_free_targets > STARTTARGETS)
- previous_free_targets = STARTTARGETS;
- if (HAS_TARGETS(entry))
- entry->ip_targets.ptr->free_targets = previous_free_targets;
- else
- entry->free_targets = previous_free_targets;
-
- entry->last_connect = NOW;
-}
-
-/*
- * IPcheck_local_connect
- *
- * Event:
- * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
- *
- * Action:
- * Update the IPcheck registry.
- * Return:
- * 1 : You're allowed to connect.
- * 0 : You're not allowed to connect.
- *
- * Throttling:
- *
- * A connection should be rejected when a connection from the same IP number was
- * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
- * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
- *
- * Free target inheritance:
- *
- * When the client is accepted, then the number of Free Targets
- * of the cptr is set to the value stored in the found IPregistry
- * structure, or left at STARTTARGETS. This can be done by changing
- * 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)
-{
- struct IPregistry *entry;
- CALCULATE_HASH(a);
- assert(0 != next_target_out);
-
- if (!(entry = IPregistry_find(hash, a)))
- {
- entry = IPregistry_add(hash);
- entry->ip_targets.ip = a; /* The IP number of registry entry */
- entry->last_connect = NOW; /* Seconds since last connect attempt */
- entry->connected = 1; /* connected clients for this IP */
- entry->connect_attempts = 1; /* Number attempts for this IP */
- entry->free_targets = STARTTARGETS; /* free targets a client gets */
- return 1;
- }
- /* Note that this also connects server connects.
- * It is hard and not interesting, to change that.
- *
- * Don't allow more then 255 connects from one IP number, ever
- */
- if (0 == ++entry->connected)
- return 0;
-
- if (CONNECTED_SINCE(entry) > IPCHECK_CLONE_PERIOD)
- entry->connect_attempts = 0;
-
- reset_connect_time(entry);
-
- if (0 == ++entry->connect_attempts) /* Check for overflow */
- --entry->connect_attempts;
-
- if (entry->connect_attempts <= IPCHECK_CLONE_LIMIT)
- *next_target_out = CurrentTime - (TARGET_DELAY * (FREE_TARGETS(entry) - 1));
-
- /* Don't refuse connection when we just rebooted the server */
- else if (CurrentTime - me.since > IPCHECK_CLONE_DELAY)
-#ifndef NOTHROTTLE
- return 0;
-#else
- return 1;
-#endif
- return 1;
-}
-
-/*
- * IPcheck_remote_connect
- *
- * Event:
- * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
- * and hostname `hostname'.
- *
- * Action:
- * Update the IPcheck registry.
- * Return -1 on failure, 0 on success.
- */
-int IPcheck_remote_connect(struct Client *cptr, const char *hostname,
- int is_burst)
-{
- struct IPregistry *entry;
- CALCULATE_HASH(cptr->ip);
- SetIPChecked(cptr); /* Mark that we did add/update an IPregistry entry */
- if (!(entry = IPregistry_find(hash, cptr->ip)))
- {
- entry = IPregistry_add(hash);
- entry->ip_targets.ip = cptr->ip; /* The IP number of registry entry */
- entry->last_connect = NOW; /* Seconds since last connect (attempt) */
- entry->connected = 1; /* Number of currently connected clients with this IP number */
- entry->connect_attempts = is_burst ? 1 : 0; /* Number of clients that connected with this IP number */
- entry->free_targets = STARTTARGETS; /* Number of free targets that a client gets on connect */
- }
- else
- {
-#ifdef GODMODE
- sendto_one(cptr,
- "%s NOTICE %s%s :I saw your face before my friend (connected: %u; connect_attempts %u; free_targets %u)",
- NumServ(&me), NumNick(cptr), entry->connected, entry->connect_attempts,
- FREE_TARGETS(entry));
-#endif
- if (++(entry->connected) == 0) /* Don't allow more then 255 connects from one IP number, ever */
- return -1;
- if (CONNECTED_SINCE(entry) > IPCHECK_CLONE_PERIOD)
- entry->connect_attempts = 0;
- if (!is_burst)
- {
- if (++(entry->connect_attempts) == 0) /* Check for overflow */
- --(entry->connect_attempts);
- reset_connect_time(entry);
- }
- }
- return 0;
-}