xref: /freebsd/contrib/unbound/util/tcp_conn_limit.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * daemon/tcp_conn_limit.h - client TCP connection limit storage for the server.
3  *
4  * Copyright (c) 2018, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file keeps track of the limit on the number of TCP connections
40  * each client makes the server.
41  */
42 
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"
47 struct config_file;
48 struct regional;
49 
50 /**
51  * TCP connection limit storage structure
52  */
53 struct tcl_list {
54 	/** regional for allocation */
55 	struct regional* region;
56 	/**
57 	 * Tree of the addresses that are TCP connection limited.
58 	 * contents of type tcl_addr.
59 	 */
60 	rbtree_type tree;
61 };
62 
63 /**
64  *
65  * An address span with connection limit information
66  */
67 struct tcl_addr {
68 	/** node in address tree */
69 	struct addr_tree_node node;
70 	/** lock on structure data */
71 	lock_quick_type lock;
72 	/** connection limit on this netblock */
73 	uint32_t limit;
74 	/** current connection count on this netblock */
75 	uint32_t count;
76 };
77 
78 /**
79  * Create TCP connection limit structure
80  * @return new structure or NULL on error.
81  */
82 struct tcl_list* tcl_list_create(void);
83 
84 /**
85  * Delete TCP connection limit structure.
86  * @param tcl: to delete.
87  */
88 void tcl_list_delete(struct tcl_list* tcl);
89 
90 /**
91  * Process TCP connection limit config.
92  * @param tcl: where to store.
93  * @param cfg: config options.
94  * @return 0 on error.
95  */
96 int tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg);
97 
98 /**
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.
103  */
104 int tcl_new_connection(struct tcl_addr* tcl);
105 
106 /**
107  * Decrement TCP connection count if found.
108  * @param tcl: structure for tcl storage, or NULL.
109  */
110 void tcl_close_connection(struct tcl_addr* tcl);
111 
112 /**
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.
118  */
119 struct tcl_addr*
120 tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
121         socklen_t addrlen);
122 
123 /**
124  * Get memory used by TCP connection limit structure.
125  * @param tcl: structure for address storage.
126  * @return bytes in use.
127  */
128 size_t tcl_list_get_mem(struct tcl_list* tcl);
129 
130 #endif /* DAEMON_TCP_CONN_LIMIT_H */
131