2007-01-25 07:40:21 +01:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* whowas.c: WHOWAS user cache.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
2016-01-23 17:16:34 +01:00
|
|
|
* Copyright (C) 2002-2012 ircd-ratbox development team
|
|
|
|
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2016-01-23 17:16:34 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
2007-01-25 07:40:21 +01:00
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <ircd/stdinc.h>
|
|
|
|
#include <ircd/hash.h>
|
|
|
|
#include <ircd/whowas.h>
|
|
|
|
#include <ircd/match.h>
|
|
|
|
#include <ircd/ircd.h>
|
|
|
|
#include <ircd/numeric.h>
|
|
|
|
#include <ircd/s_assert.h>
|
|
|
|
#include <ircd/s_serv.h>
|
|
|
|
#include <ircd/s_user.h>
|
|
|
|
#include <ircd/send.h>
|
|
|
|
#include <ircd/s_conf.h>
|
|
|
|
#include <ircd/client.h>
|
|
|
|
#include <ircd/send.h>
|
|
|
|
#include <ircd/logger.h>
|
|
|
|
#include <ircd/scache.h>
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
struct whowas_top
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
rb_dlink_list wwlist;
|
|
|
|
};
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-03-23 14:32:22 +01:00
|
|
|
static rb_radixtree *whowas_tree = NULL;
|
2016-01-23 17:16:34 +01:00
|
|
|
static rb_dlink_list whowas_list = {NULL, NULL, 0};
|
|
|
|
static unsigned int whowas_list_length = NICKNAMEHISTORYLENGTH;
|
|
|
|
static void whowas_trim(void *unused);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
static void
|
|
|
|
whowas_free_wtop(struct whowas_top *wtop)
|
|
|
|
{
|
|
|
|
if(rb_dlink_list_length(&wtop->wwlist) == 0)
|
|
|
|
{
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_radixtree_delete(whowas_tree, wtop->name);
|
2016-01-23 17:16:34 +01:00
|
|
|
rb_free(wtop->name);
|
|
|
|
rb_free(wtop);
|
|
|
|
}
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
static struct whowas_top *
|
|
|
|
whowas_get_top(const char *name)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
struct whowas_top *wtop;
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
wtop = (whowas_top *)rb_radixtree_retrieve(whowas_tree, name);
|
2016-01-23 17:16:34 +01:00
|
|
|
if (wtop != NULL)
|
|
|
|
return wtop;
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
wtop = (whowas_top *)rb_malloc(sizeof(struct whowas_top));
|
2016-01-23 17:16:34 +01:00
|
|
|
wtop->name = rb_strdup(name);
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_radixtree_add(whowas_tree, wtop->name, wtop);
|
2016-01-23 17:16:34 +01:00
|
|
|
|
|
|
|
return wtop;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
rb_dlink_list *
|
|
|
|
whowas_get_list(const char *name)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
struct whowas_top *wtop;
|
2016-07-13 07:17:21 +02:00
|
|
|
wtop = (whowas_top *)rb_radixtree_retrieve(whowas_tree, name);
|
2016-01-23 17:16:34 +01:00
|
|
|
if(wtop == NULL)
|
|
|
|
return NULL;
|
|
|
|
return &wtop->wwlist;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
void
|
|
|
|
whowas_add_history(struct Client *client_p, int online)
|
|
|
|
{
|
|
|
|
struct whowas_top *wtop;
|
|
|
|
struct Whowas *who;
|
2007-01-25 07:40:21 +01:00
|
|
|
s_assert(NULL != client_p);
|
|
|
|
|
|
|
|
if(client_p == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
/* trim some of the entries if we're getting well over our history length */
|
|
|
|
if(rb_dlink_list_length(&whowas_list) > whowas_list_length + 100)
|
|
|
|
whowas_trim(NULL);
|
|
|
|
|
|
|
|
wtop = whowas_get_top(client_p->name);
|
2016-07-13 07:17:21 +02:00
|
|
|
who = (Whowas *)rb_malloc(sizeof(struct Whowas));
|
2016-01-23 17:16:34 +01:00
|
|
|
who->wtop = wtop;
|
2008-04-02 01:53:20 +02:00
|
|
|
who->logoff = rb_current_time();
|
2016-01-23 17:16:34 +01:00
|
|
|
|
2008-04-20 06:40:40 +02:00
|
|
|
rb_strlcpy(who->name, client_p->name, sizeof(who->name));
|
2016-01-23 17:16:34 +01:00
|
|
|
rb_strlcpy(who->username, client_p->username, sizeof(who->username));
|
|
|
|
rb_strlcpy(who->hostname, client_p->host, sizeof(who->hostname));
|
|
|
|
rb_strlcpy(who->realname, client_p->info, sizeof(who->realname));
|
|
|
|
rb_strlcpy(who->sockhost, client_p->sockhost, sizeof(who->sockhost));
|
|
|
|
|
2013-09-14 12:26:32 +02:00
|
|
|
who->flags = (IsIPSpoof(client_p) ? WHOWAS_IP_SPOOFING : 0) |
|
|
|
|
(IsDynSpoof(client_p) ? WHOWAS_DYNSPOOF : 0);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
/* this is safe do to with the servername cache */
|
2007-11-23 22:11:25 +01:00
|
|
|
who->servername = scache_get_name(client_p->servptr->serv->nameinfo);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
if(online)
|
|
|
|
{
|
|
|
|
who->online = client_p;
|
2016-01-23 17:16:34 +01:00
|
|
|
rb_dlinkAdd(who, &who->cnode, &client_p->whowas_clist);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
who->online = NULL;
|
2016-01-23 17:16:34 +01:00
|
|
|
|
|
|
|
rb_dlinkAdd(who, &who->wnode, &wtop->wwlist);
|
|
|
|
rb_dlinkAdd(who, &who->whowas_node, &whowas_list);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
whowas_off_history(struct Client *client_p)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
rb_dlink_node *ptr, *next;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, client_p->whowas_clist.head)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct Whowas *who = (Whowas *)ptr->data;
|
2016-01-23 17:16:34 +01:00
|
|
|
who->online = NULL;
|
|
|
|
rb_dlinkDelete(&who->cnode, &client_p->whowas_clist);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
struct Client *
|
|
|
|
whowas_get_history(const char *nick, time_t timelimit)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
struct whowas_top *wtop;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
wtop = (whowas_top *)rb_radixtree_retrieve(whowas_tree, nick);
|
2016-01-23 17:16:34 +01:00
|
|
|
if(wtop == NULL)
|
|
|
|
return NULL;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-02 01:53:20 +02:00
|
|
|
timelimit = rb_current_time() - timelimit;
|
2016-01-23 17:16:34 +01:00
|
|
|
|
|
|
|
RB_DLINK_FOREACH_PREV(ptr, wtop->wwlist.tail)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct Whowas *who = (Whowas *)ptr->data;
|
2016-01-23 17:16:34 +01:00
|
|
|
if(who->logoff >= timelimit)
|
|
|
|
{
|
|
|
|
return who->online;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
2016-01-23 17:16:34 +01:00
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
static void
|
|
|
|
whowas_trim(void *unused)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
long over;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
if(rb_dlink_list_length(&whowas_list) < whowas_list_length)
|
|
|
|
return;
|
|
|
|
over = rb_dlink_list_length(&whowas_list) - whowas_list_length;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
/* remove whowas entries over the configured length */
|
|
|
|
for(long i = 0; i < over; i++)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
if(whowas_list.tail != NULL && whowas_list.tail->data != NULL)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct Whowas *twho = (Whowas *)whowas_list.tail->data;
|
2016-01-23 17:16:34 +01:00
|
|
|
if(twho->online != NULL)
|
|
|
|
rb_dlinkDelete(&twho->cnode, &twho->online->whowas_clist);
|
|
|
|
rb_dlinkDelete(&twho->wnode, &twho->wtop->wwlist);
|
|
|
|
rb_dlinkDelete(&twho->whowas_node, &whowas_list);
|
|
|
|
whowas_free_wtop(twho->wtop);
|
|
|
|
rb_free(twho);
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
void
|
|
|
|
whowas_init(void)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-03-06 21:17:19 +01:00
|
|
|
whowas_tree = rb_radixtree_create("whowas", irccasecanon);
|
2016-01-23 17:16:34 +01:00
|
|
|
if(whowas_list_length == 0)
|
|
|
|
{
|
|
|
|
whowas_list_length = NICKNAMEHISTORYLENGTH;
|
|
|
|
}
|
|
|
|
rb_event_add("whowas_trim", whowas_trim, NULL, 10);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
void
|
|
|
|
whowas_set_size(int len)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
whowas_list_length = len;
|
|
|
|
whowas_trim(NULL);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:34 +01:00
|
|
|
void
|
|
|
|
whowas_memory_usage(size_t * count, size_t * memused)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-01-23 17:16:34 +01:00
|
|
|
*count = rb_dlink_list_length(&whowas_list);
|
|
|
|
*memused += *count * sizeof(struct Whowas);
|
2016-03-06 21:17:19 +01:00
|
|
|
*memused += sizeof(struct whowas_top) * rb_radixtree_size(whowas_tree);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|