tcp_conn_limit.h revision 1.1.1.1 1 1.1 christos /*
2 1.1 christos * daemon/tcp_conn_limit.h - client TCP connection limit storage for the server.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2018, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos *
39 1.1 christos * This file keeps track of the limit on the number of TCP connections
40 1.1 christos * each client makes the server.
41 1.1 christos */
42 1.1 christos
43 1.1 christos #ifndef DAEMON_TCP_CONN_LIMIT_H
44 1.1 christos #define DAEMON_TCP_CONN_LIMIT_H
45 1.1 christos #include "util/storage/dnstree.h"
46 1.1 christos #include "util/locks.h"
47 1.1 christos struct config_file;
48 1.1 christos struct regional;
49 1.1 christos
50 1.1 christos /**
51 1.1 christos * TCP connection limit storage structure
52 1.1 christos */
53 1.1 christos struct tcl_list {
54 1.1 christos /** regional for allocation */
55 1.1 christos struct regional* region;
56 1.1 christos /**
57 1.1 christos * Tree of the addresses that are TCP connection limited.
58 1.1 christos * contents of type tcl_addr.
59 1.1 christos */
60 1.1 christos rbtree_type tree;
61 1.1 christos };
62 1.1 christos
63 1.1 christos /**
64 1.1 christos *
65 1.1 christos * An address span with connection limit information
66 1.1 christos */
67 1.1 christos struct tcl_addr {
68 1.1 christos /** node in address tree */
69 1.1 christos struct addr_tree_node node;
70 1.1 christos /** lock on structure data */
71 1.1 christos lock_quick_type lock;
72 1.1 christos /** connection limit on this netblock */
73 1.1 christos uint32_t limit;
74 1.1 christos /** current connection count on this netblock */
75 1.1 christos uint32_t count;
76 1.1 christos };
77 1.1 christos
78 1.1 christos /**
79 1.1 christos * Create TCP connection limit structure
80 1.1 christos * @return new structure or NULL on error.
81 1.1 christos */
82 1.1 christos struct tcl_list* tcl_list_create(void);
83 1.1 christos
84 1.1 christos /**
85 1.1 christos * Delete TCP connection limit structure.
86 1.1 christos * @param tcl: to delete.
87 1.1 christos */
88 1.1 christos void tcl_list_delete(struct tcl_list* tcl);
89 1.1 christos
90 1.1 christos /**
91 1.1 christos * Process TCP connection limit config.
92 1.1 christos * @param tcl: where to store.
93 1.1 christos * @param cfg: config options.
94 1.1 christos * @return 0 on error.
95 1.1 christos */
96 1.1 christos int tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg);
97 1.1 christos
98 1.1 christos /**
99 1.1 christos * Increment TCP connection count if found, provided the
100 1.1 christos * count was below the limit.
101 1.1 christos * @param tcl: structure for tcl storage, or NULL.
102 1.1 christos * @return: 0 if limit reached, 1 if tcl was NULL or limit not reached.
103 1.1 christos */
104 1.1 christos int tcl_new_connection(struct tcl_addr* tcl);
105 1.1 christos
106 1.1 christos /**
107 1.1 christos * Decrement TCP connection count if found.
108 1.1 christos * @param tcl: structure for tcl storage, or NULL.
109 1.1 christos */
110 1.1 christos void tcl_close_connection(struct tcl_addr* tcl);
111 1.1 christos
112 1.1 christos /**
113 1.1 christos * Lookup address to see its TCP connection limit structure
114 1.1 christos * @param tcl: structure for address storage.
115 1.1 christos * @param addr: address to check
116 1.1 christos * @param addrlen: length of addr.
117 1.1 christos * @return: tcl structure from this address.
118 1.1 christos */
119 1.1 christos struct tcl_addr*
120 1.1 christos tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
121 1.1 christos socklen_t addrlen);
122 1.1 christos
123 1.1 christos /**
124 1.1 christos * Get memory used by TCP connection limit structure.
125 1.1 christos * @param tcl: structure for address storage.
126 1.1 christos * @return bytes in use.
127 1.1 christos */
128 1.1 christos size_t tcl_list_get_mem(struct tcl_list* tcl);
129 1.1 christos
130 1.1 christos #endif /* DAEMON_TCP_CONN_LIMIT_H */
131