cf-haproxy.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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_log.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 *tcp_version;
  64. DEBUGASSERT(ctx);
  65. DEBUGASSERT(ctx->state == HAPROXY_INIT);
  66. #ifdef USE_UNIX_SOCKETS
  67. if(cf->conn->unix_domain_socket)
  68. /* the buffer is large enough to hold this! */
  69. result = Curl_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n"));
  70. else {
  71. #endif /* USE_UNIX_SOCKETS */
  72. /* Emit the correct prefix for IPv6 */
  73. tcp_version = cf->conn->bits.ipv6 ? "TCP6" : "TCP4";
  74. result = Curl_dyn_addf(&ctx->data_out, "PROXY %s %s %s %i %i\r\n",
  75. tcp_version,
  76. data->info.conn_local_ip,
  77. data->info.conn_primary_ip,
  78. data->info.conn_local_port,
  79. data->info.conn_primary_port);
  80. #ifdef USE_UNIX_SOCKETS
  81. }
  82. #endif /* USE_UNIX_SOCKETS */
  83. return result;
  84. }
  85. static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
  86. struct Curl_easy *data,
  87. bool blocking, bool *done)
  88. {
  89. struct cf_haproxy_ctx *ctx = cf->ctx;
  90. CURLcode result;
  91. size_t len;
  92. DEBUGASSERT(ctx);
  93. if(cf->connected) {
  94. *done = TRUE;
  95. return CURLE_OK;
  96. }
  97. result = cf->next->cft->connect(cf->next, data, blocking, done);
  98. if(result || !*done)
  99. return result;
  100. switch(ctx->state) {
  101. case HAPROXY_INIT:
  102. result = cf_haproxy_date_out_set(cf, data);
  103. if(result)
  104. goto out;
  105. ctx->state = HAPROXY_SEND;
  106. /* FALLTHROUGH */
  107. case HAPROXY_SEND:
  108. len = Curl_dyn_len(&ctx->data_out);
  109. if(len > 0) {
  110. ssize_t written = Curl_conn_send(data, cf->sockindex,
  111. Curl_dyn_ptr(&ctx->data_out),
  112. len, &result);
  113. if(written < 0)
  114. goto out;
  115. Curl_dyn_tail(&ctx->data_out, len - (size_t)written);
  116. if(Curl_dyn_len(&ctx->data_out) > 0) {
  117. result = CURLE_OK;
  118. goto out;
  119. }
  120. }
  121. ctx->state = HAPROXY_DONE;
  122. /* FALLTHROUGH */
  123. default:
  124. Curl_dyn_free(&ctx->data_out);
  125. break;
  126. }
  127. out:
  128. *done = (!result) && (ctx->state == HAPROXY_DONE);
  129. cf->connected = *done;
  130. return result;
  131. }
  132. static void cf_haproxy_destroy(struct Curl_cfilter *cf,
  133. struct Curl_easy *data)
  134. {
  135. (void)data;
  136. DEBUGF(LOG_CF(data, cf, "destroy"));
  137. cf_haproxy_ctx_free(cf->ctx);
  138. }
  139. static void cf_haproxy_close(struct Curl_cfilter *cf,
  140. struct Curl_easy *data)
  141. {
  142. DEBUGF(LOG_CF(data, cf, "close"));
  143. cf->connected = FALSE;
  144. cf_haproxy_ctx_reset(cf->ctx);
  145. if(cf->next)
  146. cf->next->cft->close(cf->next, data);
  147. }
  148. static int cf_haproxy_get_select_socks(struct Curl_cfilter *cf,
  149. struct Curl_easy *data,
  150. curl_socket_t *socks)
  151. {
  152. int fds;
  153. fds = cf->next->cft->get_select_socks(cf->next, data, socks);
  154. if(!fds && cf->next->connected && !cf->connected) {
  155. /* If we are not connected, but the filter "below" is
  156. * and not waiting on something, we are sending. */
  157. socks[0] = Curl_conn_cf_get_socket(cf, data);
  158. return GETSOCK_WRITESOCK(0);
  159. }
  160. return fds;
  161. }
  162. struct Curl_cftype Curl_cft_haproxy = {
  163. "HAPROXY",
  164. 0,
  165. 0,
  166. cf_haproxy_destroy,
  167. cf_haproxy_connect,
  168. cf_haproxy_close,
  169. Curl_cf_def_get_host,
  170. cf_haproxy_get_select_socks,
  171. Curl_cf_def_data_pending,
  172. Curl_cf_def_send,
  173. Curl_cf_def_recv,
  174. Curl_cf_def_cntrl,
  175. Curl_cf_def_conn_is_alive,
  176. Curl_cf_def_conn_keep_alive,
  177. Curl_cf_def_query,
  178. };
  179. static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf,
  180. struct Curl_easy *data)
  181. {
  182. struct Curl_cfilter *cf = NULL;
  183. struct cf_haproxy_ctx *ctx;
  184. CURLcode result;
  185. (void)data;
  186. ctx = calloc(sizeof(*ctx), 1);
  187. if(!ctx) {
  188. result = CURLE_OUT_OF_MEMORY;
  189. goto out;
  190. }
  191. ctx->state = HAPROXY_INIT;
  192. Curl_dyn_init(&ctx->data_out, DYN_HAXPROXY);
  193. result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx);
  194. if(result)
  195. goto out;
  196. ctx = NULL;
  197. out:
  198. cf_haproxy_ctx_free(ctx);
  199. *pcf = result? NULL : cf;
  200. return result;
  201. }
  202. CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at,
  203. struct Curl_easy *data)
  204. {
  205. struct Curl_cfilter *cf;
  206. CURLcode result;
  207. result = cf_haproxy_create(&cf, data);
  208. if(result)
  209. goto out;
  210. Curl_conn_cf_insert_after(cf_at, cf);
  211. out:
  212. return result;
  213. }
  214. #endif /* !CURL_DISABLE_PROXY */