cf-haproxy.c 6.5 KB

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