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