]> jfr.im git - solanum.git/blame - ircd/scache.c
Change struct Dictionary(*) to rb_dictionary(_\1).
[solanum.git] / ircd / scache.c
CommitLineData
212380e3 1/*
a21e57be 2 * charybdis
212380e3
AC
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
212380e3
AC
23 */
24
25#include "stdinc.h"
26#include "client.h"
27#include "common.h"
4562c604 28#include "match.h"
212380e3
AC
29#include "ircd.h"
30#include "numeric.h"
31#include "send.h"
32#include "scache.h"
994544c2 33#include "s_conf.h"
77d3d2db 34#include "s_assert.h"
a4bf26dd 35#include "rb_radixtree.h"
212380e3
AC
36
37/*
55abcbb2 38 * ircd used to store full servernames in anUser as well as in the
212380e3
AC
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
55abcbb2 42 * names internally, the server can easily save 2 or 3 megs of RAM.
212380e3 43 * -orabidoo
994544c2
JT
44 *
45 * reworked to serve for flattening/delaying /links also
46 * -- jilles
a21e57be 47 *
a4bf26dd 48 * reworked to make use of rb_radixtree.
a21e57be 49 * -- kaniini
212380e3
AC
50 */
51
994544c2
JT
52#define SC_ONLINE 1
53#define SC_HIDDEN 2
54
55struct scache_entry
212380e3
AC
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;
994544c2 63};
212380e3 64
a4bf26dd 65static struct rb_radixtree *scache_tree = NULL;
212380e3
AC
66
67void
68clear_scache_hash_table(void)
69{
a4bf26dd 70 scache_tree = rb_radixtree_create("server names cache", irccasecanon);
212380e3
AC
71}
72
994544c2 73static struct scache_entry *
212380e3
AC
74find_or_add(const char *name)
75{
994544c2 76 struct scache_entry *ptr;
212380e3 77
a4bf26dd 78 ptr = rb_radixtree_retrieve(scache_tree, name);
a21e57be
AC
79 if (ptr != NULL)
80 return ptr;
212380e3 81
eddc2ab6 82 ptr = (struct scache_entry *) rb_malloc(sizeof(struct scache_entry));
212380e3
AC
83 s_assert(0 != ptr);
84
f427c8b0 85 rb_strlcpy(ptr->name, name, sizeof(ptr->name));
994544c2
JT
86 ptr->info[0] = '\0';
87 ptr->flags = 0;
e3354945 88 ptr->known_since = rb_current_time();
994544c2
JT
89 ptr->last_connect = 0;
90 ptr->last_split = 0;
212380e3 91
a4bf26dd 92 rb_radixtree_add(scache_tree, ptr->name, ptr);
a21e57be 93
994544c2
JT
94 return ptr;
95}
96
97struct scache_entry *
98scache_connect(const char *name, const char *info, int hidden)
99{
100 struct scache_entry *ptr;
101
102 ptr = find_or_add(name);
f427c8b0 103 rb_strlcpy(ptr->info, info, sizeof(ptr->info));
994544c2
JT
104 ptr->flags |= SC_ONLINE;
105 if (hidden)
106 ptr->flags |= SC_HIDDEN;
107 else
108 ptr->flags &= ~SC_HIDDEN;
e3354945 109 ptr->last_connect = rb_current_time();
994544c2
JT
110 return ptr;
111}
112
113void
114scache_split(struct scache_entry *ptr)
115{
116 if (ptr == NULL)
117 return;
118 ptr->flags &= ~SC_ONLINE;
e3354945 119 ptr->last_split = rb_current_time();
994544c2
JT
120}
121
122const char *scache_get_name(struct scache_entry *ptr)
123{
212380e3
AC
124 return ptr->name;
125}
126
994544c2
JT
127/* scache_send_flattened_links()
128 *
129 * inputs - client to send to
130 * outputs - the cached links, us, and RPL_ENDOFLINKS
131 * side effects -
132 */
133void
134scache_send_flattened_links(struct Client *source_p)
135{
136 struct scache_entry *scache_ptr;
a4bf26dd 137 struct rb_radixtree_iteration_state iter;
994544c2
JT
138 int show;
139
a4bf26dd 140 RB_RADIXTREE_FOREACH(scache_ptr, &iter, scache_tree)
994544c2 141 {
a21e57be
AC
142 if (!irccmp(scache_ptr->name, me.name))
143 show = FALSE;
144 else if (scache_ptr->flags & SC_HIDDEN &&
145 !ConfigServerHide.disable_hidden)
146 show = FALSE;
147 else if (scache_ptr->flags & SC_ONLINE)
148 show = scache_ptr->known_since < rb_current_time() - ConfigServerHide.links_delay;
149 else
150 show = scache_ptr->last_split > rb_current_time() - ConfigServerHide.links_delay && scache_ptr->last_split - scache_ptr->known_since > ConfigServerHide.links_delay;
151 if (show)
152 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
153 scache_ptr->name, me.name, 1, scache_ptr->info);
994544c2 154 }
55abcbb2 155 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
994544c2
JT
156 me.name, me.name, 0, me.info);
157
158 sendto_one_numeric(source_p, RPL_ENDOFLINKS, form_str(RPL_ENDOFLINKS), "*");
159}
160
c0bc9fe3
JT
161#define MISSING_TIMEOUT 86400
162
163/* scache_send_missing()
164 *
165 * inputs - client to send to
166 * outputs - recently split servers
167 * side effects -
168 */
169void
170scache_send_missing(struct Client *source_p)
171{
172 struct scache_entry *scache_ptr;
a4bf26dd 173 struct rb_radixtree_iteration_state iter;
c0bc9fe3 174
a4bf26dd 175 RB_RADIXTREE_FOREACH(scache_ptr, &iter, scache_tree)
c0bc9fe3 176 {
a21e57be
AC
177 if (!(scache_ptr->flags & SC_ONLINE) && scache_ptr->last_split > rb_current_time() - MISSING_TIMEOUT)
178 sendto_one_numeric(source_p, RPL_MAP, "** %s (recently split)",
179 scache_ptr->name);
c0bc9fe3
JT
180 }
181}
a21e57be 182
212380e3
AC
183/*
184 * count_scache
185 * inputs - pointer to where to leave number of servers cached
186 * - pointer to where to leave total memory usage
187 * output - NONE
188 * side effects -
189 */
190void
191count_scache(size_t * number_servers_cached, size_t * mem_servers_cached)
192{
994544c2 193 struct scache_entry *scache_ptr;
a4bf26dd 194 struct rb_radixtree_iteration_state iter;
212380e3
AC
195
196 *number_servers_cached = 0;
197 *mem_servers_cached = 0;
198
a4bf26dd 199 RB_RADIXTREE_FOREACH(scache_ptr, &iter, scache_tree)
212380e3 200 {
a21e57be
AC
201 *number_servers_cached = *number_servers_cached + 1;
202 *mem_servers_cached = *mem_servers_cached +
203 sizeof(struct scache_entry);
212380e3
AC
204 }
205}