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