Coverage Report

Created: 2025-11-24 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unbound/util/tcp_conn_limit.c
Line
Count
Source
1
/*
2
 * daemon/tcp_conn_limit.c - 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 helps the server discard excess TCP connections.
40
 */
41
#include "config.h"
42
#include "util/regional.h"
43
#include "util/log.h"
44
#include "util/config_file.h"
45
#include "util/net_help.h"
46
#include "util/tcp_conn_limit.h"
47
#include "services/localzone.h"
48
#include "sldns/str2wire.h"
49
50
struct tcl_list*
51
tcl_list_create(void)
52
0
{
53
0
  struct tcl_list* tcl = (struct tcl_list*)calloc(1,
54
0
    sizeof(struct tcl_list));
55
0
  if(!tcl)
56
0
    return NULL;
57
0
  tcl->region = regional_create();
58
0
  if(!tcl->region) {
59
0
    tcl_list_delete(tcl);
60
0
    return NULL;
61
0
  }
62
0
  return tcl;
63
0
}
64
65
static void
66
tcl_list_free_node(rbnode_type* node, void* ATTR_UNUSED(arg))
67
0
{
68
0
  struct tcl_addr* n = (struct tcl_addr*) node;
69
0
  lock_quick_destroy(&n->lock);
70
#ifdef THREADS_DISABLED
71
  (void)n;
72
#endif
73
0
}
74
75
void
76
tcl_list_delete(struct tcl_list* tcl)
77
0
{
78
0
  if(!tcl)
79
0
    return;
80
0
  traverse_postorder(&tcl->tree, tcl_list_free_node, NULL);
81
0
  regional_destroy(tcl->region);
82
0
  free(tcl);
83
0
}
84
85
/** insert new address into tcl_list structure */
86
static struct tcl_addr*
87
tcl_list_insert(struct tcl_list* tcl, struct sockaddr_storage* addr,
88
  socklen_t addrlen, int net, uint32_t limit,
89
  int complain_duplicates)
90
0
{
91
0
  struct tcl_addr* node = regional_alloc_zero(tcl->region,
92
0
    sizeof(struct tcl_addr));
93
0
  if(!node)
94
0
    return NULL;
95
0
  lock_quick_init(&node->lock);
96
0
  node->limit = limit;
97
0
  if(!addr_tree_insert(&tcl->tree, &node->node, addr, addrlen, net)) {
98
0
    if(complain_duplicates)
99
0
      verbose(VERB_QUERY, "duplicate tcl address ignored.");
100
0
  }
101
0
  return node;
102
0
}
103
104
/** apply tcl_list string */
105
static int
106
tcl_list_str_cfg(struct tcl_list* tcl, const char* str, const char* s2,
107
  int complain_duplicates)
108
0
{
109
0
  struct sockaddr_storage addr;
110
0
  int net;
111
0
  socklen_t addrlen;
112
0
  uint32_t limit;
113
0
  if(atoi(s2) < 0) {
114
0
    log_err("bad connection limit %s", s2);
115
0
    return 0;
116
0
  }
117
0
  limit = (uint32_t)atoi(s2);
118
0
  if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
119
0
    log_err("cannot parse connection limit netblock: %s", str);
120
0
    return 0;
121
0
  }
122
0
  if(!tcl_list_insert(tcl, &addr, addrlen, net, limit,
123
0
    complain_duplicates)) {
124
0
    log_err("out of memory");
125
0
    return 0;
126
0
  }
127
0
  return 1;
128
0
}
129
130
/** read tcl_list config */
131
static int
132
read_tcl_list(struct tcl_list* tcl, struct config_file* cfg)
133
0
{
134
0
  struct config_str2list* p;
135
0
  for(p = cfg->tcp_connection_limits; p; p = p->next) {
136
0
    log_assert(p->str && p->str2);
137
0
    if(!tcl_list_str_cfg(tcl, p->str, p->str2, 1))
138
0
      return 0;
139
0
  }
140
0
  return 1;
141
0
}
142
143
int
144
tcl_list_apply_cfg(struct tcl_list* tcl, struct config_file* cfg)
145
0
{
146
0
  regional_free_all(tcl->region);
147
0
  addr_tree_init(&tcl->tree);
148
0
  if(!read_tcl_list(tcl, cfg))
149
0
    return 0;
150
0
  addr_tree_init_parents(&tcl->tree);
151
0
  return 1;
152
0
}
153
154
int
155
tcl_new_connection(struct tcl_addr* tcl)
156
0
{
157
0
  if(tcl) {
158
0
    int res = 1;
159
0
    lock_quick_lock(&tcl->lock);
160
0
    if(tcl->count >= tcl->limit)
161
0
      res = 0;
162
0
    else
163
0
      tcl->count++;
164
0
    lock_quick_unlock(&tcl->lock);
165
0
    return res;
166
0
  }
167
0
  return 1;
168
0
}
169
170
void
171
tcl_close_connection(struct tcl_addr* tcl)
172
0
{
173
0
  if(tcl) {
174
0
    lock_quick_lock(&tcl->lock);
175
0
    log_assert(tcl->count > 0);
176
0
    tcl->count--;
177
0
    lock_quick_unlock(&tcl->lock);
178
0
  }
179
0
}
180
181
struct tcl_addr*
182
tcl_addr_lookup(struct tcl_list* tcl, struct sockaddr_storage* addr,
183
        socklen_t addrlen)
184
0
{
185
0
  return (struct tcl_addr*)addr_tree_lookup(&tcl->tree,
186
0
    addr, addrlen);
187
0
}
188
189
size_t
190
tcl_list_get_mem(struct tcl_list* tcl)
191
0
{
192
0
  if(!tcl) return 0;
193
0
  return sizeof(*tcl) + regional_get_mem(tcl->region);
194
0
}
195
196
void tcl_list_swap_tree(struct tcl_list* tcl, struct tcl_list* data)
197
0
{
198
  /* swap tree and region */
199
0
  rbtree_type oldtree = tcl->tree;
200
0
  struct regional* oldregion = tcl->region;
201
0
  tcl->tree = data->tree;
202
0
  tcl->region = data->region;
203
0
  data->tree = oldtree;
204
0
  data->region = oldregion;
205
0
}