Mercurial > dovecot > core-2.2
annotate src/anvil/connect-limit.c @ 14918:8eae4e205c82
Hash table API is now (mostly) type safe.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 19 Aug 2012 13:55:34 +0300 |
parents | 1ce71b5bc94a |
children | 96fd2c3bf932 |
rev | line source |
---|---|
14133
ba770cba5598
Updated copyright notices to include year 2012.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
1 /* Copyright (c) 2009-2012 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 { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 /* ident => refcount */ |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
19 HASH_TABLE(char *, unsigned int) 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 { |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
63 return hash_table_lookup(limit->ident_hash, ident); |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 } |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 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
|
67 const char *ident) |
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 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
|
70 void *orig_key, *orig_value; |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
71 char *key; |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
72 unsigned int value; |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
74 if (!hash_table_lookup_full(limit->ident_hash, ident, |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
75 &orig_key, &orig_value)) { |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 key = i_strdup(ident); |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
77 hash_table_insert(limit->ident_hash, key, 1U); |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 } else { |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
79 key = orig_key; |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
80 value = POINTER_CAST_TO(orig_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
|
81 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
|
82 } |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 if (i == NULL) { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 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
|
89 i->ident = key; |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 i->pid = pid; |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 i->refcount = 1; |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 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
|
93 } else { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 i->refcount++; |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
95 } |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 } |
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 static void |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
99 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
|
100 { |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
101 void *orig_key, *orig_value; |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
102 char *key; |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 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
|
104 |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
105 if (!hash_table_lookup_full(limit->ident_hash, ident, |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
106 &orig_key, &orig_value)) |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
107 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
|
108 |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
109 key = orig_key; |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
110 new_refcount = POINTER_CAST_TO(orig_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) { |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
112 hash_table_update(limit->ident_hash, key, new_refcount); |
9235
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 } else { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
114 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
|
115 i_free(key); |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
116 } |
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 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
|
120 const char *ident) |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 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
|
123 |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
124 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
|
125 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
|
126 |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
127 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
|
128 if (i == NULL) { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
129 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
|
130 "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
|
131 return; |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
132 } |
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 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
|
135 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
|
136 i_free(i); |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
137 } |
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 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
|
140 } |
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 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
|
143 { |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
144 struct hash_iterate_context *iter; |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
145 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
|
146 |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
147 /* 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
|
148 should be fine. */ |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 iter = hash_table_iterate_init(limit->ident_pid_hash); |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
150 while (hash_table_iterate_t(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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 i_free(i); |
2e2b957f1cca
Implemented anvil service, which is used to implement mail_max_userip_connections.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
156 } |
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 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
|
159 } |
10145
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
160 |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
161 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
|
162 { |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
163 struct hash_iterate_context *iter; |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
164 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
|
165 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
|
166 |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
167 iter = hash_table_iterate_init(limit->ident_pid_hash); |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14917
diff
changeset
|
168 while (hash_table_iterate_t(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
|
169 str_truncate(str, 0); |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
170 str_tabescape_write(str, i->ident); |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
171 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
|
172 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
|
173 break; |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
174 } |
1110edddec36
anvil: Added CONNECT-DUMP command to dump connect-limit state.
Timo Sirainen <tss@iki.fi>
parents:
9235
diff
changeset
|
175 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
|
176 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
|
177 } |