getinfo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "getinfo.h"
  26. #include "vtls/vtls.h"
  27. #include "connect.h" /* Curl_getconnectinfo() */
  28. #include "progress.h"
  29. /* The last #include files should be: */
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /*
  33. * Initialize statistical and informational data.
  34. *
  35. * This function is called in curl_easy_reset, curl_easy_duphandle and at the
  36. * beginning of a perform session. It must reset the session-info variables,
  37. * in particular all variables in struct PureInfo.
  38. */
  39. CURLcode Curl_initinfo(struct Curl_easy *data)
  40. {
  41. struct Progress *pro = &data->progress;
  42. struct PureInfo *info = &data->info;
  43. pro->t_nslookup = 0;
  44. pro->t_connect = 0;
  45. pro->t_appconnect = 0;
  46. pro->t_pretransfer = 0;
  47. pro->t_starttransfer = 0;
  48. pro->timespent = 0;
  49. pro->t_redirect = 0;
  50. pro->is_t_startransfer_set = false;
  51. info->httpcode = 0;
  52. info->httpproxycode = 0;
  53. info->httpversion = 0;
  54. info->filetime = -1; /* -1 is an illegal time and thus means unknown */
  55. info->timecond = FALSE;
  56. info->header_size = 0;
  57. info->request_size = 0;
  58. info->proxyauthavail = 0;
  59. info->httpauthavail = 0;
  60. info->numconnects = 0;
  61. free(info->contenttype);
  62. info->contenttype = NULL;
  63. free(info->wouldredirect);
  64. info->wouldredirect = NULL;
  65. info->conn_primary_ip[0] = '\0';
  66. info->conn_local_ip[0] = '\0';
  67. info->conn_primary_port = 0;
  68. info->conn_local_port = 0;
  69. info->conn_scheme = 0;
  70. info->conn_protocol = 0;
  71. #ifdef USE_SSL
  72. Curl_ssl_free_certinfo(data);
  73. #endif
  74. return CURLE_OK;
  75. }
  76. static CURLcode getinfo_char(struct Curl_easy *data, CURLINFO info,
  77. const char **param_charp)
  78. {
  79. switch(info) {
  80. case CURLINFO_EFFECTIVE_URL:
  81. *param_charp = data->change.url?data->change.url:(char *)"";
  82. break;
  83. case CURLINFO_CONTENT_TYPE:
  84. *param_charp = data->info.contenttype;
  85. break;
  86. case CURLINFO_PRIVATE:
  87. *param_charp = (char *) data->set.private_data;
  88. break;
  89. case CURLINFO_FTP_ENTRY_PATH:
  90. /* Return the entrypath string from the most recent connection.
  91. This pointer was copied from the connectdata structure by FTP.
  92. The actual string may be free()ed by subsequent libcurl calls so
  93. it must be copied to a safer area before the next libcurl call.
  94. Callers must never free it themselves. */
  95. *param_charp = data->state.most_recent_ftp_entrypath;
  96. break;
  97. case CURLINFO_REDIRECT_URL:
  98. /* Return the URL this request would have been redirected to if that
  99. option had been enabled! */
  100. *param_charp = data->info.wouldredirect;
  101. break;
  102. case CURLINFO_PRIMARY_IP:
  103. /* Return the ip address of the most recent (primary) connection */
  104. *param_charp = data->info.conn_primary_ip;
  105. break;
  106. case CURLINFO_LOCAL_IP:
  107. /* Return the source/local ip address of the most recent (primary)
  108. connection */
  109. *param_charp = data->info.conn_local_ip;
  110. break;
  111. case CURLINFO_RTSP_SESSION_ID:
  112. *param_charp = data->set.str[STRING_RTSP_SESSION_ID];
  113. break;
  114. case CURLINFO_SCHEME:
  115. *param_charp = data->info.conn_scheme;
  116. break;
  117. default:
  118. return CURLE_UNKNOWN_OPTION;
  119. }
  120. return CURLE_OK;
  121. }
  122. static CURLcode getinfo_long(struct Curl_easy *data, CURLINFO info,
  123. long *param_longp)
  124. {
  125. curl_socket_t sockfd;
  126. union {
  127. unsigned long *to_ulong;
  128. long *to_long;
  129. } lptr;
  130. #ifdef DEBUGBUILD
  131. char *timestr = getenv("CURL_TIME");
  132. if(timestr) {
  133. unsigned long val = strtol(timestr, NULL, 10);
  134. switch(info) {
  135. case CURLINFO_LOCAL_PORT:
  136. *param_longp = (long)val;
  137. return CURLE_OK;
  138. default:
  139. break;
  140. }
  141. }
  142. /* use another variable for this to allow different values */
  143. timestr = getenv("CURL_DEBUG_SIZE");
  144. if(timestr) {
  145. unsigned long val = strtol(timestr, NULL, 10);
  146. switch(info) {
  147. case CURLINFO_HEADER_SIZE:
  148. case CURLINFO_REQUEST_SIZE:
  149. *param_longp = (long)val;
  150. return CURLE_OK;
  151. default:
  152. break;
  153. }
  154. }
  155. #endif
  156. switch(info) {
  157. case CURLINFO_RESPONSE_CODE:
  158. *param_longp = data->info.httpcode;
  159. break;
  160. case CURLINFO_HTTP_CONNECTCODE:
  161. *param_longp = data->info.httpproxycode;
  162. break;
  163. case CURLINFO_FILETIME:
  164. if(data->info.filetime > LONG_MAX)
  165. *param_longp = LONG_MAX;
  166. else if(data->info.filetime < LONG_MIN)
  167. *param_longp = LONG_MIN;
  168. else
  169. *param_longp = (long)data->info.filetime;
  170. break;
  171. case CURLINFO_HEADER_SIZE:
  172. *param_longp = (long)data->info.header_size;
  173. break;
  174. case CURLINFO_REQUEST_SIZE:
  175. *param_longp = (long)data->info.request_size;
  176. break;
  177. case CURLINFO_SSL_VERIFYRESULT:
  178. *param_longp = data->set.ssl.certverifyresult;
  179. break;
  180. case CURLINFO_PROXY_SSL_VERIFYRESULT:
  181. *param_longp = data->set.proxy_ssl.certverifyresult;
  182. break;
  183. case CURLINFO_REDIRECT_COUNT:
  184. *param_longp = data->set.followlocation;
  185. break;
  186. case CURLINFO_HTTPAUTH_AVAIL:
  187. lptr.to_long = param_longp;
  188. *lptr.to_ulong = data->info.httpauthavail;
  189. break;
  190. case CURLINFO_PROXYAUTH_AVAIL:
  191. lptr.to_long = param_longp;
  192. *lptr.to_ulong = data->info.proxyauthavail;
  193. break;
  194. case CURLINFO_OS_ERRNO:
  195. *param_longp = data->state.os_errno;
  196. break;
  197. case CURLINFO_NUM_CONNECTS:
  198. *param_longp = data->info.numconnects;
  199. break;
  200. case CURLINFO_LASTSOCKET:
  201. sockfd = Curl_getconnectinfo(data, NULL);
  202. /* note: this is not a good conversion for systems with 64 bit sockets and
  203. 32 bit longs */
  204. if(sockfd != CURL_SOCKET_BAD)
  205. *param_longp = (long)sockfd;
  206. else
  207. /* this interface is documented to return -1 in case of badness, which
  208. may not be the same as the CURL_SOCKET_BAD value */
  209. *param_longp = -1;
  210. break;
  211. case CURLINFO_PRIMARY_PORT:
  212. /* Return the (remote) port of the most recent (primary) connection */
  213. *param_longp = data->info.conn_primary_port;
  214. break;
  215. case CURLINFO_LOCAL_PORT:
  216. /* Return the local port of the most recent (primary) connection */
  217. *param_longp = data->info.conn_local_port;
  218. break;
  219. case CURLINFO_CONDITION_UNMET:
  220. if(data->info.httpcode == 304)
  221. *param_longp = 1L;
  222. else
  223. /* return if the condition prevented the document to get transferred */
  224. *param_longp = data->info.timecond ? 1L : 0L;
  225. break;
  226. case CURLINFO_RTSP_CLIENT_CSEQ:
  227. *param_longp = data->state.rtsp_next_client_CSeq;
  228. break;
  229. case CURLINFO_RTSP_SERVER_CSEQ:
  230. *param_longp = data->state.rtsp_next_server_CSeq;
  231. break;
  232. case CURLINFO_RTSP_CSEQ_RECV:
  233. *param_longp = data->state.rtsp_CSeq_recv;
  234. break;
  235. case CURLINFO_HTTP_VERSION:
  236. switch(data->info.httpversion) {
  237. case 10:
  238. *param_longp = CURL_HTTP_VERSION_1_0;
  239. break;
  240. case 11:
  241. *param_longp = CURL_HTTP_VERSION_1_1;
  242. break;
  243. case 20:
  244. *param_longp = CURL_HTTP_VERSION_2_0;
  245. break;
  246. case 30:
  247. *param_longp = CURL_HTTP_VERSION_3;
  248. break;
  249. default:
  250. *param_longp = CURL_HTTP_VERSION_NONE;
  251. break;
  252. }
  253. break;
  254. case CURLINFO_PROTOCOL:
  255. *param_longp = data->info.conn_protocol;
  256. break;
  257. default:
  258. return CURLE_UNKNOWN_OPTION;
  259. }
  260. return CURLE_OK;
  261. }
  262. #define DOUBLE_SECS(x) (double)(x)/1000000
  263. static CURLcode getinfo_offt(struct Curl_easy *data, CURLINFO info,
  264. curl_off_t *param_offt)
  265. {
  266. #ifdef DEBUGBUILD
  267. char *timestr = getenv("CURL_TIME");
  268. if(timestr) {
  269. unsigned long val = strtol(timestr, NULL, 10);
  270. switch(info) {
  271. case CURLINFO_TOTAL_TIME_T:
  272. case CURLINFO_NAMELOOKUP_TIME_T:
  273. case CURLINFO_CONNECT_TIME_T:
  274. case CURLINFO_APPCONNECT_TIME_T:
  275. case CURLINFO_PRETRANSFER_TIME_T:
  276. case CURLINFO_STARTTRANSFER_TIME_T:
  277. case CURLINFO_REDIRECT_TIME_T:
  278. case CURLINFO_SPEED_DOWNLOAD_T:
  279. case CURLINFO_SPEED_UPLOAD_T:
  280. *param_offt = (curl_off_t)val;
  281. return CURLE_OK;
  282. default:
  283. break;
  284. }
  285. }
  286. #endif
  287. switch(info) {
  288. case CURLINFO_FILETIME_T:
  289. *param_offt = (curl_off_t)data->info.filetime;
  290. break;
  291. case CURLINFO_SIZE_UPLOAD_T:
  292. *param_offt = data->progress.uploaded;
  293. break;
  294. case CURLINFO_SIZE_DOWNLOAD_T:
  295. *param_offt = data->progress.downloaded;
  296. break;
  297. case CURLINFO_SPEED_DOWNLOAD_T:
  298. *param_offt = data->progress.dlspeed;
  299. break;
  300. case CURLINFO_SPEED_UPLOAD_T:
  301. *param_offt = data->progress.ulspeed;
  302. break;
  303. case CURLINFO_CONTENT_LENGTH_DOWNLOAD_T:
  304. *param_offt = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  305. data->progress.size_dl:-1;
  306. break;
  307. case CURLINFO_CONTENT_LENGTH_UPLOAD_T:
  308. *param_offt = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  309. data->progress.size_ul:-1;
  310. break;
  311. case CURLINFO_TOTAL_TIME_T:
  312. *param_offt = data->progress.timespent;
  313. break;
  314. case CURLINFO_NAMELOOKUP_TIME_T:
  315. *param_offt = data->progress.t_nslookup;
  316. break;
  317. case CURLINFO_CONNECT_TIME_T:
  318. *param_offt = data->progress.t_connect;
  319. break;
  320. case CURLINFO_APPCONNECT_TIME_T:
  321. *param_offt = data->progress.t_appconnect;
  322. break;
  323. case CURLINFO_PRETRANSFER_TIME_T:
  324. *param_offt = data->progress.t_pretransfer;
  325. break;
  326. case CURLINFO_STARTTRANSFER_TIME_T:
  327. *param_offt = data->progress.t_starttransfer;
  328. break;
  329. case CURLINFO_REDIRECT_TIME_T:
  330. *param_offt = data->progress.t_redirect;
  331. break;
  332. case CURLINFO_RETRY_AFTER:
  333. *param_offt = data->info.retry_after;
  334. break;
  335. default:
  336. return CURLE_UNKNOWN_OPTION;
  337. }
  338. return CURLE_OK;
  339. }
  340. static CURLcode getinfo_double(struct Curl_easy *data, CURLINFO info,
  341. double *param_doublep)
  342. {
  343. #ifdef DEBUGBUILD
  344. char *timestr = getenv("CURL_TIME");
  345. if(timestr) {
  346. unsigned long val = strtol(timestr, NULL, 10);
  347. switch(info) {
  348. case CURLINFO_TOTAL_TIME:
  349. case CURLINFO_NAMELOOKUP_TIME:
  350. case CURLINFO_CONNECT_TIME:
  351. case CURLINFO_APPCONNECT_TIME:
  352. case CURLINFO_PRETRANSFER_TIME:
  353. case CURLINFO_STARTTRANSFER_TIME:
  354. case CURLINFO_REDIRECT_TIME:
  355. case CURLINFO_SPEED_DOWNLOAD:
  356. case CURLINFO_SPEED_UPLOAD:
  357. *param_doublep = (double)val;
  358. return CURLE_OK;
  359. default:
  360. break;
  361. }
  362. }
  363. #endif
  364. switch(info) {
  365. case CURLINFO_TOTAL_TIME:
  366. *param_doublep = DOUBLE_SECS(data->progress.timespent);
  367. break;
  368. case CURLINFO_NAMELOOKUP_TIME:
  369. *param_doublep = DOUBLE_SECS(data->progress.t_nslookup);
  370. break;
  371. case CURLINFO_CONNECT_TIME:
  372. *param_doublep = DOUBLE_SECS(data->progress.t_connect);
  373. break;
  374. case CURLINFO_APPCONNECT_TIME:
  375. *param_doublep = DOUBLE_SECS(data->progress.t_appconnect);
  376. break;
  377. case CURLINFO_PRETRANSFER_TIME:
  378. *param_doublep = DOUBLE_SECS(data->progress.t_pretransfer);
  379. break;
  380. case CURLINFO_STARTTRANSFER_TIME:
  381. *param_doublep = DOUBLE_SECS(data->progress.t_starttransfer);
  382. break;
  383. case CURLINFO_SIZE_UPLOAD:
  384. *param_doublep = (double)data->progress.uploaded;
  385. break;
  386. case CURLINFO_SIZE_DOWNLOAD:
  387. *param_doublep = (double)data->progress.downloaded;
  388. break;
  389. case CURLINFO_SPEED_DOWNLOAD:
  390. *param_doublep = (double)data->progress.dlspeed;
  391. break;
  392. case CURLINFO_SPEED_UPLOAD:
  393. *param_doublep = (double)data->progress.ulspeed;
  394. break;
  395. case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
  396. *param_doublep = (data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  397. (double)data->progress.size_dl:-1;
  398. break;
  399. case CURLINFO_CONTENT_LENGTH_UPLOAD:
  400. *param_doublep = (data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  401. (double)data->progress.size_ul:-1;
  402. break;
  403. case CURLINFO_REDIRECT_TIME:
  404. *param_doublep = DOUBLE_SECS(data->progress.t_redirect);
  405. break;
  406. default:
  407. return CURLE_UNKNOWN_OPTION;
  408. }
  409. return CURLE_OK;
  410. }
  411. static CURLcode getinfo_slist(struct Curl_easy *data, CURLINFO info,
  412. struct curl_slist **param_slistp)
  413. {
  414. union {
  415. struct curl_certinfo *to_certinfo;
  416. struct curl_slist *to_slist;
  417. } ptr;
  418. switch(info) {
  419. case CURLINFO_SSL_ENGINES:
  420. *param_slistp = Curl_ssl_engines_list(data);
  421. break;
  422. case CURLINFO_COOKIELIST:
  423. *param_slistp = Curl_cookie_list(data);
  424. break;
  425. case CURLINFO_CERTINFO:
  426. /* Return the a pointer to the certinfo struct. Not really an slist
  427. pointer but we can pretend it is here */
  428. ptr.to_certinfo = &data->info.certs;
  429. *param_slistp = ptr.to_slist;
  430. break;
  431. case CURLINFO_TLS_SESSION:
  432. case CURLINFO_TLS_SSL_PTR:
  433. {
  434. struct curl_tlssessioninfo **tsip = (struct curl_tlssessioninfo **)
  435. param_slistp;
  436. struct curl_tlssessioninfo *tsi = &data->tsi;
  437. #ifdef USE_SSL
  438. struct connectdata *conn = data->conn;
  439. #endif
  440. *tsip = tsi;
  441. tsi->backend = Curl_ssl_backend();
  442. tsi->internals = NULL;
  443. #ifdef USE_SSL
  444. if(conn && tsi->backend != CURLSSLBACKEND_NONE) {
  445. unsigned int i;
  446. for(i = 0; i < (sizeof(conn->ssl) / sizeof(conn->ssl[0])); ++i) {
  447. if(conn->ssl[i].use) {
  448. tsi->internals = Curl_ssl->get_internals(&conn->ssl[i], info);
  449. break;
  450. }
  451. }
  452. }
  453. #endif
  454. }
  455. break;
  456. default:
  457. return CURLE_UNKNOWN_OPTION;
  458. }
  459. return CURLE_OK;
  460. }
  461. static CURLcode getinfo_socket(struct Curl_easy *data, CURLINFO info,
  462. curl_socket_t *param_socketp)
  463. {
  464. switch(info) {
  465. case CURLINFO_ACTIVESOCKET:
  466. *param_socketp = Curl_getconnectinfo(data, NULL);
  467. break;
  468. default:
  469. return CURLE_UNKNOWN_OPTION;
  470. }
  471. return CURLE_OK;
  472. }
  473. CURLcode Curl_getinfo(struct Curl_easy *data, CURLINFO info, ...)
  474. {
  475. va_list arg;
  476. long *param_longp = NULL;
  477. double *param_doublep = NULL;
  478. curl_off_t *param_offt = NULL;
  479. const char **param_charp = NULL;
  480. struct curl_slist **param_slistp = NULL;
  481. curl_socket_t *param_socketp = NULL;
  482. int type;
  483. CURLcode result = CURLE_UNKNOWN_OPTION;
  484. if(!data)
  485. return result;
  486. va_start(arg, info);
  487. type = CURLINFO_TYPEMASK & (int)info;
  488. switch(type) {
  489. case CURLINFO_STRING:
  490. param_charp = va_arg(arg, const char **);
  491. if(param_charp)
  492. result = getinfo_char(data, info, param_charp);
  493. break;
  494. case CURLINFO_LONG:
  495. param_longp = va_arg(arg, long *);
  496. if(param_longp)
  497. result = getinfo_long(data, info, param_longp);
  498. break;
  499. case CURLINFO_DOUBLE:
  500. param_doublep = va_arg(arg, double *);
  501. if(param_doublep)
  502. result = getinfo_double(data, info, param_doublep);
  503. break;
  504. case CURLINFO_OFF_T:
  505. param_offt = va_arg(arg, curl_off_t *);
  506. if(param_offt)
  507. result = getinfo_offt(data, info, param_offt);
  508. break;
  509. case CURLINFO_SLIST:
  510. param_slistp = va_arg(arg, struct curl_slist **);
  511. if(param_slistp)
  512. result = getinfo_slist(data, info, param_slistp);
  513. break;
  514. case CURLINFO_SOCKET:
  515. param_socketp = va_arg(arg, curl_socket_t *);
  516. if(param_socketp)
  517. result = getinfo_socket(data, info, param_socketp);
  518. break;
  519. default:
  520. break;
  521. }
  522. va_end(arg);
  523. return result;
  524. }