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