cf-socket.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, <[email protected]>, 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 "curlx/nonblock.h" /* for curlx_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. struct ip_quadruple;
  35. /*
  36. * The Curl_sockaddr_ex structure is basically libcurl's external API
  37. * curl_sockaddr structure with enough space available to directly hold any
  38. * protocol-specific address structures. The variable declared here will be
  39. * used to pass / receive data to/from the fopensocket callback if this has
  40. * been set, before that, it is initialized from parameters.
  41. */
  42. struct Curl_sockaddr_ex {
  43. int family;
  44. int socktype;
  45. int protocol;
  46. unsigned int addrlen;
  47. union {
  48. struct sockaddr sa;
  49. struct Curl_sockaddr_storage buf;
  50. } addr;
  51. };
  52. #define curl_sa_addr addr.sa
  53. #define curl_sa_addrbuf addr.buf
  54. /*
  55. * Parse interface option, and return the interface name and the host part.
  56. */
  57. CURLcode Curl_parse_interface(const char *input,
  58. char **dev, char **iface, char **host);
  59. /*
  60. * Create a socket based on info from 'conn' and 'ai'.
  61. *
  62. * Fill in 'addr' and 'sockfd' accordingly if OK is returned. If the open
  63. * socket callback is set, used that!
  64. *
  65. */
  66. CURLcode Curl_socket_open(struct Curl_easy *data,
  67. const struct Curl_addrinfo *ai,
  68. struct Curl_sockaddr_ex *addr,
  69. int transport,
  70. curl_socket_t *sockfd);
  71. int Curl_socket_close(struct Curl_easy *data, struct connectdata *conn,
  72. curl_socket_t sock);
  73. #ifdef USE_WINSOCK
  74. /* When you run a program that uses the Windows Sockets API, you may
  75. experience slow performance when you copy data to a TCP server.
  76. https://learn.microsoft.com/troubleshoot/windows-server/networking/slow-performance-copy-data-tcp-server-sockets-api
  77. Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
  78. Buffer Size
  79. */
  80. void Curl_sndbuf_init(curl_socket_t sockfd);
  81. #else
  82. #define Curl_sndbuf_init(y) Curl_nop_stmt
  83. #endif
  84. /**
  85. * Creates a cfilter that opens a TCP socket to the given address
  86. * when calling its `connect` implementation.
  87. * The filter will not touch any connection/data flags and can be
  88. * used in happy eyeballing. Once selected for use, its `_active()`
  89. * method needs to be called.
  90. */
  91. CURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf,
  92. struct Curl_easy *data,
  93. struct connectdata *conn,
  94. const struct Curl_addrinfo *ai,
  95. int transport);
  96. /**
  97. * Creates a cfilter that opens a UDP socket to the given address
  98. * when calling its `connect` implementation.
  99. * The filter will not touch any connection/data flags and can be
  100. * used in happy eyeballing. Once selected for use, its `_active()`
  101. * method needs to be called.
  102. */
  103. CURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf,
  104. struct Curl_easy *data,
  105. struct connectdata *conn,
  106. const struct Curl_addrinfo *ai,
  107. int transport);
  108. /**
  109. * Creates a cfilter that opens a UNIX socket to the given address
  110. * when calling its `connect` implementation.
  111. * The filter will not touch any connection/data flags and can be
  112. * used in happy eyeballing. Once selected for use, its `_active()`
  113. * method needs to be called.
  114. */
  115. CURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf,
  116. struct Curl_easy *data,
  117. struct connectdata *conn,
  118. const struct Curl_addrinfo *ai,
  119. int transport);
  120. /**
  121. * Creates a cfilter that keeps a listening socket.
  122. */
  123. CURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data,
  124. struct connectdata *conn,
  125. int sockindex,
  126. curl_socket_t *s);
  127. /**
  128. * Return TRUE iff the last filter at `sockindex` was set via
  129. * Curl_conn_tcp_listen_set().
  130. */
  131. bool Curl_conn_is_tcp_listen(struct Curl_easy *data,
  132. int sockindex);
  133. /**
  134. * Peek at the socket and remote ip/port the socket filter is using.
  135. * The filter owns all returned values.
  136. * @param psock pointer to hold socket descriptor or NULL
  137. * @param paddr pointer to hold addr reference or NULL
  138. * @param pip pointer to get IP quadruple or NULL
  139. * Returns error if the filter is of invalid type.
  140. */
  141. CURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf,
  142. struct Curl_easy *data,
  143. curl_socket_t *psock,
  144. const struct Curl_sockaddr_ex **paddr,
  145. struct ip_quadruple *pip) WARN_UNUSED_RESULT;
  146. extern struct Curl_cftype Curl_cft_tcp;
  147. extern struct Curl_cftype Curl_cft_udp;
  148. extern struct Curl_cftype Curl_cft_unix;
  149. extern struct Curl_cftype Curl_cft_tcp_accept;
  150. #endif /* HEADER_CURL_CF_SOCKET_H */