xref: /curl/src/tool_libinfo.c (revision ce7d0d41)
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 
87 static struct feature_name_presentp {
88   const char   *feature_name;
89   bool         *feature_presentp;
90   int           feature_bitmask;
91 } const maybe_feature[] = {
92   /* Keep alphabetically sorted. */
93   {"alt-svc",        &feature_altsvc,     CURL_VERSION_ALTSVC},
94   {"AsynchDNS",      NULL,                CURL_VERSION_ASYNCHDNS},
95   {"brotli",         &feature_brotli,     CURL_VERSION_BROTLI},
96   {"CharConv",       NULL,                CURL_VERSION_CONV},
97   {"Debug",          NULL,                CURL_VERSION_DEBUG},
98   {"gsasl",          NULL,                CURL_VERSION_GSASL},
99   {"GSS-API",        NULL,                CURL_VERSION_GSSAPI},
100   {"HSTS",           &feature_hsts,       CURL_VERSION_HSTS},
101   {"HTTP2",          &feature_http2,      CURL_VERSION_HTTP2},
102   {"HTTP3",          &feature_http3,      CURL_VERSION_HTTP3},
103   {"HTTPS-proxy",    &feature_httpsproxy, CURL_VERSION_HTTPS_PROXY},
104   {"IDN",            NULL,                CURL_VERSION_IDN},
105   {"IPv6",           NULL,                CURL_VERSION_IPV6},
106   {"Kerberos",       NULL,                CURL_VERSION_KERBEROS5},
107   {"Largefile",      NULL,                CURL_VERSION_LARGEFILE},
108   {"libz",           &feature_libz,       CURL_VERSION_LIBZ},
109   {"MultiSSL",       NULL,                CURL_VERSION_MULTI_SSL},
110   {"NTLM",           &feature_ntlm,       CURL_VERSION_NTLM},
111   {"NTLM_WB",        &feature_ntlm_wb,    CURL_VERSION_NTLM_WB},
112   {"PSL",            NULL,                CURL_VERSION_PSL},
113   {"SPNEGO",         &feature_spnego,     CURL_VERSION_SPNEGO},
114   {"SSL",            &feature_ssl,        CURL_VERSION_SSL},
115   {"SSPI",           NULL,                CURL_VERSION_SSPI},
116   {"threadsafe",     NULL,                CURL_VERSION_THREADSAFE},
117   {"TLS-SRP",        &feature_tls_srp,    CURL_VERSION_TLSAUTH_SRP},
118   {"TrackMemory",    NULL,                CURL_VERSION_CURLDEBUG},
119   {"Unicode",        NULL,                CURL_VERSION_UNICODE},
120   {"UnixSockets",    NULL,                CURL_VERSION_UNIX_SOCKETS},
121   {"zstd",           &feature_zstd,       CURL_VERSION_ZSTD},
122   {NULL,             NULL,                0}
123 };
124 
125 static const char *fnames[sizeof(maybe_feature) / sizeof(maybe_feature[0])];
126 const char * const *feature_names = fnames;
127 size_t feature_count;
128 
129 /*
130  * libcurl_info_init: retrieves runtime information about libcurl,
131  * setting a global pointer 'curlinfo' to libcurl's runtime info
132  * struct, count protocols and flag those we are interested in.
133  * Global pointer feature_names is set to the feature names array. If
134  * the latter is not returned by curl_version_info(), it is built from
135  * the returned features bit mask.
136  */
137 
get_libcurl_info(void)138 CURLcode get_libcurl_info(void)
139 {
140   CURLcode result = CURLE_OK;
141   const char *const *builtin;
142 
143   /* Pointer to libcurl's runtime version information */
144   curlinfo = curl_version_info(CURLVERSION_NOW);
145   if(!curlinfo)
146     return CURLE_FAILED_INIT;
147 
148   if(curlinfo->protocols) {
149     const struct proto_name_tokenp *p;
150 
151     built_in_protos = curlinfo->protocols;
152 
153     for(builtin = built_in_protos; !result && *builtin; builtin++) {
154       /* Identify protocols we are interested in. */
155       for(p = possibly_built_in; p->proto_name; p++)
156         if(curl_strequal(p->proto_name, *builtin)) {
157           *p->proto_tokenp = *builtin;
158           break;
159         }
160     }
161     proto_count = builtin - built_in_protos;
162   }
163 
164   if(curlinfo->age >= CURLVERSION_ELEVENTH && curlinfo->feature_names)
165     feature_names = curlinfo->feature_names;
166   else {
167     const struct feature_name_presentp *p;
168     const char **cpp = fnames;
169 
170     for(p = maybe_feature; p->feature_name; p++)
171       if(curlinfo->features & p->feature_bitmask)
172         *cpp++ = p->feature_name;
173     *cpp = NULL;
174   }
175 
176   /* Identify features we are interested in. */
177   for(builtin = feature_names; *builtin; builtin++) {
178     const struct feature_name_presentp *p;
179 
180     for(p = maybe_feature; p->feature_name; p++)
181       if(curl_strequal(p->feature_name, *builtin)) {
182         if(p->feature_presentp)
183           *p->feature_presentp = TRUE;
184         break;
185       }
186     ++feature_count;
187   }
188 
189   return CURLE_OK;
190 }
191 
192 /* Tokenize a protocol name.
193  * Return the address of the protocol name listed by the library, or NULL if
194  * not found.
195  * Although this may seem useless, this always returns the same address for
196  * a given protocol and thus allows comparing pointers rather than strings.
197  * In addition, the returned pointer is not deallocated until the program ends.
198  */
199 
proto_token(const char * proto)200 const char *proto_token(const char *proto)
201 {
202   const char * const *builtin;
203 
204   if(!proto)
205     return NULL;
206   for(builtin = built_in_protos; *builtin; builtin++)
207     if(curl_strequal(*builtin, proto))
208       break;
209   return *builtin;
210 }
211