]> jfr.im git - irc/rqf/shadowircd.git/blame - src/scache.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.git] / src / scache.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * scache.c: Server names cache.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
212380e3 24 */
25
26#include "stdinc.h"
27#include "client.h"
28#include "common.h"
13ae2f4b 29#include "match.h"
212380e3 30#include "ircd.h"
31#include "numeric.h"
32#include "send.h"
33#include "scache.h"
994544c2 34#include "s_conf.h"
212380e3 35
36
37/*
38 * ircd used to store full servernames in anUser as well as in the
39 * whowas info. there can be some 40k such structures alive at any
40 * given time, while the number of unique server names a server sees
41 * in its lifetime is at most a few hundred. by tokenizing server
42 * names internally, the server can easily save 2 or 3 megs of RAM.
43 * -orabidoo
994544c2
JT
44 *
45 * reworked to serve for flattening/delaying /links also
46 * -- jilles
212380e3 47 */
48
49
50#define SCACHE_HASH_SIZE 257
51
994544c2
JT
52#define SC_ONLINE 1
53#define SC_HIDDEN 2
54
55struct scache_entry
212380e3 56{
57 char name[HOSTLEN + 1];
994544c2
JT
58 char info[REALLEN + 1];
59 int flags;
60 time_t known_since;
61 time_t last_connect;
62 time_t last_split;
212380e3 63 struct scache_entry *next;
994544c2 64};
212380e3 65
994544c2 66static struct scache_entry *scache_hash[SCACHE_HASH_SIZE];
212380e3 67
68void
69clear_scache_hash_table(void)
70{
71 memset(scache_hash, 0, sizeof(scache_hash));
72}
73
74static int
75sc_hash(const char *string)
76{
77 int hash_value;
78
79 hash_value = 0;
80 while (*string)
81 {
82 hash_value += ToLower(*string++);
83 }
84
85 return hash_value % SCACHE_HASH_SIZE;
86}
87
994544c2 88static struct scache_entry *
212380e3 89find_or_add(const char *name)
90{
91 int hash_index;
994544c2 92 struct scache_entry *ptr;
212380e3 93
94 ptr = scache_hash[hash_index = sc_hash(name)];
95 for (; ptr; ptr = ptr->next)
96 {
97 if(!irccmp(ptr->name, name))
994544c2 98 return ptr;
212380e3 99 }
100
8e43b0b4 101 ptr = (struct scache_entry *) rb_malloc(sizeof(struct scache_entry));
212380e3 102 s_assert(0 != ptr);
103
907468c4 104 rb_strlcpy(ptr->name, name, sizeof(ptr->name));
994544c2
JT
105 ptr->info[0] = '\0';
106 ptr->flags = 0;
9f6bbe3c 107 ptr->known_since = rb_current_time();
994544c2
JT
108 ptr->last_connect = 0;
109 ptr->last_split = 0;
212380e3 110
111 ptr->next = scache_hash[hash_index];
112 scache_hash[hash_index] = ptr;
994544c2
JT
113 return ptr;
114}
115
116struct scache_entry *
117scache_connect(const char *name, const char *info, int hidden)
118{
119 struct scache_entry *ptr;
120
121 ptr = find_or_add(name);
907468c4 122 rb_strlcpy(ptr->info, info, sizeof(ptr->info));
994544c2
JT
123 ptr->flags |= SC_ONLINE;
124 if (hidden)
125 ptr->flags |= SC_HIDDEN;
126 else
127 ptr->flags &= ~SC_HIDDEN;
9f6bbe3c 128 ptr->last_connect = rb_current_time();
994544c2
JT
129 return ptr;
130}
131
132void
133scache_split(struct scache_entry *ptr)
134{
135 if (ptr == NULL)
136 return;
137 ptr->flags &= ~SC_ONLINE;
9f6bbe3c 138 ptr->last_split = rb_current_time();
994544c2
JT
139}
140
141const char *scache_get_name(struct scache_entry *ptr)
142{
212380e3 143 return ptr->name;
144}
145
994544c2
JT
146/* scache_send_flattened_links()
147 *
148 * inputs - client to send to
149 * outputs - the cached links, us, and RPL_ENDOFLINKS
150 * side effects -
151 */
152void
153scache_send_flattened_links(struct Client *source_p)
154{
155 struct scache_entry *scache_ptr;
156 int i;
157 int show;
158
159 for (i = 0; i < SCACHE_HASH_SIZE; i++)
160 {
161 scache_ptr = scache_hash[i];
162 while (scache_ptr)
163 {
164 if (!irccmp(scache_ptr->name, me.name))
165 show = FALSE;
166 else if (scache_ptr->flags & SC_HIDDEN &&
167 !ConfigServerHide.disable_hidden)
168 show = FALSE;
169 else if (scache_ptr->flags & SC_ONLINE)
9f6bbe3c 170 show = scache_ptr->known_since < rb_current_time() - ConfigServerHide.links_delay;
994544c2 171 else
9f6bbe3c 172 show = scache_ptr->last_split > rb_current_time() - ConfigServerHide.links_delay && scache_ptr->last_split - scache_ptr->known_since > ConfigServerHide.links_delay;
994544c2
JT
173 if (show)
174 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
175 scache_ptr->name, me.name, 1, scache_ptr->info);
176
177 scache_ptr = scache_ptr->next;
178 }
179 }
180 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
181 me.name, me.name, 0, me.info);
182
183 sendto_one_numeric(source_p, RPL_ENDOFLINKS, form_str(RPL_ENDOFLINKS), "*");
184}
185
c0bc9fe3
JT
186#define MISSING_TIMEOUT 86400
187
188/* scache_send_missing()
189 *
190 * inputs - client to send to
191 * outputs - recently split servers
192 * side effects -
193 */
194void
195scache_send_missing(struct Client *source_p)
196{
197 struct scache_entry *scache_ptr;
198 int i;
199
200 for (i = 0; i < SCACHE_HASH_SIZE; i++)
201 {
202 scache_ptr = scache_hash[i];
203 while (scache_ptr)
204 {
9f6bbe3c 205 if (!(scache_ptr->flags & SC_ONLINE) && scache_ptr->last_split > rb_current_time() - MISSING_TIMEOUT)
c0bc9fe3
JT
206 sendto_one_numeric(source_p, RPL_MAP, "** %s (recently split)",
207 scache_ptr->name);
208
209 scache_ptr = scache_ptr->next;
210 }
211 }
212}
212380e3 213/*
214 * count_scache
215 * inputs - pointer to where to leave number of servers cached
216 * - pointer to where to leave total memory usage
217 * output - NONE
218 * side effects -
219 */
220void
221count_scache(size_t * number_servers_cached, size_t * mem_servers_cached)
222{
994544c2 223 struct scache_entry *scache_ptr;
212380e3 224 int i;
225
226 *number_servers_cached = 0;
227 *mem_servers_cached = 0;
228
229 for (i = 0; i < SCACHE_HASH_SIZE; i++)
230 {
231 scache_ptr = scache_hash[i];
232 while (scache_ptr)
233 {
234 *number_servers_cached = *number_servers_cached + 1;
235 *mem_servers_cached = *mem_servers_cached +
994544c2 236 sizeof(struct scache_entry );
212380e3 237
238 scache_ptr = scache_ptr->next;
239 }
240 }
241}