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