1 /*
2  * DNS Reply Tool (drool)
3  *
4  * Copyright (c) 2017-2018, OARC, Inc.
5  * Copyright (c) 2017, Comcast Corporation
6  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. Neither the name of the copyright holder nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __drool_client_pool_h
39 #define __drool_client_pool_h
40 
41 #include "sllq/sllq.h"
42 #include "query.h"
43 #include "conf.h"
44 #include "client.h"
45 #include "client_pool_sendas.h"
46 
47 #include <pthread.h>
48 #ifdef HAVE_LIBEV_EV_H
49 #include <libev/ev.h>
50 #else
51 #include <ev.h>
52 #endif
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netdb.h>
56 
57 typedef enum drool_client_pool_state drool_client_pool_state_t;
58 enum drool_client_pool_state {
59     CLIENT_POOL_INACTIVE = 0,
60     CLIENT_POOL_RUNNING,
61     CLIENT_POOL_STOPPED,
62     CLIENT_POOL_ERROR
63 };
64 
65 typedef struct drool_client_pool drool_client_pool_t;
66 struct drool_client_pool {
67     drool_client_pool_t* next;
68 
69     unsigned short have_queued_queries : 1;
70     unsigned short is_stopping : 1;
71 
72     const drool_conf_t*       conf;
73     drool_client_pool_state_t state;
74     pthread_t                 thread_id;
75 
76     struct ev_loop* ev_loop;
77     sllq_t          queries;
78     drool_query_t*  query;
79     ev_async        notify_query;
80     ev_async        notify_stop;
81     ev_timer        timeout;
82     ev_timer        retry;
83 
84     drool_client_t* client_list_first;
85     drool_client_t* client_list_last;
86     size_t          clients;
87     size_t          max_clients;
88     ev_tstamp       client_ttl;
89 
90     struct addrinfo* addrinfo;
91 
92     drool_client_t* reuse_client_list;
93     size_t          reuse_clients;
94     size_t          max_reuse_clients;
95 
96     drool_client_pool_sendas_t sendas;
97 };
98 
99 drool_client_pool_t* client_pool_new(const drool_conf_t* conf);
100 void client_pool_free(drool_client_pool_t* client_pool);
101 int client_pool_start(drool_client_pool_t* client_pool);
102 int client_pool_stop(drool_client_pool_t* client_pool);
103 int client_pool_query(drool_client_pool_t* client_pool, drool_query_t* query);
104 
105 #endif /* __drool_client_pool_h */
106