version.c 13 KB

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