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: CURLINFO_ACTIVESOCKET 5*6236dae4SAndroid Build Coastguard WorkerSection: 3 6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl 7*6236dae4SAndroid Build Coastguard WorkerSee-also: 8*6236dae4SAndroid Build Coastguard Worker - CURLINFO_LASTSOCKET (3) 9*6236dae4SAndroid Build Coastguard Worker - CURLOPT_CONNECT_ONLY (3) 10*6236dae4SAndroid Build Coastguard Worker - curl_easy_getinfo (3) 11*6236dae4SAndroid Build Coastguard Worker - curl_easy_setopt (3) 12*6236dae4SAndroid Build Coastguard WorkerProtocol: 13*6236dae4SAndroid Build Coastguard Worker - All 14*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.45.0 15*6236dae4SAndroid Build Coastguard Worker--- 16*6236dae4SAndroid Build Coastguard Worker 17*6236dae4SAndroid Build Coastguard Worker# NAME 18*6236dae4SAndroid Build Coastguard Worker 19*6236dae4SAndroid Build Coastguard WorkerCURLINFO_ACTIVESOCKET - get the active socket 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 WorkerCURLcode curl_easy_getinfo(CURL *handle, CURLINFO_ACTIVESOCKET, 27*6236dae4SAndroid Build Coastguard Worker curl_socket_t *socket); 28*6236dae4SAndroid Build Coastguard Worker~~~ 29*6236dae4SAndroid Build Coastguard Worker 30*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION 31*6236dae4SAndroid Build Coastguard Worker 32*6236dae4SAndroid Build Coastguard WorkerPass a pointer to a curl_socket_t to receive the most recently active socket 33*6236dae4SAndroid Build Coastguard Workerused for the transfer connection by this curl session. If the socket is no 34*6236dae4SAndroid Build Coastguard Workerlonger valid, *CURL_SOCKET_BAD* is returned. When you are finished working 35*6236dae4SAndroid Build Coastguard Workerwith the socket, you must call curl_easy_cleanup(3) as usual on the easy 36*6236dae4SAndroid Build Coastguard Workerhandle and let libcurl close the socket and cleanup other resources associated 37*6236dae4SAndroid Build Coastguard Workerwith the handle. This option returns the active socket only after the transfer 38*6236dae4SAndroid Build Coastguard Workeris complete, and is typically used in combination with 39*6236dae4SAndroid Build Coastguard WorkerCURLOPT_CONNECT_ONLY(3), which skips the transfer phase. 40*6236dae4SAndroid Build Coastguard Worker 41*6236dae4SAndroid Build Coastguard WorkerCURLINFO_ACTIVESOCKET(3) was added as a replacement for 42*6236dae4SAndroid Build Coastguard WorkerCURLINFO_LASTSOCKET(3) since that one is not working on all platforms. 43*6236dae4SAndroid Build Coastguard Worker 44*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS% 45*6236dae4SAndroid Build Coastguard Worker 46*6236dae4SAndroid Build Coastguard Worker# EXAMPLE 47*6236dae4SAndroid Build Coastguard Worker 48*6236dae4SAndroid Build Coastguard Worker~~~c 49*6236dae4SAndroid Build Coastguard Workerint main(void) 50*6236dae4SAndroid Build Coastguard Worker{ 51*6236dae4SAndroid Build Coastguard Worker CURL *curl = curl_easy_init(); 52*6236dae4SAndroid Build Coastguard Worker if(curl) { 53*6236dae4SAndroid Build Coastguard Worker CURLcode res; 54*6236dae4SAndroid Build Coastguard Worker curl_socket_t sockfd; 55*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 56*6236dae4SAndroid Build Coastguard Worker 57*6236dae4SAndroid Build Coastguard Worker /* Do not do the transfer - only connect to host */ 58*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); 59*6236dae4SAndroid Build Coastguard Worker res = curl_easy_perform(curl); 60*6236dae4SAndroid Build Coastguard Worker if(res != CURLE_OK) { 61*6236dae4SAndroid Build Coastguard Worker printf("Error: %s\n", curl_easy_strerror(res)); 62*6236dae4SAndroid Build Coastguard Worker curl_easy_cleanup(curl); 63*6236dae4SAndroid Build Coastguard Worker return 1; 64*6236dae4SAndroid Build Coastguard Worker } 65*6236dae4SAndroid Build Coastguard Worker 66*6236dae4SAndroid Build Coastguard Worker /* Extract the socket from the curl handle */ 67*6236dae4SAndroid Build Coastguard Worker res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd); 68*6236dae4SAndroid Build Coastguard Worker if(!res && sockfd != CURL_SOCKET_BAD) { 69*6236dae4SAndroid Build Coastguard Worker /* operate on sockfd */ 70*6236dae4SAndroid Build Coastguard Worker } 71*6236dae4SAndroid Build Coastguard Worker 72*6236dae4SAndroid Build Coastguard Worker curl_easy_cleanup(curl); 73*6236dae4SAndroid Build Coastguard Worker } 74*6236dae4SAndroid Build Coastguard Worker} 75*6236dae4SAndroid Build Coastguard Worker~~~ 76*6236dae4SAndroid Build Coastguard Worker 77*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY% 78*6236dae4SAndroid Build Coastguard Worker 79*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE 80*6236dae4SAndroid Build Coastguard Worker 81*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. 82