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