Fix bug #5453: Use GetSystemNativeInfo() function if found on the machine running...
[gd/wireshark/.git] / version_info.c
1 /* version_info.c
2  * Routines to report version information for stuff used by Wireshark
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_PYTHON
30 #include <Python.h> /* to get the Python version number (PY_VERSION) */
31 #endif
32
33 #include <glib.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #ifdef HAVE_LIBZ
41 #include <zlib.h>       /* to get the libz version number */
42 #endif
43
44 #ifdef HAVE_LIBPCRE
45 #include <pcre.h>       /* to get the libpcre version number */
46 #endif /* HAVE_LIBPCRE */
47
48 #ifdef HAVE_SYS_UTSNAME_H
49 #include <sys/utsname.h>
50 #endif
51
52 #include "version_info.h"
53 #include "capture-pcap-util.h"
54 #include <wsutil/unicode-utils.h>
55
56 #include "svnversion.h"
57
58 #ifdef HAVE_WINDOWS_H
59 #include <windows.h>
60 #endif
61
62 #ifdef HAVE_OS_X_FRAMEWORKS
63 #include <CoreServices/CoreServices.h>
64 #endif
65
66 #ifdef HAVE_LIBCAP
67 # include <sys/capability.h>
68 #endif
69
70 #ifdef HAVE_GEOIP
71 #include <epan/geoip_db.h>
72 #endif
73
74 #ifdef SVNVERSION
75         const char *wireshark_svnversion = " (" SVNVERSION " from " SVNPATH ")";
76 #else
77         const char *wireshark_svnversion = "";
78 #endif
79
80 /*
81  * If the string doesn't end with a newline, append one.
82  * Then word-wrap it to 80 columns.
83  */
84 static void
85 end_string(GString *str)
86 {
87         size_t point;
88         char *p, *q;
89
90         point = str->len;
91         if (point == 0 || str->str[point - 1] != '\n')
92                 g_string_append(str, "\n");
93         p = str->str;
94         while (*p != '\0') {
95                 q = strchr(p, '\n');
96                 if (q - p > 80) {
97                         /*
98                          * Break at or before this point.
99                          */
100                         q = p + 80;
101                         while (q > p && *q != ' ')
102                                 q--;
103                         if (q != p)
104                                 *q = '\n';
105                 }
106                 p = q + 1;
107         }
108 }
109
110 /*
111  * Get various library compile-time versions and append them to
112  * the specified GString.
113  *
114  * "additional_info" is called at the end to append any additional
115  * information; this is required in order to, for example, put the
116  * Portaudio information at the end of the string, as we currently
117  * don't use Portaudio in TShark.
118  */
119 void
120 get_compiled_version_info(GString *str, void (*prepend_info)(GString *), 
121                           void (*append_info)(GString *))
122 {
123         if (sizeof(str) == 4)
124                 g_string_append(str, "(32-bit) ");
125         else
126                 g_string_append(str, "(64-bit) ");
127
128         if (prepend_info)
129                 (*prepend_info)(str);
130
131         /* GLIB */
132         g_string_append(str, "with ");
133         g_string_append_printf(str,
134 #ifdef GLIB_MAJOR_VERSION
135             "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
136             GLIB_MICRO_VERSION);
137 #else
138             "GLib (version unknown)");
139 #endif
140
141         /* Libpcap */
142         g_string_append(str, ", ");
143         get_compiled_pcap_version(str);
144
145         /* LIBZ */
146         g_string_append(str, ", ");
147 #ifdef HAVE_LIBZ
148         g_string_append(str, "with libz ");
149 #ifdef ZLIB_VERSION
150         g_string_append(str, ZLIB_VERSION);
151 #else /* ZLIB_VERSION */
152         g_string_append(str, "(version unknown)");
153 #endif /* ZLIB_VERSION */
154 #else /* HAVE_LIBZ */
155         g_string_append(str, "without libz");
156 #endif /* HAVE_LIBZ */
157
158         /* LIBCAP */
159         g_string_append(str, ", ");
160 #ifdef HAVE_LIBCAP
161         g_string_append(str, "with POSIX capabilities");
162 #ifdef _LINUX_CAPABILITY_VERSION
163         g_string_append(str, " (Linux)");
164 #endif /* _LINUX_CAPABILITY_VERSION */
165 #else /* HAVE_LIBCAP */
166         g_string_append(str, "without POSIX capabilities");
167 #endif /* HAVE_LIBCAP */
168
169         /* Additional application-dependent information */
170         if (append_info)
171                 (*append_info)(str);
172         g_string_append(str, ".");
173
174 #if !defined(HAVE_LIBPCRE) && !GLIB_CHECK_VERSION(2,14,0)
175         g_string_append(str,
176         "\nNOTE: this build doesn't support the \"matches\" operator for Wireshark filter syntax");
177         g_string_append(str, ".");
178 #endif  /* HAVE_LIBPCRE */
179
180         end_string(str);
181 }
182
183 /*
184  * Get various library run-time versions, and the OS version, and append
185  * them to the specified GString.
186  */
187 void
188 get_runtime_version_info(GString *str, void (*additional_info)(GString *))
189 {
190 #if defined(_WIN32)
191         OSVERSIONINFOEX info;
192         SYSTEM_INFO system_info;
193         typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
194         PGNSI pGNSI;
195 #elif defined(HAVE_SYS_UTSNAME_H)
196         struct utsname name;
197 #endif
198 #if HAVE_OS_X_FRAMEWORKS
199         SInt32 macosx_ver, macosx_major_ver, macosx_minor_ver, macosx_bugfix_ver;
200 #endif
201
202         g_string_append(str, "on ");
203
204 #if defined(_WIN32)
205         /*
206          * See
207          *
208          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
209          *
210          * for more than you ever wanted to know about determining the
211          * flavor of Windows on which you're running.  Implementing more
212          * of that is left as an exercise to the reader - who should
213          * check any copyright information about code samples on MSDN
214          * before cutting and pasting into Wireshark.
215          *
216          * They should also note that you need an OSVERSIONINFOEX structure
217          * to get some of that information, and that not only is that
218          * structure not supported on older versions of Windows, you might
219          * not even be able to compile code that *uses* that structure with
220          * older versions of the SDK.
221          */
222
223         memset(&info, '\0', sizeof info);
224         info.dwOSVersionInfoSize = sizeof info;
225         if (!GetVersionEx((OSVERSIONINFO *)&info)) {
226                 /*
227                  * XXX - get the failure reason.
228                  */
229                 g_string_append(str, "unknown Windows version");
230                 return;
231         }
232
233         memset(&system_info, '\0', sizeof system_info);
234
235         /* Detect if the system we're *running on* supports the GetNativeSystemInfo() function (Windows XP/Server 2003 and higher),
236          * so we get the correct CPU architecture when running under "WOW64" x86 emulation on a 64-bit system. */
237         pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
238         if(NULL != pGNSI)
239                 pGNSI(&system_info); /* Call GetNativeSystemInfo() if found */
240         else
241                 GetSystemInfo(&system_info);    /* Fallback to GetSystemInfo() - only for W2K or greater .... (which is what we support) */
242
243         switch (info.dwPlatformId) {
244
245         case VER_PLATFORM_WIN32s:
246                 /* Shyeah, right. */
247                 g_string_append_printf(str, "Windows 3.1 with Win32s");
248                 break;
249
250         case VER_PLATFORM_WIN32_WINDOWS:
251                 /* Windows OT */
252                 switch (info.dwMajorVersion) {
253
254                 case 4:
255                         /* 3 cheers for Microsoft marketing! */
256                         switch (info.dwMinorVersion) {
257
258                         case 0:
259                                 g_string_append_printf(str, "Windows 95");
260                                 break;
261
262                         case 10:
263                                 g_string_append_printf(str, "Windows 98");
264                                 break;
265
266                         case 90:
267                                 g_string_append_printf(str, "Windows Me");
268                                 break;
269
270                         default:
271                                 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
272                                     info.dwMajorVersion, info.dwMinorVersion);
273                                 break;
274                         }
275                         break;
276
277                 default:
278                         g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
279                             info.dwMajorVersion, info.dwMinorVersion);
280                         break;
281                 }
282                 break;
283
284         case VER_PLATFORM_WIN32_NT:
285                 /* Windows NT */
286                 switch (info.dwMajorVersion) {
287
288                 case 3:
289                 case 4:
290                         g_string_append_printf(str, "Windows NT %lu.%lu",
291                             info.dwMajorVersion, info.dwMinorVersion);
292                         break;
293
294                 case 5:
295                         /* 3 cheers for Microsoft marketing! */
296                         switch (info.dwMinorVersion) {
297
298                         case 0:
299                                 g_string_append_printf(str, "Windows 2000");
300                                 break;
301
302                         case 1:
303                                 g_string_append_printf(str, "Windows XP");
304                                 break;
305
306                         case 2:
307                                 if ((info.wProductType == VER_NT_WORKSTATION) &&
308                                     (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
309                                         g_string_append_printf(str, "Windows XP Professional x64 Edition");
310                                 } else {
311                                         g_string_append_printf(str, "Windows Server 2003");
312                                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
313                                                 g_string_append_printf(str, " x64 Edition");
314                                 }
315                                 break;
316
317                         default:
318                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
319                                                        info.dwMajorVersion, info.dwMinorVersion);
320                                 break;
321                         }
322                         break;
323
324                 case 6: {
325                         gboolean is_nt_workstation;
326
327                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
328                                 g_string_append(str, "64-bit ");
329                         else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
330                                 g_string_append(str, "32-bit ");
331 #ifndef VER_NT_WORKSTATION
332 #define VER_NT_WORKSTATION 0x01
333                         is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
334 #else
335                         is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
336 #endif
337                         switch (info.dwMinorVersion) {
338                         case 0:
339                                 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
340                                 break;
341                         case 1:
342                                 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
343                                 break;
344                         default:
345                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
346                                                        info.dwMajorVersion, info.dwMinorVersion);
347                                 break;
348                         }
349                         break;
350                 }  /* case 6 */
351                 default:
352                         g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
353                             info.dwMajorVersion, info.dwMinorVersion);
354                         break;
355                 } /* info.dwMajorVersion */
356                 break;
357
358         default:
359                 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
360                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
361                 break;
362         }
363         if (info.szCSDVersion[0] != '\0')
364                 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
365         g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
366 #elif defined(HAVE_SYS_UTSNAME_H)
367         /*
368          * We have <sys/utsname.h>, so we assume we have "uname()".
369          */
370         if (uname(&name) < 0) {
371                 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
372                     strerror(errno));
373                 return;
374         }
375
376         if (strcmp(name.sysname, "AIX") == 0) {
377                 /*
378                  * Yay, IBM!  Thanks for doing something different
379                  * from most of the other UNIXes out there, and
380                  * making "name.version" apparently be the major
381                  * version number and "name.release" be the minor
382                  * version number.
383                  */
384                 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
385                     name.release);
386         } else {
387                 /*
388                  * XXX - get "version" on any other platforms?
389                  *
390                  * On Digital/Tru64 UNIX, it's something unknown.
391                  * On Solaris, it's some kind of build information.
392                  * On HP-UX, it appears to be some sort of subrevision
393                  * thing.
394                  * On *BSD and Darwin/OS X, it's a long string giving
395                  * a build date, config file name, etc., etc., etc..
396                  */
397 #ifdef HAVE_OS_X_FRAMEWORKS
398                 /*
399                  * On Mac OS X, report the Mac OS X version number as
400                  * the OS, and put the Darwin information in parentheses.
401                  *
402                  * XXX - can we get the build name?  There's no API to
403                  * get it; it's currently in
404                  * /System/Library/CoreServices/SystemVersion.plist
405                  * but there's no guarantee that it will continue to
406                  * be there.
407                  */
408                 Gestalt(gestaltSystemVersion, &macosx_ver);
409
410                 /* The following functions are only available in Mac OS 10.4+ */
411                 if(macosx_ver >= 0x1040) {
412                         Gestalt(gestaltSystemVersionMajor, &macosx_major_ver);
413                         Gestalt(gestaltSystemVersionMinor, &macosx_minor_ver);
414                         Gestalt(gestaltSystemVersionBugFix, &macosx_bugfix_ver);
415
416                         g_string_append_printf(str, "Mac OS %ld.%ld.%ld",
417                                           (long)macosx_major_ver,
418                                           (long)macosx_minor_ver,
419                                           (long)macosx_bugfix_ver);
420                 } else {
421                         g_string_append_printf(str, "Mac OS X < 10.4 [%lx]",
422                                           (long)macosx_ver);
423                         /* See Apple's Gestalt Manager Reference for meanings
424                          * of the macosx_ver values. */
425                 }
426                 g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
427 #else /* HAVE_OS_X_FRAMEWORKS */
428                 /*
429                  * XXX - on Linux, are there any APIs to get the distribution
430                  * name and version number?  I think some distributions have
431                  * that.
432                  *
433                  * At least on Linux Standard Base-compliant distributions,
434                  * there's an "lsb_release" command.  However:
435                  *
436                  *      http://forums.fedoraforum.org/showthread.php?t=220885
437                  *
438                  * seems to suggest that if you don't have the redhat-lsb
439                  * package installed, you don't have lsb_release, and that
440                  * /etc/fedora-release has the release information on
441                  * Fedora.
442                  *
443                  *      http://linux.die.net/man/1/lsb_release
444                  *
445                  * suggests that there's an /etc/distrib-release file, but
446                  * it doesn't indicate whether "distrib" is literally
447                  * "distrib" or is the name for the distribution, and
448                  * also speaks of an /etc/debian_version file.
449                  *
450                  * "lsb_release" apparently parses /etc/lsb-release, which
451                  * has shell-style assignments, assigning to, among other
452                  * values, DISTRIB_ID (distributor/distribution name),
453                  * DISTRIB_RELEASE (release number of the distribution),
454                  * DISTRIB_DESCRIPTION (*might* be name followed by version,
455                  * but the manpage for lsb_release seems to indicate that's
456                  * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
457                  * "licentious" for the Ubuntu Licentious Lemur release).
458                  * the lsb_release man page also speaks of the distrib-release
459                  * file, but Debian doesn't have one, and Ubuntu 7's
460                  * lsb_release command doesn't look for one.
461                  *
462                  * I've seen references to /etc/redhat-release as well.
463                  *
464                  * At least on my Ubuntu 7 system, /etc/debian_version
465                  * doesn't contain anything interesting (just some Debian
466                  * codenames).
467                  *
468                  * See also
469                  *
470                  *      http://bugs.python.org/issue1322
471                  *
472                  *      http://www.novell.com/coolsolutions/feature/11251.html
473                  *
474                  *      http://linuxmafia.com/faq/Admin/release-files.html
475                  *
476                  * and the Lib/Platform.py file in recent Python 2.x
477                  * releases.
478                  */
479                 g_string_append_printf(str, "%s %s", name.sysname, name.release);
480 #endif /* HAVE_OS_X_FRAMEWORKS */
481         }
482 #else
483         g_string_append(str, "an unknown OS");
484 #endif
485
486         /* Libpcap */
487         g_string_append(str, ", ");
488         get_runtime_pcap_version(str);
489
490         /* zlib */
491 #if defined(HAVE_LIBZ) && !defined(_WIN32)
492         g_string_append_printf(str, ", with libz %s", zlibVersion());
493 #endif
494
495         /* Additional application-dependent information */
496         if (additional_info)
497                 (*additional_info)(str);
498
499         g_string_append(str, ".");
500
501         /* Compiler info */
502
503         /*
504          * See http://predef.sourceforge.net/precomp.html for
505          * information on various defined strings.
506          *
507          * GCC's __VERSION__ is a nice text string for humans to
508          * read.  The page at predef.sourceforge.net largely
509          * describes numeric #defines that encode the version;
510          * if the compiler doesn't also offer a nice printable
511          * string, we should probably prettify the number somehow.
512          */
513 #if defined(__GNUC__) && defined(__VERSION__)
514 #if defined(__clang__)
515         g_string_append_printf(str, "\n\nBuilt using clang %s.\n", __VERSION__);
516 #elif defined(__llvm__)
517         g_string_append_printf(str, "\n\nBuilt using llvm-gcc %s.\n", __VERSION__);
518 #else /* boring old GCC */
519         g_string_append_printf(str, "\n\nBuilt using gcc %s.\n", __VERSION__);
520 #endif /* llvm */
521 #elif defined(__HP_aCC)
522         g_string_append_printf(str, "\n\nBuilt using HP aCC %d.\n", __HP_aCC);
523 #elif defined(__xlC__)
524         g_string_append_printf(str, "\n\nBuilt using IBM XL C %d.%d\n",
525             (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
526 #ifdef __IBMC__
527         if ((__IBMC__ % 10) != 0)
528                 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
529 #endif /* __IBMC__ */
530         g_string_append_printf(str, "\n");
531 #elif defined(__INTEL_COMPILER)
532         g_string_append_printf(str, "\n\nBuilt using Intel C %d.%d",
533             __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
534         if ((__INTEL_COMPILER % 10) != 0)
535                 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
536 #ifdef __INTEL_COMPILER_BUILD_DATE
537         g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
538             __INTEL_COMPILER_BUILD_DATE / 10000,
539             (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
540             __INTEL_COMPILER_BUILD_DATE % 100);
541 #endif /* __INTEL_COMPILER_BUILD_DATE */
542         g_string_append_printf(str, "\n");
543 #elif defined(_MSC_FULL_VER)
544         if (_MSC_FULL_VER > 99999999) {
545                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
546                     (_MSC_FULL_VER / 10000000) - 6,
547                     (_MSC_FULL_VER / 100000) % 100);
548                 if ((_MSC_FULL_VER % 100000) != 0)
549                         g_string_append_printf(str, " build %d",
550                             _MSC_FULL_VER % 100000);
551         } else {
552                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
553                     (_MSC_FULL_VER / 1000000) - 6,
554                     (_MSC_FULL_VER / 10000) % 100);
555                 if ((_MSC_FULL_VER % 10000) != 0)
556                         g_string_append_printf(str, " build %d",
557                             _MSC_FULL_VER % 10000);
558         }
559         g_string_append_printf(str, "\n");
560 #elif defined(_MSC_VER)
561         /* _MSC_FULL_VER not defined, but _MSC_VER defined */
562         g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d\n",
563             (_MSC_VER / 100) - 6, _MSC_VER % 100);
564 #elif defined(__SUNPRO_C)
565         g_string_append_printf(str, "\n\nBuilt using Sun C %d.%d",
566             (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
567         if ((__SUNPRO_C & 0xF) != 0)
568                 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
569         g_string_append_printf(str, "\n");
570 #endif
571
572         end_string(str);
573 }
574
575 /*
576  * Get copyright information.
577  */
578 const char *
579 get_copyright_info(void)
580 {
581         return
582 "Copyright 1998-2010 Gerald Combs <gerald@wireshark.org> and contributors.\n"
583 "This is free software; see the source for copying conditions. There is NO\n"
584 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
585 }
586
587 #if defined(_WIN32)
588 /*
589  * Get the major OS version.
590  */
591 /* XXX - Should this return the minor version as well, e.g. 0x00050002? */
592 guint32
593 get_os_major_version()
594 {
595         OSVERSIONINFO info;
596         info.dwOSVersionInfoSize = sizeof info;
597         if (GetVersionEx(&info)) {
598                 return info.dwMajorVersion;
599         }
600         return 0;
601 }
602 #endif
603
604 /*
605  * Editor modelines
606  *
607  * Local Variables:
608  * c-basic-offset: 8
609  * tab-width: 8
610  * indent-tabs-mode: t
611  * End:
612  *
613  * ex: set shiftwidth=8 tabstop=8 noexpandtab
614  * :indentSize=8:tabSize=8:noTabs=false:
615  */