| 1 |
|
/* |
| 2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
< |
* hash.c: Maintains hashtables. |
| 2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
|
* |
| 4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 4 |
> |
* Copyright (c) 1997-2016 ircd-hybrid development team |
| 5 |
|
* |
| 6 |
|
* This program is free software; you can redistribute it and/or modify |
| 7 |
|
* it under the terms of the GNU General Public License as published by |
| 15 |
|
* |
| 16 |
|
* You should have received a copy of the GNU General Public License |
| 17 |
|
* along with this program; if not, write to the Free Software |
| 18 |
< |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
|
* USA |
| 20 |
< |
* |
| 21 |
< |
* $Id$ |
| 20 |
> |
*/ |
| 21 |
> |
|
| 22 |
> |
/*! \file hash.c |
| 23 |
> |
* \brief Hash table management. |
| 24 |
> |
* \version $Id$ |
| 25 |
|
*/ |
| 26 |
|
|
| 27 |
|
#include "stdinc.h" |
| 30 |
|
#include "channel.h" |
| 31 |
|
#include "channel_mode.h" |
| 32 |
|
#include "client.h" |
| 31 |
– |
#include "modules.h" |
| 33 |
|
#include "hash.h" |
| 34 |
< |
#include "resv.h" |
| 34 |
> |
#include "id.h" |
| 35 |
|
#include "rng_mt.h" |
| 36 |
|
#include "userhost.h" |
| 37 |
|
#include "irc_string.h" |
| 39 |
|
#include "numeric.h" |
| 40 |
|
#include "send.h" |
| 41 |
|
#include "memory.h" |
| 41 |
– |
#include "mempool.h" |
| 42 |
|
#include "dbuf.h" |
| 43 |
– |
#include "s_user.h" |
| 44 |
– |
|
| 43 |
|
|
| 46 |
– |
static mp_pool_t *userhost_pool = NULL; |
| 47 |
– |
static mp_pool_t *namehost_pool = NULL; |
| 44 |
|
|
| 45 |
< |
static unsigned int hashf_xor_key = 0; |
| 45 |
> |
static unsigned int hashf_xor_key; |
| 46 |
|
|
| 47 |
|
/* The actual hash tables, They MUST be of the same HASHSIZE, variable |
| 48 |
|
* size tables could be supported but the rehash routine should also |
| 49 |
< |
* rebuild the transformation maps, I kept the tables of equal size |
| 49 |
> |
* rebuild the transformation maps, I kept the tables of equal size |
| 50 |
|
* so that I can use one hash function. |
| 51 |
|
*/ |
| 52 |
|
static struct Client *idTable[HASHSIZE]; |
| 53 |
|
static struct Client *clientTable[HASHSIZE]; |
| 54 |
|
static struct Channel *channelTable[HASHSIZE]; |
| 55 |
|
static struct UserHost *userhostTable[HASHSIZE]; |
| 60 |
– |
static struct MaskItem *resvchannelTable[HASHSIZE]; |
| 56 |
|
|
| 57 |
|
|
| 58 |
< |
/* init_hash() |
| 58 |
> |
/* hash_init() |
| 59 |
|
* |
| 60 |
|
* inputs - NONE |
| 61 |
|
* output - NONE |
| 63 |
|
* functions and clear the tables |
| 64 |
|
*/ |
| 65 |
|
void |
| 66 |
< |
init_hash(void) |
| 66 |
> |
hash_init(void) |
| 67 |
|
{ |
| 68 |
< |
/* Default the userhost/namehost sizes to CLIENT_HEAP_SIZE for now, |
| 69 |
< |
* should be a good close approximation anyway |
| 70 |
< |
* - Dianora |
| 76 |
< |
*/ |
| 77 |
< |
userhost_pool = mp_pool_new(sizeof(struct UserHost), MP_CHUNK_SIZE_CLIENT); |
| 78 |
< |
namehost_pool = mp_pool_new(sizeof(struct NameHost), MP_CHUNK_SIZE_CLIENT); |
| 79 |
< |
|
| 80 |
< |
hashf_xor_key = genrand_int32() % 256; /* better than nothing --adx */ |
| 68 |
> |
do |
| 69 |
> |
hashf_xor_key = genrand_int32() % 256; /* better than nothing --adx */ |
| 70 |
> |
while (!hashf_xor_key); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
/* |
| 83 |
|
const unsigned char *p = (const unsigned char *)name; |
| 84 |
|
unsigned int hval = FNV1_32_INIT; |
| 85 |
|
|
| 86 |
< |
if (*p == '\0') |
| 86 |
> |
if (EmptyString(p)) |
| 87 |
|
return 0; |
| 88 |
< |
for (; *p != '\0'; ++p) |
| 88 |
> |
|
| 89 |
> |
for (; *p; ++p) |
| 90 |
|
{ |
| 91 |
|
hval += (hval << 1) + (hval << 4) + (hval << 7) + |
| 92 |
|
(hval << 8) + (hval << 24); |
| 118 |
|
void |
| 119 |
|
hash_add_client(struct Client *client_p) |
| 120 |
|
{ |
| 121 |
< |
unsigned int hashv = strhash(client_p->name); |
| 121 |
> |
const unsigned int hashv = strhash(client_p->name); |
| 122 |
|
|
| 123 |
|
client_p->hnext = clientTable[hashv]; |
| 124 |
|
clientTable[hashv] = client_p; |
| 137 |
|
void |
| 138 |
|
hash_add_channel(struct Channel *chptr) |
| 139 |
|
{ |
| 140 |
< |
unsigned int hashv = strhash(chptr->chname); |
| 140 |
> |
const unsigned int hashv = strhash(chptr->name); |
| 141 |
|
|
| 142 |
|
chptr->hnextch = channelTable[hashv]; |
| 143 |
|
channelTable[hashv] = chptr; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
void |
| 156 |
– |
hash_add_resv(struct MaskItem *conf) |
| 157 |
– |
{ |
| 158 |
– |
unsigned int hashv = strhash(conf->name); |
| 159 |
– |
|
| 160 |
– |
conf->hnext = resvchannelTable[hashv]; |
| 161 |
– |
resvchannelTable[hashv] = conf; |
| 162 |
– |
} |
| 163 |
– |
|
| 164 |
– |
void |
| 147 |
|
hash_add_userhost(struct UserHost *userhost) |
| 148 |
|
{ |
| 149 |
< |
unsigned int hashv = strhash(userhost->host); |
| 149 |
> |
const unsigned int hashv = strhash(userhost->host); |
| 150 |
|
|
| 151 |
|
userhost->next = userhostTable[hashv]; |
| 152 |
|
userhostTable[hashv] = userhost; |
| 155 |
|
void |
| 156 |
|
hash_add_id(struct Client *client_p) |
| 157 |
|
{ |
| 158 |
< |
unsigned int hashv = strhash(client_p->id); |
| 158 |
> |
const unsigned int hashv = strhash(client_p->id); |
| 159 |
|
|
| 160 |
|
client_p->idhnext = idTable[hashv]; |
| 161 |
|
idTable[hashv] = client_p; |
| 170 |
|
void |
| 171 |
|
hash_del_id(struct Client *client_p) |
| 172 |
|
{ |
| 173 |
< |
unsigned int hashv = strhash(client_p->id); |
| 173 |
> |
const unsigned int hashv = strhash(client_p->id); |
| 174 |
|
struct Client *tmp = idTable[hashv]; |
| 175 |
|
|
| 176 |
< |
if (tmp != NULL) |
| 176 |
> |
if (tmp) |
| 177 |
|
{ |
| 178 |
|
if (tmp == client_p) |
| 179 |
|
{ |
| 201 |
|
void |
| 202 |
|
hash_del_client(struct Client *client_p) |
| 203 |
|
{ |
| 204 |
< |
unsigned int hashv = strhash(client_p->name); |
| 204 |
> |
const unsigned int hashv = strhash(client_p->name); |
| 205 |
|
struct Client *tmp = clientTable[hashv]; |
| 206 |
|
|
| 207 |
< |
if (tmp != NULL) |
| 207 |
> |
if (tmp) |
| 208 |
|
{ |
| 209 |
|
if (tmp == client_p) |
| 210 |
|
{ |
| 232 |
|
void |
| 233 |
|
hash_del_userhost(struct UserHost *userhost) |
| 234 |
|
{ |
| 235 |
< |
unsigned int hashv = strhash(userhost->host); |
| 235 |
> |
const unsigned int hashv = strhash(userhost->host); |
| 236 |
|
struct UserHost *tmp = userhostTable[hashv]; |
| 237 |
|
|
| 238 |
< |
if (tmp != NULL) |
| 238 |
> |
if (tmp) |
| 239 |
|
{ |
| 240 |
|
if (tmp == userhost) |
| 241 |
|
{ |
| 264 |
|
void |
| 265 |
|
hash_del_channel(struct Channel *chptr) |
| 266 |
|
{ |
| 267 |
< |
unsigned int hashv = strhash(chptr->chname); |
| 267 |
> |
const unsigned int hashv = strhash(chptr->name); |
| 268 |
|
struct Channel *tmp = channelTable[hashv]; |
| 269 |
|
|
| 270 |
< |
if (tmp != NULL) |
| 270 |
> |
if (tmp) |
| 271 |
|
{ |
| 272 |
|
if (tmp == chptr) |
| 273 |
|
{ |
| 286 |
|
} |
| 287 |
|
} |
| 288 |
|
|
| 307 |
– |
void |
| 308 |
– |
hash_del_resv(struct MaskItem *chptr) |
| 309 |
– |
{ |
| 310 |
– |
unsigned int hashv = strhash(chptr->name); |
| 311 |
– |
struct MaskItem *tmp = resvchannelTable[hashv]; |
| 312 |
– |
|
| 313 |
– |
if (tmp != NULL) |
| 314 |
– |
{ |
| 315 |
– |
if (tmp == chptr) |
| 316 |
– |
{ |
| 317 |
– |
resvchannelTable[hashv] = chptr->hnext; |
| 318 |
– |
chptr->hnext = chptr; |
| 319 |
– |
} |
| 320 |
– |
else |
| 321 |
– |
{ |
| 322 |
– |
while (tmp->hnext != chptr) |
| 323 |
– |
if ((tmp = tmp->hnext) == NULL) |
| 324 |
– |
return; |
| 325 |
– |
|
| 326 |
– |
tmp->hnext = tmp->hnext->hnext; |
| 327 |
– |
chptr->hnext = chptr; |
| 328 |
– |
} |
| 329 |
– |
} |
| 330 |
– |
} |
| 331 |
– |
|
| 289 |
|
/* hash_find_client() |
| 290 |
|
* |
| 291 |
|
* inputs - pointer to name |
| 297 |
|
struct Client * |
| 298 |
|
hash_find_client(const char *name) |
| 299 |
|
{ |
| 300 |
< |
unsigned int hashv = strhash(name); |
| 300 |
> |
const unsigned int hashv = strhash(name); |
| 301 |
|
struct Client *client_p; |
| 302 |
|
|
| 303 |
< |
if ((client_p = clientTable[hashv]) != NULL) |
| 303 |
> |
if ((client_p = clientTable[hashv])) |
| 304 |
|
{ |
| 305 |
|
if (irccmp(name, client_p->name)) |
| 306 |
|
{ |
| 307 |
|
struct Client *prev; |
| 308 |
|
|
| 309 |
< |
while (prev = client_p, (client_p = client_p->hnext) != NULL) |
| 309 |
> |
while (prev = client_p, (client_p = client_p->hnext)) |
| 310 |
|
{ |
| 311 |
|
if (!irccmp(name, client_p->name)) |
| 312 |
|
{ |
| 325 |
|
struct Client * |
| 326 |
|
hash_find_id(const char *name) |
| 327 |
|
{ |
| 328 |
< |
unsigned int hashv = strhash(name); |
| 328 |
> |
const unsigned int hashv = strhash(name); |
| 329 |
|
struct Client *client_p; |
| 330 |
|
|
| 331 |
< |
if ((client_p = idTable[hashv]) != NULL) |
| 331 |
> |
if ((client_p = idTable[hashv])) |
| 332 |
|
{ |
| 333 |
|
if (strcmp(name, client_p->id)) |
| 334 |
|
{ |
| 335 |
|
struct Client *prev; |
| 336 |
|
|
| 337 |
< |
while (prev = client_p, (client_p = client_p->idhnext) != NULL) |
| 337 |
> |
while (prev = client_p, (client_p = client_p->idhnext)) |
| 338 |
|
{ |
| 339 |
|
if (!strcmp(name, client_p->id)) |
| 340 |
|
{ |
| 353 |
|
struct Client * |
| 354 |
|
hash_find_server(const char *name) |
| 355 |
|
{ |
| 356 |
< |
unsigned int hashv = strhash(name); |
| 356 |
> |
const unsigned int hashv = strhash(name); |
| 357 |
|
struct Client *client_p = NULL; |
| 358 |
|
|
| 359 |
|
if (IsDigit(*name) && strlen(name) == IRC_MAXSID) |
| 360 |
|
return hash_find_id(name); |
| 361 |
|
|
| 362 |
< |
if ((client_p = clientTable[hashv]) != NULL) |
| 362 |
> |
if ((client_p = clientTable[hashv])) |
| 363 |
|
{ |
| 364 |
|
if ((!IsServer(client_p) && !IsMe(client_p)) || |
| 365 |
|
irccmp(name, client_p->name)) |
| 366 |
|
{ |
| 367 |
|
struct Client *prev; |
| 368 |
|
|
| 369 |
< |
while (prev = client_p, (client_p = client_p->hnext) != NULL) |
| 369 |
> |
while (prev = client_p, (client_p = client_p->hnext)) |
| 370 |
|
{ |
| 371 |
|
if ((IsServer(client_p) || IsMe(client_p)) && |
| 372 |
|
!irccmp(name, client_p->name)) |
| 387 |
|
* |
| 388 |
|
* inputs - pointer to name |
| 389 |
|
* output - NONE |
| 390 |
< |
* side effects - New semantics: finds a channel whose name is 'name', |
| 390 |
> |
* side effects - New semantics: finds a channel whose name is 'name', |
| 391 |
|
* if can't find one returns NULL, if can find it moves |
| 392 |
|
* it to the top of the list and returns it. |
| 393 |
|
*/ |
| 394 |
|
struct Channel * |
| 395 |
|
hash_find_channel(const char *name) |
| 396 |
|
{ |
| 397 |
< |
unsigned int hashv = strhash(name); |
| 397 |
> |
const unsigned int hashv = strhash(name); |
| 398 |
|
struct Channel *chptr = NULL; |
| 399 |
|
|
| 400 |
< |
if ((chptr = channelTable[hashv]) != NULL) |
| 400 |
> |
if ((chptr = channelTable[hashv])) |
| 401 |
|
{ |
| 402 |
< |
if (irccmp(name, chptr->chname)) |
| 402 |
> |
if (irccmp(name, chptr->name)) |
| 403 |
|
{ |
| 404 |
|
struct Channel *prev; |
| 405 |
|
|
| 406 |
< |
while (prev = chptr, (chptr = chptr->hnextch) != NULL) |
| 406 |
> |
while (prev = chptr, (chptr = chptr->hnextch)) |
| 407 |
|
{ |
| 408 |
< |
if (!irccmp(name, chptr->chname)) |
| 408 |
> |
if (!irccmp(name, chptr->name)) |
| 409 |
|
{ |
| 410 |
|
prev->hnextch = chptr->hnextch; |
| 411 |
|
chptr->hnextch = channelTable[hashv]; |
| 419 |
|
return chptr; |
| 420 |
|
} |
| 421 |
|
|
| 422 |
+ |
struct UserHost * |
| 423 |
+ |
hash_find_userhost(const char *host) |
| 424 |
+ |
{ |
| 425 |
+ |
const unsigned int hashv = strhash(host); |
| 426 |
+ |
struct UserHost *userhost; |
| 427 |
+ |
|
| 428 |
+ |
if ((userhost = userhostTable[hashv])) |
| 429 |
+ |
{ |
| 430 |
+ |
if (irccmp(host, userhost->host)) |
| 431 |
+ |
{ |
| 432 |
+ |
struct UserHost *prev; |
| 433 |
+ |
|
| 434 |
+ |
while (prev = userhost, (userhost = userhost->next)) |
| 435 |
+ |
{ |
| 436 |
+ |
if (!irccmp(host, userhost->host)) |
| 437 |
+ |
{ |
| 438 |
+ |
prev->next = userhost->next; |
| 439 |
+ |
userhost->next = userhostTable[hashv]; |
| 440 |
+ |
userhostTable[hashv] = userhost; |
| 441 |
+ |
break; |
| 442 |
+ |
} |
| 443 |
+ |
} |
| 444 |
+ |
} |
| 445 |
+ |
} |
| 446 |
+ |
|
| 447 |
+ |
return userhost; |
| 448 |
+ |
} |
| 449 |
+ |
|
| 450 |
|
/* hash_get_bucket(int type, unsigned int hashv) |
| 451 |
|
* |
| 452 |
|
* inputs - hash value (must be between 0 and HASHSIZE - 1) |
| 461 |
|
hash_get_bucket(int type, unsigned int hashv) |
| 462 |
|
{ |
| 463 |
|
assert(hashv < HASHSIZE); |
| 464 |
+ |
|
| 465 |
|
if (hashv >= HASHSIZE) |
| 466 |
< |
return NULL; |
| 466 |
> |
return NULL; |
| 467 |
|
|
| 468 |
|
switch (type) |
| 469 |
|
{ |
| 479 |
|
case HASH_TYPE_USERHOST: |
| 480 |
|
return userhostTable[hashv]; |
| 481 |
|
break; |
| 496 |
– |
case HASH_TYPE_RESERVED: |
| 497 |
– |
return resvchannelTable[hashv]; |
| 498 |
– |
break; |
| 482 |
|
default: |
| 483 |
|
assert(0); |
| 484 |
|
} |
| 486 |
|
return NULL; |
| 487 |
|
} |
| 488 |
|
|
| 506 |
– |
/* hash_find_resv() |
| 507 |
– |
* |
| 508 |
– |
* inputs - pointer to name |
| 509 |
– |
* output - NONE |
| 510 |
– |
* side effects - New semantics: finds a reserved channel whose name is 'name', |
| 511 |
– |
* if can't find one returns NULL, if can find it moves |
| 512 |
– |
* it to the top of the list and returns it. |
| 513 |
– |
*/ |
| 514 |
– |
struct MaskItem * |
| 515 |
– |
hash_find_resv(const char *name) |
| 516 |
– |
{ |
| 517 |
– |
unsigned int hashv = strhash(name); |
| 518 |
– |
struct MaskItem *chptr; |
| 519 |
– |
|
| 520 |
– |
if ((chptr = resvchannelTable[hashv]) != NULL) |
| 521 |
– |
{ |
| 522 |
– |
if (irccmp(name, chptr->name)) |
| 523 |
– |
{ |
| 524 |
– |
struct MaskItem *prev; |
| 525 |
– |
|
| 526 |
– |
while (prev = chptr, (chptr = chptr->hnext) != NULL) |
| 527 |
– |
{ |
| 528 |
– |
if (!irccmp(name, chptr->name)) |
| 529 |
– |
{ |
| 530 |
– |
prev->hnext = chptr->hnext; |
| 531 |
– |
chptr->hnext = resvchannelTable[hashv]; |
| 532 |
– |
resvchannelTable[hashv] = chptr; |
| 533 |
– |
break; |
| 534 |
– |
} |
| 535 |
– |
} |
| 536 |
– |
} |
| 537 |
– |
} |
| 538 |
– |
|
| 539 |
– |
return chptr; |
| 540 |
– |
} |
| 541 |
– |
|
| 542 |
– |
struct UserHost * |
| 543 |
– |
hash_find_userhost(const char *host) |
| 544 |
– |
{ |
| 545 |
– |
unsigned int hashv = strhash(host); |
| 546 |
– |
struct UserHost *userhost; |
| 547 |
– |
|
| 548 |
– |
if ((userhost = userhostTable[hashv])) |
| 549 |
– |
{ |
| 550 |
– |
if (irccmp(host, userhost->host)) |
| 551 |
– |
{ |
| 552 |
– |
struct UserHost *prev; |
| 553 |
– |
|
| 554 |
– |
while (prev = userhost, (userhost = userhost->next) != NULL) |
| 555 |
– |
{ |
| 556 |
– |
if (!irccmp(host, userhost->host)) |
| 557 |
– |
{ |
| 558 |
– |
prev->next = userhost->next; |
| 559 |
– |
userhost->next = userhostTable[hashv]; |
| 560 |
– |
userhostTable[hashv] = userhost; |
| 561 |
– |
break; |
| 562 |
– |
} |
| 563 |
– |
} |
| 564 |
– |
} |
| 565 |
– |
} |
| 566 |
– |
|
| 567 |
– |
return userhost; |
| 568 |
– |
} |
| 569 |
– |
|
| 570 |
– |
/* count_user_host() |
| 571 |
– |
* |
| 572 |
– |
* inputs - user name |
| 573 |
– |
* - hostname |
| 574 |
– |
* - int flag 1 if global, 0 if local |
| 575 |
– |
* - pointer to where global count should go |
| 576 |
– |
* - pointer to where local count should go |
| 577 |
– |
* - pointer to where identd count should go (local clients only) |
| 578 |
– |
* output - none |
| 579 |
– |
* side effects - |
| 580 |
– |
*/ |
| 581 |
– |
void |
| 582 |
– |
count_user_host(const char *user, const char *host, unsigned int *global_p, |
| 583 |
– |
unsigned int *local_p, unsigned int *icount_p) |
| 584 |
– |
{ |
| 585 |
– |
dlink_node *ptr; |
| 586 |
– |
struct UserHost *found_userhost; |
| 587 |
– |
struct NameHost *nameh; |
| 588 |
– |
|
| 589 |
– |
if ((found_userhost = hash_find_userhost(host)) == NULL) |
| 590 |
– |
return; |
| 591 |
– |
|
| 592 |
– |
DLINK_FOREACH(ptr, found_userhost->list.head) |
| 593 |
– |
{ |
| 594 |
– |
nameh = ptr->data; |
| 595 |
– |
|
| 596 |
– |
if (!irccmp(user, nameh->name)) |
| 597 |
– |
{ |
| 598 |
– |
if (global_p != NULL) |
| 599 |
– |
*global_p = nameh->gcount; |
| 600 |
– |
if (local_p != NULL) |
| 601 |
– |
*local_p = nameh->lcount; |
| 602 |
– |
if (icount_p != NULL) |
| 603 |
– |
*icount_p = nameh->icount; |
| 604 |
– |
return; |
| 605 |
– |
} |
| 606 |
– |
} |
| 607 |
– |
} |
| 608 |
– |
|
| 609 |
– |
/* find_or_add_userhost() |
| 610 |
– |
* |
| 611 |
– |
* inputs - host name |
| 612 |
– |
* output - none |
| 613 |
– |
* side effects - find UserHost * for given host name |
| 614 |
– |
*/ |
| 615 |
– |
static struct UserHost * |
| 616 |
– |
find_or_add_userhost(const char *host) |
| 617 |
– |
{ |
| 618 |
– |
struct UserHost *userhost; |
| 619 |
– |
|
| 620 |
– |
if ((userhost = hash_find_userhost(host)) != NULL) |
| 621 |
– |
return userhost; |
| 622 |
– |
|
| 623 |
– |
userhost = mp_pool_get(userhost_pool); |
| 624 |
– |
|
| 625 |
– |
memset(userhost, 0, sizeof(*userhost)); |
| 626 |
– |
strlcpy(userhost->host, host, sizeof(userhost->host)); |
| 627 |
– |
hash_add_userhost(userhost); |
| 628 |
– |
|
| 629 |
– |
return userhost; |
| 630 |
– |
} |
| 631 |
– |
|
| 632 |
– |
/* add_user_host() |
| 633 |
– |
* |
| 634 |
– |
* inputs - user name |
| 635 |
– |
* - hostname |
| 636 |
– |
* - int flag 1 if global, 0 if local |
| 637 |
– |
* output - none |
| 638 |
– |
* side effects - add given user@host to hash tables |
| 639 |
– |
*/ |
| 640 |
– |
void |
| 641 |
– |
add_user_host(const char *user, const char *host, int global) |
| 642 |
– |
{ |
| 643 |
– |
dlink_node *ptr; |
| 644 |
– |
struct UserHost *found_userhost; |
| 645 |
– |
struct NameHost *nameh; |
| 646 |
– |
int hasident = 1; |
| 647 |
– |
|
| 648 |
– |
if (*user == '~') |
| 649 |
– |
{ |
| 650 |
– |
hasident = 0; |
| 651 |
– |
++user; |
| 652 |
– |
} |
| 653 |
– |
|
| 654 |
– |
if ((found_userhost = find_or_add_userhost(host)) == NULL) |
| 655 |
– |
return; |
| 656 |
– |
|
| 657 |
– |
DLINK_FOREACH(ptr, found_userhost->list.head) |
| 658 |
– |
{ |
| 659 |
– |
nameh = ptr->data; |
| 660 |
– |
|
| 661 |
– |
if (!irccmp(user, nameh->name)) |
| 662 |
– |
{ |
| 663 |
– |
nameh->gcount++; |
| 664 |
– |
|
| 665 |
– |
if (!global) |
| 666 |
– |
{ |
| 667 |
– |
if (hasident) |
| 668 |
– |
nameh->icount++; |
| 669 |
– |
nameh->lcount++; |
| 670 |
– |
} |
| 671 |
– |
|
| 672 |
– |
return; |
| 673 |
– |
} |
| 674 |
– |
} |
| 675 |
– |
|
| 676 |
– |
nameh = mp_pool_get(namehost_pool); |
| 677 |
– |
memset(nameh, 0, sizeof(*nameh)); |
| 678 |
– |
strlcpy(nameh->name, user, sizeof(nameh->name)); |
| 679 |
– |
|
| 680 |
– |
nameh->gcount = 1; |
| 681 |
– |
|
| 682 |
– |
if (!global) |
| 683 |
– |
{ |
| 684 |
– |
if (hasident) |
| 685 |
– |
nameh->icount = 1; |
| 686 |
– |
nameh->lcount = 1; |
| 687 |
– |
} |
| 688 |
– |
|
| 689 |
– |
dlinkAdd(nameh, &nameh->node, &found_userhost->list); |
| 690 |
– |
} |
| 691 |
– |
|
| 692 |
– |
/* delete_user_host() |
| 693 |
– |
* |
| 694 |
– |
* inputs - user name |
| 695 |
– |
* - hostname |
| 696 |
– |
* - int flag 1 if global, 0 if local |
| 697 |
– |
* output - none |
| 698 |
– |
* side effects - delete given user@host to hash tables |
| 699 |
– |
*/ |
| 700 |
– |
void |
| 701 |
– |
delete_user_host(const char *user, const char *host, int global) |
| 702 |
– |
{ |
| 703 |
– |
dlink_node *ptr = NULL, *next_ptr = NULL; |
| 704 |
– |
struct UserHost *found_userhost; |
| 705 |
– |
struct NameHost *nameh; |
| 706 |
– |
int hasident = 1; |
| 707 |
– |
|
| 708 |
– |
if (*user == '~') |
| 709 |
– |
{ |
| 710 |
– |
hasident = 0; |
| 711 |
– |
++user; |
| 712 |
– |
} |
| 713 |
– |
|
| 714 |
– |
if ((found_userhost = hash_find_userhost(host)) == NULL) |
| 715 |
– |
return; |
| 716 |
– |
|
| 717 |
– |
DLINK_FOREACH_SAFE(ptr, next_ptr, found_userhost->list.head) |
| 718 |
– |
{ |
| 719 |
– |
nameh = ptr->data; |
| 720 |
– |
|
| 721 |
– |
if (!irccmp(user, nameh->name)) |
| 722 |
– |
{ |
| 723 |
– |
if (nameh->gcount > 0) |
| 724 |
– |
nameh->gcount--; |
| 725 |
– |
if (!global) |
| 726 |
– |
{ |
| 727 |
– |
if (nameh->lcount > 0) |
| 728 |
– |
nameh->lcount--; |
| 729 |
– |
if (hasident && nameh->icount > 0) |
| 730 |
– |
nameh->icount--; |
| 731 |
– |
} |
| 732 |
– |
|
| 733 |
– |
if (nameh->gcount == 0 && nameh->lcount == 0) |
| 734 |
– |
{ |
| 735 |
– |
dlinkDelete(&nameh->node, &found_userhost->list); |
| 736 |
– |
mp_pool_release(nameh); |
| 737 |
– |
} |
| 738 |
– |
|
| 739 |
– |
if (dlink_list_length(&found_userhost->list) == 0) |
| 740 |
– |
{ |
| 741 |
– |
hash_del_userhost(found_userhost); |
| 742 |
– |
mp_pool_release(found_userhost); |
| 743 |
– |
} |
| 744 |
– |
|
| 745 |
– |
return; |
| 746 |
– |
} |
| 747 |
– |
} |
| 748 |
– |
} |
| 749 |
– |
|
| 489 |
|
/* |
| 490 |
|
* Safe list code. |
| 491 |
|
* |
| 509 |
|
* Sendq limit is fairly conservative at 1/2 (In original anyway) |
| 510 |
|
*/ |
| 511 |
|
static int |
| 512 |
< |
exceeding_sendq(struct Client *to) |
| 512 |
> |
exceeding_sendq(const struct Client *to) |
| 513 |
|
{ |
| 514 |
< |
if (dbuf_length(&to->localClient->buf_sendq) > (get_sendq(&to->localClient->confs) / 2)) |
| 514 |
> |
if (dbuf_length(&to->connection->buf_sendq) > (get_sendq(&to->connection->confs) / 2)) |
| 515 |
|
return 1; |
| 516 |
|
else |
| 517 |
|
return 0; |
| 518 |
|
} |
| 519 |
|
|
| 520 |
|
void |
| 521 |
< |
free_list_task(struct ListTask *lt, struct Client *source_p) |
| 521 |
> |
free_list_task(struct Client *source_p) |
| 522 |
|
{ |
| 523 |
< |
dlink_node *dl, *dln; |
| 523 |
> |
struct ListTask *const lt = source_p->connection->list_task; |
| 524 |
> |
dlink_node *node = NULL, *node_next = NULL; |
| 525 |
|
|
| 526 |
< |
if ((dl = dlinkFindDelete(&listing_client_list, source_p)) != NULL) |
| 787 |
< |
free_dlink_node(dl); |
| 526 |
> |
dlinkDelete(<->node, &listing_client_list); |
| 527 |
|
|
| 528 |
< |
DLINK_FOREACH_SAFE(dl, dln, lt->show_mask.head) |
| 528 |
> |
DLINK_FOREACH_SAFE(node, node_next, lt->show_mask.head) |
| 529 |
|
{ |
| 530 |
< |
MyFree(dl->data); |
| 531 |
< |
free_dlink_node(dl); |
| 530 |
> |
xfree(node->data); |
| 531 |
> |
free_dlink_node(node); |
| 532 |
|
} |
| 533 |
|
|
| 534 |
< |
DLINK_FOREACH_SAFE(dl, dln, lt->hide_mask.head) |
| 534 |
> |
DLINK_FOREACH_SAFE(node, node_next, lt->hide_mask.head) |
| 535 |
|
{ |
| 536 |
< |
MyFree(dl->data); |
| 537 |
< |
free_dlink_node(dl); |
| 536 |
> |
xfree(node->data); |
| 537 |
> |
free_dlink_node(node); |
| 538 |
|
} |
| 539 |
|
|
| 540 |
< |
MyFree(lt); |
| 541 |
< |
|
| 803 |
< |
if (MyConnect(source_p)) |
| 804 |
< |
source_p->localClient->list_task = NULL; |
| 540 |
> |
xfree(lt); |
| 541 |
> |
source_p->connection->list_task = NULL; |
| 542 |
|
} |
| 543 |
|
|
| 544 |
|
/* list_allow_channel() |
| 550 |
|
* side effects - |
| 551 |
|
*/ |
| 552 |
|
static int |
| 553 |
< |
list_allow_channel(const char *chname, struct ListTask *lt) |
| 553 |
> |
list_allow_channel(const char *name, const struct ListTask *lt) |
| 554 |
|
{ |
| 555 |
< |
dlink_node *dl = NULL; |
| 555 |
> |
const dlink_node *node = NULL; |
| 556 |
|
|
| 557 |
< |
DLINK_FOREACH(dl, lt->show_mask.head) |
| 558 |
< |
if (match(dl->data, chname) != 0) |
| 557 |
> |
DLINK_FOREACH(node, lt->show_mask.head) |
| 558 |
> |
if (match(node->data, name) != 0) |
| 559 |
|
return 0; |
| 560 |
|
|
| 561 |
< |
DLINK_FOREACH(dl, lt->hide_mask.head) |
| 562 |
< |
if (match(dl->data, chname) == 0) |
| 561 |
> |
DLINK_FOREACH(node, lt->hide_mask.head) |
| 562 |
> |
if (match(node->data, name) == 0) |
| 563 |
|
return 0; |
| 564 |
|
|
| 565 |
|
return 1; |
| 574 |
|
* side effects - |
| 575 |
|
*/ |
| 576 |
|
static void |
| 577 |
< |
list_one_channel(struct Client *source_p, struct Channel *chptr, |
| 841 |
< |
struct ListTask *list_task) |
| 577 |
> |
list_one_channel(struct Client *source_p, struct Channel *chptr) |
| 578 |
|
{ |
| 579 |
< |
if (SecretChannel(chptr) && !IsMember(source_p, chptr)) |
| 579 |
> |
const struct ListTask *const lt = source_p->connection->list_task; |
| 580 |
> |
char listbuf[MODEBUFLEN] = ""; |
| 581 |
> |
char modebuf[MODEBUFLEN] = ""; |
| 582 |
> |
char parabuf[MODEBUFLEN] = ""; |
| 583 |
> |
|
| 584 |
> |
if (SecretChannel(chptr) && |
| 585 |
> |
!(HasUMode(source_p, UMODE_ADMIN) || IsMember(source_p, chptr))) |
| 586 |
|
return; |
| 587 |
< |
if (dlink_list_length(&chptr->members) < list_task->users_min || |
| 588 |
< |
dlink_list_length(&chptr->members) > list_task->users_max || |
| 589 |
< |
(chptr->channelts != 0 && |
| 590 |
< |
((unsigned int)chptr->channelts < list_task->created_min || |
| 591 |
< |
(unsigned int)chptr->channelts > list_task->created_max)) || |
| 592 |
< |
(unsigned int)chptr->topic_time < list_task->topicts_min || |
| 587 |
> |
|
| 588 |
> |
if (dlink_list_length(&chptr->members) < lt->users_min || |
| 589 |
> |
dlink_list_length(&chptr->members) > lt->users_max || |
| 590 |
> |
(chptr->creationtime != 0 && |
| 591 |
> |
((unsigned int)chptr->creationtime < lt->created_min || |
| 592 |
> |
(unsigned int)chptr->creationtime > lt->created_max)) || |
| 593 |
> |
(unsigned int)chptr->topic_time < lt->topicts_min || |
| 594 |
|
(chptr->topic_time ? (unsigned int)chptr->topic_time : UINT_MAX) > |
| 595 |
< |
list_task->topicts_max) |
| 595 |
> |
lt->topicts_max) |
| 596 |
> |
return; |
| 597 |
> |
|
| 598 |
> |
if (lt->topic[0] && match(lt->topic, chptr->topic)) |
| 599 |
|
return; |
| 600 |
|
|
| 601 |
< |
if (!list_allow_channel(chptr->chname, list_task)) |
| 601 |
> |
if (!list_allow_channel(chptr->name, lt)) |
| 602 |
|
return; |
| 603 |
< |
sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, |
| 604 |
< |
chptr->chname, dlink_list_length(&chptr->members), |
| 605 |
< |
chptr->topic); |
| 603 |
> |
|
| 604 |
> |
channel_modes(chptr, source_p, modebuf, parabuf); |
| 605 |
> |
|
| 606 |
> |
if (chptr->topic[0]) |
| 607 |
> |
snprintf(listbuf, sizeof(listbuf), "[%s] ", modebuf); |
| 608 |
> |
else |
| 609 |
> |
snprintf(listbuf, sizeof(listbuf), "[%s]", modebuf); |
| 610 |
> |
|
| 611 |
> |
sendto_one_numeric(source_p, &me, RPL_LIST, chptr->name, |
| 612 |
> |
dlink_list_length(&chptr->members), |
| 613 |
> |
listbuf, chptr->topic); |
| 614 |
|
} |
| 615 |
|
|
| 616 |
|
/* safe_list_channels() |
| 622 |
|
* Walk the channel buckets, ensure all pointers in a bucket are |
| 623 |
|
* traversed before blocking on a sendq. This means, no locking is needed. |
| 624 |
|
* |
| 871 |
– |
* N.B. This code is "remote" safe, but is not currently used for |
| 872 |
– |
* remote clients. |
| 873 |
– |
* |
| 625 |
|
* - Dianora |
| 626 |
|
*/ |
| 627 |
|
void |
| 628 |
< |
safe_list_channels(struct Client *source_p, struct ListTask *list_task, |
| 878 |
< |
int only_unmasked_channels) |
| 628 |
> |
safe_list_channels(struct Client *source_p, int only_unmasked_channels) |
| 629 |
|
{ |
| 630 |
+ |
struct ListTask *const lt = source_p->connection->list_task; |
| 631 |
|
struct Channel *chptr = NULL; |
| 632 |
|
|
| 633 |
|
if (!only_unmasked_channels) |
| 634 |
|
{ |
| 635 |
< |
unsigned int i; |
| 885 |
< |
|
| 886 |
< |
for (i = list_task->hash_index; i < HASHSIZE; ++i) |
| 635 |
> |
for (unsigned int i = lt->hash_index; i < HASHSIZE; ++i) |
| 636 |
|
{ |
| 637 |
< |
if (exceeding_sendq(source_p->from)) |
| 637 |
> |
if (exceeding_sendq(source_p)) |
| 638 |
|
{ |
| 639 |
< |
list_task->hash_index = i; |
| 640 |
< |
return; /* still more to do */ |
| 639 |
> |
lt->hash_index = i; |
| 640 |
> |
return; /* Still more to do */ |
| 641 |
|
} |
| 642 |
|
|
| 643 |
|
for (chptr = channelTable[i]; chptr; chptr = chptr->hnextch) |
| 644 |
< |
list_one_channel(source_p, chptr, list_task); |
| 644 |
> |
list_one_channel(source_p, chptr); |
| 645 |
|
} |
| 646 |
|
} |
| 647 |
|
else |
| 648 |
|
{ |
| 649 |
< |
dlink_node *dl; |
| 649 |
> |
dlink_node *node = NULL; |
| 650 |
|
|
| 651 |
< |
DLINK_FOREACH(dl, list_task->show_mask.head) |
| 652 |
< |
if ((chptr = hash_find_channel(dl->data)) != NULL) |
| 653 |
< |
list_one_channel(source_p, chptr, list_task); |
| 651 |
> |
DLINK_FOREACH(node, lt->show_mask.head) |
| 652 |
> |
if ((chptr = hash_find_channel(node->data))) |
| 653 |
> |
list_one_channel(source_p, chptr); |
| 654 |
|
} |
| 655 |
|
|
| 656 |
< |
free_list_task(list_task, source_p); |
| 657 |
< |
sendto_one(source_p, form_str(RPL_LISTEND), |
| 909 |
< |
me.name, source_p->name); |
| 656 |
> |
free_list_task(source_p); |
| 657 |
> |
sendto_one_numeric(source_p, &me, RPL_LISTEND); |
| 658 |
|
} |