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