CURLOPT_SOCKOPTFUNCTION.3 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. .\" generated by cd2nroff 0.1 from CURLOPT_SOCKOPTFUNCTION.md
  2. .TH CURLOPT_SOCKOPTFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_SOCKOPTFUNCTION \- callback for setting socket options
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. typedef enum {
  9. CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */
  10. CURLSOCKTYPE_ACCEPT, /* socket created by accept() call */
  11. CURLSOCKTYPE_LAST /* never use */
  12. } curlsocktype;
  13. #define CURL_SOCKOPT_OK 0
  14. #define CURL_SOCKOPT_ERROR 1 /* causes libcurl to abort and return
  15. CURLE_ABORTED_BY_CALLBACK */
  16. #define CURL_SOCKOPT_ALREADY_CONNECTED 2
  17. int sockopt_callback(void *clientp,
  18. curl_socket_t curlfd,
  19. curlsocktype purpose);
  20. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  21. .fi
  22. .SH DESCRIPTION
  23. Pass a pointer to your callback function, which should match the prototype
  24. shown above.
  25. When set, this callback function gets called by libcurl when the socket has
  26. been created, but before the connect call to allow applications to change
  27. specific socket options. The callback\(aqs \fIpurpose\fP argument identifies the
  28. exact purpose for this particular socket:
  29. \fICURLSOCKTYPE_IPCXN\fP for actively created connections or since 7.28.0
  30. \fICURLSOCKTYPE_ACCEPT\fP for FTP when the connection was setup with PORT/EPSV
  31. (in earlier versions these sockets were not passed to this callback).
  32. Future versions of libcurl may support more purposes. libcurl passes the newly
  33. created socket descriptor to the callback in the \fIcurlfd\fP parameter so
  34. additional setsockopt() calls can be done at the user\(aqs discretion.
  35. The \fIclientp\fP pointer contains whatever user\-defined value set using the
  36. \fICURLOPT_SOCKOPTDATA(3)\fP function.
  37. Return \fICURL_SOCKOPT_OK\fP from the callback on success. Return
  38. \fICURL_SOCKOPT_ERROR\fP from the callback function to signal an unrecoverable
  39. error to the library and it closes the socket and returns
  40. \fICURLE_COULDNT_CONNECT\fP. Alternatively, the callback function can return
  41. \fICURL_SOCKOPT_ALREADY_CONNECTED\fP, to tell libcurl that the socket is
  42. already connected and then libcurl does no attempt to connect. This allows an
  43. application to pass in an already connected socket with
  44. \fICURLOPT_OPENSOCKETFUNCTION(3)\fP and then have this function make libcurl
  45. not attempt to connect (again).
  46. .SH DEFAULT
  47. NULL
  48. .SH PROTOCOLS
  49. This functionality affects all supported protocols
  50. .SH EXAMPLE
  51. .nf
  52. /* make libcurl use the already established socket 'sockfd' */
  53. static curl_socket_t opensocket(void *clientp,
  54. curlsocktype purpose,
  55. struct curl_sockaddr *address)
  56. {
  57. curl_socket_t sockfd;
  58. sockfd = *(curl_socket_t *)clientp;
  59. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  60. option */
  61. return sockfd;
  62. }
  63. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  64. curlsocktype purpose)
  65. {
  66. /* This return code was added in libcurl 7.21.5 */
  67. return CURL_SOCKOPT_ALREADY_CONNECTED;
  68. }
  69. int main(void)
  70. {
  71. CURL *curl = curl_easy_init();
  72. if(curl) {
  73. CURLcode res;
  74. int sockfd; /* our custom file descriptor */
  75. /* libcurl thinks that you connect to the host
  76. * and port that you specify in the URL option. */
  77. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  78. /* call this function to get a socket */
  79. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  80. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  81. /* call this function to set options for the socket */
  82. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  83. res = curl_easy_perform(curl);
  84. curl_easy_cleanup(curl);
  85. }
  86. }
  87. .fi
  88. .SH AVAILABILITY
  89. Added in curl 7.16.0
  90. .SH RETURN VALUE
  91. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  92. .SH SEE ALSO
  93. .BR CURLOPT_OPENSOCKETFUNCTION (3),
  94. .BR CURLOPT_SEEKFUNCTION (3),
  95. .BR CURLOPT_SOCKOPTDATA (3)