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

Comparing:
ircd-hybrid/src/whowas.c (file contents), Revision 1028 by michael, Sun Nov 8 13:03:38 2009 UTC vs.
ircd-hybrid/trunk/src/whowas.c (file contents), Revision 7449 by michael, Fri Mar 11 17:22:15 2016 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  whowas.c: WHOWAS user cache.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2005 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2016 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 16 | 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 < *  $Id$
20 > */
21 >
22 > /*! \file whowas.c
23 > * \brief WHOWAS user cache.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
28 + #include "list.h"
29 + #include "mempool.h"
30   #include "whowas.h"
31   #include "client.h"
28 #include "common.h"
32   #include "hash.h"
33   #include "irc_string.h"
34   #include "ircd.h"
32 #include "ircd_defs.h"
33 #include "numeric.h"
34 #include "s_serv.h"
35 #include "s_user.h"
35   #include "send.h"
36 < #include "s_conf.h"
38 < #include "memory.h"
36 > #include "conf.h"
37  
40 /* internally defined function */
41 static void add_whowas_to_clist(struct Whowas **, struct Whowas *);
42 static void del_whowas_from_clist(struct Whowas **, struct Whowas *);
43 static void add_whowas_to_list(struct Whowas **, struct Whowas *);
44 static void del_whowas_from_list(struct Whowas **, struct Whowas *);
38  
39 < struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH];
47 < struct Whowas *WHOWASHASH[HASHSIZE];
39 > static mp_pool_t *whowas_pool;
40  
41 < static unsigned int whowas_next = 0;
41 > static dlink_list whowas_list;  /*! Chain of struct Whowas pointers */
42 > static dlink_list whowas_hash[HASHSIZE];
43  
44 +
45 + /*! \brief Initializes the whowas memory pool.
46 + */
47   void
48 < add_history(struct Client *client_p, int online)
48 > whowas_init(void)
49   {
50 <  struct Whowas *who = &WHOWAS[whowas_next];
51 <
56 <  assert(client_p && client_p->servptr);
57 <
58 <  if (who->hashv != -1)
59 <  {
60 <    if (who->online)
61 <      del_whowas_from_clist(&(who->online->whowas),who);
62 <    del_whowas_from_list(&WHOWASHASH[who->hashv], who);
63 <  }
50 >  whowas_pool = mp_pool_new(sizeof(struct Whowas), MP_CHUNK_SIZE_WHOWAS);
51 > }
52  
53 <  who->hashv = strhash(client_p->name);
54 <  who->logoff = CurrentTime;
53 > /*! \brief Returns a slot of the whowas_hash by the hash value associated with it.
54 > * \param hashv Hash value.
55 > */
56 > const dlink_list *
57 > whowas_get_hash(unsigned int hashv)
58 > {
59 >  if (hashv >= HASHSIZE)
60 >    return NULL;
61  
62 <  /* NOTE: strcpy ok here, the sizes in the client struct MUST
63 <   * match the sizes in the whowas struct
70 <   */
71 <  strlcpy(who->name, client_p->name, sizeof(who->name));
72 <  strcpy(who->username, client_p->username);
73 <  strcpy(who->hostname, client_p->host);
74 <  strcpy(who->realname, client_p->info);
62 >  return &whowas_hash[hashv];
63 > }
64  
65 <  strlcpy(who->servername, client_p->servptr->name, sizeof(who->servername));
65 > /*! \brief Unlinks a Whowas struct from its associated lists.
66 > * \param whowas Pointer to Whowas struct to be unlinked.
67 > */
68 > static struct Whowas *
69 > whowas_unlink(struct Whowas *whowas)
70 > {
71 >  if (!whowas)  /* Can be NULL. */
72 >    return NULL;
73  
74 <  if (online)
75 <  {
80 <    who->online = client_p;
81 <    add_whowas_to_clist(&(client_p->whowas), who);
82 <  }
83 <  else
84 <    who->online = NULL;
74 >  if (whowas->online)
75 >    dlinkDelete(&whowas->cnode, &whowas->online->whowas);
76  
77 <  add_whowas_to_list(&WHOWASHASH[who->hashv], who);
78 <  whowas_next++;
77 >  dlinkDelete(&whowas->hnode, &whowas_hash[whowas->hashv]);
78 >  dlinkDelete(&whowas->lnode, &whowas_list);
79  
80 <  if (whowas_next == NICKNAMEHISTORYLENGTH)
90 <    whowas_next = 0;
80 >  return whowas;
81   }
82  
83 < void
84 < off_history(struct Client *client_p)
83 > /*! \brief Unlinks a Whowas struct from its associated lists
84 > *         and returns memory back to the pooling allocator.
85 > * \param whowas Pointer to Whowas struct to be unlinked and freed.
86 > */
87 > static void
88 > whowas_free(struct Whowas *whowas)
89   {
90 <  struct Whowas *temp, *next;
91 <
98 <  for (temp = client_p->whowas; temp; temp=next)
99 <  {
100 <    next = temp->cnext;
101 <    temp->online = NULL;
102 <    del_whowas_from_clist(&(client_p->whowas), temp);
103 <  }
90 >  whowas_unlink(whowas);
91 >  mp_pool_release(whowas);
92   }
93  
94 < struct Client *
95 < get_history(const char *nick, time_t timelimit)
94 > /*! \brief Returns a Whowas struct for further use. Either allocates
95 > *         a new one, or returns the oldest entry from the whowas_list
96 > *         if it ran over ConfigGeneral.whowas_history_length
97 > */
98 > static struct Whowas *
99 > whowas_make(void)
100   {
101 <  struct Whowas *temp;
110 <
111 <  timelimit = CurrentTime - timelimit;
112 <  temp = WHOWASHASH[strhash(nick)];
101 >  struct Whowas *whowas;
102  
103 <  for (; temp; temp = temp->next)
104 <  {
105 <    if (irccmp(nick, temp->name))
106 <      continue;
118 <    if (temp->logoff < timelimit)
119 <      continue;
120 <    return(temp->online);
121 <  }
103 >  if (dlink_list_length(&whowas_list) >= ConfigGeneral.whowas_history_length)
104 >    whowas = whowas_unlink(whowas_list.tail->data);  /* Re-use oldest item */
105 >  else
106 >    whowas = mp_pool_get(whowas_pool);
107  
108 <  return(NULL);
108 >  return whowas;
109   }
110  
111 + /*! \brief Trims the whowas_list if necessary until there are no
112 + *         more than ConfigGeneral.whowas_history_length Whowas
113 + *         struct items.
114 + */
115   void
116 < count_whowas_memory(unsigned int *wwu, uint64_t *wwum)
116 > whowas_trim(void)
117   {
118 <  const struct Whowas *tmp;
130 <  int i;
131 <  unsigned int u = 0;
132 <  uint64_t um = 0;
133 <
134 <  /* count the number of used whowas structs in 'u'   */
135 <  /* count up the memory used of whowas structs in um */
136 <  for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; ++i, ++tmp)
118 >  while (dlink_list_length(&whowas_list) >= ConfigGeneral.whowas_history_length)
119    {
120 <    if (tmp->hashv != -1)
121 <    {
140 <      ++u;
141 <      um += sizeof(struct Whowas);
142 <    }
143 <  }
120 >    if (!whowas_list.tail->data)
121 >      return;  /* whowas_list is now empty. No more items can be freed. */
122  
123 <  *wwu = u;
124 <  *wwum = um;
123 >    whowas_free(whowas_list.tail->data);
124 >  }
125   }
126  
127 + /*! \brief Adds the currently defined name of the client to history.
128 + *         Usually called before changing to a new name (nick).
129 + *         Client must be a fully registered user.
130 + * \param client_p Pointer to Client struct to add to whowas history
131 + * \param online   Either 1 if it's a nick change or 0 on client exit
132 + */
133   void
134 < init_whowas(void)
134 > whowas_add_history(struct Client *client_p, const int online)
135   {
136 <  int i;
136 >  struct Whowas *whowas = whowas_make();
137 >
138 >  if (!whowas)  /* Can be NULL. */
139 >    return;
140  
141 <  for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
141 >  assert(IsClient(client_p));
142 >
143 >  whowas->hashv = strhash(client_p->name);
144 >  whowas->shide = IsHidden(client_p->servptr) != 0;
145 >  whowas->logoff = CurrentTime;
146 >
147 >  strlcpy(whowas->account, client_p->account, sizeof(whowas->account));
148 >  strlcpy(whowas->name, client_p->name, sizeof(whowas->name));
149 >  strlcpy(whowas->username, client_p->username, sizeof(whowas->username));
150 >  strlcpy(whowas->hostname, client_p->host, sizeof(whowas->hostname));
151 >  strlcpy(whowas->sockhost, client_p->sockhost, sizeof(whowas->sockhost));
152 >  strlcpy(whowas->realname, client_p->info, sizeof(whowas->realname));
153 >  strlcpy(whowas->servername, client_p->servptr->name, sizeof(whowas->servername));
154 >
155 >  if (online)
156    {
157 <    memset(&WHOWAS[i], 0, sizeof(struct Whowas));
158 <    WHOWAS[i].hashv = -1;
157 >    whowas->online = client_p;
158 >    dlinkAdd(whowas, &whowas->cnode, &client_p->whowas);
159    }
160 +  else
161 +    whowas->online = NULL;
162  
163 <  for (i = 0; i < HASHSIZE; ++i)
164 <    WHOWASHASH[i] = NULL;        
163 >  dlinkAdd(whowas, &whowas->hnode, &whowas_hash[whowas->hashv]);
164 >  dlinkAdd(whowas, &whowas->lnode, &whowas_list);
165   }
166  
167 < static void
168 < add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas)
167 > /*! \brief This must be called when the client structure is about to
168 > *         be released. History mechanism keeps pointers to client
169 > *         structures and it must know when they cease to exist.
170 > * \param client_p Pointer to Client struct
171 > */
172 > void
173 > whowas_off_history(struct Client *client_p)
174   {
175 <  whowas->cprev = NULL;
175 >  while (client_p->whowas.head)
176 >  {
177 >    struct Whowas *whowas = client_p->whowas.head->data;
178  
179 <  if ((whowas->cnext = *bucket) != NULL)
180 <    whowas->cnext->cprev = whowas;
181 <  *bucket = whowas;
172 < }
173 <
174 < static void
175 < del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas)
176 < {
177 <  if (whowas->cprev)
178 <    whowas->cprev->cnext = whowas->cnext;
179 <  else
180 <    *bucket = whowas->cnext;
181 <  if (whowas->cnext)
182 <    whowas->cnext->cprev = whowas->cprev;
179 >    whowas->online = NULL;
180 >    dlinkDelete(&whowas->cnode, &client_p->whowas);
181 >  }
182   }
183  
184 < static void
185 < add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas)
184 > /*! \brief Returns the current client that was using the given
185 > *         nickname within the timelimit. Returns NULL, if no
186 > *         one found.
187 > * \param name      Name of the nick
188 > * \param timelimit Maximum age for a client since log-off
189 > */
190 > struct Client *
191 > whowas_get_history(const char *name, uintmax_t timelimit)
192   {
193 <  whowas->prev = NULL;
193 >  dlink_node *node = NULL;
194  
195 <  if ((whowas->next = *bucket) != NULL)
196 <    whowas->next->prev = whowas;
197 <  *bucket = whowas;
195 >  timelimit = CurrentTime - timelimit;
196 >
197 >  DLINK_FOREACH(node, whowas_hash[strhash(name)].head)
198 >  {
199 >    struct Whowas *whowas = node->data;
200 >
201 >    if (whowas->logoff < timelimit)
202 >      continue;
203 >    if (irccmp(name, whowas->name))
204 >      continue;
205 >    return whowas->online;
206 >  }
207 >
208 >  return NULL;
209   }
210 <
211 < static void
212 < del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas)
210 >
211 > /*! \brief For debugging. Counts allocated structures stored in whowas_list
212 > */
213 > void
214 > whowas_count_memory(unsigned int *const count, size_t *const bytes)
215   {
216 <  if (whowas->prev)
217 <    whowas->prev->next = whowas->next;
200 <  else
201 <    *bucket = whowas->next;
202 <  if (whowas->next)
203 <    whowas->next->prev = whowas->prev;
216 >  (*count) = dlink_list_length(&whowas_list);
217 >  (*bytes) = dlink_list_length(&whowas_list) * sizeof(struct Whowas);
218   }

Diff Legend

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