CURLOPT_OPENSOCKETFUNCTION.3 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2017, 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.haxx.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_OPENSOCKETFUNCTION 3 "May 15, 2017" "libcurl 7.61.1" "curl_easy_setopt options"
  24. .SH NAME
  25. CURLOPT_OPENSOCKETFUNCTION \- set callback for opening sockets
  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. struct curl_sockaddr {
  35. int family;
  36. int socktype;
  37. int protocol;
  38. unsigned int addrlen;
  39. struct sockaddr addr;
  40. };
  41. curl_socket_t opensocket_callback(void *clientp,
  42. curlsocktype purpose,
  43. struct curl_sockaddr *address);
  44. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
  45. .SH DESCRIPTION
  46. Pass a pointer to your callback function, which should match the prototype
  47. shown above.
  48. This callback function gets called by libcurl instead of the \fIsocket(2)\fP
  49. call. The callback's \fIpurpose\fP argument identifies the exact purpose for
  50. this particular socket: \fICURLSOCKTYPE_IPCXN\fP is for IP based connections
  51. and \fICURLSOCKTYPE_ACCEPT\fP is for sockets created after accept() - such as
  52. when doing active FTP. Future versions of libcurl may support more
  53. purposes.
  54. The \fIclientp\fP pointer contains whatever user-defined value set using the
  55. \fICURLOPT_OPENSOCKETDATA(3)\fP function.
  56. The callback gets the resolved peer address as the \fIaddress\fP argument and
  57. is allowed to modify the address or refuse to connect completely. The callback
  58. function should return the newly created socket or \fICURL_SOCKET_BAD\fP in
  59. case no connection could be established or another error was detected. Any
  60. additional \fIsetsockopt(2)\fP calls can of course be done on the socket at
  61. the user's discretion. A \fICURL_SOCKET_BAD\fP return value from the callback
  62. function will signal an unrecoverable error to libcurl and it will return
  63. \fICURLE_COULDNT_CONNECT\fP from the function that triggered this callback.
  64. This return code can be used for IP address blacklisting.
  65. If you want to pass in a socket with an already established connection, pass
  66. the socket back with this callback and then use
  67. \fICURLOPT_SOCKOPTFUNCTION(3)\fP to signal that it already is connected.
  68. .SH DEFAULT
  69. The default behavior is the equivalent of this:
  70. .nf
  71. return socket(addr->family, addr->socktype, addr->protocol);
  72. .fi
  73. .SH PROTOCOLS
  74. All
  75. .SH EXAMPLE
  76. .nf
  77. /* make libcurl use the already established socket 'sockfd' */
  78. static curl_socket_t opensocket(void *clientp,
  79. curlsocktype purpose,
  80. struct curl_sockaddr *address)
  81. {
  82. curl_socket_t sockfd;
  83. sockfd = *(curl_socket_t *)clientp;
  84. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  85. option */
  86. return sockfd;
  87. }
  88. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  89. curlsocktype purpose)
  90. {
  91. /* This return code was added in libcurl 7.21.5 */
  92. return CURL_SOCKOPT_ALREADY_CONNECTED;
  93. }
  94. curl = curl_easy_init();
  95. if(curl) {
  96. /* libcurl will internally think that you connect to the host
  97. * and port that you specify in the URL option. */
  98. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  99. /* call this function to get a socket */
  100. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  101. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  102. /* call this function to set options for the socket */
  103. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  104. res = curl_easy_perform(curl);
  105. curl_easy_cleanup(curl);
  106. }
  107. .fi
  108. .SH AVAILABILITY
  109. Added in 7.17.1.
  110. .SH RETURN VALUE
  111. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  112. .SH "SEE ALSO"
  113. .BR CURLOPT_OPENSOCKETDATA "(3), " CURLOPT_SOCKOPTFUNCTION "(3), "
  114. .BR CURLOPT_CLOSESOCKETFUNCTION "(3), "