3 * scache.c: Server names cache.
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
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.
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.
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
35 #include "irc_radixtree.h"
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.
45 * reworked to serve for flattening/delaying /links also
48 * reworked to make use of irc_radixtree.
57 char name
[HOSTLEN
+ 1];
58 char info
[REALLEN
+ 1];
65 static struct irc_radixtree
*scache_tree
= NULL
;
68 clear_scache_hash_table(void)
70 scache_tree
= irc_radixtree_create("server names cache", irc_radixtree_irccasecanon
);
73 static struct scache_entry
*
74 find_or_add(const char *name
)
76 struct scache_entry
*ptr
;
78 ptr
= irc_radixtree_retrieve(scache_tree
, name
);
82 ptr
= (struct scache_entry
*) rb_malloc(sizeof(struct scache_entry
));
85 rb_strlcpy(ptr
->name
, name
, sizeof(ptr
->name
));
88 ptr
->known_since
= rb_current_time();
89 ptr
->last_connect
= 0;
92 irc_radixtree_add(scache_tree
, ptr
->name
, ptr
);
98 scache_connect(const char *name
, const char *info
, int hidden
)
100 struct scache_entry
*ptr
;
102 ptr
= find_or_add(name
);
103 rb_strlcpy(ptr
->info
, info
, sizeof(ptr
->info
));
104 ptr
->flags
|= SC_ONLINE
;
106 ptr
->flags
|= SC_HIDDEN
;
108 ptr
->flags
&= ~SC_HIDDEN
;
109 ptr
->last_connect
= rb_current_time();
114 scache_split(struct scache_entry
*ptr
)
118 ptr
->flags
&= ~SC_ONLINE
;
119 ptr
->last_split
= rb_current_time();
122 const char *scache_get_name(struct scache_entry
*ptr
)
127 /* scache_send_flattened_links()
129 * inputs - client to send to
130 * outputs - the cached links, us, and RPL_ENDOFLINKS
134 scache_send_flattened_links(struct Client
*source_p
)
136 struct scache_entry
*scache_ptr
;
137 struct irc_radixtree_iteration_state iter
;
140 IRC_RADIXTREE_FOREACH(scache_ptr
, &iter
, scache_tree
)
142 if (!irccmp(scache_ptr
->name
, me
.name
))
144 else if (scache_ptr
->flags
& SC_HIDDEN
&&
145 !ConfigServerHide
.disable_hidden
)
147 else if (scache_ptr
->flags
& SC_ONLINE
)
148 show
= scache_ptr
->known_since
< rb_current_time() - ConfigServerHide
.links_delay
;
150 show
= scache_ptr
->last_split
> rb_current_time() - ConfigServerHide
.links_delay
&& scache_ptr
->last_split
- scache_ptr
->known_since
> ConfigServerHide
.links_delay
;
152 sendto_one_numeric(source_p
, RPL_LINKS
, form_str(RPL_LINKS
),
153 scache_ptr
->name
, me
.name
, 1, scache_ptr
->info
);
155 sendto_one_numeric(source_p
, RPL_LINKS
, form_str(RPL_LINKS
),
156 me
.name
, me
.name
, 0, me
.info
);
158 sendto_one_numeric(source_p
, RPL_ENDOFLINKS
, form_str(RPL_ENDOFLINKS
), "*");
161 #define MISSING_TIMEOUT 86400
163 /* scache_send_missing()
165 * inputs - client to send to
166 * outputs - recently split servers
170 scache_send_missing(struct Client
*source_p
)
172 struct scache_entry
*scache_ptr
;
173 struct irc_radixtree_iteration_state iter
;
175 IRC_RADIXTREE_FOREACH(scache_ptr
, &iter
, scache_tree
)
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)",
185 * inputs - pointer to where to leave number of servers cached
186 * - pointer to where to leave total memory usage
191 count_scache(size_t * number_servers_cached
, size_t * mem_servers_cached
)
193 struct scache_entry
*scache_ptr
;
194 struct irc_radixtree_iteration_state iter
;
196 *number_servers_cached
= 0;
197 *mem_servers_cached
= 0;
199 IRC_RADIXTREE_FOREACH(scache_ptr
, &iter
, scache_tree
)
201 *number_servers_cached
= *number_servers_cached
+ 1;
202 *mem_servers_cached
= *mem_servers_cached
+
203 sizeof(struct scache_entry
);