version.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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(_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 snprintf(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 = snprintf(ptr, left, " zlib/%s", zlibVersion());
  109. left -= len;
  110. ptr += len;
  111. #endif
  112. #ifdef HAVE_BROTLI
  113. len = snprintf(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 = snprintf(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 = snprintf(ptr, left, " libidn2/%s", idn2_check_version(NULL));
  129. left -= len;
  130. ptr += len;
  131. }
  132. #endif
  133. #ifdef USE_LIBPSL
  134. len = snprintf(ptr, left, " libpsl/%s", psl_get_version());
  135. left -= len;
  136. ptr += len;
  137. #endif
  138. #ifdef USE_WIN32_IDN
  139. len = snprintf(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 = snprintf(ptr, left, " iconv/%d.%d",
  146. _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
  147. #else
  148. /* version unknown */
  149. len = snprintf(ptr, left, " iconv");
  150. #endif /* _LIBICONV_VERSION */
  151. left -= len;
  152. ptr += len;
  153. #endif
  154. #ifdef USE_LIBSSH2
  155. len = snprintf(ptr, left, " libssh2/%s", CURL_LIBSSH2_VERSION);
  156. left -= len;
  157. ptr += len;
  158. #endif
  159. #ifdef USE_LIBSSH
  160. len = snprintf(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. snprintf(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 snprintf() returned
  185. left -= len;
  186. ptr += len;
  187. */
  188. }
  189. #endif
  190. initialized = true;
  191. return version;
  192. }
  193. /* data for curl_version_info
  194. Keep the list sorted alphabetically. It is also written so that each
  195. protocol line has its own #if line to make things easier on the eye.
  196. */
  197. static const char * const protocols[] = {
  198. #ifndef CURL_DISABLE_DICT
  199. "dict",
  200. #endif
  201. #ifndef CURL_DISABLE_FILE
  202. "file",
  203. #endif
  204. #ifndef CURL_DISABLE_FTP
  205. "ftp",
  206. #endif
  207. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  208. "ftps",
  209. #endif
  210. #ifndef CURL_DISABLE_GOPHER
  211. "gopher",
  212. #endif
  213. #ifndef CURL_DISABLE_HTTP
  214. "http",
  215. #endif
  216. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  217. "https",
  218. #endif
  219. #ifndef CURL_DISABLE_IMAP
  220. "imap",
  221. #endif
  222. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  223. "imaps",
  224. #endif
  225. #ifndef CURL_DISABLE_LDAP
  226. "ldap",
  227. #if !defined(CURL_DISABLE_LDAPS) && \
  228. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  229. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  230. "ldaps",
  231. #endif
  232. #endif
  233. #ifndef CURL_DISABLE_POP3
  234. "pop3",
  235. #endif
  236. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  237. "pop3s",
  238. #endif
  239. #ifdef USE_LIBRTMP
  240. "rtmp",
  241. #endif
  242. #ifndef CURL_DISABLE_RTSP
  243. "rtsp",
  244. #endif
  245. #if defined(USE_LIBSSH) || defined(USE_LIBSSH2)
  246. "scp",
  247. "sftp",
  248. #endif
  249. #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
  250. (CURL_SIZEOF_CURL_OFF_T > 4) && \
  251. (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
  252. "smb",
  253. # ifdef USE_SSL
  254. "smbs",
  255. # endif
  256. #endif
  257. #ifndef CURL_DISABLE_SMTP
  258. "smtp",
  259. #endif
  260. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  261. "smtps",
  262. #endif
  263. #ifndef CURL_DISABLE_TELNET
  264. "telnet",
  265. #endif
  266. #ifndef CURL_DISABLE_TFTP
  267. "tftp",
  268. #endif
  269. NULL
  270. };
  271. static curl_version_info_data version_info = {
  272. CURLVERSION_NOW,
  273. LIBCURL_VERSION,
  274. LIBCURL_VERSION_NUM,
  275. OS, /* as found by configure or set by hand at build-time */
  276. 0 /* features is 0 by default */
  277. #ifdef ENABLE_IPV6
  278. | CURL_VERSION_IPV6
  279. #endif
  280. #ifdef USE_SSL
  281. | CURL_VERSION_SSL
  282. #endif
  283. #ifdef USE_NTLM
  284. | CURL_VERSION_NTLM
  285. #endif
  286. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  287. defined(NTLM_WB_ENABLED)
  288. | CURL_VERSION_NTLM_WB
  289. #endif
  290. #ifdef USE_SPNEGO
  291. | CURL_VERSION_SPNEGO
  292. #endif
  293. #ifdef USE_KERBEROS5
  294. | CURL_VERSION_KERBEROS5
  295. #endif
  296. #ifdef HAVE_GSSAPI
  297. | CURL_VERSION_GSSAPI
  298. #endif
  299. #ifdef USE_WINDOWS_SSPI
  300. | CURL_VERSION_SSPI
  301. #endif
  302. #ifdef HAVE_LIBZ
  303. | CURL_VERSION_LIBZ
  304. #endif
  305. #ifdef DEBUGBUILD
  306. | CURL_VERSION_DEBUG
  307. #endif
  308. #ifdef CURLDEBUG
  309. | CURL_VERSION_CURLDEBUG
  310. #endif
  311. #ifdef CURLRES_ASYNCH
  312. | CURL_VERSION_ASYNCHDNS
  313. #endif
  314. #if (CURL_SIZEOF_CURL_OFF_T > 4) && \
  315. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  316. | CURL_VERSION_LARGEFILE
  317. #endif
  318. #if defined(CURL_DOES_CONVERSIONS)
  319. | CURL_VERSION_CONV
  320. #endif
  321. #if defined(USE_TLS_SRP)
  322. | CURL_VERSION_TLSAUTH_SRP
  323. #endif
  324. #if defined(USE_NGHTTP2)
  325. | CURL_VERSION_HTTP2
  326. #endif
  327. #if defined(USE_UNIX_SOCKETS)
  328. | CURL_VERSION_UNIX_SOCKETS
  329. #endif
  330. #if defined(USE_LIBPSL)
  331. | CURL_VERSION_PSL
  332. #endif
  333. #if defined(CURL_WITH_MULTI_SSL)
  334. | CURL_VERSION_MULTI_SSL
  335. #endif
  336. #if defined(HAVE_BROTLI)
  337. | CURL_VERSION_BROTLI
  338. #endif
  339. ,
  340. NULL, /* ssl_version */
  341. 0, /* ssl_version_num, this is kept at zero */
  342. NULL, /* zlib_version */
  343. protocols,
  344. NULL, /* c-ares version */
  345. 0, /* c-ares version numerical */
  346. NULL, /* libidn version */
  347. 0, /* iconv version */
  348. NULL, /* ssh lib version */
  349. 0, /* brotli_ver_num */
  350. NULL, /* brotli version */
  351. };
  352. curl_version_info_data *curl_version_info(CURLversion stamp)
  353. {
  354. static bool initialized;
  355. #if defined(USE_LIBSSH) || defined(USE_LIBSSH2)
  356. static char ssh_buffer[80];
  357. #endif
  358. #ifdef USE_SSL
  359. static char ssl_buffer[80];
  360. #endif
  361. #ifdef HAVE_BROTLI
  362. static char brotli_buffer[80];
  363. #endif
  364. if(initialized)
  365. return &version_info;
  366. #ifdef USE_SSL
  367. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  368. version_info.ssl_version = ssl_buffer;
  369. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  370. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  371. else
  372. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  373. #endif
  374. #ifdef HAVE_LIBZ
  375. version_info.libz_version = zlibVersion();
  376. /* libz left NULL if non-existing */
  377. #endif
  378. #ifdef USE_ARES
  379. {
  380. int aresnum;
  381. version_info.ares = ares_version(&aresnum);
  382. version_info.ares_num = aresnum;
  383. }
  384. #endif
  385. #ifdef USE_LIBIDN2
  386. /* This returns a version string if we use the given version or later,
  387. otherwise it returns NULL */
  388. version_info.libidn = idn2_check_version(IDN2_VERSION);
  389. if(version_info.libidn)
  390. version_info.features |= CURL_VERSION_IDN;
  391. #elif defined(USE_WIN32_IDN)
  392. version_info.features |= CURL_VERSION_IDN;
  393. #endif
  394. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  395. #ifdef _LIBICONV_VERSION
  396. version_info.iconv_ver_num = _LIBICONV_VERSION;
  397. #else
  398. /* version unknown */
  399. version_info.iconv_ver_num = -1;
  400. #endif /* _LIBICONV_VERSION */
  401. #endif
  402. #if defined(USE_LIBSSH2)
  403. snprintf(ssh_buffer, sizeof(ssh_buffer), "libssh2/%s", LIBSSH2_VERSION);
  404. version_info.libssh_version = ssh_buffer;
  405. #elif defined(USE_LIBSSH)
  406. snprintf(ssh_buffer, sizeof(ssh_buffer), "libssh/%s", CURL_LIBSSH_VERSION);
  407. version_info.libssh_version = ssh_buffer;
  408. #endif
  409. #ifdef HAVE_BROTLI
  410. version_info.brotli_ver_num = BrotliDecoderVersion();
  411. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  412. version_info.brotli_version = brotli_buffer;
  413. #endif
  414. (void)stamp; /* avoid compiler warnings, we don't use this */
  415. initialized = true;
  416. return &version_info;
  417. }