2
0

CURLOPT_SOCKOPTFUNCTION.3 4.9 KB

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