CURLOPT_OPENSOCKETFUNCTION.3 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .\" generated by cd2nroff 0.1 from CURLOPT_OPENSOCKETFUNCTION.md
  2. .TH CURLOPT_OPENSOCKETFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_OPENSOCKETFUNCTION \- callback for opening socket
  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;
  11. struct curl_sockaddr {
  12. int family;
  13. int socktype;
  14. int protocol;
  15. unsigned int addrlen;
  16. struct sockaddr addr;
  17. };
  18. curl_socket_t opensocket_callback(void *clientp,
  19. curlsocktype purpose,
  20. struct curl_sockaddr *address);
  21. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
  22. .fi
  23. .SH DESCRIPTION
  24. Pass a pointer to your callback function, which should match the prototype
  25. shown above.
  26. This callback function gets called by libcurl instead of the \fIsocket(2)\fP
  27. call. The callback\(aqs \fIpurpose\fP argument identifies the exact purpose for
  28. this particular socket. \fICURLSOCKTYPE_IPCXN\fP is for IP based connections
  29. and is the only purpose currently used in libcurl. Future versions of libcurl
  30. may support more purposes.
  31. The \fIclientp\fP pointer contains whatever user\-defined value set using the
  32. \fICURLOPT_OPENSOCKETDATA(3)\fP function.
  33. The callback gets the resolved peer address as the \fIaddress\fP argument and
  34. is allowed to modify the address or refuse to connect completely. The callback
  35. function should return the newly created socket or \fICURL_SOCKET_BAD\fP in
  36. case no connection could be established or another error was detected. Any
  37. additional \fIsetsockopt(2)\fP calls can of course be done on the socket at
  38. the user\(aqs discretion. A \fICURL_SOCKET_BAD\fP return value from the callback
  39. function signals an unrecoverable error to libcurl and it returns
  40. \fICURLE_COULDNT_CONNECT\fP from the function that triggered this callback.
  41. This return code can be used for IP address block listing.
  42. If you want to pass in a socket with an already established connection, pass
  43. the socket back with this callback and then use
  44. \fICURLOPT_SOCKOPTFUNCTION(3)\fP to signal that it already is connected.
  45. .SH DEFAULT
  46. The equivalent of this:
  47. .nf
  48. return socket(addr->family, addr->socktype, addr->protocol);
  49. .fi
  50. .SH PROTOCOLS
  51. This functionality affects all supported protocols
  52. .SH EXAMPLE
  53. .nf
  54. /* make libcurl use the already established socket 'sockfd' */
  55. static curl_socket_t opensocket(void *clientp,
  56. curlsocktype purpose,
  57. struct curl_sockaddr *address)
  58. {
  59. curl_socket_t sockfd;
  60. sockfd = *(curl_socket_t *)clientp;
  61. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  62. option */
  63. return sockfd;
  64. }
  65. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  66. curlsocktype purpose)
  67. {
  68. /* This return code was added in libcurl 7.21.5 */
  69. return CURL_SOCKOPT_ALREADY_CONNECTED;
  70. }
  71. int main(void)
  72. {
  73. CURL *curl = curl_easy_init();
  74. if(curl) {
  75. CURLcode res;
  76. extern int sockfd; /* the already connected one */
  77. /* libcurl thinks that you connect to the host
  78. * and port that you specify in the URL option. */
  79. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  80. /* call this function to get a socket */
  81. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  82. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  83. /* call this function to set options for the socket */
  84. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  85. res = curl_easy_perform(curl);
  86. curl_easy_cleanup(curl);
  87. }
  88. }
  89. .fi
  90. .SH AVAILABILITY
  91. Added in curl 7.17.1
  92. .SH RETURN VALUE
  93. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  94. .SH SEE ALSO
  95. .BR CURLOPT_CLOSESOCKETFUNCTION (3),
  96. .BR CURLOPT_OPENSOCKETFUNCTION (3),
  97. .BR CURLOPT_SOCKOPTFUNCTION (3)