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