ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ipcache.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/ipcache.c (file contents):
Revision 4325 by michael, Fri Aug 1 18:06:07 2014 UTC vs.
Revision 8349 by michael, Sun Mar 4 13:24:03 2018 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 1997-2014 ircd-hybrid development team
4 > *  Copyright (c) 1997-2018 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 15 | Line 15
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18 < *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19   *  USA
20   */
21  
22   /*! \file ipcache.c
23   * \brief Routines to count connections from particular IP addresses.
24 < * \version $Id: ipcache.c 4324 2014-08-01 16:59:35Z michael $
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
# Line 37 | Line 37 | static dlink_list ip_hash_table[IP_HASH_
37   static mp_pool_t *ip_entry_pool;
38  
39  
40 –
40   /* ipcache_hash_address()
41   *
42   * input        - pointer to an irc_inaddr
# Line 49 | Line 48 | ipcache_hash_address(const struct irc_ss
48   {
49    if (addr->ss.ss_family == AF_INET)
50    {
51 <    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
51 >    const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
52      uint32_t hash = 0, ip = ntohl(v4->sin_addr.s_addr);
53  
54      hash = ((ip >> 12) + ip) & (IP_HASH_SIZE - 1);
55      return hash;
56    }
58 – #ifdef IPV6
57    else
58    {
59 <    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
60 <    uint32_t hash = 0, *ip = (uint32_t *)&v6->sin6_addr.s6_addr;
59 >    const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
60 >    uint32_t hash = 0, *const ip = (uint32_t *)&v6->sin6_addr.s6_addr;
61  
62      hash  = ip[0] ^ ip[3];
63      hash ^= hash >> 16;
# Line 67 | Line 65 | ipcache_hash_address(const struct irc_ss
65      hash  = hash & (IP_HASH_SIZE - 1);
66      return hash;
67    }
70 – #else
71 –  return 0;
72 – #endif
68   }
69  
70   /* ipcache_find_or_add_address()
# Line 82 | Line 77 | ipcache_hash_address(const struct irc_ss
77   * count set to 0.
78   */
79   struct ip_entry *
80 < ipcache_find_or_add_address(struct irc_ssaddr *addr)
80 > ipcache_find_or_add_address(const struct irc_ssaddr *addr)
81   {
82 <  dlink_node *ptr = NULL;
83 <  struct ip_entry *iptr = NULL;
84 <  uint32_t hash_index = ipcache_hash_address(addr);
85 <  int res = 0;
91 <  struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4;
92 < #ifdef IPV6
93 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6;
94 < #endif
82 >  dlink_node *node;
83 >  const uint32_t hash_index = ipcache_hash_address(addr);
84 >  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr, *ptr_v4;
85 >  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr, *ptr_v6;
86  
87 <  DLINK_FOREACH(ptr, ip_hash_table[hash_index].head)
87 >  DLINK_FOREACH(node, ip_hash_table[hash_index].head)
88    {
89 <    iptr = ptr->data;
90 < #ifdef IPV6
89 >    struct ip_entry *iptr = node->data;
90 >
91      if (iptr->ip.ss.ss_family != addr->ss.ss_family)
92        continue;
93  
94      if (addr->ss.ss_family == AF_INET6)
95      {
96 <      ptr_v6 = (struct sockaddr_in6 *)&iptr->ip;
97 <      res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr));
96 >      ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
97 >      if (!memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
98 >        return iptr;  /* Found entry already in hash, return it. */
99      }
100      else
109 – #endif
101      {
102 <      ptr_v4 = (struct sockaddr_in *)&iptr->ip;
103 <      res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr));
102 >      ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
103 >      if (!memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
104 >        return iptr;  /* Found entry already in hash, return it. */
105      }
114 –
115 –    if (res == 0)
116 –      return iptr;  /* Found entry already in hash, return it. */
106    }
107  
108 <  iptr = mp_pool_get(ip_entry_pool);
108 >  struct ip_entry *iptr = mp_pool_get(ip_entry_pool);
109    memcpy(&iptr->ip, addr, sizeof(struct irc_ssaddr));
110  
111    dlinkAdd(iptr, &iptr->node, &ip_hash_table[hash_index]);
# Line 134 | Line 123 | ipcache_find_or_add_address(struct irc_s
123   *                 the struct ip_entry is returned to the ip_entry_heap
124   */
125   void
126 < ipcache_remove_address(struct irc_ssaddr *addr)
126 > ipcache_remove_address(const struct irc_ssaddr *addr)
127   {
128 <  dlink_node *ptr = NULL;
129 <  uint32_t hash_index = ipcache_hash_address(addr);
130 <  int res = 0;
131 <  struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4;
143 < #ifdef IPV6
144 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6;
145 < #endif
128 >  dlink_node *node;
129 >  const uint32_t hash_index = ipcache_hash_address(addr);
130 >  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr, *ptr_v4;
131 >  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr, *ptr_v6;
132  
133 <  DLINK_FOREACH(ptr, ip_hash_table[hash_index].head)
133 >  DLINK_FOREACH(node, ip_hash_table[hash_index].head)
134    {
135 <    struct ip_entry *iptr = ptr->data;
136 < #ifdef IPV6
135 >    struct ip_entry *iptr = node->data;
136 >
137      if (iptr->ip.ss.ss_family != addr->ss.ss_family)
138        continue;
139 +
140      if (addr->ss.ss_family == AF_INET6)
141      {
142 <      ptr_v6 = (struct sockaddr_in6 *)&iptr->ip;
143 <      res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr));
142 >      ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
143 >      if (memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
144 >        continue;
145      }
146      else
159 – #endif
147      {
148 <      ptr_v4 = (struct sockaddr_in *)&iptr->ip;
149 <      res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr));
148 >      ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
149 >      if (memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
150 >        continue;
151      }
152  
165 –    if (res)
166 –      continue;
153      assert(iptr->count > 0);
154  
155      if (--iptr->count == 0 &&
156 <        (CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time)
156 >        (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
157      {
158        dlinkDelete(&iptr->node, &ip_hash_table[hash_index]);
159        mp_pool_release(iptr);
# Line 185 | Line 171 | ipcache_remove_address(struct irc_ssaddr
171   static void
172   ipcache_remove_expired_entries(void *unused)
173   {
174 <  dlink_node *ptr = NULL, *ptr_next = NULL;
174 >  dlink_node *node, *node_next;
175  
176    for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
177    {
178 <    DLINK_FOREACH_SAFE(ptr, ptr_next, ip_hash_table[i].head)
178 >    DLINK_FOREACH_SAFE(node, node_next, ip_hash_table[i].head)
179      {
180 <      struct ip_entry *iptr = ptr->data;
180 >      struct ip_entry *iptr = node->data;
181  
182        if (iptr->count == 0 &&
183 <          (CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time)
183 >          (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
184        {
185          dlinkDelete(&iptr->node, &ip_hash_table[i]);
186          mp_pool_release(iptr);
# Line 214 | Line 200 | ipcache_remove_expired_entries(void *unu
200   * used in the hash.
201   */
202   void
203 < ipcache_get_stats(unsigned int *number_ips_stored, uint64_t *mem_ips_stored)
203 > ipcache_get_stats(unsigned int *const number_ips_stored, size_t *const mem_ips_stored)
204   {
219 –  *number_ips_stored = 0;
220 –  *mem_ips_stored    = 0;
221 –
205    for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
206      *number_ips_stored += dlink_list_length(&ip_hash_table[i]);
207    *mem_ips_stored = *number_ips_stored * sizeof(struct ip_entry);

Comparing ircd-hybrid/trunk/src/ipcache.c (property svn:eol-style):
Revision 4325 by michael, Fri Aug 1 18:06:07 2014 UTC vs.
Revision 8349 by michael, Sun Mar 4 13:24:03 2018 UTC

# Line 0 | Line 1
1 + native

Comparing ircd-hybrid/trunk/src/ipcache.c (property svn:keywords):
Revision 4325 by michael, Fri Aug 1 18:06:07 2014 UTC vs.
Revision 8349 by michael, Sun Mar 4 13:24:03 2018 UTC

# Line 0 | Line 1
1 + Id

Diff Legend

– Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)