version.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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.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 "vtls/vtls.h"
  26. #include "http2.h"
  27. #include "vssh/ssh.h"
  28. #include "quic.h"
  29. #include "curl_printf.h"
  30. #ifdef USE_ARES
  31. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  32. defined(WIN32)
  33. # define CARES_STATICLIB
  34. # endif
  35. # include <ares.h>
  36. #endif
  37. #ifdef USE_LIBIDN2
  38. #include <idn2.h>
  39. #endif
  40. #ifdef USE_LIBPSL
  41. #include <libpsl.h>
  42. #endif
  43. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  44. #include <iconv.h>
  45. #endif
  46. #ifdef USE_LIBRTMP
  47. #include <librtmp/rtmp.h>
  48. #endif
  49. #ifdef HAVE_ZLIB_H
  50. #include <zlib.h>
  51. #endif
  52. #ifdef HAVE_BROTLI
  53. #include <brotli/decode.h>
  54. #endif
  55. #ifdef HAVE_ZSTD
  56. #include <zstd.h>
  57. #endif
  58. #ifdef HAVE_BROTLI
  59. static size_t brotli_version(char *buf, size_t bufsz)
  60. {
  61. uint32_t brotli_version = BrotliDecoderVersion();
  62. unsigned int major = brotli_version >> 24;
  63. unsigned int minor = (brotli_version & 0x00FFFFFF) >> 12;
  64. unsigned int patch = brotli_version & 0x00000FFF;
  65. return msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
  66. }
  67. #endif
  68. #ifdef HAVE_ZSTD
  69. static size_t zstd_version(char *buf, size_t bufsz)
  70. {
  71. unsigned long zstd_version = (unsigned long)ZSTD_versionNumber();
  72. unsigned int major = (unsigned int)(zstd_version / (100 * 100));
  73. unsigned int minor = (unsigned int)((zstd_version -
  74. (major * 100 * 100)) / 100);
  75. unsigned int patch = (unsigned int)(zstd_version -
  76. (major * 100 * 100) - (minor * 100));
  77. return msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
  78. }
  79. #endif
  80. /*
  81. * curl_version() returns a pointer to a static buffer.
  82. *
  83. * It is implemented to work multi-threaded by making sure repeated invokes
  84. * generate the exact same string and never write any temporary data like
  85. * zeros in the data.
  86. */
  87. #define VERSION_PARTS 15 /* number of substrings we can concatenate */
  88. char *curl_version(void)
  89. {
  90. static char out[300];
  91. char *outp;
  92. size_t outlen;
  93. const char *src[VERSION_PARTS];
  94. #ifdef USE_SSL
  95. char ssl_version[200];
  96. #endif
  97. #ifdef HAVE_LIBZ
  98. char z_version[40];
  99. #endif
  100. #ifdef HAVE_BROTLI
  101. char br_version[40] = "brotli/";
  102. #endif
  103. #ifdef HAVE_ZSTD
  104. char zst_version[40] = "zstd/";
  105. #endif
  106. #ifdef USE_ARES
  107. char cares_version[40];
  108. #endif
  109. #if defined(USE_LIBIDN2)
  110. char idn_version[40];
  111. #endif
  112. #ifdef USE_LIBPSL
  113. char psl_version[40];
  114. #endif
  115. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  116. char iconv_version[40]="iconv";
  117. #endif
  118. #ifdef USE_SSH
  119. char ssh_version[40];
  120. #endif
  121. #ifdef USE_NGHTTP2
  122. char h2_version[40];
  123. #endif
  124. #ifdef ENABLE_QUIC
  125. char h3_version[40];
  126. #endif
  127. #ifdef USE_LIBRTMP
  128. char rtmp_version[40];
  129. #endif
  130. #ifdef USE_HYPER
  131. char hyper_buf[30];
  132. #endif
  133. int i = 0;
  134. int j;
  135. #ifdef DEBUGBUILD
  136. /* Override version string when environment variable CURL_VERSION is set */
  137. const char *debugversion = getenv("CURL_VERSION");
  138. if(debugversion) {
  139. strncpy(out, debugversion, sizeof(out)-1);
  140. out[sizeof(out)-1] = '\0';
  141. return out;
  142. }
  143. #endif
  144. src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
  145. #ifdef USE_SSL
  146. Curl_ssl_version(ssl_version, sizeof(ssl_version));
  147. src[i++] = ssl_version;
  148. #endif
  149. #ifdef HAVE_LIBZ
  150. msnprintf(z_version, sizeof(z_version), "zlib/%s", zlibVersion());
  151. src[i++] = z_version;
  152. #endif
  153. #ifdef HAVE_BROTLI
  154. brotli_version(&br_version[7], sizeof(br_version) - 7);
  155. src[i++] = br_version;
  156. #endif
  157. #ifdef HAVE_ZSTD
  158. zstd_version(&zst_version[5], sizeof(zst_version) - 5);
  159. src[i++] = zst_version;
  160. #endif
  161. #ifdef USE_ARES
  162. msnprintf(cares_version, sizeof(cares_version),
  163. "c-ares/%s", ares_version(NULL));
  164. src[i++] = cares_version;
  165. #endif
  166. #ifdef USE_LIBIDN2
  167. msnprintf(idn_version, sizeof(idn_version),
  168. "libidn2/%s", idn2_check_version(NULL));
  169. src[i++] = idn_version;
  170. #elif defined(USE_WIN32_IDN)
  171. src[i++] = (char *)"WinIDN";
  172. #endif
  173. #ifdef USE_LIBPSL
  174. msnprintf(psl_version, sizeof(psl_version), "libpsl/%s", psl_get_version());
  175. src[i++] = psl_version;
  176. #endif
  177. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  178. #ifdef _LIBICONV_VERSION
  179. msnprintf(iconv_version, sizeof(iconv_version), "iconv/%d.%d",
  180. _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
  181. #else
  182. /* version unknown, let the default stand */
  183. #endif /* _LIBICONV_VERSION */
  184. src[i++] = iconv_version;
  185. #endif
  186. #ifdef USE_SSH
  187. Curl_ssh_version(ssh_version, sizeof(ssh_version));
  188. src[i++] = ssh_version;
  189. #endif
  190. #ifdef USE_NGHTTP2
  191. Curl_http2_ver(h2_version, sizeof(h2_version));
  192. src[i++] = h2_version;
  193. #endif
  194. #ifdef ENABLE_QUIC
  195. Curl_quic_ver(h3_version, sizeof(h3_version));
  196. src[i++] = h3_version;
  197. #endif
  198. #ifdef USE_LIBRTMP
  199. {
  200. char suff[2];
  201. if(RTMP_LIB_VERSION & 0xff) {
  202. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  203. suff[1] = '\0';
  204. }
  205. else
  206. suff[0] = '\0';
  207. msnprintf(rtmp_version, sizeof(rtmp_version), "librtmp/%d.%d%s",
  208. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  209. suff);
  210. src[i++] = rtmp_version;
  211. }
  212. #endif
  213. #ifdef USE_HYPER
  214. msnprintf(hyper_buf, sizeof(hyper_buf), "Hyper/%s", hyper_version());
  215. src[i++] = hyper_buf;
  216. #endif
  217. DEBUGASSERT(i <= VERSION_PARTS);
  218. outp = &out[0];
  219. outlen = sizeof(out);
  220. for(j = 0; j < i; j++) {
  221. size_t n = strlen(src[j]);
  222. /* we need room for a space, the string and the final zero */
  223. if(outlen <= (n + 2))
  224. break;
  225. if(j) {
  226. /* prepend a space if not the first */
  227. *outp++ = ' ';
  228. outlen--;
  229. }
  230. memcpy(outp, src[j], n);
  231. outp += n;
  232. outlen -= n;
  233. }
  234. *outp = 0;
  235. return out;
  236. }
  237. /* data for curl_version_info
  238. Keep the list sorted alphabetically. It is also written so that each
  239. protocol line has its own #if line to make things easier on the eye.
  240. */
  241. static const char * const protocols[] = {
  242. #ifndef CURL_DISABLE_DICT
  243. "dict",
  244. #endif
  245. #ifndef CURL_DISABLE_FILE
  246. "file",
  247. #endif
  248. #ifndef CURL_DISABLE_FTP
  249. "ftp",
  250. #endif
  251. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  252. "ftps",
  253. #endif
  254. #ifndef CURL_DISABLE_GOPHER
  255. "gopher",
  256. #endif
  257. #if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)
  258. "gophers",
  259. #endif
  260. #ifndef CURL_DISABLE_HTTP
  261. "http",
  262. #endif
  263. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  264. "https",
  265. #endif
  266. #ifndef CURL_DISABLE_IMAP
  267. "imap",
  268. #endif
  269. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  270. "imaps",
  271. #endif
  272. #ifndef CURL_DISABLE_LDAP
  273. "ldap",
  274. #if !defined(CURL_DISABLE_LDAPS) && \
  275. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  276. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  277. "ldaps",
  278. #endif
  279. #endif
  280. #ifndef CURL_DISABLE_MQTT
  281. "mqtt",
  282. #endif
  283. #ifndef CURL_DISABLE_POP3
  284. "pop3",
  285. #endif
  286. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  287. "pop3s",
  288. #endif
  289. #ifdef USE_LIBRTMP
  290. "rtmp",
  291. #endif
  292. #ifndef CURL_DISABLE_RTSP
  293. "rtsp",
  294. #endif
  295. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  296. "scp",
  297. #endif
  298. #ifdef USE_SSH
  299. "sftp",
  300. #endif
  301. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
  302. (CURL_SIZEOF_CURL_OFF_T > 4)
  303. "smb",
  304. # ifdef USE_SSL
  305. "smbs",
  306. # endif
  307. #endif
  308. #ifndef CURL_DISABLE_SMTP
  309. "smtp",
  310. #endif
  311. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  312. "smtps",
  313. #endif
  314. #ifndef CURL_DISABLE_TELNET
  315. "telnet",
  316. #endif
  317. #ifndef CURL_DISABLE_TFTP
  318. "tftp",
  319. #endif
  320. NULL
  321. };
  322. static curl_version_info_data version_info = {
  323. CURLVERSION_NOW,
  324. LIBCURL_VERSION,
  325. LIBCURL_VERSION_NUM,
  326. OS, /* as found by configure or set by hand at build-time */
  327. 0 /* features is 0 by default */
  328. #ifdef ENABLE_IPV6
  329. | CURL_VERSION_IPV6
  330. #endif
  331. #ifdef USE_SSL
  332. | CURL_VERSION_SSL
  333. #endif
  334. #ifdef USE_NTLM
  335. | CURL_VERSION_NTLM
  336. #endif
  337. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  338. defined(NTLM_WB_ENABLED)
  339. | CURL_VERSION_NTLM_WB
  340. #endif
  341. #ifdef USE_SPNEGO
  342. | CURL_VERSION_SPNEGO
  343. #endif
  344. #ifdef USE_KERBEROS5
  345. | CURL_VERSION_KERBEROS5
  346. #endif
  347. #ifdef HAVE_GSSAPI
  348. | CURL_VERSION_GSSAPI
  349. #endif
  350. #ifdef USE_WINDOWS_SSPI
  351. | CURL_VERSION_SSPI
  352. #endif
  353. #ifdef HAVE_LIBZ
  354. | CURL_VERSION_LIBZ
  355. #endif
  356. #ifdef DEBUGBUILD
  357. | CURL_VERSION_DEBUG
  358. #endif
  359. #ifdef CURLDEBUG
  360. | CURL_VERSION_CURLDEBUG
  361. #endif
  362. #ifdef CURLRES_ASYNCH
  363. | CURL_VERSION_ASYNCHDNS
  364. #endif
  365. #if (CURL_SIZEOF_CURL_OFF_T > 4) && \
  366. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  367. | CURL_VERSION_LARGEFILE
  368. #endif
  369. #if defined(WIN32) && defined(UNICODE) && defined(_UNICODE)
  370. | CURL_VERSION_UNICODE
  371. #endif
  372. #if defined(CURL_DOES_CONVERSIONS)
  373. | CURL_VERSION_CONV
  374. #endif
  375. #if defined(USE_TLS_SRP)
  376. | CURL_VERSION_TLSAUTH_SRP
  377. #endif
  378. #if defined(USE_NGHTTP2) || defined(USE_HYPER)
  379. | CURL_VERSION_HTTP2
  380. #endif
  381. #if defined(ENABLE_QUIC)
  382. | CURL_VERSION_HTTP3
  383. #endif
  384. #if defined(USE_UNIX_SOCKETS)
  385. | CURL_VERSION_UNIX_SOCKETS
  386. #endif
  387. #if defined(USE_LIBPSL)
  388. | CURL_VERSION_PSL
  389. #endif
  390. #if defined(CURL_WITH_MULTI_SSL)
  391. | CURL_VERSION_MULTI_SSL
  392. #endif
  393. #if defined(HAVE_BROTLI)
  394. | CURL_VERSION_BROTLI
  395. #endif
  396. #if defined(HAVE_ZSTD)
  397. | CURL_VERSION_ZSTD
  398. #endif
  399. #ifndef CURL_DISABLE_ALTSVC
  400. | CURL_VERSION_ALTSVC
  401. #endif
  402. #if defined(USE_HSTS)
  403. | CURL_VERSION_HSTS
  404. #endif
  405. ,
  406. NULL, /* ssl_version */
  407. 0, /* ssl_version_num, this is kept at zero */
  408. NULL, /* zlib_version */
  409. protocols,
  410. NULL, /* c-ares version */
  411. 0, /* c-ares version numerical */
  412. NULL, /* libidn version */
  413. 0, /* iconv version */
  414. NULL, /* ssh lib version */
  415. 0, /* brotli_ver_num */
  416. NULL, /* brotli version */
  417. 0, /* nghttp2 version number */
  418. NULL, /* nghttp2 version string */
  419. NULL, /* quic library string */
  420. #ifdef CURL_CA_BUNDLE
  421. CURL_CA_BUNDLE, /* cainfo */
  422. #else
  423. NULL,
  424. #endif
  425. #ifdef CURL_CA_PATH
  426. CURL_CA_PATH, /* capath */
  427. #else
  428. NULL,
  429. #endif
  430. 0, /* zstd_ver_num */
  431. NULL, /* zstd version */
  432. NULL /* Hyper version */
  433. };
  434. curl_version_info_data *curl_version_info(CURLversion stamp)
  435. {
  436. #if defined(USE_SSH)
  437. static char ssh_buffer[80];
  438. #endif
  439. #ifdef USE_SSL
  440. #ifdef CURL_WITH_MULTI_SSL
  441. static char ssl_buffer[200];
  442. #else
  443. static char ssl_buffer[80];
  444. #endif
  445. #endif
  446. #ifdef HAVE_BROTLI
  447. static char brotli_buffer[80];
  448. #endif
  449. #ifdef HAVE_ZSTD
  450. static char zstd_buffer[80];
  451. #endif
  452. #ifdef USE_SSL
  453. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  454. version_info.ssl_version = ssl_buffer;
  455. #ifndef CURL_DISABLE_PROXY
  456. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  457. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  458. else
  459. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  460. #endif
  461. #endif
  462. #ifdef HAVE_LIBZ
  463. version_info.libz_version = zlibVersion();
  464. /* libz left NULL if non-existing */
  465. #endif
  466. #ifdef USE_ARES
  467. {
  468. int aresnum;
  469. version_info.ares = ares_version(&aresnum);
  470. version_info.ares_num = aresnum;
  471. }
  472. #endif
  473. #ifdef USE_LIBIDN2
  474. /* This returns a version string if we use the given version or later,
  475. otherwise it returns NULL */
  476. version_info.libidn = idn2_check_version(IDN2_VERSION);
  477. if(version_info.libidn)
  478. version_info.features |= CURL_VERSION_IDN;
  479. #elif defined(USE_WIN32_IDN)
  480. version_info.features |= CURL_VERSION_IDN;
  481. #endif
  482. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  483. #ifdef _LIBICONV_VERSION
  484. version_info.iconv_ver_num = _LIBICONV_VERSION;
  485. #else
  486. /* version unknown */
  487. version_info.iconv_ver_num = -1;
  488. #endif /* _LIBICONV_VERSION */
  489. #endif
  490. #if defined(USE_SSH)
  491. Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
  492. version_info.libssh_version = ssh_buffer;
  493. #endif
  494. #ifdef HAVE_BROTLI
  495. version_info.brotli_ver_num = BrotliDecoderVersion();
  496. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  497. version_info.brotli_version = brotli_buffer;
  498. #endif
  499. #ifdef HAVE_ZSTD
  500. version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
  501. zstd_version(zstd_buffer, sizeof(zstd_buffer));
  502. version_info.zstd_version = zstd_buffer;
  503. #endif
  504. #ifdef USE_NGHTTP2
  505. {
  506. nghttp2_info *h2 = nghttp2_version(0);
  507. version_info.nghttp2_ver_num = h2->version_num;
  508. version_info.nghttp2_version = h2->version_str;
  509. }
  510. #endif
  511. #ifdef ENABLE_QUIC
  512. {
  513. static char quicbuffer[80];
  514. Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
  515. version_info.quic_version = quicbuffer;
  516. }
  517. #endif
  518. #ifdef USE_HYPER
  519. {
  520. static char hyper_buffer[30];
  521. msnprintf(hyper_buffer, sizeof(hyper_buffer), "Hyper/%s", hyper_version());
  522. version_info.hyper_version = hyper_buffer;
  523. }
  524. #endif
  525. (void)stamp; /* avoid compiler warnings, we don't use this */
  526. return &version_info;
  527. }