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