CURLOPT_OPENSOCKETDATA.3 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. .\" generated by cd2nroff 0.1 from CURLOPT_OPENSOCKETDATA.md
  2. .TH CURLOPT_OPENSOCKETDATA 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_OPENSOCKETDATA \- pointer passed to open socket callback
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETDATA, void *pointer);
  9. .fi
  10. .SH DESCRIPTION
  11. Pass a \fIpointer\fP that is untouched by libcurl and passed as the first
  12. argument in the open socket callback set with
  13. \fICURLOPT_OPENSOCKETFUNCTION(3)\fP.
  14. .SH DEFAULT
  15. NULL
  16. .SH PROTOCOLS
  17. This functionality affects all supported protocols
  18. .SH EXAMPLE
  19. .nf
  20. /* make libcurl use the already established socket 'sockfd' */
  21. static curl_socket_t opensocket(void *clientp,
  22. curlsocktype purpose,
  23. struct curl_sockaddr *address)
  24. {
  25. curl_socket_t sockfd;
  26. sockfd = *(curl_socket_t *)clientp;
  27. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  28. option */
  29. return sockfd;
  30. }
  31. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  32. curlsocktype purpose)
  33. {
  34. /* This return code was added in libcurl 7.21.5 */
  35. return CURL_SOCKOPT_ALREADY_CONNECTED;
  36. }
  37. int main(void)
  38. {
  39. CURL *curl = curl_easy_init();
  40. if(curl) {
  41. CURLcode res;
  42. extern int sockfd; /* the already connected one */
  43. /* libcurl thinks that you connect to the host
  44. * and port that you specify in the URL option. */
  45. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  46. /* call this function to get a socket */
  47. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  48. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  49. /* call this function to set options for the socket */
  50. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  51. res = curl_easy_perform(curl);
  52. curl_easy_cleanup(curl);
  53. }
  54. }
  55. .fi
  56. .SH AVAILABILITY
  57. Added in curl 7.17.1
  58. .SH RETURN VALUE
  59. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  60. .SH SEE ALSO
  61. .BR CURLOPT_CLOSESOCKETFUNCTION (3),
  62. .BR CURLOPT_OPENSOCKETFUNCTION (3),
  63. .BR CURLOPT_SOCKOPTFUNCTION (3)