| 1 |
.\" ************************************************************************** |
| 2 |
.\" * _ _ ____ _ |
| 3 |
.\" * Project ___| | | | _ \| | |
| 4 |
.\" * / __| | | | |_) | | |
| 5 |
.\" * | (__| |_| | _ <| |___ |
| 6 |
.\" * \___|\___/|_| \_\_____| |
| 7 |
.\" * |
| 8 |
.\" * Copyright (C) 1998 - 2017, 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_SOCKOPTFUNCTION 3 "16 Jun 2014" "libcurl 7.37.0" "curl_easy_setopt options" |
| 24 |
.SH NAME |
| 25 |
CURLOPT_SOCKOPTFUNCTION \- set callback for setting socket options |
| 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_ACCEPT, /* socket created by accept() call */ |
| 33 |
CURLSOCKTYPE_LAST /* never use */ |
| 34 |
} curlsocktype; |
| 35 |
|
| 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 |
|
| 41 |
int sockopt_callback(void *clientp, |
| 42 |
curl_socket_t curlfd, |
| 43 |
curlsocktype purpose); |
| 44 |
|
| 45 |
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SOCKOPTFUNCTION, sockopt_callback); |
| 46 |
.SH DESCRIPTION |
| 47 |
Pass a pointer to your callback function, which should match the prototype |
| 48 |
shown above. |
| 49 |
|
| 50 |
When set, this callback function gets called by libcurl when the socket has |
| 51 |
been created, but before the connect call to allow applications to change |
| 52 |
specific socket options. The callback's \fIpurpose\fP argument identifies the |
| 53 |
exact purpose for this particular socket: |
| 54 |
|
| 55 |
\fICURLSOCKTYPE_IPCXN\fP for actively created connections or since 7.28.0 |
| 56 |
\fICURLSOCKTYPE_ACCEPT\fP for FTP when the connection was setup with PORT/EPSV |
| 57 |
(in earlier versions these sockets weren't passed to this callback). |
| 58 |
|
| 59 |
Future versions of libcurl may support more purposes. libcurl passes the newly |
| 60 |
created socket descriptor to the callback in the \fIcurlfd\fP parameter so |
| 61 |
additional setsockopt() calls can be done at the user's discretion. |
| 62 |
|
| 63 |
The \fIclientp\fP pointer contains whatever user-defined value set using the |
| 64 |
\fICURLOPT_SOCKOPTDATA(3)\fP function. |
| 65 |
|
| 66 |
Return \fICURL_SOCKOPT_OK\fP from the callback on success. Return |
| 67 |
\fICURL_SOCKOPT_ERROR\fP from the callback function to signal an unrecoverable |
| 68 |
error to the library and it will close the socket and return |
| 69 |
\fICURLE_COULDNT_CONNECT\fP. |
| 70 |
Alternatively, the callback function can return |
| 71 |
\fICURL_SOCKOPT_ALREADY_CONNECTED\fP, to tell libcurl that the socket is |
| 72 |
already connected and then libcurl will not attempt to connect it. This allows |
| 73 |
an application to pass in an already connected socket with |
| 74 |
\fICURLOPT_OPENSOCKETFUNCTION(3)\fP and then have this function make libcurl |
| 75 |
not attempt to connect (again). |
| 76 |
.SH DEFAULT |
| 77 |
By default, this callback is NULL and unused. |
| 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 |
.fi |
| 118 |
.SH AVAILABILITY |
| 119 |
Added in 7.16.0. The \fICURL_SOCKOPT_ALREADY_CONNECTED\fP return code was |
| 120 |
added in 7.21.5. |
| 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_SOCKOPTDATA "(3), " CURLOPT_OPENSOCKETFUNCTION "(3), " |