1 /* MIT License
2  *
3  * Copyright (c) 1998 Massachusetts Institute of Technology
4  * Copyright (c) The c-ares project and its contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * SPDX-License-Identifier: MIT
26  */
27 
28 #include "ares_setup.h"
29 
30 #include "ares.h"
31 #include "ares_private.h"
32 #include <assert.h>
33 
ares__requeue_queries(struct server_connection *conn)34 static void ares__requeue_queries(struct server_connection *conn)
35 {
36   struct query  *query;
37   struct timeval now = ares__tvnow();
38 
39   while ((query = ares__llist_first_val(conn->queries_to_conn)) != NULL) {
40     ares__requeue_query(query, &now);
41   }
42 }
43 
ares__close_connection(struct server_connection *conn)44 void ares__close_connection(struct server_connection *conn)
45 {
46   struct server_state *server  = conn->server;
47   ares_channel_t      *channel = server->channel;
48 
49   /* Unlink */
50   ares__llist_node_claim(
51     ares__htable_asvp_get_direct(channel->connnode_by_socket, conn->fd));
52   ares__htable_asvp_remove(channel->connnode_by_socket, conn->fd);
53 
54   if (conn->is_tcp) {
55     /* Reset any existing input and output buffer. */
56     ares__buf_consume(server->tcp_parser, ares__buf_len(server->tcp_parser));
57     ares__buf_consume(server->tcp_send, ares__buf_len(server->tcp_send));
58     server->tcp_conn = NULL;
59   }
60 
61   /* Requeue queries to other connections */
62   ares__requeue_queries(conn);
63 
64   ares__llist_destroy(conn->queries_to_conn);
65 
66   SOCK_STATE_CALLBACK(channel, conn->fd, 0, 0);
67   ares__close_socket(channel, conn->fd);
68 
69   ares_free(conn);
70 }
71 
ares__close_sockets(struct server_state *server)72 void ares__close_sockets(struct server_state *server)
73 {
74   ares__llist_node_t *node;
75 
76   while ((node = ares__llist_node_first(server->connections)) != NULL) {
77     struct server_connection *conn = ares__llist_node_val(node);
78     ares__close_connection(conn);
79   }
80 }
81 
ares__check_cleanup_conn(const ares_channel_t *channel, struct server_connection *conn)82 void ares__check_cleanup_conn(const ares_channel_t     *channel,
83                               struct server_connection *conn)
84 {
85   ares_bool_t do_cleanup = ARES_FALSE;
86 
87   if (channel == NULL || conn == NULL) {
88     return;
89   }
90 
91   if (ares__llist_len(conn->queries_to_conn)) {
92     return;
93   }
94 
95   /* If we are configured not to stay open, close it out */
96   if (!(channel->flags & ARES_FLAG_STAYOPEN)) {
97     do_cleanup = ARES_TRUE;
98   }
99 
100   /* If the udp connection hit its max queries, always close it */
101   if (!conn->is_tcp && channel->udp_max_queries > 0 &&
102       conn->total_queries >= channel->udp_max_queries) {
103     do_cleanup = ARES_TRUE;
104   }
105 
106   if (!do_cleanup) {
107     return;
108   }
109 
110   ares__close_connection(conn);
111 }
112