xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_PREREQFUNCTION.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_PREREQFUNCTION
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLINFO_PRIMARY_IP (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLINFO_PRIMARY_PORT (3)
10*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_PREREQDATA (3)
11*6236dae4SAndroid Build Coastguard WorkerProtocol:
12*6236dae4SAndroid Build Coastguard Worker  - All
13*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.80.0
14*6236dae4SAndroid Build Coastguard Worker---
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Worker# NAME
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard WorkerCURLOPT_PREREQFUNCTION - user callback called when a connection has been
19*6236dae4SAndroid Build Coastguard Workerestablished, but before a request has been made.
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker~~~c
24*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
25*6236dae4SAndroid Build Coastguard Worker
26*6236dae4SAndroid Build Coastguard Worker/* These are the return codes for the pre-request callback. */
27*6236dae4SAndroid Build Coastguard Worker#define CURL_PREREQFUNC_OK 0
28*6236dae4SAndroid Build Coastguard Worker#define CURL_PREREQFUNC_ABORT 1 /* fail the entire transfer */
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard Workerint prereq_callback(void *clientp,
31*6236dae4SAndroid Build Coastguard Worker                    char *conn_primary_ip,
32*6236dae4SAndroid Build Coastguard Worker                    char *conn_local_ip,
33*6236dae4SAndroid Build Coastguard Worker                    int conn_primary_port,
34*6236dae4SAndroid Build Coastguard Worker                    int conn_local_port);
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_PREREQFUNCTION, prereq_callback);
37*6236dae4SAndroid Build Coastguard Worker~~~
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
40*6236dae4SAndroid Build Coastguard Worker
41*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype
42*6236dae4SAndroid Build Coastguard Workershown above.
43*6236dae4SAndroid Build Coastguard Worker
44*6236dae4SAndroid Build Coastguard WorkerThis function gets called by libcurl after a connection has been established
45*6236dae4SAndroid Build Coastguard Workeror a connection has been reused (including any SSL handshaking), but before any
46*6236dae4SAndroid Build Coastguard Workerrequest is actually made on the connection. For example, for HTTP, this
47*6236dae4SAndroid Build Coastguard Workercallback is called once a connection has been established to the server, but
48*6236dae4SAndroid Build Coastguard Workerbefore a GET/HEAD/POST/etc request has been sent.
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard WorkerThis function may be called multiple times if redirections are enabled and are
51*6236dae4SAndroid Build Coastguard Workerbeing followed (see CURLOPT_FOLLOWLOCATION(3)).
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard WorkerThe callback function must return *CURL_PREREQFUNC_OK* on success, or
54*6236dae4SAndroid Build Coastguard Worker*CURL_PREREQFUNC_ABORT* to cause the transfer to fail.
55*6236dae4SAndroid Build Coastguard Worker
56*6236dae4SAndroid Build Coastguard WorkerThis function is passed the following arguments:
57*6236dae4SAndroid Build Coastguard Worker
58*6236dae4SAndroid Build Coastguard Worker## `conn_primary_ip`
59*6236dae4SAndroid Build Coastguard Worker
60*6236dae4SAndroid Build Coastguard WorkerA null-terminated pointer to a C string containing the primary IP of the
61*6236dae4SAndroid Build Coastguard Workerremote server established with this connection. For FTP, this is the IP for
62*6236dae4SAndroid Build Coastguard Workerthe control connection. IPv6 addresses are represented without surrounding
63*6236dae4SAndroid Build Coastguard Workerbrackets.
64*6236dae4SAndroid Build Coastguard Worker
65*6236dae4SAndroid Build Coastguard Worker## `conn_local_ip`
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerA null-terminated pointer to a C string containing the originating IP for this
68*6236dae4SAndroid Build Coastguard Workerconnection. IPv6 addresses are represented without surrounding brackets.
69*6236dae4SAndroid Build Coastguard Worker
70*6236dae4SAndroid Build Coastguard Worker## `conn_primary_port`
71*6236dae4SAndroid Build Coastguard Worker
72*6236dae4SAndroid Build Coastguard WorkerThe primary port number on the remote server established with this connection.
73*6236dae4SAndroid Build Coastguard WorkerFor FTP, this is the port for the control connection. This can be a TCP or a
74*6236dae4SAndroid Build Coastguard WorkerUDP port number depending on the protocol.
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard Worker## `conn_local_port`
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard WorkerThe originating port number for this connection. This can be a TCP or a UDP
79*6236dae4SAndroid Build Coastguard Workerport number depending on the protocol.
80*6236dae4SAndroid Build Coastguard Worker
81*6236dae4SAndroid Build Coastguard Worker## `clientp`
82*6236dae4SAndroid Build Coastguard Worker
83*6236dae4SAndroid Build Coastguard WorkerThe pointer you set with CURLOPT_PREREQDATA(3).
84*6236dae4SAndroid Build Coastguard Worker
85*6236dae4SAndroid Build Coastguard Worker# DEFAULT
86*6236dae4SAndroid Build Coastguard Worker
87*6236dae4SAndroid Build Coastguard WorkerNULL
88*6236dae4SAndroid Build Coastguard Worker
89*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
90*6236dae4SAndroid Build Coastguard Worker
91*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
92*6236dae4SAndroid Build Coastguard Worker
93*6236dae4SAndroid Build Coastguard Worker~~~c
94*6236dae4SAndroid Build Coastguard Workerstruct priv {
95*6236dae4SAndroid Build Coastguard Worker  void *custom;
96*6236dae4SAndroid Build Coastguard Worker};
97*6236dae4SAndroid Build Coastguard Worker
98*6236dae4SAndroid Build Coastguard Workerstatic int prereq_callback(void *clientp,
99*6236dae4SAndroid Build Coastguard Worker                           char *conn_primary_ip,
100*6236dae4SAndroid Build Coastguard Worker                           char *conn_local_ip,
101*6236dae4SAndroid Build Coastguard Worker                           int conn_primary_port,
102*6236dae4SAndroid Build Coastguard Worker                           int conn_local_port)
103*6236dae4SAndroid Build Coastguard Worker{
104*6236dae4SAndroid Build Coastguard Worker  printf("Connection made to %s:%d\n", conn_primary_ip, conn_primary_port);
105*6236dae4SAndroid Build Coastguard Worker  return CURL_PREREQFUNC_OK;
106*6236dae4SAndroid Build Coastguard Worker}
107*6236dae4SAndroid Build Coastguard Worker
108*6236dae4SAndroid Build Coastguard Workerint main(void)
109*6236dae4SAndroid Build Coastguard Worker{
110*6236dae4SAndroid Build Coastguard Worker  struct priv prereq_data;
111*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
112*6236dae4SAndroid Build Coastguard Worker  if(curl) {
113*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback);
114*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_data);
115*6236dae4SAndroid Build Coastguard Worker    curl_easy_perform(curl);
116*6236dae4SAndroid Build Coastguard Worker  }
117*6236dae4SAndroid Build Coastguard Worker}
118*6236dae4SAndroid Build Coastguard Worker~~~
119*6236dae4SAndroid Build Coastguard Worker
120*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
121*6236dae4SAndroid Build Coastguard Worker
122*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
123*6236dae4SAndroid Build Coastguard Worker
124*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
125