tool_libinfo.c 7.0 KB

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