ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/whowas.c
Revision: 7449
Committed: Fri Mar 11 17:22:15 2016 UTC (9 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 6177 byte(s)
Log Message:
- Further cleanups to recent WHOWAS changes

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 1997-2016 ircd-hybrid development team
5 adx 30 *
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
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file whowas.c
23     * \brief WHOWAS user cache.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1358 #include "list.h"
29 michael 7437 #include "mempool.h"
30 adx 30 #include "whowas.h"
31     #include "client.h"
32     #include "hash.h"
33     #include "irc_string.h"
34     #include "ircd.h"
35 michael 7437 #include "send.h"
36     #include "conf.h"
37 adx 30
38    
39 michael 7437 static mp_pool_t *whowas_pool;
40    
41     static dlink_list whowas_list; /*! Chain of struct Whowas pointers */
42 michael 7449 static dlink_list whowas_hash[HASHSIZE];
43 adx 30
44    
45 michael 7442 /*! \brief Initializes the whowas memory pool.
46 michael 7393 */
47 adx 30 void
48 michael 1997 whowas_init(void)
49     {
50 michael 7442 whowas_pool = mp_pool_new(sizeof(struct Whowas), MP_CHUNK_SIZE_WHOWAS);
51 michael 1997 }
52    
53 michael 7449 /*! \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     return &whowas_hash[hashv];
63     }
64    
65 michael 7437 /*! \brief Unlinks a Whowas struct from its associated lists.
66 michael 7442 * \param whowas Pointer to Whowas struct to be unlinked.
67 michael 7437 */
68     static struct Whowas *
69     whowas_unlink(struct Whowas *whowas)
70     {
71     if (!whowas) /* Can be NULL. */
72     return NULL;
73    
74     if (whowas->online)
75     dlinkDelete(&whowas->cnode, &whowas->online->whowas);
76    
77 michael 7449 dlinkDelete(&whowas->hnode, &whowas_hash[whowas->hashv]);
78 michael 7437 dlinkDelete(&whowas->lnode, &whowas_list);
79    
80     return whowas;
81     }
82    
83     /*! \brief Unlinks a Whowas struct from its associated lists
84     * and returns memory back to the pooling allocator.
85 michael 7442 * \param whowas Pointer to Whowas struct to be unlinked and freed.
86 michael 7437 */
87     static void
88     whowas_free(struct Whowas *whowas)
89     {
90     whowas_unlink(whowas);
91     mp_pool_release(whowas);
92     }
93    
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 *whowas;
102    
103     if (dlink_list_length(&whowas_list) >= ConfigGeneral.whowas_history_length)
104 michael 7441 whowas = whowas_unlink(whowas_list.tail->data); /* Re-use oldest item */
105 michael 7437 else
106     whowas = mp_pool_get(whowas_pool);
107    
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     whowas_trim(void)
117     {
118     while (dlink_list_length(&whowas_list) >= ConfigGeneral.whowas_history_length)
119     {
120     if (!whowas_list.tail->data)
121     return; /* whowas_list is now empty. No more items can be freed. */
122    
123     whowas_free(whowas_list.tail->data);
124     }
125     }
126    
127 michael 7393 /*! \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 michael 7442 * \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 michael 7393 */
133 michael 1997 void
134 michael 2300 whowas_add_history(struct Client *client_p, const int online)
135 adx 30 {
136 michael 7437 struct Whowas *whowas = whowas_make();
137 adx 30
138 michael 7437 if (!whowas) /* Can be NULL. */
139     return;
140    
141 michael 3273 assert(IsClient(client_p));
142 adx 30
143 michael 6654 whowas->hashv = strhash(client_p->name);
144     whowas->shide = IsHidden(client_p->servptr) != 0;
145     whowas->logoff = CurrentTime;
146 adx 30
147 michael 6654 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 adx 30
155     if (online)
156     {
157 michael 6654 whowas->online = client_p;
158     dlinkAdd(whowas, &whowas->cnode, &client_p->whowas);
159 adx 30 }
160     else
161 michael 6654 whowas->online = NULL;
162 adx 30
163 michael 7449 dlinkAdd(whowas, &whowas->hnode, &whowas_hash[whowas->hashv]);
164 michael 7437 dlinkAdd(whowas, &whowas->lnode, &whowas_list);
165 adx 30 }
166    
167 michael 7393 /*! \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 michael 7442 * \param client_p Pointer to Client struct
171 michael 7393 */
172 adx 30 void
173 michael 2300 whowas_off_history(struct Client *client_p)
174 adx 30 {
175 michael 7244 while (client_p->whowas.head)
176 adx 30 {
177 michael 7244 struct Whowas *whowas = client_p->whowas.head->data;
178 michael 1358
179 michael 6654 whowas->online = NULL;
180     dlinkDelete(&whowas->cnode, &client_p->whowas);
181 adx 30 }
182     }
183    
184 michael 7393 /*! \brief Returns the current client that was using the given
185     * nickname within the timelimit. Returns NULL, if no
186 michael 7437 * one found.
187 michael 7442 * \param name Name of the nick
188     * \param timelimit Maximum age for a client since log-off
189 michael 7393 */
190 adx 30 struct Client *
191 michael 7394 whowas_get_history(const char *name, uintmax_t timelimit)
192 adx 30 {
193 michael 4800 dlink_node *node = NULL;
194 adx 30
195     timelimit = CurrentTime - timelimit;
196    
197 michael 7449 DLINK_FOREACH(node, whowas_hash[strhash(name)].head)
198 adx 30 {
199 michael 6654 struct Whowas *whowas = node->data;
200 michael 1358
201 michael 6654 if (whowas->logoff < timelimit)
202 michael 1358 continue;
203 michael 7394 if (irccmp(name, whowas->name))
204 adx 30 continue;
205 michael 6654 return whowas->online;
206 adx 30 }
207    
208 michael 1298 return NULL;
209 adx 30 }
210    
211 michael 7437 /*! \brief For debugging. Counts allocated structures stored in whowas_list
212 michael 7393 */
213 adx 30 void
214 michael 6719 whowas_count_memory(unsigned int *const count, size_t *const bytes)
215 adx 30 {
216 michael 7437 (*count) = dlink_list_length(&whowas_list);
217     (*bytes) = dlink_list_length(&whowas_list) * sizeof(struct Whowas);
218 adx 30 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision