annotate src/anvil/connect-limit.c @ 22656:1789bf2a1e01

director: Make sure HOST-RESET-USERS isn't used with max_moving_users=0 The reset command would just hang in that case. doveadm would never have sent this, so this is just an extra sanity check.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Sun, 05 Nov 2017 23:51:56 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 19552
diff changeset
1 /* Copyright (c) 2009-2017 Dovecot authors, see the included COPYING file */
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "common.h"
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "hash.h"
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
5 #include "str.h"
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
6 #include "strescape.h"
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
7 #include "ostream.h"
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "connect-limit.h"
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 struct ident_pid {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 /* ident string points to ident_hash keys */
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 const char *ident;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 pid_t pid;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 unsigned int refcount;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 };
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 struct connect_limit {
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
18 /* ident => unsigned int refcount */
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
19 HASH_TABLE(char *, void *) ident_hash;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 /* struct ident_pid => struct ident_pid */
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
21 HASH_TABLE(struct ident_pid *, struct ident_pid *) ident_pid_hash;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 };
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
24 static unsigned int ident_pid_hash(const struct ident_pid *i)
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 return str_hash(i->ident) ^ i->pid;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
29 static int ident_pid_cmp(const struct ident_pid *i1, const struct ident_pid *i2)
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 if (i1->pid < i2->pid)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 return -1;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 else if (i1->pid > i2->pid)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 return 1;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 else
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 return strcmp(i1->ident, i2->ident);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 struct connect_limit *connect_limit_init(void)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 struct connect_limit *limit;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 limit = i_new(struct connect_limit, 1);
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
44 hash_table_create(&limit->ident_hash, default_pool, 0, str_hash, strcmp);
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
45 hash_table_create(&limit->ident_pid_hash, default_pool, 0,
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
46 ident_pid_hash, ident_pid_cmp);
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 return limit;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 void connect_limit_deinit(struct connect_limit **_limit)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 struct connect_limit *limit = *_limit;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 *_limit = NULL;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 hash_table_destroy(&limit->ident_hash);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 hash_table_destroy(&limit->ident_pid_hash);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 i_free(limit);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 unsigned int connect_limit_lookup(struct connect_limit *limit,
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 const char *ident)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 {
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
63 void *value;
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
64
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
65 value = hash_table_lookup(limit->ident_hash, ident);
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
66 return POINTER_CAST_TO(value, unsigned int);
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 void connect_limit_connect(struct connect_limit *limit, pid_t pid,
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 const char *ident)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 struct ident_pid *i, lookup_i;
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
73 char *key;
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
74 void *value;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
76 if (!hash_table_lookup_full(limit->ident_hash, ident,
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
77 &key, &value)) {
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 key = i_strdup(ident);
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
79 value = POINTER_CAST(1);
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
80 hash_table_insert(limit->ident_hash, key, value);
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 } else {
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
82 value = POINTER_CAST(POINTER_CAST_TO(value, unsigned int) + 1);
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 hash_table_update(limit->ident_hash, key, value);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 lookup_i.ident = ident;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 lookup_i.pid = pid;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 i = hash_table_lookup(limit->ident_pid_hash, &lookup_i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 if (i == NULL) {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 i = i_new(struct ident_pid, 1);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 i->ident = key;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 i->pid = pid;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 i->refcount = 1;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 hash_table_insert(limit->ident_pid_hash, i, i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 } else {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 i->refcount++;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 static void
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 connect_limit_ident_hash_unref(struct connect_limit *limit, const char *ident)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 {
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
103 char *key;
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
104 void *value;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 unsigned int new_refcount;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
107 if (!hash_table_lookup_full(limit->ident_hash, ident, &key, &value))
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 i_panic("connect limit hash tables are inconsistent");
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
110 new_refcount = POINTER_CAST_TO(value, unsigned int) - 1;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 if (new_refcount > 0) {
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
112 value = POINTER_CAST(new_refcount);
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
113 hash_table_update(limit->ident_hash, key, value);
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 } else {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 hash_table_remove(limit->ident_hash, key);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 i_free(key);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 void connect_limit_disconnect(struct connect_limit *limit, pid_t pid,
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 const char *ident)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 struct ident_pid *i, lookup_i;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 lookup_i.ident = ident;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 lookup_i.pid = pid;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 i = hash_table_lookup(limit->ident_pid_hash, &lookup_i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 if (i == NULL) {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 i_error("connect limit: disconnection for unknown "
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 "pid %s + ident %s", dec2str(pid), ident);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 return;
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 if (--i->refcount == 0) {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 hash_table_remove(limit->ident_pid_hash, i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 i_free(i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 connect_limit_ident_hash_unref(limit, ident);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 void connect_limit_disconnect_pid(struct connect_limit *limit, pid_t pid)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 struct hash_iterate_context *iter;
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
146 struct ident_pid *i, *value;
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 /* this should happen rarely (or never), so this slow implementation
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 should be fine. */
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 iter = hash_table_iterate_init(limit->ident_pid_hash);
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
151 while (hash_table_iterate(iter, limit->ident_pid_hash, &i, &value)) {
9235
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 if (i->pid == pid) {
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 hash_table_remove(limit->ident_pid_hash, i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 for (; i->refcount > 0; i->refcount--)
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 connect_limit_ident_hash_unref(limit, i->ident);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 i_free(i);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 }
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 hash_table_iterate_deinit(&iter);
2e2b957f1cca Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 }
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
161
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
162 void connect_limit_dump(struct connect_limit *limit, struct ostream *output)
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
163 {
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
164 struct hash_iterate_context *iter;
14918
8eae4e205c82 Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents: 14917
diff changeset
165 struct ident_pid *i, *value;
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
166 string_t *str = t_str_new(256);
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
167
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
168 iter = hash_table_iterate_init(limit->ident_pid_hash);
14923
96fd2c3bf932 Reverted "support for non-pointers" part of the hash table API changes.
Timo Sirainen <tss@iki.fi>
parents: 14918
diff changeset
169 while (hash_table_iterate(iter, limit->ident_pid_hash, &i, &value)) {
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
170 str_truncate(str, 0);
15068
002e0a120c2a Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents: 14923
diff changeset
171 str_append_tabescaped(str, i->ident);
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
172 str_printfa(str, "\t%ld\t%u\n", (long)i->pid, i->refcount);
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
173 if (o_stream_send(output, str_data(str), str_len(str)) < 0)
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
174 break;
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
175 }
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
176 hash_table_iterate_deinit(&iter);
14681
ca37d1577291 Added o_stream_nsend*() and related functions to make delayed error handling safer.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
177 o_stream_nsend(output, "\n", 1);
10145
1110edddec36 anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents: 9235
diff changeset
178 }