1 |
/* |
2 |
* servers.C: Server and network state tracking. |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id: servers.C,v 1.4 2007-08-28 17:08:12 pippijn Exp $"; |
9 |
|
10 |
#include "atheme.h" |
11 |
#include "servers.h" |
12 |
#include <account/myuser.h> |
13 |
|
14 |
typedef std::pair<char const * const, server_t *> server_pair; |
15 |
typedef std::map<char const * const, server_t *, irccase_lt> server_map; |
16 |
server_map sidlist; |
17 |
server_map servlist; |
18 |
list_t tldlist; |
19 |
server_t::callbacks server_t::callback; |
20 |
|
21 |
/* |
22 |
* init_servers() |
23 |
* |
24 |
* Initializes the server heap and server/sid DTree structures. |
25 |
* |
26 |
* Inputs: |
27 |
* - nothing |
28 |
* |
29 |
* Outputs: |
30 |
* - nothing |
31 |
* |
32 |
* Side Effects: |
33 |
* - if the heap or dtrees fail to initialize, the program |
34 |
* will abort. |
35 |
*/ |
36 |
void |
37 |
init_servers (void) |
38 |
{ |
39 |
#if 0 |
40 |
serv_heap = BlockHeapCreate (sizeof (server_t), HEAP_SERVER); |
41 |
tld_heap = BlockHeapCreate (sizeof (tld_t), 4); |
42 |
#endif |
43 |
} |
44 |
|
45 |
/* |
46 |
* server_add(char const * const name, unsigned int hops, char const * const uplink, |
47 |
* char const * const id, char const * const desc) |
48 |
* |
49 |
* Server object factory. |
50 |
* |
51 |
* Inputs: |
52 |
* - name of server object to create |
53 |
* - amount of hops server has from services |
54 |
* - name of server's uplink or NULL if it's us |
55 |
* - SID of uplink if applicable otherwise NULL |
56 |
* - server's description |
57 |
* |
58 |
* Outputs: |
59 |
* - on success, a new server object |
60 |
* |
61 |
* Side Effects: |
62 |
* - the new server object is added to the server and sid DTree. |
63 |
*/ |
64 |
server_t * |
65 |
server_add (char const * const name, unsigned int hops, char const * const uplink, char const * const id, char const * const description) |
66 |
{ |
67 |
server_t *s, *u = NULL; |
68 |
char const *tld; |
69 |
char const *desc = description; |
70 |
|
71 |
if (uplink) |
72 |
{ |
73 |
if (id != NULL) |
74 |
slog (LG_NETWORK, "server_add(): %s (%s), uplink %s", name, id, uplink); |
75 |
else |
76 |
slog (LG_NETWORK, "server_add(): %s, uplink %s", name, uplink); |
77 |
u = server_find (uplink); |
78 |
} |
79 |
else |
80 |
slog (LG_DEBUG, "server_add(): %s, root", name); |
81 |
|
82 |
s = new server_t; |
83 |
|
84 |
if (id != NULL) |
85 |
{ |
86 |
s->sid = sstrdup (id); |
87 |
sidlist[s->sid] = s; |
88 |
} |
89 |
|
90 |
/* check to see if it's hidden */ |
91 |
if (!strncmp (desc, "(H)", 3)) |
92 |
{ |
93 |
s->flags |= SF_HIDE; |
94 |
desc += 3; |
95 |
if (*desc == ' ') |
96 |
desc++; |
97 |
} |
98 |
|
99 |
s->name = sstrdup (name); |
100 |
s->desc = sstrdup (desc); |
101 |
s->hops = hops; |
102 |
s->connected_since = NOW; |
103 |
|
104 |
servlist[s->name] = s; |
105 |
|
106 |
if (u) |
107 |
{ |
108 |
s->uplink = u; |
109 |
node_add (s, node_create (), &u->children); |
110 |
} |
111 |
|
112 |
/* tld list for global noticer */ |
113 |
tld = strrchr (name, '.'); |
114 |
|
115 |
if (tld != NULL) |
116 |
{ |
117 |
if (!tld_find (tld)) |
118 |
tld_add (tld); |
119 |
} |
120 |
|
121 |
cnt.server++; |
122 |
|
123 |
return s; |
124 |
} |
125 |
|
126 |
void |
127 |
server_delete (char const * const name) |
128 |
{ |
129 |
server_t *s = server_find (name); |
130 |
server_delete (s); |
131 |
|
132 |
if (!s) |
133 |
{ |
134 |
slog (LG_DEBUG, "server_delete(): called for nonexistant server: %s", name); |
135 |
|
136 |
return; |
137 |
} |
138 |
} |
139 |
|
140 |
/* |
141 |
* server_delete(char const * const name) |
142 |
* |
143 |
* Finds and recursively destroys a server object. |
144 |
* |
145 |
* Inputs: |
146 |
* - name of server to find and destroy |
147 |
* |
148 |
* Outputs: |
149 |
* - nothing |
150 |
* |
151 |
* Side Effects: |
152 |
* - all users and servers attached to the target are recursively deleted |
153 |
*/ |
154 |
void |
155 |
server_delete (server_t *s, bool force) |
156 |
{ |
157 |
server_t *child; |
158 |
user_t *u; |
159 |
node_t *n, *tn; |
160 |
|
161 |
if (!force && s == me.me) |
162 |
{ |
163 |
/* Deleting this would cause confusion, so let's not do it. |
164 |
* Some ircds send SQUIT <myname> when we are squitted. |
165 |
* -- jilles |
166 |
*/ |
167 |
slog (LG_DEBUG, "server_delete(): tried to delete myself"); |
168 |
return; |
169 |
} |
170 |
|
171 |
slog (me.connected ? LG_NETWORK : LG_DEBUG, "server_delete(): %s, uplink %s (%d users)", s->name, s->uplink != NULL ? s->uplink->name : "<none>", s->users); |
172 |
|
173 |
/* first go through it's users and kill all of them */ |
174 |
LIST_FOREACH_SAFE (n, tn, s->userlist.head) |
175 |
{ |
176 |
u = (user_t *) n->data; |
177 |
/* This user split, allow bursted logins for the account. |
178 |
* XXX should we do this here? |
179 |
* -- jilles */ |
180 |
if (u->myuser != NULL) |
181 |
u->myuser->flags &= ~MU_NOBURSTLOGIN; |
182 |
user_delete (u); |
183 |
} |
184 |
|
185 |
LIST_FOREACH_SAFE (n, tn, s->children.head) |
186 |
{ |
187 |
child = static_cast<server_t *> (n->data); |
188 |
server_delete (child->name); |
189 |
} |
190 |
|
191 |
/* now remove the server */ |
192 |
servlist.erase (s->name); |
193 |
|
194 |
if (s->sid) |
195 |
sidlist.erase (s->sid); |
196 |
|
197 |
if (s->uplink) |
198 |
{ |
199 |
n = node_find (s, &s->uplink->children); |
200 |
node_del (n, &s->uplink->children); |
201 |
node_free (n); |
202 |
} |
203 |
|
204 |
/* If unconnect semantics SQUIT was confirmed, introduce the jupe |
205 |
* now. This must be after removing the server from the dtrees. |
206 |
* -- jilles */ |
207 |
if (s->flags & SF_JUPE_PENDING) |
208 |
phandler->jupe (s->name, "Juped"); |
209 |
|
210 |
sfree (s->name); |
211 |
sfree (s->desc); |
212 |
if (s->sid) |
213 |
sfree (s->sid); |
214 |
|
215 |
delete s; |
216 |
|
217 |
cnt.server--; |
218 |
} |
219 |
|
220 |
/* |
221 |
* server_find(char const * const name) |
222 |
* |
223 |
* Finds a server object. |
224 |
* |
225 |
* Inputs: |
226 |
* - name of server to find |
227 |
* |
228 |
* Outputs: |
229 |
* - on success, the server object |
230 |
* - on failure, NULL. |
231 |
* |
232 |
* Side Effects: |
233 |
* - none |
234 |
*/ |
235 |
server_t * |
236 |
server_find (char const * const name) |
237 |
{ |
238 |
server_map::iterator it; |
239 |
|
240 |
if (ircd->uses_uid) |
241 |
{ |
242 |
it = sidlist.find (name); |
243 |
if (it != sidlist.end ()) |
244 |
return it->second; |
245 |
} |
246 |
|
247 |
return ((it = servlist.find (name)) == servlist.end () |
248 |
? NULL |
249 |
: it->second); |
250 |
} |
251 |
|
252 |
/* |
253 |
* tld_add(char const * const name) |
254 |
* |
255 |
* TLD object factory. |
256 |
* |
257 |
* Inputs: |
258 |
* - name of TLD to cache as an object |
259 |
* |
260 |
* Outputs: |
261 |
* - on success, a TLD object |
262 |
* - on failure, NULL |
263 |
* |
264 |
* Side Effects: |
265 |
* - the TLD object is registered with the TLD list. |
266 |
*/ |
267 |
tld_t * |
268 |
tld_add (char const * const name) |
269 |
{ |
270 |
tld_t *tld; |
271 |
node_t *n = node_create (); |
272 |
|
273 |
slog (LG_DEBUG, "tld_add(): %s", name); |
274 |
|
275 |
tld = new tld_t; |
276 |
|
277 |
node_add (tld, n, &tldlist); |
278 |
|
279 |
tld->name = sstrdup (name); |
280 |
|
281 |
cnt.tld++; |
282 |
|
283 |
return tld; |
284 |
} |
285 |
|
286 |
/* |
287 |
* tld_delete(char const * const name) |
288 |
* |
289 |
* Destroys a TLD object. |
290 |
* |
291 |
* Inputs: |
292 |
* - name of TLD object to destroy |
293 |
* |
294 |
* Outputs: |
295 |
* - nothing |
296 |
* |
297 |
* Side Effects: |
298 |
* - the TLD object is removed and deregistered from the TLD list. |
299 |
*/ |
300 |
void |
301 |
tld_delete (char const * const name) |
302 |
{ |
303 |
tld_t *tld = tld_find (name); |
304 |
node_t *n; |
305 |
|
306 |
if (!tld) |
307 |
{ |
308 |
slog (LG_DEBUG, "tld_delete(): called for nonexistant tld: %s", name); |
309 |
|
310 |
return; |
311 |
} |
312 |
|
313 |
slog (LG_DEBUG, "tld_delete(): %s", tld->name); |
314 |
|
315 |
n = node_find (tld, &tldlist); |
316 |
node_del (n, &tldlist); |
317 |
node_free (n); |
318 |
|
319 |
sfree (tld->name); |
320 |
delete tld; |
321 |
|
322 |
cnt.tld--; |
323 |
} |
324 |
|
325 |
/* |
326 |
* tld_find(char const * const name) |
327 |
* |
328 |
* Looks up a TLD object. |
329 |
* |
330 |
* Inputs: |
331 |
* - name of TLD object to look up |
332 |
* |
333 |
* Outputs: |
334 |
* - on success, the TLD object |
335 |
* - on failure, NULL |
336 |
* |
337 |
* Side Effects: |
338 |
* - none |
339 |
*/ |
340 |
tld_t * |
341 |
tld_find (char const * const name) |
342 |
{ |
343 |
tld_t *tld; |
344 |
node_t *n; |
345 |
|
346 |
if (name == NULL) |
347 |
return NULL; |
348 |
|
349 |
LIST_FOREACH (n, tldlist.head) |
350 |
{ |
351 |
tld = (tld_t *) n->data; |
352 |
|
353 |
if (!strcasecmp (name, tld->name)) |
354 |
return tld; |
355 |
} |
356 |
|
357 |
return NULL; |
358 |
} |
359 |
|
360 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
361 |
* vim:ts=8 |
362 |
* vim:sw=8 |
363 |
* vim:noexpandtab |
364 |
*/ |