tool_libinfo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #include "strcase.h"
  26. #define ENABLE_CURLX_PRINTF
  27. /* use our own printf() functions */
  28. #include "curlx.h"
  29. #include "tool_libinfo.h"
  30. #include "memdebug.h" /* keep this as LAST include */
  31. /* global variable definitions, for libcurl run-time info */
  32. static const char *no_protos = NULL;
  33. curl_version_info_data *curlinfo = NULL;
  34. const char * const *built_in_protos = &no_protos;
  35. size_t proto_count = 0;
  36. const char *proto_file = NULL;
  37. const char *proto_ftp = NULL;
  38. const char *proto_ftps = NULL;
  39. const char *proto_http = NULL;
  40. const char *proto_https = NULL;
  41. const char *proto_rtsp = NULL;
  42. const char *proto_scp = NULL;
  43. const char *proto_sftp = NULL;
  44. const char *proto_tftp = NULL;
  45. const char *proto_ipfs = "ipfs";
  46. const char *proto_ipns = "ipns";
  47. static struct proto_name_tokenp {
  48. const char *proto_name;
  49. const char **proto_tokenp;
  50. } const possibly_built_in[] = {
  51. { "file", &proto_file },
  52. { "ftp", &proto_ftp },
  53. { "ftps", &proto_ftps },
  54. { "http", &proto_http },
  55. { "https", &proto_https },
  56. { "rtsp", &proto_rtsp },
  57. { "scp", &proto_scp },
  58. { "sftp", &proto_sftp },
  59. { "tftp", &proto_tftp },
  60. { NULL, NULL }
  61. };
  62. bool feature_altsvc = FALSE;
  63. bool feature_brotli = FALSE;
  64. bool feature_hsts = FALSE;
  65. bool feature_http2 = FALSE;
  66. bool feature_http3 = FALSE;
  67. bool feature_httpsproxy = FALSE;
  68. bool feature_libz = FALSE;
  69. bool feature_ntlm = FALSE;
  70. bool feature_ntlm_wb = FALSE;
  71. bool feature_spnego = FALSE;
  72. bool feature_ssl = FALSE;
  73. bool feature_tls_srp = FALSE;
  74. bool feature_zstd = FALSE;
  75. static struct feature_name_presentp {
  76. const char *feature_name;
  77. bool *feature_presentp;
  78. int feature_bitmask;
  79. } const maybe_feature[] = {
  80. /* Keep alphabetically sorted. */
  81. {"alt-svc", &feature_altsvc, CURL_VERSION_ALTSVC},
  82. {"AsynchDNS", NULL, CURL_VERSION_ASYNCHDNS},
  83. {"brotli", &feature_brotli, CURL_VERSION_BROTLI},
  84. {"CharConv", NULL, CURL_VERSION_CONV},
  85. {"Debug", NULL, CURL_VERSION_DEBUG},
  86. {"gsasl", NULL, CURL_VERSION_GSASL},
  87. {"GSS-API", NULL, CURL_VERSION_GSSAPI},
  88. {"HSTS", &feature_hsts, CURL_VERSION_HSTS},
  89. {"HTTP2", &feature_http2, CURL_VERSION_HTTP2},
  90. {"HTTP3", &feature_http3, CURL_VERSION_HTTP3},
  91. {"HTTPS-proxy", &feature_httpsproxy, CURL_VERSION_HTTPS_PROXY},
  92. {"IDN", NULL, CURL_VERSION_IDN},
  93. {"IPv6", NULL, CURL_VERSION_IPV6},
  94. {"Kerberos", NULL, CURL_VERSION_KERBEROS5},
  95. {"Largefile", NULL, CURL_VERSION_LARGEFILE},
  96. {"libz", &feature_libz, CURL_VERSION_LIBZ},
  97. {"MultiSSL", NULL, CURL_VERSION_MULTI_SSL},
  98. {"NTLM", &feature_ntlm, CURL_VERSION_NTLM},
  99. {"NTLM_WB", &feature_ntlm_wb, CURL_VERSION_NTLM_WB},
  100. {"PSL", NULL, CURL_VERSION_PSL},
  101. {"SPNEGO", &feature_spnego, CURL_VERSION_SPNEGO},
  102. {"SSL", &feature_ssl, CURL_VERSION_SSL},
  103. {"SSPI", NULL, CURL_VERSION_SSPI},
  104. {"threadsafe", NULL, CURL_VERSION_THREADSAFE},
  105. {"TLS-SRP", &feature_tls_srp, CURL_VERSION_TLSAUTH_SRP},
  106. {"TrackMemory", NULL, CURL_VERSION_CURLDEBUG},
  107. {"Unicode", NULL, CURL_VERSION_UNICODE},
  108. {"UnixSockets", NULL, CURL_VERSION_UNIX_SOCKETS},
  109. {"zstd", &feature_zstd, CURL_VERSION_ZSTD},
  110. {NULL, NULL, 0}
  111. };
  112. static const char *fnames[sizeof(maybe_feature) / sizeof(maybe_feature[0])];
  113. const char * const *feature_names = fnames;
  114. /*
  115. * libcurl_info_init: retrieves run-time information about libcurl,
  116. * setting a global pointer 'curlinfo' to libcurl's run-time info
  117. * struct, count protocols and flag those we are interested in.
  118. * Global pointer feature_names is set to the feature names array. If
  119. * the latter is not returned by curl_version_info(), it is built from
  120. * the returned features bit mask.
  121. */
  122. CURLcode get_libcurl_info(void)
  123. {
  124. CURLcode result = CURLE_OK;
  125. const char *const *builtin;
  126. /* Pointer to libcurl's run-time version information */
  127. curlinfo = curl_version_info(CURLVERSION_NOW);
  128. if(!curlinfo)
  129. return CURLE_FAILED_INIT;
  130. if(curlinfo->protocols) {
  131. const struct proto_name_tokenp *p;
  132. built_in_protos = curlinfo->protocols;
  133. for(builtin = built_in_protos; !result && *builtin; builtin++) {
  134. /* Identify protocols we are interested in. */
  135. for(p = possibly_built_in; p->proto_name; p++)
  136. if(curl_strequal(p->proto_name, *builtin)) {
  137. *p->proto_tokenp = *builtin;
  138. break;
  139. }
  140. }
  141. proto_count = builtin - built_in_protos;
  142. }
  143. if(curlinfo->age >= CURLVERSION_ELEVENTH && curlinfo->feature_names)
  144. feature_names = curlinfo->feature_names;
  145. else {
  146. const struct feature_name_presentp *p;
  147. const char **cpp = fnames;
  148. for(p = maybe_feature; p->feature_name; p++)
  149. if(curlinfo->features & p->feature_bitmask)
  150. *cpp++ = p->feature_name;
  151. *cpp = NULL;
  152. }
  153. /* Identify features we are interested in. */
  154. for(builtin = feature_names; *builtin; builtin++) {
  155. const struct feature_name_presentp *p;
  156. for(p = maybe_feature; p->feature_name; p++)
  157. if(curl_strequal(p->feature_name, *builtin)) {
  158. if(p->feature_presentp)
  159. *p->feature_presentp = TRUE;
  160. break;
  161. }
  162. }
  163. return CURLE_OK;
  164. }
  165. /* Tokenize a protocol name.
  166. * Return the address of the protocol name listed by the library, or NULL if
  167. * not found.
  168. * Although this may seem useless, this always returns the same address for
  169. * a given protocol and thus allows comparing pointers rather than strings.
  170. * In addition, the returned pointer is not deallocated until the program ends.
  171. */
  172. const char *proto_token(const char *proto)
  173. {
  174. const char * const *builtin;
  175. if(!proto)
  176. return NULL;
  177. for(builtin = built_in_protos; *builtin; builtin++)
  178. if(curl_strequal(*builtin, proto))
  179. break;
  180. return *builtin;
  181. }