2
0

cf-socket.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef HEADER_CURL_CF_SOCKET_H
  2. #define HEADER_CURL_CF_SOCKET_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include "nonblock.h" /* for curlx_nonblock(), formerly Curl_nonblock() */
  28. #include "sockaddr.h"
  29. struct Curl_addrinfo;
  30. struct Curl_cfilter;
  31. struct Curl_easy;
  32. struct connectdata;
  33. struct Curl_sockaddr_ex;
  34. #ifndef SIZEOF_CURL_SOCKET_T
  35. /* configure and cmake check and set the define */
  36. # ifdef _WIN64
  37. # define SIZEOF_CURL_SOCKET_T 8
  38. # else
  39. /* default guess */
  40. # define SIZEOF_CURL_SOCKET_T 4
  41. # endif
  42. #endif
  43. #if SIZEOF_CURL_SOCKET_T < 8
  44. # define CURL_FORMAT_SOCKET_T "d"
  45. #else
  46. # define CURL_FORMAT_SOCKET_T "qd"
  47. #endif
  48. /*
  49. * The Curl_sockaddr_ex structure is basically libcurl's external API
  50. * curl_sockaddr structure with enough space available to directly hold any
  51. * protocol-specific address structures. The variable declared here will be
  52. * used to pass / receive data to/from the fopensocket callback if this has
  53. * been set, before that, it is initialized from parameters.
  54. */
  55. struct Curl_sockaddr_ex {
  56. int family;
  57. int socktype;
  58. int protocol;
  59. unsigned int addrlen;
  60. union {
  61. struct sockaddr addr;
  62. struct Curl_sockaddr_storage buff;
  63. } _sa_ex_u;
  64. };
  65. #define sa_addr _sa_ex_u.addr
  66. /*
  67. * Create a socket based on info from 'conn' and 'ai'.
  68. *
  69. * Fill in 'addr' and 'sockfd' accordingly if OK is returned. If the open
  70. * socket callback is set, used that!
  71. *
  72. */
  73. CURLcode Curl_socket_open(struct Curl_easy *data,
  74. const struct Curl_addrinfo *ai,
  75. struct Curl_sockaddr_ex *addr,
  76. int transport,
  77. curl_socket_t *sockfd);
  78. int Curl_socket_close(struct Curl_easy *data, struct connectdata *conn,
  79. curl_socket_t sock);
  80. /**
  81. * Determine the curl code for a socket connect() == -1 with errno.
  82. */
  83. CURLcode Curl_socket_connect_result(struct Curl_easy *data,
  84. const char *ipaddress, int error);
  85. #ifdef USE_WINSOCK
  86. /* When you run a program that uses the Windows Sockets API, you may
  87. experience slow performance when you copy data to a TCP server.
  88. https://support.microsoft.com/kb/823764
  89. Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
  90. Buffer Size
  91. */
  92. void Curl_sndbufset(curl_socket_t sockfd);
  93. #else
  94. #define Curl_sndbufset(y) Curl_nop_stmt
  95. #endif
  96. /**
  97. * Assign the address `ai` to the Curl_sockaddr_ex `dest` and
  98. * set the transport used.
  99. */
  100. void Curl_sock_assign_addr(struct Curl_sockaddr_ex *dest,
  101. const struct Curl_addrinfo *ai,
  102. int transport);
  103. /**
  104. * Creates a cfilter that opens a TCP socket to the given address
  105. * when calling its `connect` implementation.
  106. * The filter will not touch any connection/data flags and can be
  107. * used in happy eyeballing. Once selected for use, its `_active()`
  108. * method needs to be called.
  109. */
  110. CURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf,
  111. struct Curl_easy *data,
  112. struct connectdata *conn,
  113. const struct Curl_addrinfo *ai,
  114. int transport);
  115. /**
  116. * Creates a cfilter that opens a UDP socket to the given address
  117. * when calling its `connect` implementation.
  118. * The filter will not touch any connection/data flags and can be
  119. * used in happy eyeballing. Once selected for use, its `_active()`
  120. * method needs to be called.
  121. */
  122. CURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf,
  123. struct Curl_easy *data,
  124. struct connectdata *conn,
  125. const struct Curl_addrinfo *ai,
  126. int transport);
  127. /**
  128. * Creates a cfilter that opens a UNIX socket to the given address
  129. * when calling its `connect` implementation.
  130. * The filter will not touch any connection/data flags and can be
  131. * used in happy eyeballing. Once selected for use, its `_active()`
  132. * method needs to be called.
  133. */
  134. CURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf,
  135. struct Curl_easy *data,
  136. struct connectdata *conn,
  137. const struct Curl_addrinfo *ai,
  138. int transport);
  139. /**
  140. * Creates a cfilter that keeps a listening socket.
  141. */
  142. CURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data,
  143. struct connectdata *conn,
  144. int sockindex,
  145. curl_socket_t *s);
  146. /**
  147. * Replace the listen socket with the accept()ed one.
  148. */
  149. CURLcode Curl_conn_tcp_accepted_set(struct Curl_easy *data,
  150. struct connectdata *conn,
  151. int sockindex,
  152. curl_socket_t *s);
  153. /**
  154. * Return TRUE iff `cf` is a socket filter.
  155. */
  156. bool Curl_cf_is_socket(struct Curl_cfilter *cf);
  157. /**
  158. * Peek at the socket and remote ip/port the socket filter is using.
  159. * The filter owns all returned values.
  160. * @param psock pointer to hold socket descriptor or NULL
  161. * @param paddr pointer to hold addr reference or NULL
  162. * @param pr_ip_str pointer to hold remote addr as string or NULL
  163. * @param pr_port pointer to hold remote port number or NULL
  164. * @param pl_ip_str pointer to hold local addr as string or NULL
  165. * @param pl_port pointer to hold local port number or NULL
  166. * Returns error if the filter is of invalid type.
  167. */
  168. CURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf,
  169. struct Curl_easy *data,
  170. curl_socket_t *psock,
  171. const struct Curl_sockaddr_ex **paddr,
  172. const char **pr_ip_str, int *pr_port,
  173. const char **pl_ip_str, int *pl_port);
  174. extern struct Curl_cftype Curl_cft_tcp;
  175. extern struct Curl_cftype Curl_cft_udp;
  176. extern struct Curl_cftype Curl_cft_unix;
  177. extern struct Curl_cftype Curl_cft_tcp_accept;
  178. #endif /* HEADER_CURL_CF_SOCKET_H */