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