| 1 |
pippijn |
1.1 |
/* |
| 2 |
|
|
* servers.C: Server and network state tracking. |
| 3 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
| 4 |
pippijn |
1.1 |
* |
| 5 |
pippijn |
1.4 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
| 6 |
pippijn |
1.1 |
*/ |
| 7 |
|
|
|
| 8 |
pippijn |
1.5 |
static char const rcsid[] = "$Id: servers.C,v 1.4 2007-08-28 17:08:12 pippijn Exp $"; |
| 9 |
pippijn |
1.1 |
|
| 10 |
|
|
#include "atheme.h" |
| 11 |
pippijn |
1.5 |
#include "servers.h" |
| 12 |
pippijn |
1.1 |
#include <account/myuser.h> |
| 13 |
|
|
|
| 14 |
pippijn |
1.4 |
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 |
pippijn |
1.1 |
list_t tldlist; |
| 19 |
pippijn |
1.4 |
server_t::callbacks server_t::callback; |
| 20 |
pippijn |
1.1 |
|
| 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 |
pippijn |
1.4 |
#if 0 |
| 40 |
pippijn |
1.1 |
serv_heap = BlockHeapCreate (sizeof (server_t), HEAP_SERVER); |
| 41 |
|
|
tld_heap = BlockHeapCreate (sizeof (tld_t), 4); |
| 42 |
pippijn |
1.4 |
#endif |
| 43 |
pippijn |
1.1 |
} |
| 44 |
|
|
|
| 45 |
|
|
/* |
| 46 |
pippijn |
1.4 |
* server_add(char const * const name, unsigned int hops, char const * const uplink, |
| 47 |
|
|
* char const * const id, char const * const desc) |
| 48 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.4 |
server_add (char const * const name, unsigned int hops, char const * const uplink, char const * const id, char const * const description) |
| 66 |
pippijn |
1.1 |
{ |
| 67 |
|
|
server_t *s, *u = NULL; |
| 68 |
pippijn |
1.4 |
char const *tld; |
| 69 |
|
|
char const *desc = description; |
| 70 |
pippijn |
1.1 |
|
| 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 |
pippijn |
1.4 |
s = new server_t; |
| 83 |
pippijn |
1.1 |
|
| 84 |
|
|
if (id != NULL) |
| 85 |
|
|
{ |
| 86 |
|
|
s->sid = sstrdup (id); |
| 87 |
pippijn |
1.4 |
sidlist[s->sid] = s; |
| 88 |
pippijn |
1.1 |
} |
| 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 |
pippijn |
1.4 |
servlist[s->name] = s; |
| 105 |
pippijn |
1.1 |
|
| 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 |
pippijn |
1.5 |
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 |
pippijn |
1.1 |
/* |
| 141 |
pippijn |
1.4 |
* server_delete(char const * const name) |
| 142 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.5 |
server_delete (server_t *s, bool force) |
| 156 |
pippijn |
1.1 |
{ |
| 157 |
|
|
server_t *child; |
| 158 |
|
|
user_t *u; |
| 159 |
|
|
node_t *n, *tn; |
| 160 |
|
|
|
| 161 |
pippijn |
1.5 |
if (!force && s == me.me) |
| 162 |
pippijn |
1.1 |
{ |
| 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 |
pippijn |
1.4 |
servlist.erase (s->name); |
| 193 |
pippijn |
1.1 |
|
| 194 |
|
|
if (s->sid) |
| 195 |
pippijn |
1.4 |
sidlist.erase (s->sid); |
| 196 |
pippijn |
1.1 |
|
| 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 |
pippijn |
1.4 |
phandler->jupe (s->name, "Juped"); |
| 209 |
pippijn |
1.1 |
|
| 210 |
pippijn |
1.4 |
sfree (s->name); |
| 211 |
|
|
sfree (s->desc); |
| 212 |
pippijn |
1.1 |
if (s->sid) |
| 213 |
pippijn |
1.4 |
sfree (s->sid); |
| 214 |
pippijn |
1.1 |
|
| 215 |
pippijn |
1.4 |
delete s; |
| 216 |
pippijn |
1.1 |
|
| 217 |
|
|
cnt.server--; |
| 218 |
|
|
} |
| 219 |
|
|
|
| 220 |
|
|
/* |
| 221 |
pippijn |
1.4 |
* server_find(char const * const name) |
| 222 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.4 |
server_find (char const * const name) |
| 237 |
pippijn |
1.1 |
{ |
| 238 |
pippijn |
1.4 |
server_map::iterator it; |
| 239 |
pippijn |
1.1 |
|
| 240 |
|
|
if (ircd->uses_uid) |
| 241 |
|
|
{ |
| 242 |
pippijn |
1.4 |
it = sidlist.find (name); |
| 243 |
|
|
if (it != sidlist.end ()) |
| 244 |
|
|
return it->second; |
| 245 |
pippijn |
1.1 |
} |
| 246 |
|
|
|
| 247 |
pippijn |
1.4 |
return ((it = servlist.find (name)) == servlist.end () |
| 248 |
|
|
? NULL |
| 249 |
|
|
: it->second); |
| 250 |
pippijn |
1.1 |
} |
| 251 |
|
|
|
| 252 |
|
|
/* |
| 253 |
pippijn |
1.4 |
* tld_add(char const * const name) |
| 254 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.4 |
tld_add (char const * const name) |
| 269 |
pippijn |
1.1 |
{ |
| 270 |
|
|
tld_t *tld; |
| 271 |
|
|
node_t *n = node_create (); |
| 272 |
|
|
|
| 273 |
|
|
slog (LG_DEBUG, "tld_add(): %s", name); |
| 274 |
|
|
|
| 275 |
pippijn |
1.4 |
tld = new tld_t; |
| 276 |
pippijn |
1.1 |
|
| 277 |
|
|
node_add (tld, n, &tldlist); |
| 278 |
|
|
|
| 279 |
|
|
tld->name = sstrdup (name); |
| 280 |
|
|
|
| 281 |
|
|
cnt.tld++; |
| 282 |
|
|
|
| 283 |
|
|
return tld; |
| 284 |
|
|
} |
| 285 |
|
|
|
| 286 |
|
|
/* |
| 287 |
pippijn |
1.4 |
* tld_delete(char const * const name) |
| 288 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.4 |
tld_delete (char const * const name) |
| 302 |
pippijn |
1.1 |
{ |
| 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 |
pippijn |
1.4 |
sfree (tld->name); |
| 320 |
|
|
delete tld; |
| 321 |
pippijn |
1.1 |
|
| 322 |
|
|
cnt.tld--; |
| 323 |
|
|
} |
| 324 |
|
|
|
| 325 |
|
|
/* |
| 326 |
pippijn |
1.4 |
* tld_find(char const * const name) |
| 327 |
pippijn |
1.1 |
* |
| 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 |
pippijn |
1.4 |
tld_find (char const * const name) |
| 342 |
pippijn |
1.1 |
{ |
| 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 |
|
|
*/ |