quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares_close_sockets.c (4426B)


      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_private.h"
     29 #include <assert.h>
     30 
     31 static void ares_requeue_queries(ares_conn_t  *conn,
     32                                  ares_status_t requeue_status)
     33 {
     34   ares_query_t  *query;
     35   ares_timeval_t now;
     36 
     37   ares_tvnow(&now);
     38 
     39   while ((query = ares_llist_first_val(conn->queries_to_conn)) != NULL) {
     40     ares_requeue_query(query, &now, requeue_status, ARES_TRUE, NULL, NULL);
     41   }
     42 }
     43 
     44 void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status)
     45 {
     46   ares_server_t  *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->flags & ARES_CONN_FLAG_TCP) {
     55     server->tcp_conn = NULL;
     56   }
     57 
     58   ares_buf_destroy(conn->in_buf);
     59   ares_buf_destroy(conn->out_buf);
     60 
     61   /* Requeue queries to other connections */
     62   ares_requeue_queries(conn, requeue_status);
     63 
     64   ares_llist_destroy(conn->queries_to_conn);
     65 
     66   ares_conn_sock_state_cb_update(conn, ARES_CONN_STATE_NONE);
     67 
     68   ares_socket_close(channel, conn->fd);
     69 
     70   ares_free(conn);
     71 }
     72 
     73 void ares_close_sockets(ares_server_t *server)
     74 {
     75   ares_llist_node_t *node;
     76 
     77   while ((node = ares_llist_node_first(server->connections)) != NULL) {
     78     ares_conn_t *conn = ares_llist_node_val(node);
     79     ares_close_connection(conn, ARES_SUCCESS);
     80   }
     81 }
     82 
     83 void ares_check_cleanup_conns(const ares_channel_t *channel)
     84 {
     85   ares_slist_node_t *snode;
     86 
     87   if (channel == NULL) {
     88     return; /* LCOV_EXCL_LINE: DefensiveCoding */
     89   }
     90 
     91   /* Iterate across each server */
     92   for (snode = ares_slist_node_first(channel->servers); snode != NULL;
     93        snode = ares_slist_node_next(snode)) {
     94     ares_server_t     *server = ares_slist_node_val(snode);
     95     ares_llist_node_t *cnode;
     96 
     97     /* Iterate across each connection */
     98     cnode = ares_llist_node_first(server->connections);
     99     while (cnode != NULL) {
    100       ares_llist_node_t *next       = ares_llist_node_next(cnode);
    101       ares_conn_t       *conn       = ares_llist_node_val(cnode);
    102       ares_bool_t        do_cleanup = ARES_FALSE;
    103       cnode                         = next;
    104 
    105       /* Has connections, not eligible */
    106       if (ares_llist_len(conn->queries_to_conn)) {
    107         continue;
    108       }
    109 
    110       /* If we are configured not to stay open, close it out */
    111       if (!(channel->flags & ARES_FLAG_STAYOPEN)) {
    112         do_cleanup = ARES_TRUE;
    113       }
    114 
    115       /* If the associated server has failures, close it out. Resetting the
    116        * connection (and specifically the source port number) can help resolve
    117        * situations where packets are being dropped.
    118        */
    119       if (conn->server->consec_failures > 0) {
    120         do_cleanup = ARES_TRUE;
    121       }
    122 
    123       /* If the udp connection hit its max queries, always close it */
    124       if (!(conn->flags & ARES_CONN_FLAG_TCP) && channel->udp_max_queries > 0 &&
    125           conn->total_queries >= channel->udp_max_queries) {
    126         do_cleanup = ARES_TRUE;
    127       }
    128 
    129       if (!do_cleanup) {
    130         continue;
    131       }
    132 
    133       /* Clean it up */
    134       ares_close_connection(conn, ARES_SUCCESS);
    135     }
    136   }
    137 }