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