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-2014 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 |
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: whowas.c,v 7.31 2005/09/19 06:15:21 metalrock Exp $ |
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 "whowas.h" |
30 |
|
#include "client.h" |
28 |
– |
#include "common.h" |
31 |
|
#include "hash.h" |
32 |
|
#include "irc_string.h" |
33 |
|
#include "ircd.h" |
32 |
– |
#include "ircd_defs.h" |
33 |
– |
#include "numeric.h" |
34 |
– |
#include "s_serv.h" |
35 |
– |
#include "s_user.h" |
36 |
– |
#include "send.h" |
37 |
– |
#include "s_conf.h" |
38 |
– |
#include "memory.h" |
39 |
– |
|
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 *); |
34 |
|
|
46 |
– |
struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH]; |
47 |
– |
struct Whowas *WHOWASHASH[HASHSIZE]; |
35 |
|
|
36 |
< |
static unsigned int whowas_next = 0; |
36 |
> |
static struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH]; |
37 |
> |
dlink_list WHOWASHASH[HASHSIZE]; |
38 |
> |
|
39 |
|
|
40 |
|
void |
41 |
< |
add_history(struct Client *client_p, int online) |
41 |
> |
whowas_init(void) |
42 |
|
{ |
43 |
< |
struct Whowas *who = &WHOWAS[whowas_next]; |
43 |
> |
for (unsigned int i = 0; i < NICKNAMEHISTORYLENGTH; ++i) |
44 |
> |
WHOWAS[i].hashv = -1; |
45 |
> |
} |
46 |
|
|
47 |
< |
assert(client_p != NULL); |
47 |
> |
void |
48 |
> |
whowas_add_history(struct Client *client_p, const int online) |
49 |
> |
{ |
50 |
> |
static unsigned int whowas_next = 0; |
51 |
> |
struct Whowas *const who = &WHOWAS[whowas_next]; |
52 |
|
|
53 |
< |
if (client_p == NULL) |
59 |
< |
return; |
53 |
> |
assert(IsClient(client_p)); |
54 |
|
|
55 |
< |
/* XXX when is this possible? Looks like it could happen |
56 |
< |
* (with a half registered client.) |
63 |
< |
* and what is the correct action here? - Dianora |
64 |
< |
*/ |
65 |
< |
if (client_p->servptr == NULL) |
66 |
< |
return; |
55 |
> |
if (++whowas_next == NICKNAMEHISTORYLENGTH) |
56 |
> |
whowas_next = 0; |
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); |
61 |
> |
dlinkDelete(&who->cnode, &who->online->whowas); |
62 |
> |
|
63 |
> |
dlinkDelete(&who->tnode, &WHOWASHASH[who->hashv]); |
64 |
|
} |
65 |
|
|
66 |
< |
who->hashv = strhash(client_p->name); |
66 |
> |
who->hashv = strhash(client_p->name); |
67 |
> |
who->shide = IsHidden(client_p->servptr) != 0; |
68 |
|
who->logoff = CurrentTime; |
69 |
|
|
70 |
< |
/* NOTE: strcpy ok here, the sizes in the client struct MUST |
79 |
< |
* match the sizes in the whowas struct |
80 |
< |
*/ |
70 |
> |
strlcpy(who->svid, client_p->svid, sizeof(who->svid)); |
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); |
85 |
< |
|
72 |
> |
strlcpy(who->username, client_p->username, sizeof(who->username)); |
73 |
> |
strlcpy(who->hostname, client_p->host, sizeof(who->hostname)); |
74 |
> |
strlcpy(who->realname, client_p->info, sizeof(who->realname)); |
75 |
|
strlcpy(who->servername, client_p->servptr->name, sizeof(who->servername)); |
76 |
|
|
77 |
|
if (online) |
78 |
|
{ |
79 |
|
who->online = client_p; |
80 |
< |
add_whowas_to_clist(&(client_p->whowas), who); |
80 |
> |
dlinkAdd(who, &who->cnode, &client_p->whowas); |
81 |
|
} |
82 |
|
else |
83 |
|
who->online = NULL; |
84 |
|
|
85 |
< |
add_whowas_to_list(&WHOWASHASH[who->hashv], who); |
97 |
< |
whowas_next++; |
98 |
< |
|
99 |
< |
if (whowas_next == NICKNAMEHISTORYLENGTH) |
100 |
< |
whowas_next = 0; |
85 |
> |
dlinkAdd(who, &who->tnode, &WHOWASHASH[who->hashv]); |
86 |
|
} |
87 |
|
|
88 |
|
void |
89 |
< |
off_history(struct Client *client_p) |
89 |
> |
whowas_off_history(struct Client *client_p) |
90 |
|
{ |
91 |
< |
struct Whowas *temp, *next; |
91 |
> |
dlink_node *ptr = NULL, *ptr_next = NULL; |
92 |
|
|
93 |
< |
for (temp = client_p->whowas; temp; temp=next) |
93 |
> |
DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->whowas.head) |
94 |
|
{ |
95 |
< |
next = temp->cnext; |
95 |
> |
struct Whowas *temp = ptr->data; |
96 |
> |
|
97 |
|
temp->online = NULL; |
98 |
< |
del_whowas_from_clist(&(client_p->whowas), temp); |
98 |
> |
dlinkDelete(&temp->cnode, &client_p->whowas); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
struct Client * |
103 |
< |
get_history(const char *nick, time_t timelimit) |
103 |
> |
whowas_get_history(const char *nick, time_t timelimit) |
104 |
|
{ |
105 |
< |
struct Whowas *temp; |
105 |
> |
dlink_node *ptr = NULL; |
106 |
|
|
107 |
|
timelimit = CurrentTime - timelimit; |
122 |
– |
temp = WHOWASHASH[strhash(nick)]; |
108 |
|
|
109 |
< |
for (; temp; temp = temp->next) |
109 |
> |
DLINK_FOREACH(ptr, WHOWASHASH[strhash(nick)].head) |
110 |
|
{ |
111 |
< |
if (irccmp(nick, temp->name)) |
112 |
< |
continue; |
111 |
> |
struct Whowas *temp = ptr->data; |
112 |
> |
|
113 |
|
if (temp->logoff < timelimit) |
114 |
|
continue; |
115 |
< |
return(temp->online); |
115 |
> |
if (irccmp(nick, temp->name)) |
116 |
> |
continue; |
117 |
> |
return temp->online; |
118 |
|
} |
119 |
|
|
120 |
< |
return(NULL); |
120 |
> |
return NULL; |
121 |
|
} |
122 |
|
|
123 |
|
void |
124 |
< |
count_whowas_memory(int *wwu, unsigned long *wwum) |
124 |
> |
whowas_count_memory(unsigned int *const count, uint64_t *const bytes) |
125 |
|
{ |
126 |
< |
struct Whowas *tmp; |
127 |
< |
int i; |
128 |
< |
int u = 0; |
142 |
< |
unsigned long um = 0; |
143 |
< |
|
144 |
< |
/* count the number of used whowas structs in 'u' */ |
145 |
< |
/* count up the memory used of whowas structs in um */ |
146 |
< |
for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++) |
126 |
> |
const struct Whowas *tmp = &WHOWAS[0]; |
127 |
> |
|
128 |
> |
for (unsigned int i = 0; i < NICKNAMEHISTORYLENGTH; ++i, ++tmp) |
129 |
|
{ |
130 |
|
if (tmp->hashv != -1) |
131 |
|
{ |
132 |
< |
u++; |
133 |
< |
um += sizeof(struct Whowas); |
132 |
> |
(*count)++; |
133 |
> |
(*bytes) += sizeof(struct Whowas); |
134 |
|
} |
135 |
|
} |
154 |
– |
|
155 |
– |
*wwu = u; |
156 |
– |
*wwum = um; |
157 |
– |
} |
158 |
– |
|
159 |
– |
void |
160 |
– |
init_whowas(void) |
161 |
– |
{ |
162 |
– |
int i; |
163 |
– |
|
164 |
– |
for (i = 0; i < NICKNAMEHISTORYLENGTH; i++) |
165 |
– |
{ |
166 |
– |
memset(&WHOWAS[i], 0, sizeof(struct Whowas)); |
167 |
– |
WHOWAS[i].hashv = -1; |
168 |
– |
} |
169 |
– |
|
170 |
– |
for (i = 0; i < HASHSIZE; ++i) |
171 |
– |
WHOWASHASH[i] = NULL; |
172 |
– |
} |
173 |
– |
|
174 |
– |
static void |
175 |
– |
add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas) |
176 |
– |
{ |
177 |
– |
whowas->cprev = NULL; |
178 |
– |
|
179 |
– |
if ((whowas->cnext = *bucket) != NULL) |
180 |
– |
whowas->cnext->cprev = whowas; |
181 |
– |
*bucket = whowas; |
182 |
– |
} |
183 |
– |
|
184 |
– |
static void |
185 |
– |
del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas) |
186 |
– |
{ |
187 |
– |
if (whowas->cprev) |
188 |
– |
whowas->cprev->cnext = whowas->cnext; |
189 |
– |
else |
190 |
– |
*bucket = whowas->cnext; |
191 |
– |
if (whowas->cnext) |
192 |
– |
whowas->cnext->cprev = whowas->cprev; |
193 |
– |
} |
194 |
– |
|
195 |
– |
static void |
196 |
– |
add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas) |
197 |
– |
{ |
198 |
– |
whowas->prev = NULL; |
199 |
– |
|
200 |
– |
if ((whowas->next = *bucket) != NULL) |
201 |
– |
whowas->next->prev = whowas; |
202 |
– |
*bucket = whowas; |
203 |
– |
} |
204 |
– |
|
205 |
– |
static void |
206 |
– |
del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas) |
207 |
– |
{ |
208 |
– |
if (whowas->prev) |
209 |
– |
whowas->prev->next = whowas->next; |
210 |
– |
else |
211 |
– |
*bucket = whowas->next; |
212 |
– |
if (whowas->next) |
213 |
– |
whowas->next->prev = whowas->prev; |
136 |
|
} |