1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2003-2018 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 |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file userhost.c |
23 |
* \brief Global user limits. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "list.h" |
28 |
#include "hash.h" |
29 |
#include "client.h" |
30 |
#include "userhost.h" |
31 |
#include "mempool.h" |
32 |
#include "irc_string.h" |
33 |
|
34 |
|
35 |
static mp_pool_t *userhost_pool; |
36 |
|
37 |
|
38 |
void |
39 |
userhost_init(void) |
40 |
{ |
41 |
userhost_pool = mp_pool_new(sizeof(struct UserHost), MP_CHUNK_SIZE_USERHOST); |
42 |
} |
43 |
|
44 |
/* userhost_count() |
45 |
* |
46 |
* inputs - user name |
47 |
* - hostname |
48 |
* - int flag 1 if global, 0 if local |
49 |
* - pointer to where global count should go |
50 |
* - pointer to where local count should go |
51 |
* - pointer to where identd count should go (local clients only) |
52 |
* output - none |
53 |
* side effects - |
54 |
*/ |
55 |
void |
56 |
userhost_count(const char *host, unsigned int *global_p, unsigned int *local_p) |
57 |
{ |
58 |
struct UserHost *userhost; |
59 |
|
60 |
if ((userhost = hash_find_userhost(host)) == NULL) |
61 |
return; |
62 |
|
63 |
if (global_p) |
64 |
*global_p = userhost->gcount; |
65 |
if (local_p) |
66 |
*local_p = userhost->lcount; |
67 |
} |
68 |
|
69 |
/* userhost_find_or_add() |
70 |
* |
71 |
* inputs - host name |
72 |
* output - none |
73 |
* side effects - find UserHost * for given host name |
74 |
*/ |
75 |
static struct UserHost * |
76 |
userhost_find_or_add(const char *host) |
77 |
{ |
78 |
struct UserHost *userhost; |
79 |
|
80 |
if ((userhost = hash_find_userhost(host))) |
81 |
return userhost; |
82 |
|
83 |
userhost = mp_pool_get(userhost_pool); |
84 |
|
85 |
strlcpy(userhost->host, host, sizeof(userhost->host)); |
86 |
hash_add_userhost(userhost); |
87 |
|
88 |
return userhost; |
89 |
} |
90 |
|
91 |
/* userhost_add() |
92 |
* |
93 |
* inputs - user name |
94 |
* - hostname |
95 |
* - int flag 1 if global, 0 if local |
96 |
* output - none |
97 |
* side effects - add given user@host to hash tables |
98 |
*/ |
99 |
void |
100 |
userhost_add(const char *host, int global) |
101 |
{ |
102 |
struct UserHost *userhost; |
103 |
|
104 |
if ((userhost = userhost_find_or_add(host)) == NULL) |
105 |
return; |
106 |
|
107 |
userhost->gcount++; |
108 |
|
109 |
if (!global) |
110 |
userhost->lcount++; |
111 |
} |
112 |
|
113 |
/* userhost_del() |
114 |
* |
115 |
* inputs - user name |
116 |
* - hostname |
117 |
* - int flag 1 if global, 0 if local |
118 |
* output - none |
119 |
* side effects - delete given user@host to hash tables |
120 |
*/ |
121 |
void |
122 |
userhost_del(const char *host, int global) |
123 |
{ |
124 |
struct UserHost *userhost; |
125 |
|
126 |
if ((userhost = hash_find_userhost(host)) == NULL) |
127 |
return; |
128 |
|
129 |
if (userhost->gcount > 0) |
130 |
userhost->gcount--; |
131 |
|
132 |
if (!global) |
133 |
if (userhost->lcount > 0) |
134 |
userhost->lcount--; |
135 |
|
136 |
if (userhost->gcount == 0 && userhost->lcount == 0) |
137 |
{ |
138 |
hash_del_userhost(userhost); |
139 |
mp_pool_release(userhost); |
140 |
} |
141 |
} |