CURLOPT_SOCKOPTFUNCTION.3 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" **************************************************************************
  22. .\"
  23. .TH CURLOPT_SOCKOPTFUNCTION 3 "16 Jun 2014" "libcurl 7.37.0" "curl_easy_setopt options"
  24. .SH NAME
  25. CURLOPT_SOCKOPTFUNCTION \- callback for setting socket options
  26. .SH SYNOPSIS
  27. .nf
  28. #include <curl/curl.h>
  29. typedef enum {
  30. CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */
  31. CURLSOCKTYPE_ACCEPT, /* socket created by accept() call */
  32. CURLSOCKTYPE_LAST /* never use */
  33. } curlsocktype;
  34. #define CURL_SOCKOPT_OK 0
  35. #define CURL_SOCKOPT_ERROR 1 /* causes libcurl to abort and return
  36. CURLE_ABORTED_BY_CALLBACK */
  37. #define CURL_SOCKOPT_ALREADY_CONNECTED 2
  38. int sockopt_callback(void *clientp,
  39. curl_socket_t curlfd,
  40. curlsocktype purpose);
  41. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  42. .SH DESCRIPTION
  43. Pass a pointer to your callback function, which should match the prototype
  44. shown above.
  45. When set, this callback function gets called by libcurl when the socket has
  46. been created, but before the connect call to allow applications to change
  47. specific socket options. The callback's \fIpurpose\fP argument identifies the
  48. exact purpose for this particular socket:
  49. \fICURLSOCKTYPE_IPCXN\fP for actively created connections or since 7.28.0
  50. \fICURLSOCKTYPE_ACCEPT\fP for FTP when the connection was setup with PORT/EPSV
  51. (in earlier versions these sockets were not passed to this callback).
  52. Future versions of libcurl may support more purposes. libcurl passes the newly
  53. created socket descriptor to the callback in the \fIcurlfd\fP parameter so
  54. additional setsockopt() calls can be done at the user's discretion.
  55. The \fIclientp\fP pointer contains whatever user-defined value set using the
  56. \fICURLOPT_SOCKOPTDATA(3)\fP function.
  57. Return \fICURL_SOCKOPT_OK\fP from the callback on success. Return
  58. \fICURL_SOCKOPT_ERROR\fP from the callback function to signal an unrecoverable
  59. error to the library and it will close the socket and return
  60. \fICURLE_COULDNT_CONNECT\fP.
  61. Alternatively, the callback function can return
  62. \fICURL_SOCKOPT_ALREADY_CONNECTED\fP, to tell libcurl that the socket is
  63. already connected and then libcurl will not attempt to connect it. This allows
  64. an application to pass in an already connected socket with
  65. \fICURLOPT_OPENSOCKETFUNCTION(3)\fP and then have this function make libcurl
  66. not attempt to connect (again).
  67. .SH DEFAULT
  68. By default, this callback is NULL and unused.
  69. .SH PROTOCOLS
  70. All
  71. .SH EXAMPLE
  72. .nf
  73. /* make libcurl use the already established socket 'sockfd' */
  74. static curl_socket_t opensocket(void *clientp,
  75. curlsocktype purpose,
  76. struct curl_sockaddr *address)
  77. {
  78. curl_socket_t sockfd;
  79. sockfd = *(curl_socket_t *)clientp;
  80. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  81. option */
  82. return sockfd;
  83. }
  84. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  85. curlsocktype purpose)
  86. {
  87. /* This return code was added in libcurl 7.21.5 */
  88. return CURL_SOCKOPT_ALREADY_CONNECTED;
  89. }
  90. curl = curl_easy_init();
  91. if(curl) {
  92. /* libcurl will internally think that you connect to the host
  93. * and port that you specify in the URL option. */
  94. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  95. /* call this function to get a socket */
  96. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  97. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  98. /* call this function to set options for the socket */
  99. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  100. res = curl_easy_perform(curl);
  101. curl_easy_cleanup(curl);
  102. .fi
  103. .SH AVAILABILITY
  104. Added in 7.16.0. The \fICURL_SOCKOPT_ALREADY_CONNECTED\fP return code was
  105. added in 7.21.5.
  106. .SH RETURN VALUE
  107. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  108. .SH "SEE ALSO"
  109. .BR CURLOPT_SOCKOPTDATA "(3), " CURLOPT_OPENSOCKETFUNCTION "(3), "