CURLOPT_OPENSOCKETFUNCTION.3 4.7 KB

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