1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
30 #include "curl_memory.h"
31 #include "vtls/vtls.h"
32 #include "connect.h" /* Curl_getconnectinfo() */
35 /* Make this the last #include */
39 * This is supposed to be called in the beginning of a perform() session
40 * and should reset all session-info variables
42 CURLcode Curl_initinfo(struct SessionHandle *data)
44 struct Progress *pro = &data->progress;
45 struct PureInfo *info =&data->info;
49 pro->t_appconnect = 0;
50 pro->t_pretransfer = 0;
51 pro->t_starttransfer = 0;
56 info->httpproxycode = 0;
57 info->httpversion = 0;
58 info->filetime = -1; /* -1 is an illegal time and thus means unknown */
59 info->timecond = FALSE;
62 free(info->contenttype);
63 info->contenttype = NULL;
65 info->header_size = 0;
66 info->request_size = 0;
67 info->numconnects = 0;
69 info->conn_primary_ip[0] = '\0';
70 info->conn_local_ip[0] = '\0';
71 info->conn_primary_port = 0;
72 info->conn_local_port = 0;
77 static CURLcode getinfo_char(struct SessionHandle *data, CURLINFO info,
81 case CURLINFO_EFFECTIVE_URL:
82 *param_charp = data->change.url?data->change.url:(char *)"";
84 case CURLINFO_CONTENT_TYPE:
85 *param_charp = data->info.contenttype;
87 case CURLINFO_PRIVATE:
88 *param_charp = (char *) data->set.private_data;
90 case CURLINFO_FTP_ENTRY_PATH:
91 /* Return the entrypath string from the most recent connection.
92 This pointer was copied from the connectdata structure by FTP.
93 The actual string may be free()ed by subsequent libcurl calls so
94 it must be copied to a safer area before the next libcurl call.
95 Callers must never free it themselves. */
96 *param_charp = data->state.most_recent_ftp_entrypath;
98 case CURLINFO_REDIRECT_URL:
99 /* Return the URL this request would have been redirected to if that
100 option had been enabled! */
101 *param_charp = data->info.wouldredirect;
103 case CURLINFO_PRIMARY_IP:
104 /* Return the ip address of the most recent (primary) connection */
105 *param_charp = data->info.conn_primary_ip;
107 case CURLINFO_LOCAL_IP:
108 /* Return the source/local ip address of the most recent (primary)
110 *param_charp = data->info.conn_local_ip;
112 case CURLINFO_RTSP_SESSION_ID:
113 *param_charp = data->set.str[STRING_RTSP_SESSION_ID];
117 return CURLE_BAD_FUNCTION_ARGUMENT;
122 static CURLcode getinfo_long(struct SessionHandle *data, CURLINFO info,
125 curl_socket_t sockfd;
128 unsigned long *to_ulong;
133 case CURLINFO_RESPONSE_CODE:
134 *param_longp = data->info.httpcode;
136 case CURLINFO_HTTP_CONNECTCODE:
137 *param_longp = data->info.httpproxycode;
139 case CURLINFO_FILETIME:
140 *param_longp = data->info.filetime;
142 case CURLINFO_HEADER_SIZE:
143 *param_longp = data->info.header_size;
145 case CURLINFO_REQUEST_SIZE:
146 *param_longp = data->info.request_size;
148 case CURLINFO_SSL_VERIFYRESULT:
149 *param_longp = data->set.ssl.certverifyresult;
151 case CURLINFO_REDIRECT_COUNT:
152 *param_longp = data->set.followlocation;
154 case CURLINFO_HTTPAUTH_AVAIL:
155 lptr.to_long = param_longp;
156 *lptr.to_ulong = data->info.httpauthavail;
158 case CURLINFO_PROXYAUTH_AVAIL:
159 lptr.to_long = param_longp;
160 *lptr.to_ulong = data->info.proxyauthavail;
162 case CURLINFO_OS_ERRNO:
163 *param_longp = data->state.os_errno;
165 case CURLINFO_NUM_CONNECTS:
166 *param_longp = data->info.numconnects;
168 case CURLINFO_LASTSOCKET:
169 sockfd = Curl_getconnectinfo(data, NULL);
171 /* note: this is not a good conversion for systems with 64 bit sockets and
173 if(sockfd != CURL_SOCKET_BAD)
174 *param_longp = (long)sockfd;
176 /* this interface is documented to return -1 in case of badness, which
177 may not be the same as the CURL_SOCKET_BAD value */
180 case CURLINFO_PRIMARY_PORT:
181 /* Return the (remote) port of the most recent (primary) connection */
182 *param_longp = data->info.conn_primary_port;
184 case CURLINFO_LOCAL_PORT:
185 /* Return the local port of the most recent (primary) connection */
186 *param_longp = data->info.conn_local_port;
188 case CURLINFO_CONDITION_UNMET:
189 /* return if the condition prevented the document to get transferred */
190 *param_longp = data->info.timecond ? 1L : 0L;
192 case CURLINFO_RTSP_CLIENT_CSEQ:
193 *param_longp = data->state.rtsp_next_client_CSeq;
195 case CURLINFO_RTSP_SERVER_CSEQ:
196 *param_longp = data->state.rtsp_next_server_CSeq;
198 case CURLINFO_RTSP_CSEQ_RECV:
199 *param_longp = data->state.rtsp_CSeq_recv;
203 return CURLE_BAD_FUNCTION_ARGUMENT;
208 static CURLcode getinfo_double(struct SessionHandle *data, CURLINFO info,
209 double *param_doublep)
212 case CURLINFO_TOTAL_TIME:
213 *param_doublep = data->progress.timespent;
215 case CURLINFO_NAMELOOKUP_TIME:
216 *param_doublep = data->progress.t_nslookup;
218 case CURLINFO_CONNECT_TIME:
219 *param_doublep = data->progress.t_connect;
221 case CURLINFO_APPCONNECT_TIME:
222 *param_doublep = data->progress.t_appconnect;
224 case CURLINFO_PRETRANSFER_TIME:
225 *param_doublep = data->progress.t_pretransfer;
227 case CURLINFO_STARTTRANSFER_TIME:
228 *param_doublep = data->progress.t_starttransfer;
230 case CURLINFO_SIZE_UPLOAD:
231 *param_doublep = (double)data->progress.uploaded;
233 case CURLINFO_SIZE_DOWNLOAD:
234 *param_doublep = (double)data->progress.downloaded;
236 case CURLINFO_SPEED_DOWNLOAD:
237 *param_doublep = (double)data->progress.dlspeed;
239 case CURLINFO_SPEED_UPLOAD:
240 *param_doublep = (double)data->progress.ulspeed;
242 case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
243 *param_doublep = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
244 (double)data->progress.size_dl:-1;
246 case CURLINFO_CONTENT_LENGTH_UPLOAD:
247 *param_doublep = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
248 (double)data->progress.size_ul:-1;
250 case CURLINFO_REDIRECT_TIME:
251 *param_doublep = data->progress.t_redirect;
255 return CURLE_BAD_FUNCTION_ARGUMENT;
260 static CURLcode getinfo_slist(struct SessionHandle *data, CURLINFO info,
261 struct curl_slist **param_slistp)
264 struct curl_certinfo * to_certinfo;
265 struct curl_slist * to_slist;
269 case CURLINFO_SSL_ENGINES:
270 *param_slistp = Curl_ssl_engines_list(data);
272 case CURLINFO_COOKIELIST:
273 *param_slistp = Curl_cookie_list(data);
275 case CURLINFO_CERTINFO:
276 /* Return the a pointer to the certinfo struct. Not really an slist
277 pointer but we can pretend it is here */
278 ptr.to_certinfo = &data->info.certs;
279 *param_slistp = ptr.to_slist;
281 case CURLINFO_TLS_SESSION:
283 struct curl_tlssessioninfo **tsip = (struct curl_tlssessioninfo **)
285 struct curl_tlssessioninfo *tsi = &data->tsi;
286 struct connectdata *conn = data->easy_conn;
287 unsigned int sockindex = 0;
290 tsi->backend = CURLSSLBACKEND_NONE;
291 tsi->internals = NULL;
296 /* Find the active ("in use") SSL connection, if any */
297 while((sockindex < sizeof(conn->ssl) / sizeof(conn->ssl[0])) &&
298 (!conn->ssl[sockindex].use))
301 if(sockindex == sizeof(conn->ssl) / sizeof(conn->ssl[0]))
302 break; /* no SSL session found */
304 /* Return the TLS session information from the relevant backend */
306 tsi->backend = CURLSSLBACKEND_OPENSSL;
307 tsi->internals = conn->ssl[sockindex].ctx;
310 tsi->backend = CURLSSLBACKEND_GNUTLS;
311 tsi->internals = conn->ssl[sockindex].session;
314 tsi->backend = CURLSSLBACKEND_NSS;
315 tsi->internals = conn->ssl[sockindex].handle;
318 tsi->backend = CURLSSLBACKEND_QSOSSL;
319 tsi->internals = conn->ssl[sockindex].handle;
322 tsi->backend = CURLSSLBACKEND_GSKIT;
323 tsi->internals = conn->ssl[sockindex].handle;
325 /* NOTE: For other SSL backends, it is not immediately clear what data
326 to return from 'struct ssl_connect_data'; thus, for now we keep the
327 backend as CURLSSLBACKEND_NONE in those cases, which should be
328 interpreted as "not supported" */
332 return CURLE_BAD_FUNCTION_ARGUMENT;
337 CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
340 long *param_longp=NULL;
341 double *param_doublep=NULL;
342 char **param_charp=NULL;
343 struct curl_slist **param_slistp=NULL;
345 /* default return code is to error out! */
346 CURLcode ret = CURLE_BAD_FUNCTION_ARGUMENT;
353 type = CURLINFO_TYPEMASK & (int)info;
355 case CURLINFO_STRING:
356 param_charp = va_arg(arg, char **);
357 if(NULL != param_charp)
358 ret = getinfo_char(data, info, param_charp);
361 param_longp = va_arg(arg, long *);
362 if(NULL != param_longp)
363 ret = getinfo_long(data, info, param_longp);
365 case CURLINFO_DOUBLE:
366 param_doublep = va_arg(arg, double *);
367 if(NULL != param_doublep)
368 ret = getinfo_double(data, info, param_doublep);
371 param_slistp = va_arg(arg, struct curl_slist **);
372 if(NULL != param_slistp)
373 ret = getinfo_slist(data, info, param_slistp);