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 #include "config.h"
39 
40 #include "conf_client_pool.h"
41 #include "conf.h"
42 #include "assert.h"
43 
44 #include <stdlib.h>
45 #include <string.h>
46 
conf_client_pool_new(void)47 drool_conf_client_pool_t* conf_client_pool_new(void)
48 {
49     drool_conf_client_pool_t* conf_client_pool = calloc(1, sizeof(drool_conf_client_pool_t));
50     return conf_client_pool;
51 }
52 
conf_client_pool_free(drool_conf_client_pool_t * conf_client_pool)53 void conf_client_pool_free(drool_conf_client_pool_t* conf_client_pool)
54 {
55     if (conf_client_pool) {
56         if (conf_client_pool->target_host)
57             free(conf_client_pool->target_host);
58         if (conf_client_pool->target_service)
59             free(conf_client_pool->target_service);
60         free(conf_client_pool);
61     }
62 }
63 
conf_client_pool_have_target(const drool_conf_client_pool_t * conf_client_pool)64 inline int conf_client_pool_have_target(const drool_conf_client_pool_t* conf_client_pool)
65 {
66     drool_assert(conf_client_pool);
67     return conf_client_pool->have_target;
68 }
69 
conf_client_pool_have_max_clients(const drool_conf_client_pool_t * conf_client_pool)70 inline int conf_client_pool_have_max_clients(const drool_conf_client_pool_t* conf_client_pool)
71 {
72     drool_assert(conf_client_pool);
73     return conf_client_pool->have_max_clients;
74 }
75 
conf_client_pool_have_client_ttl(const drool_conf_client_pool_t * conf_client_pool)76 inline int conf_client_pool_have_client_ttl(const drool_conf_client_pool_t* conf_client_pool)
77 {
78     drool_assert(conf_client_pool);
79     return conf_client_pool->have_client_ttl;
80 }
81 
conf_client_pool_have_max_reuse_clients(const drool_conf_client_pool_t * conf_client_pool)82 inline int conf_client_pool_have_max_reuse_clients(const drool_conf_client_pool_t* conf_client_pool)
83 {
84     drool_assert(conf_client_pool);
85     return conf_client_pool->have_max_reuse_clients;
86 }
87 
conf_client_pool_have_sendas(const drool_conf_client_pool_t * conf_client_pool)88 inline int conf_client_pool_have_sendas(const drool_conf_client_pool_t* conf_client_pool)
89 {
90     drool_assert(conf_client_pool);
91     return conf_client_pool->have_sendas;
92 }
93 
conf_client_pool_next(const drool_conf_client_pool_t * conf_client_pool)94 inline const drool_conf_client_pool_t* conf_client_pool_next(const drool_conf_client_pool_t* conf_client_pool)
95 {
96     drool_assert(conf_client_pool);
97     return conf_client_pool->next;
98 }
99 
conf_client_pool_target_host(const drool_conf_client_pool_t * conf_client_pool)100 inline const char* conf_client_pool_target_host(const drool_conf_client_pool_t* conf_client_pool)
101 {
102     drool_assert(conf_client_pool);
103     return conf_client_pool->target_host;
104 }
105 
conf_client_pool_target_service(const drool_conf_client_pool_t * conf_client_pool)106 inline const char* conf_client_pool_target_service(const drool_conf_client_pool_t* conf_client_pool)
107 {
108     drool_assert(conf_client_pool);
109     return conf_client_pool->target_service;
110 }
111 
conf_client_pool_max_clients(const drool_conf_client_pool_t * conf_client_pool)112 inline size_t conf_client_pool_max_clients(const drool_conf_client_pool_t* conf_client_pool)
113 {
114     drool_assert(conf_client_pool);
115     return conf_client_pool->max_clients;
116 }
117 
conf_client_pool_client_ttl(const drool_conf_client_pool_t * conf_client_pool)118 inline double conf_client_pool_client_ttl(const drool_conf_client_pool_t* conf_client_pool)
119 {
120     drool_assert(conf_client_pool);
121     return conf_client_pool->client_ttl;
122 }
123 
conf_client_pool_skip_reply(const drool_conf_client_pool_t * conf_client_pool)124 inline int conf_client_pool_skip_reply(const drool_conf_client_pool_t* conf_client_pool)
125 {
126     drool_assert(conf_client_pool);
127     return conf_client_pool->skip_reply;
128 }
129 
conf_client_pool_max_reuse_clients(const drool_conf_client_pool_t * conf_client_pool)130 inline size_t conf_client_pool_max_reuse_clients(const drool_conf_client_pool_t* conf_client_pool)
131 {
132     drool_assert(conf_client_pool);
133     return conf_client_pool->max_reuse_clients;
134 }
135 
conf_client_pool_sendas(const drool_conf_client_pool_t * conf_client_pool)136 inline drool_client_pool_sendas_t conf_client_pool_sendas(const drool_conf_client_pool_t* conf_client_pool)
137 {
138     drool_assert(conf_client_pool);
139     return conf_client_pool->sendas;
140 }
141 
conf_client_pool_set_next(drool_conf_client_pool_t * conf_client_pool,drool_conf_client_pool_t * next)142 int conf_client_pool_set_next(drool_conf_client_pool_t* conf_client_pool, drool_conf_client_pool_t* next)
143 {
144     if (!conf_client_pool) {
145         return CONF_EINVAL;
146     }
147     if (!next) {
148         return CONF_EINVAL;
149     }
150 
151     conf_client_pool->next = next;
152 
153     return CONF_OK;
154 }
155 
conf_client_pool_set_target(drool_conf_client_pool_t * conf_client_pool,const char * host,size_t host_length,const char * service,size_t service_length)156 int conf_client_pool_set_target(drool_conf_client_pool_t* conf_client_pool, const char* host, size_t host_length, const char* service, size_t service_length)
157 {
158     if (!conf_client_pool) {
159         return CONF_EINVAL;
160     }
161     if (!host) {
162         return CONF_EINVAL;
163     }
164     if (!host_length) {
165         return CONF_EINVAL;
166     }
167     if (!service) {
168         return CONF_EINVAL;
169     }
170     if (!service_length) {
171         return CONF_EINVAL;
172     }
173 
174     if (conf_client_pool->target_host)
175         free(conf_client_pool->target_host);
176     if (conf_client_pool->target_service)
177         free(conf_client_pool->target_service);
178     conf_client_pool->have_target = 0;
179     if (!(conf_client_pool->target_host = strndup(host, host_length))) {
180         return CONF_ENOMEM;
181     }
182     if (!(conf_client_pool->target_service = strndup(service, service_length))) {
183         return CONF_ENOMEM;
184     }
185     conf_client_pool->have_target = 1;
186 
187     return CONF_OK;
188 }
189 
conf_client_pool_set_max_clients(drool_conf_client_pool_t * conf_client_pool,size_t max_clients)190 int conf_client_pool_set_max_clients(drool_conf_client_pool_t* conf_client_pool, size_t max_clients)
191 {
192     if (!conf_client_pool) {
193         return CONF_EINVAL;
194     }
195 
196     conf_client_pool->max_clients      = max_clients;
197     conf_client_pool->have_max_clients = 1;
198 
199     return CONF_OK;
200 }
201 
conf_client_pool_set_client_ttl(drool_conf_client_pool_t * conf_client_pool,double client_ttl)202 int conf_client_pool_set_client_ttl(drool_conf_client_pool_t* conf_client_pool, double client_ttl)
203 {
204     if (!conf_client_pool) {
205         return CONF_EINVAL;
206     }
207 
208     conf_client_pool->client_ttl      = client_ttl;
209     conf_client_pool->have_client_ttl = 1;
210 
211     return CONF_OK;
212 }
213 
conf_client_pool_set_skip_reply(drool_conf_client_pool_t * conf_client_pool)214 int conf_client_pool_set_skip_reply(drool_conf_client_pool_t* conf_client_pool)
215 {
216     if (!conf_client_pool) {
217         return CONF_EINVAL;
218     }
219 
220     conf_client_pool->skip_reply = 1;
221 
222     return CONF_OK;
223 }
224 
conf_client_pool_set_max_reuse_clients(drool_conf_client_pool_t * conf_client_pool,size_t max_reuse_clients)225 int conf_client_pool_set_max_reuse_clients(drool_conf_client_pool_t* conf_client_pool, size_t max_reuse_clients)
226 {
227     if (!conf_client_pool) {
228         return CONF_EINVAL;
229     }
230 
231     conf_client_pool->max_reuse_clients      = max_reuse_clients;
232     conf_client_pool->have_max_reuse_clients = 1;
233 
234     return CONF_OK;
235 }
236 
conf_client_pool_set_sendas(drool_conf_client_pool_t * conf_client_pool,drool_client_pool_sendas_t sendas)237 int conf_client_pool_set_sendas(drool_conf_client_pool_t* conf_client_pool, drool_client_pool_sendas_t sendas)
238 {
239     if (!conf_client_pool) {
240         return CONF_EINVAL;
241     }
242 
243     conf_client_pool->sendas      = sendas;
244     conf_client_pool->have_sendas = 1;
245 
246     return CONF_OK;
247 }
248