1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2012 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #include "FeatureConfig.h"
36 
37 #include <sstream>
38 #include <cstring>
39 
40 #ifdef HAVE_ZLIB
41 #  include <zlib.h>
42 #endif // HAVE_ZLIB
43 #ifdef HAVE_LIBXML2
44 #  include <libxml/xmlversion.h>
45 #endif // HAVE_LIBXML2
46 #ifdef HAVE_LIBEXPAT
47 #  include <expat.h>
48 #endif // HAVE_LIBEXPAT
49 #ifdef HAVE_SQLITE3
50 #  include <sqlite3.h>
51 #endif // HAVE_SQLITE3
52 #ifdef HAVE_LIBGNUTLS
53 #  include <gnutls/gnutls.h>
54 #endif // HAVE_LIBGNUTLS
55 #ifdef HAVE_OPENSSL
56 #  include <openssl/opensslv.h>
57 #endif // HAVE_OPENSSL
58 #ifdef HAVE_LIBGMP
59 #  include <gmp.h>
60 #endif // HAVE_LIBGMP
61 #ifdef HAVE_LIBGCRYPT
62 #  include <gcrypt.h>
63 #endif // HAVE_LIBGCRYPT
64 #ifdef HAVE_LIBCARES
65 #  include <ares.h>
66 #endif // HAVE_LIBCARES
67 #ifdef HAVE_SYS_UTSNAME_H
68 #  include <sys/utsname.h>
69 #endif // HAVE_SYS_UTSNAME_H
70 #ifdef HAVE_LIBSSH2
71 #  include <libssh2.h>
72 #endif // HAVE_LIBSSH2
73 #include "util.h"
74 
75 namespace aria2 {
76 
getDefaultPort(const std::string & protocol)77 uint16_t getDefaultPort(const std::string& protocol)
78 {
79   if (protocol == "http") {
80     return 80;
81   }
82   else if (protocol == "https") {
83     return 443;
84   }
85   else if (protocol == "ftp") {
86     return 21;
87   }
88   else if (protocol == "sftp") {
89     return 22;
90   }
91   else {
92     return 0;
93   }
94 }
95 
featureSummary()96 std::string featureSummary()
97 {
98   std::string s;
99   int first;
100   for (first = 0; first < MAX_FEATURE && !strSupportedFeature(first); ++first)
101     ;
102   if (first < MAX_FEATURE) {
103     s += strSupportedFeature(first);
104     for (int i = first + 1; i < MAX_FEATURE; ++i) {
105       const char* name = strSupportedFeature(i);
106       if (name) {
107         s += ", ";
108         s += name;
109       }
110     }
111   }
112   return s;
113 }
114 
strSupportedFeature(int feature)115 const char* strSupportedFeature(int feature)
116 {
117   switch (feature) {
118   case (FEATURE_ASYNC_DNS):
119 #ifdef ENABLE_ASYNC_DNS
120     return "Async DNS";
121 #else  // !ENABLE_ASYNC_DNS
122     return nullptr;
123 #endif // !ENABLE_ASYNC_DNS
124     break;
125 
126   case (FEATURE_BITTORRENT):
127 #ifdef ENABLE_BITTORRENT
128     return "BitTorrent";
129 #else  // !ENABLE_BITTORRENT
130     return nullptr;
131 #endif // !ENABLE_BITTORRENT
132     break;
133 
134   case (FEATURE_FF3_COOKIE):
135 #ifdef HAVE_SQLITE3
136     return "Firefox3 Cookie";
137 #else  // !HAVE_SQLITE3
138     return nullptr;
139 #endif // !HAVE_SQLITE3
140     break;
141 
142   case (FEATURE_GZIP):
143 #ifdef HAVE_ZLIB
144     return "GZip";
145 #else  // !HAVE_ZLIB
146     return nullptr;
147 #endif // !HAVE_ZLIB
148     break;
149 
150   case (FEATURE_HTTPS):
151 #ifdef ENABLE_SSL
152     return "HTTPS";
153 #else  // !ENABLE_SSL
154     return nullptr;
155 #endif // !ENABLE_SSL
156     break;
157 
158   case (FEATURE_MESSAGE_DIGEST):
159     return "Message Digest";
160     break;
161 
162   case (FEATURE_METALINK):
163 #ifdef ENABLE_METALINK
164     return "Metalink";
165 #else  // !ENABLE_METALINK
166     return nullptr;
167 #endif // !ENABLE_METALINK
168     break;
169 
170   case (FEATURE_XML_RPC):
171 #ifdef ENABLE_XML_RPC
172     return "XML-RPC";
173 #else  // !ENABLE_XML_RPC
174     return nullptr;
175 #endif // !ENABLE_XML_RPC
176     break;
177 
178   case (FEATURE_SFTP):
179 #ifdef HAVE_LIBSSH2
180     return "SFTP";
181 #else  // !HAVE_LIBSSH2
182     return nullptr;
183 #endif // !HAVE_LIBSSH2
184     break;
185 
186   default:
187     return nullptr;
188   }
189 }
190 
usedLibs()191 std::string usedLibs()
192 {
193   std::string res;
194 #ifdef HAVE_ZLIB
195   res += "zlib/" ZLIB_VERSION " ";
196 #endif // HAVE_ZLIB
197 #ifdef HAVE_LIBXML2
198   res += "libxml2/" LIBXML_DOTTED_VERSION " ";
199 #endif // HAVE_LIBXML2
200 #ifdef HAVE_LIBEXPAT
201   res += fmt("expat/%d.%d.%d ", XML_MAJOR_VERSION, XML_MINOR_VERSION,
202              XML_MICRO_VERSION);
203 #endif // HAVE_LIBEXPAT
204 #ifdef HAVE_SQLITE3
205   res += "sqlite3/" SQLITE_VERSION " ";
206 #endif // HAVE_SQLITE3
207 #ifdef HAVE_APPLETLS
208   res += "AppleTLS ";
209 #endif // HAVE_APPLETLS
210 #ifdef HAVE_WINTLS
211   res += "WinTLS ";
212 #endif // HAVE_WINTLS
213 #ifdef HAVE_LIBGNUTLS
214   res += "GnuTLS/" GNUTLS_VERSION " ";
215 #endif // HAVE_LIBGNUTLS
216 #ifdef HAVE_OPENSSL
217   res += fmt("OpenSSL/%ld.%ld.%ld", OPENSSL_VERSION_NUMBER >> 28,
218              (OPENSSL_VERSION_NUMBER >> 20) & 0xff,
219              (OPENSSL_VERSION_NUMBER >> 12) & 0xff);
220   if ((OPENSSL_VERSION_NUMBER >> 4) & 0xff) {
221     res += 'a' + ((OPENSSL_VERSION_NUMBER >> 4) & 0xff) - 1;
222   }
223   res += " ";
224 #endif // HAVE_OPENSSL
225 #ifdef HAVE_LIBNETTLE
226   // No library version in header files.
227   res += "nettle ";
228 #endif // HAVE_LIBNETTLE
229 #ifdef HAVE_LIBGMP
230   res += fmt("GMP/%d.%d.%d ", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR,
231              __GNU_MP_VERSION_PATCHLEVEL);
232 #endif // HAVE_LIBGMP
233 #ifdef HAVE_LIBGCRYPT
234   res += "libgcrypt/" GCRYPT_VERSION " ";
235 #endif // HAVE_LIBGCRYPT
236 #ifdef HAVE_LIBCARES
237   res += "c-ares/" ARES_VERSION_STR " ";
238 #endif // HAVE_LIBCARES
239 
240 #ifdef HAVE_LIBSSH2
241   res += "libssh2/" LIBSSH2_VERSION " ";
242 #endif // HAVE_LIBSSH2
243 
244   if (!res.empty()) {
245     res.erase(res.length() - 1);
246   }
247   return res;
248 }
249 
usedCompilerAndPlatform()250 std::string usedCompilerAndPlatform()
251 {
252   std::stringstream rv;
253 #if defined(__clang_version__)
254 
255 #  ifdef __apple_build_version__
256   rv << "Apple LLVM ";
257 #  else  // !__apple_build_version__
258   rv << "clang ";
259 #  endif // !__apple_build_version__
260   rv << __clang_version__;
261 
262 #elif defined(__INTEL_COMPILER)
263 
264   rv << "Intel ICC " << __VERSION__;
265 
266 #elif defined(__MINGW64_VERSION_STR)
267 
268   rv << "mingw-w64 " << __MINGW64_VERSION_STR;
269 #  ifdef __MINGW64_VERSION_STATE
270   rv << " (" << __MINGW64_VERSION_STATE << ")";
271 #  endif // __MINGW64_VERSION_STATE
272   rv << " / gcc " << __VERSION__;
273 
274 #elif defined(__GNUG__)
275 
276 #  ifdef __MINGW32__
277   rv << "mingw ";
278 #    ifdef __MINGW32_MAJOR_VERSION
279   rv << (int)__MINGW32_MAJOR_VERSION;
280 #    endif // __MINGW32_MAJOR_VERSION
281 #    ifdef __MINGW32_MINOR_VERSION
282   rv << "." << (int)__MINGW32_MINOR_VERSION;
283 #    endif // __MINGW32_MINOR_VERSION
284   rv << " / ";
285 #  endif   // __MINGW32__
286   rv << "gcc " << __VERSION__;
287 
288 #else // !defined(__GNUG__)
289 
290   rv << "Unknown compiler/platform";
291 
292 #endif // !defined(__GNUG__)
293 
294   rv << "\n  built by  " << BUILD;
295   if (strcmp(BUILD, TARGET)) {
296     rv << "\n  targeting " << TARGET;
297   }
298   rv << "\n  on        " << __DATE__ << " " << __TIME__;
299 
300   return rv.str();
301 }
302 
getOperatingSystemInfo()303 std::string getOperatingSystemInfo()
304 {
305 #ifdef _WIN32
306   std::stringstream rv;
307   rv << "Windows ";
308   OSVERSIONINFOEX ovi = {sizeof(OSVERSIONINFOEX)};
309   if (!GetVersionEx((LPOSVERSIONINFO)&ovi)) {
310     rv << "Unknown";
311     return rv.str();
312   }
313   if (ovi.dwMajorVersion < 6) {
314     rv << "Legacy, probably XP";
315     return rv.str();
316   }
317   switch (ovi.dwMinorVersion) {
318   case 0:
319     if (ovi.wProductType == VER_NT_WORKSTATION) {
320       rv << "Vista";
321     }
322     else {
323       rv << "Server 2008";
324     }
325     break;
326 
327   case 1:
328     if (ovi.wProductType == VER_NT_WORKSTATION) {
329       rv << "7";
330     }
331     else {
332       rv << "Server 2008 R2";
333     }
334     break;
335 
336   default:
337     // Windows above 6.2 does not actually say so. :p
338 
339     rv << ovi.dwMajorVersion;
340     if (ovi.dwMinorVersion) {
341       rv << "." << ovi.dwMinorVersion;
342     }
343     if (ovi.wProductType != VER_NT_WORKSTATION) {
344       rv << " Server";
345     }
346     break;
347   }
348   if (ovi.szCSDVersion[0]) {
349     rv << " (" << ovi.szCSDVersion << ")";
350   }
351 #  ifdef _WIN64
352   rv << " (x86_64)";
353 #  endif // _WIN64
354   rv << " (" << ovi.dwMajorVersion << "." << ovi.dwMinorVersion << ")";
355   return rv.str();
356 #else //! _WIN32
357 #  ifdef HAVE_SYS_UTSNAME_H
358   struct utsname name;
359   if (!uname(&name)) {
360     if (!strstr(name.version, name.sysname) ||
361         !strstr(name.version, name.release) ||
362         !strstr(name.version, name.machine)) {
363       std::stringstream ss;
364       ss << name.sysname << " " << name.release << " " << name.version << " "
365          << name.machine;
366       return ss.str();
367     }
368     return name.version;
369   }
370 #  endif // HAVE_SYS_UTSNAME_H
371   return "Unknown system";
372 #endif   // !_WIN32
373 }
374 
375 } // namespace aria2
376