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