/src/curl/lib/cf-haproxy.c
Line | Count | Source |
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 | | #ifndef 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 | | #include "select.h" |
36 | | |
37 | | |
38 | | typedef enum { |
39 | | HAPROXY_INIT, /* init/default/no tunnel state */ |
40 | | HAPROXY_SEND, /* data_out being sent */ |
41 | | HAPROXY_DONE /* all work done */ |
42 | | } haproxy_state; |
43 | | |
44 | | struct cf_haproxy_ctx { |
45 | | int state; |
46 | | struct dynbuf data_out; |
47 | | }; |
48 | | |
49 | | static void cf_haproxy_ctx_reset(struct cf_haproxy_ctx *ctx) |
50 | 0 | { |
51 | 0 | DEBUGASSERT(ctx); |
52 | 0 | ctx->state = HAPROXY_INIT; |
53 | 0 | curlx_dyn_reset(&ctx->data_out); |
54 | 0 | } |
55 | | |
56 | | static void cf_haproxy_ctx_free(struct cf_haproxy_ctx *ctx) |
57 | 0 | { |
58 | 0 | if(ctx) { |
59 | 0 | curlx_dyn_free(&ctx->data_out); |
60 | 0 | curlx_free(ctx); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter *cf, |
65 | | struct Curl_easy *data) |
66 | 0 | { |
67 | 0 | struct cf_haproxy_ctx *ctx = cf->ctx; |
68 | 0 | CURLcode result; |
69 | 0 | const char *client_ip; |
70 | 0 | struct ip_quadruple ipquad; |
71 | 0 | bool is_ipv6; |
72 | |
|
73 | 0 | DEBUGASSERT(ctx); |
74 | 0 | DEBUGASSERT(ctx->state == HAPROXY_INIT); |
75 | 0 | #ifdef USE_UNIX_SOCKETS |
76 | 0 | if(cf->conn->unix_domain_socket) |
77 | | /* the buffer is large enough to hold this! */ |
78 | 0 | result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n")); |
79 | 0 | else { |
80 | 0 | #endif /* USE_UNIX_SOCKETS */ |
81 | 0 | result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad); |
82 | 0 | if(result) |
83 | 0 | return result; |
84 | | |
85 | | /* Emit the correct prefix for IPv6 */ |
86 | 0 | if(data->set.str[STRING_HAPROXY_CLIENT_IP]) |
87 | 0 | client_ip = data->set.str[STRING_HAPROXY_CLIENT_IP]; |
88 | 0 | else |
89 | 0 | client_ip = ipquad.local_ip; |
90 | |
|
91 | 0 | result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %i %i\r\n", |
92 | 0 | is_ipv6 ? "TCP6" : "TCP4", |
93 | 0 | client_ip, ipquad.remote_ip, |
94 | 0 | ipquad.local_port, ipquad.remote_port); |
95 | |
|
96 | 0 | #ifdef USE_UNIX_SOCKETS |
97 | 0 | } |
98 | 0 | #endif /* USE_UNIX_SOCKETS */ |
99 | 0 | return result; |
100 | 0 | } |
101 | | |
102 | | static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf, |
103 | | struct Curl_easy *data, |
104 | | bool *done) |
105 | 0 | { |
106 | 0 | struct cf_haproxy_ctx *ctx = cf->ctx; |
107 | 0 | CURLcode result; |
108 | 0 | size_t len; |
109 | |
|
110 | 0 | DEBUGASSERT(ctx); |
111 | 0 | if(cf->connected) { |
112 | 0 | *done = TRUE; |
113 | 0 | return CURLE_OK; |
114 | 0 | } |
115 | | |
116 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
117 | 0 | if(result || !*done) |
118 | 0 | return result; |
119 | | |
120 | 0 | switch(ctx->state) { |
121 | 0 | case HAPROXY_INIT: |
122 | 0 | result = cf_haproxy_date_out_set(cf, data); |
123 | 0 | if(result) |
124 | 0 | goto out; |
125 | 0 | ctx->state = HAPROXY_SEND; |
126 | 0 | FALLTHROUGH(); |
127 | 0 | case HAPROXY_SEND: |
128 | 0 | len = curlx_dyn_len(&ctx->data_out); |
129 | 0 | if(len > 0) { |
130 | 0 | size_t nwritten; |
131 | 0 | result = Curl_conn_cf_send(cf->next, data, |
132 | 0 | curlx_dyn_uptr(&ctx->data_out), len, FALSE, |
133 | 0 | &nwritten); |
134 | 0 | if(result) { |
135 | 0 | if(result != CURLE_AGAIN) |
136 | 0 | goto out; |
137 | 0 | result = CURLE_OK; |
138 | 0 | nwritten = 0; |
139 | 0 | } |
140 | 0 | curlx_dyn_tail(&ctx->data_out, len - nwritten); |
141 | 0 | if(curlx_dyn_len(&ctx->data_out) > 0) { |
142 | 0 | result = CURLE_OK; |
143 | 0 | goto out; |
144 | 0 | } |
145 | 0 | } |
146 | 0 | ctx->state = HAPROXY_DONE; |
147 | 0 | FALLTHROUGH(); |
148 | 0 | default: |
149 | 0 | curlx_dyn_free(&ctx->data_out); |
150 | 0 | break; |
151 | 0 | } |
152 | | |
153 | 0 | out: |
154 | 0 | *done = (!result) && (ctx->state == HAPROXY_DONE); |
155 | 0 | cf->connected = *done; |
156 | 0 | return result; |
157 | 0 | } |
158 | | |
159 | | static void cf_haproxy_destroy(struct Curl_cfilter *cf, |
160 | | struct Curl_easy *data) |
161 | 0 | { |
162 | 0 | (void)data; |
163 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
164 | 0 | cf_haproxy_ctx_free(cf->ctx); |
165 | 0 | } |
166 | | |
167 | | static void cf_haproxy_close(struct Curl_cfilter *cf, |
168 | | struct Curl_easy *data) |
169 | 0 | { |
170 | 0 | CURL_TRC_CF(data, cf, "close"); |
171 | 0 | cf->connected = FALSE; |
172 | 0 | cf_haproxy_ctx_reset(cf->ctx); |
173 | 0 | if(cf->next) |
174 | 0 | cf->next->cft->do_close(cf->next, data); |
175 | 0 | } |
176 | | |
177 | | static CURLcode cf_haproxy_adjust_pollset(struct Curl_cfilter *cf, |
178 | | struct Curl_easy *data, |
179 | | struct easy_pollset *ps) |
180 | 0 | { |
181 | 0 | if(cf->next->connected && !cf->connected) { |
182 | | /* If we are not connected, but the filter "below" is |
183 | | * and not waiting on something, we are sending. */ |
184 | 0 | return Curl_pollset_set_out_only( |
185 | 0 | data, ps, Curl_conn_cf_get_socket(cf, data)); |
186 | 0 | } |
187 | 0 | return CURLE_OK; |
188 | 0 | } |
189 | | |
190 | | struct Curl_cftype Curl_cft_haproxy = { |
191 | | "HAPROXY", |
192 | | CF_TYPE_PROXY, |
193 | | 0, |
194 | | cf_haproxy_destroy, |
195 | | cf_haproxy_connect, |
196 | | cf_haproxy_close, |
197 | | Curl_cf_def_shutdown, |
198 | | cf_haproxy_adjust_pollset, |
199 | | Curl_cf_def_data_pending, |
200 | | Curl_cf_def_send, |
201 | | Curl_cf_def_recv, |
202 | | Curl_cf_def_cntrl, |
203 | | Curl_cf_def_conn_is_alive, |
204 | | Curl_cf_def_conn_keep_alive, |
205 | | Curl_cf_def_query, |
206 | | }; |
207 | | |
208 | | static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf, |
209 | | struct Curl_easy *data) |
210 | 0 | { |
211 | 0 | struct Curl_cfilter *cf = NULL; |
212 | 0 | struct cf_haproxy_ctx *ctx; |
213 | 0 | CURLcode result; |
214 | |
|
215 | 0 | (void)data; |
216 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
217 | 0 | if(!ctx) { |
218 | 0 | result = CURLE_OUT_OF_MEMORY; |
219 | 0 | goto out; |
220 | 0 | } |
221 | 0 | ctx->state = HAPROXY_INIT; |
222 | 0 | curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY); |
223 | |
|
224 | 0 | result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx); |
225 | 0 | if(result) |
226 | 0 | goto out; |
227 | 0 | ctx = NULL; |
228 | |
|
229 | 0 | out: |
230 | 0 | cf_haproxy_ctx_free(ctx); |
231 | 0 | *pcf = result ? NULL : cf; |
232 | 0 | return result; |
233 | 0 | } |
234 | | |
235 | | CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at, |
236 | | struct Curl_easy *data) |
237 | 0 | { |
238 | 0 | struct Curl_cfilter *cf; |
239 | 0 | CURLcode result; |
240 | |
|
241 | 0 | result = cf_haproxy_create(&cf, data); |
242 | 0 | if(result) |
243 | 0 | goto out; |
244 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
245 | |
|
246 | 0 | out: |
247 | 0 | return result; |
248 | 0 | } |
249 | | |
250 | | #endif /* !CURL_DISABLE_PROXY */ |