version.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. void Curl_version_init(void);
  60. /* For thread safety purposes this function is called by global_init so that
  61. the static data in both version functions is initialized. */
  62. void Curl_version_init(void)
  63. {
  64. curl_version();
  65. curl_version_info(CURLVERSION_NOW);
  66. }
  67. #ifdef HAVE_BROTLI
  68. static size_t brotli_version(char *buf, size_t bufsz)
  69. {
  70. uint32_t brotli_version = BrotliDecoderVersion();
  71. unsigned int major = brotli_version >> 24;
  72. unsigned int minor = (brotli_version & 0x00FFFFFF) >> 12;
  73. unsigned int patch = brotli_version & 0x00000FFF;
  74. return msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
  75. }
  76. #endif
  77. char *curl_version(void)
  78. {
  79. static bool initialized;
  80. static char version[250];
  81. char *ptr = version;
  82. size_t len;
  83. size_t left = sizeof(version);
  84. if(initialized)
  85. return version;
  86. strcpy(ptr, LIBCURL_NAME "/" LIBCURL_VERSION);
  87. len = strlen(ptr);
  88. left -= len;
  89. ptr += len;
  90. len = Curl_ssl_version(ptr + 1, left - 1);
  91. if(len > 0) {
  92. *ptr = ' ';
  93. left -= ++len;
  94. ptr += len;
  95. }
  96. #ifdef HAVE_LIBZ
  97. len = msnprintf(ptr, left, " zlib/%s", zlibVersion());
  98. left -= len;
  99. ptr += len;
  100. #endif
  101. #ifdef HAVE_BROTLI
  102. len = msnprintf(ptr, left, "%s", " brotli/");
  103. left -= len;
  104. ptr += len;
  105. len = brotli_version(ptr, left);
  106. left -= len;
  107. ptr += len;
  108. #endif
  109. #ifdef USE_ARES
  110. /* this function is only present in c-ares, not in the original ares */
  111. len = msnprintf(ptr, left, " c-ares/%s", ares_version(NULL));
  112. left -= len;
  113. ptr += len;
  114. #endif
  115. #ifdef USE_LIBIDN2
  116. if(idn2_check_version(IDN2_VERSION)) {
  117. len = msnprintf(ptr, left, " libidn2/%s", idn2_check_version(NULL));
  118. left -= len;
  119. ptr += len;
  120. }
  121. #endif
  122. #ifdef USE_LIBPSL
  123. len = msnprintf(ptr, left, " libpsl/%s", psl_get_version());
  124. left -= len;
  125. ptr += len;
  126. #endif
  127. #ifdef USE_WIN32_IDN
  128. len = msnprintf(ptr, left, " WinIDN");
  129. left -= len;
  130. ptr += len;
  131. #endif
  132. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  133. #ifdef _LIBICONV_VERSION
  134. len = msnprintf(ptr, left, " iconv/%d.%d",
  135. _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
  136. #else
  137. /* version unknown */
  138. len = msnprintf(ptr, left, " iconv");
  139. #endif /* _LIBICONV_VERSION */
  140. left -= len;
  141. ptr += len;
  142. #endif
  143. #ifdef USE_SSH
  144. if(left) {
  145. *ptr++=' ';
  146. left--;
  147. }
  148. len = Curl_ssh_version(ptr, left);
  149. left -= len;
  150. ptr += len;
  151. #endif
  152. #ifdef USE_NGHTTP2
  153. len = Curl_http2_ver(ptr, left);
  154. left -= len;
  155. ptr += len;
  156. #endif
  157. #ifdef ENABLE_QUIC
  158. len = Curl_quic_ver(ptr, left);
  159. left -= len;
  160. ptr += len;
  161. #endif
  162. #ifdef USE_LIBRTMP
  163. {
  164. char suff[2];
  165. if(RTMP_LIB_VERSION & 0xff) {
  166. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  167. suff[1] = '\0';
  168. }
  169. else
  170. suff[0] = '\0';
  171. msnprintf(ptr, left, " librtmp/%d.%d%s",
  172. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  173. suff);
  174. /*
  175. If another lib version is added below this one, this code would
  176. also have to do:
  177. len = what msnprintf() returned
  178. left -= len;
  179. ptr += len;
  180. */
  181. }
  182. #endif
  183. /* Silent scan-build even if librtmp is not enabled. */
  184. (void) left;
  185. (void) ptr;
  186. initialized = true;
  187. return version;
  188. }
  189. /* data for curl_version_info
  190. Keep the list sorted alphabetically. It is also written so that each
  191. protocol line has its own #if line to make things easier on the eye.
  192. */
  193. static const char * const protocols[] = {
  194. #ifndef CURL_DISABLE_DICT
  195. "dict",
  196. #endif
  197. #ifndef CURL_DISABLE_FILE
  198. "file",
  199. #endif
  200. #ifndef CURL_DISABLE_FTP
  201. "ftp",
  202. #endif
  203. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  204. "ftps",
  205. #endif
  206. #ifndef CURL_DISABLE_GOPHER
  207. "gopher",
  208. #endif
  209. #ifndef CURL_DISABLE_HTTP
  210. "http",
  211. #endif
  212. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  213. "https",
  214. #endif
  215. #ifndef CURL_DISABLE_IMAP
  216. "imap",
  217. #endif
  218. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  219. "imaps",
  220. #endif
  221. #ifndef CURL_DISABLE_LDAP
  222. "ldap",
  223. #if !defined(CURL_DISABLE_LDAPS) && \
  224. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  225. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  226. "ldaps",
  227. #endif
  228. #endif
  229. #ifndef CURL_DISABLE_POP3
  230. "pop3",
  231. #endif
  232. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  233. "pop3s",
  234. #endif
  235. #ifdef USE_LIBRTMP
  236. "rtmp",
  237. #endif
  238. #ifndef CURL_DISABLE_RTSP
  239. "rtsp",
  240. #endif
  241. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  242. "scp",
  243. #endif
  244. #ifdef USE_SSH
  245. "sftp",
  246. #endif
  247. #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
  248. (CURL_SIZEOF_CURL_OFF_T > 4) && \
  249. (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
  250. "smb",
  251. # ifdef USE_SSL
  252. "smbs",
  253. # endif
  254. #endif
  255. #ifndef CURL_DISABLE_SMTP
  256. "smtp",
  257. #endif
  258. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  259. "smtps",
  260. #endif
  261. #ifndef CURL_DISABLE_TELNET
  262. "telnet",
  263. #endif
  264. #ifndef CURL_DISABLE_TFTP
  265. "tftp",
  266. #endif
  267. NULL
  268. };
  269. static curl_version_info_data version_info = {
  270. CURLVERSION_NOW,
  271. LIBCURL_VERSION,
  272. LIBCURL_VERSION_NUM,
  273. OS, /* as found by configure or set by hand at build-time */
  274. 0 /* features is 0 by default */
  275. #ifdef ENABLE_IPV6
  276. | CURL_VERSION_IPV6
  277. #endif
  278. #ifdef USE_SSL
  279. | CURL_VERSION_SSL
  280. #endif
  281. #ifdef USE_NTLM
  282. | CURL_VERSION_NTLM
  283. #endif
  284. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  285. defined(NTLM_WB_ENABLED)
  286. | CURL_VERSION_NTLM_WB
  287. #endif
  288. #ifdef USE_SPNEGO
  289. | CURL_VERSION_SPNEGO
  290. #endif
  291. #ifdef USE_KERBEROS5
  292. | CURL_VERSION_KERBEROS5
  293. #endif
  294. #ifdef HAVE_GSSAPI
  295. | CURL_VERSION_GSSAPI
  296. #endif
  297. #ifdef USE_WINDOWS_SSPI
  298. | CURL_VERSION_SSPI
  299. #endif
  300. #ifdef HAVE_LIBZ
  301. | CURL_VERSION_LIBZ
  302. #endif
  303. #ifdef DEBUGBUILD
  304. | CURL_VERSION_DEBUG
  305. #endif
  306. #ifdef CURLDEBUG
  307. | CURL_VERSION_CURLDEBUG
  308. #endif
  309. #ifdef CURLRES_ASYNCH
  310. | CURL_VERSION_ASYNCHDNS
  311. #endif
  312. #if (CURL_SIZEOF_CURL_OFF_T > 4) && \
  313. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  314. | CURL_VERSION_LARGEFILE
  315. #endif
  316. #if defined(CURL_DOES_CONVERSIONS)
  317. | CURL_VERSION_CONV
  318. #endif
  319. #if defined(USE_TLS_SRP)
  320. | CURL_VERSION_TLSAUTH_SRP
  321. #endif
  322. #if defined(USE_NGHTTP2)
  323. | CURL_VERSION_HTTP2
  324. #endif
  325. #if defined(ENABLE_QUIC)
  326. | CURL_VERSION_HTTP3
  327. #endif
  328. #if defined(USE_UNIX_SOCKETS)
  329. | CURL_VERSION_UNIX_SOCKETS
  330. #endif
  331. #if defined(USE_LIBPSL)
  332. | CURL_VERSION_PSL
  333. #endif
  334. #if defined(CURL_WITH_MULTI_SSL)
  335. | CURL_VERSION_MULTI_SSL
  336. #endif
  337. #if defined(HAVE_BROTLI)
  338. | CURL_VERSION_BROTLI
  339. #endif
  340. #if defined(USE_ALTSVC)
  341. | CURL_VERSION_ALTSVC
  342. #endif
  343. #ifdef USE_ESNI
  344. | CURL_VERSION_ESNI
  345. #endif
  346. ,
  347. NULL, /* ssl_version */
  348. 0, /* ssl_version_num, this is kept at zero */
  349. NULL, /* zlib_version */
  350. protocols,
  351. NULL, /* c-ares version */
  352. 0, /* c-ares version numerical */
  353. NULL, /* libidn version */
  354. 0, /* iconv version */
  355. NULL, /* ssh lib version */
  356. 0, /* brotli_ver_num */
  357. NULL, /* brotli version */
  358. 0, /* nghttp2 version number */
  359. NULL, /* nghttp2 version string */
  360. NULL /* quic library string */
  361. };
  362. curl_version_info_data *curl_version_info(CURLversion stamp)
  363. {
  364. static bool initialized;
  365. #if defined(USE_SSH)
  366. static char ssh_buffer[80];
  367. #endif
  368. #ifdef USE_SSL
  369. #ifdef CURL_WITH_MULTI_SSL
  370. static char ssl_buffer[200];
  371. #else
  372. static char ssl_buffer[80];
  373. #endif
  374. #endif
  375. #ifdef HAVE_BROTLI
  376. static char brotli_buffer[80];
  377. #endif
  378. if(initialized)
  379. return &version_info;
  380. #ifdef USE_SSL
  381. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  382. version_info.ssl_version = ssl_buffer;
  383. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  384. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  385. else
  386. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  387. #endif
  388. #ifdef HAVE_LIBZ
  389. version_info.libz_version = zlibVersion();
  390. /* libz left NULL if non-existing */
  391. #endif
  392. #ifdef USE_ARES
  393. {
  394. int aresnum;
  395. version_info.ares = ares_version(&aresnum);
  396. version_info.ares_num = aresnum;
  397. }
  398. #endif
  399. #ifdef USE_LIBIDN2
  400. /* This returns a version string if we use the given version or later,
  401. otherwise it returns NULL */
  402. version_info.libidn = idn2_check_version(IDN2_VERSION);
  403. if(version_info.libidn)
  404. version_info.features |= CURL_VERSION_IDN;
  405. #elif defined(USE_WIN32_IDN)
  406. version_info.features |= CURL_VERSION_IDN;
  407. #endif
  408. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  409. #ifdef _LIBICONV_VERSION
  410. version_info.iconv_ver_num = _LIBICONV_VERSION;
  411. #else
  412. /* version unknown */
  413. version_info.iconv_ver_num = -1;
  414. #endif /* _LIBICONV_VERSION */
  415. #endif
  416. #if defined(USE_SSH)
  417. Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
  418. version_info.libssh_version = ssh_buffer;
  419. #endif
  420. #ifdef HAVE_BROTLI
  421. version_info.brotli_ver_num = BrotliDecoderVersion();
  422. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  423. version_info.brotli_version = brotli_buffer;
  424. #endif
  425. #ifdef USE_NGHTTP2
  426. {
  427. nghttp2_info *h2 = nghttp2_version(0);
  428. version_info.nghttp2_ver_num = h2->version_num;
  429. version_info.nghttp2_version = h2->version_str;
  430. }
  431. #endif
  432. #ifdef ENABLE_QUIC
  433. {
  434. static char quicbuffer[80];
  435. Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
  436. version_info.quic_version = quicbuffer;
  437. }
  438. #endif
  439. (void)stamp; /* avoid compiler warnings, we don't use this */
  440. initialized = true;
  441. return &version_info;
  442. }