/src/PROJ/curl/lib/cf-haproxy.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #if !defined(CURL_DISABLE_PROXY) |
28 | | |
29 | | #include <curl/curl.h> |
30 | | #include "urldata.h" |
31 | | #include "cfilters.h" |
32 | | #include "cf-haproxy.h" |
33 | | #include "curl_trc.h" |
34 | | #include "multiif.h" |
35 | | |
36 | | /* The last 3 #include files should be in this order */ |
37 | | #include "curl_printf.h" |
38 | | #include "curl_memory.h" |
39 | | #include "memdebug.h" |
40 | | |
41 | | |
42 | | typedef enum { |
43 | | HAPROXY_INIT, /* init/default/no tunnel state */ |
44 | | HAPROXY_SEND, /* data_out being sent */ |
45 | | HAPROXY_DONE /* all work done */ |
46 | | } haproxy_state; |
47 | | |
48 | | struct cf_haproxy_ctx { |
49 | | int state; |
50 | | struct dynbuf data_out; |
51 | | }; |
52 | | |
53 | | static void cf_haproxy_ctx_reset(struct cf_haproxy_ctx *ctx) |
54 | 0 | { |
55 | 0 | DEBUGASSERT(ctx); |
56 | 0 | ctx->state = HAPROXY_INIT; |
57 | 0 | curlx_dyn_reset(&ctx->data_out); |
58 | 0 | } |
59 | | |
60 | | static void cf_haproxy_ctx_free(struct cf_haproxy_ctx *ctx) |
61 | 0 | { |
62 | 0 | if(ctx) { |
63 | 0 | curlx_dyn_free(&ctx->data_out); |
64 | 0 | free(ctx); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter*cf, |
69 | | struct Curl_easy *data) |
70 | 0 | { |
71 | 0 | struct cf_haproxy_ctx *ctx = cf->ctx; |
72 | 0 | CURLcode result; |
73 | 0 | const char *client_ip; |
74 | 0 | struct ip_quadruple ipquad; |
75 | 0 | int is_ipv6; |
76 | |
|
77 | 0 | DEBUGASSERT(ctx); |
78 | 0 | DEBUGASSERT(ctx->state == HAPROXY_INIT); |
79 | 0 | #ifdef USE_UNIX_SOCKETS |
80 | 0 | if(cf->conn->unix_domain_socket) |
81 | | /* the buffer is large enough to hold this! */ |
82 | 0 | result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n")); |
83 | 0 | else { |
84 | 0 | #endif /* USE_UNIX_SOCKETS */ |
85 | 0 | result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad); |
86 | 0 | if(result) |
87 | 0 | return result; |
88 | | |
89 | | /* Emit the correct prefix for IPv6 */ |
90 | 0 | if(data->set.str[STRING_HAPROXY_CLIENT_IP]) |
91 | 0 | client_ip = data->set.str[STRING_HAPROXY_CLIENT_IP]; |
92 | 0 | else |
93 | 0 | client_ip = ipquad.local_ip; |
94 | |
|
95 | 0 | result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %i %i\r\n", |
96 | 0 | is_ipv6 ? "TCP6" : "TCP4", |
97 | 0 | client_ip, ipquad.remote_ip, |
98 | 0 | ipquad.local_port, ipquad.remote_port); |
99 | |
|
100 | 0 | #ifdef USE_UNIX_SOCKETS |
101 | 0 | } |
102 | 0 | #endif /* USE_UNIX_SOCKETS */ |
103 | 0 | return result; |
104 | 0 | } |
105 | | |
106 | | static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf, |
107 | | struct Curl_easy *data, |
108 | | bool *done) |
109 | 0 | { |
110 | 0 | struct cf_haproxy_ctx *ctx = cf->ctx; |
111 | 0 | CURLcode result; |
112 | 0 | size_t len; |
113 | |
|
114 | 0 | DEBUGASSERT(ctx); |
115 | 0 | if(cf->connected) { |
116 | 0 | *done = TRUE; |
117 | 0 | return CURLE_OK; |
118 | 0 | } |
119 | | |
120 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
121 | 0 | if(result || !*done) |
122 | 0 | return result; |
123 | | |
124 | 0 | switch(ctx->state) { |
125 | 0 | case HAPROXY_INIT: |
126 | 0 | result = cf_haproxy_date_out_set(cf, data); |
127 | 0 | if(result) |
128 | 0 | goto out; |
129 | 0 | ctx->state = HAPROXY_SEND; |
130 | 0 | FALLTHROUGH(); |
131 | 0 | case HAPROXY_SEND: |
132 | 0 | len = curlx_dyn_len(&ctx->data_out); |
133 | 0 | if(len > 0) { |
134 | 0 | size_t nwritten; |
135 | 0 | result = Curl_conn_cf_send(cf->next, data, |
136 | 0 | curlx_dyn_ptr(&ctx->data_out), len, FALSE, |
137 | 0 | &nwritten); |
138 | 0 | if(result) { |
139 | 0 | if(result != CURLE_AGAIN) |
140 | 0 | goto out; |
141 | 0 | result = CURLE_OK; |
142 | 0 | nwritten = 0; |
143 | 0 | } |
144 | 0 | curlx_dyn_tail(&ctx->data_out, len - nwritten); |
145 | 0 | if(curlx_dyn_len(&ctx->data_out) > 0) { |
146 | 0 | result = CURLE_OK; |
147 | 0 | goto out; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | ctx->state = HAPROXY_DONE; |
151 | 0 | FALLTHROUGH(); |
152 | 0 | default: |
153 | 0 | curlx_dyn_free(&ctx->data_out); |
154 | 0 | break; |
155 | 0 | } |
156 | | |
157 | 0 | out: |
158 | 0 | *done = (!result) && (ctx->state == HAPROXY_DONE); |
159 | 0 | cf->connected = *done; |
160 | 0 | return result; |
161 | 0 | } |
162 | | |
163 | | static void cf_haproxy_destroy(struct Curl_cfilter *cf, |
164 | | struct Curl_easy *data) |
165 | 0 | { |
166 | 0 | (void)data; |
167 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
168 | 0 | cf_haproxy_ctx_free(cf->ctx); |
169 | 0 | } |
170 | | |
171 | | static void cf_haproxy_close(struct Curl_cfilter *cf, |
172 | | struct Curl_easy *data) |
173 | 0 | { |
174 | 0 | CURL_TRC_CF(data, cf, "close"); |
175 | 0 | cf->connected = FALSE; |
176 | 0 | cf_haproxy_ctx_reset(cf->ctx); |
177 | 0 | if(cf->next) |
178 | 0 | cf->next->cft->do_close(cf->next, data); |
179 | 0 | } |
180 | | |
181 | | static void cf_haproxy_adjust_pollset(struct Curl_cfilter *cf, |
182 | | struct Curl_easy *data, |
183 | | struct easy_pollset *ps) |
184 | 0 | { |
185 | 0 | if(cf->next->connected && !cf->connected) { |
186 | | /* If we are not connected, but the filter "below" is |
187 | | * and not waiting on something, we are sending. */ |
188 | 0 | Curl_pollset_set_out_only(data, ps, Curl_conn_cf_get_socket(cf, data)); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | struct Curl_cftype Curl_cft_haproxy = { |
193 | | "HAPROXY", |
194 | | CF_TYPE_PROXY, |
195 | | 0, |
196 | | cf_haproxy_destroy, |
197 | | cf_haproxy_connect, |
198 | | cf_haproxy_close, |
199 | | Curl_cf_def_shutdown, |
200 | | cf_haproxy_adjust_pollset, |
201 | | Curl_cf_def_data_pending, |
202 | | Curl_cf_def_send, |
203 | | Curl_cf_def_recv, |
204 | | Curl_cf_def_cntrl, |
205 | | Curl_cf_def_conn_is_alive, |
206 | | Curl_cf_def_conn_keep_alive, |
207 | | Curl_cf_def_query, |
208 | | }; |
209 | | |
210 | | static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf, |
211 | | struct Curl_easy *data) |
212 | 0 | { |
213 | 0 | struct Curl_cfilter *cf = NULL; |
214 | 0 | struct cf_haproxy_ctx *ctx; |
215 | 0 | CURLcode result; |
216 | |
|
217 | 0 | (void)data; |
218 | 0 | ctx = calloc(1, sizeof(*ctx)); |
219 | 0 | if(!ctx) { |
220 | 0 | result = CURLE_OUT_OF_MEMORY; |
221 | 0 | goto out; |
222 | 0 | } |
223 | 0 | ctx->state = HAPROXY_INIT; |
224 | 0 | curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY); |
225 | |
|
226 | 0 | result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx); |
227 | 0 | if(result) |
228 | 0 | goto out; |
229 | 0 | ctx = NULL; |
230 | |
|
231 | 0 | out: |
232 | 0 | cf_haproxy_ctx_free(ctx); |
233 | 0 | *pcf = result ? NULL : cf; |
234 | 0 | return result; |
235 | 0 | } |
236 | | |
237 | | CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at, |
238 | | struct Curl_easy *data) |
239 | 0 | { |
240 | 0 | struct Curl_cfilter *cf; |
241 | 0 | CURLcode result; |
242 | |
|
243 | 0 | result = cf_haproxy_create(&cf, data); |
244 | 0 | if(result) |
245 | 0 | goto out; |
246 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
247 | |
|
248 | 0 | out: |
249 | 0 | return result; |
250 | 0 | } |
251 | | |
252 | | #endif /* !CURL_DISABLE_PROXY */ |