version.c 10 KB

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