2 * Routines to report operating system version information
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
32 #ifdef HAVE_OS_X_FRAMEWORKS
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <wsutil/cfutils.h>
39 #include <wsutil/unicode-utils.h>
41 #include <wsutil/os_version_info.h>
44 typedef void (WINAPI *nativesi_func_ptr)(LPSYSTEM_INFO);
48 * Handles the rather elaborate process of getting OS version information
49 * from macOS (we want the macOS version, not the Darwin version, the latter
50 * being easy to get with uname()).
52 #ifdef HAVE_OS_X_FRAMEWORKS
55 * Fetch a string, as a UTF-8 C string, from a dictionary, given a key.
58 get_string_from_dictionary(CFPropertyListRef dict, CFStringRef key)
62 cfstring = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)dict,
66 if (CFGetTypeID(cfstring) != CFStringGetTypeID()) {
67 /* It isn't a string. Punt. */
70 return CFString_to_C_string(cfstring);
74 * Get the macOS version information, and append it to the GString.
75 * Return TRUE if we succeed, FALSE if we fail.
78 get_os_x_version_info(GString *str)
80 static const UInt8 server_version_plist_path[] =
81 "/System/Library/CoreServices/ServerVersion.plist";
82 static const UInt8 system_version_plist_path[] =
83 "/System/Library/CoreServices/SystemVersion.plist";
84 CFURLRef version_plist_file_url;
85 CFReadStreamRef version_plist_stream;
86 CFDictionaryRef version_dict;
90 * On macOS, report the macOS version number as the OS, and put
91 * the Darwin information in parentheses.
93 * Alas, Gestalt() is deprecated in Mountain Lion, so the build
94 * fails if you treat deprecation warnings as fatal. I don't
95 * know of any replacement API, so we fall back on reading
96 * /System/Library/CoreServices/ServerVersion.plist if it
97 * exists, otherwise /System/Library/CoreServices/SystemVersion.plist,
98 * and using ProductUserVisibleVersion. We also get the build
99 * version from ProductBuildVersion and the product name from
102 version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
103 server_version_plist_path, sizeof server_version_plist_path - 1,
105 if (version_plist_file_url == NULL)
107 version_plist_stream = CFReadStreamCreateWithFile(NULL,
108 version_plist_file_url);
109 CFRelease(version_plist_file_url);
110 if (version_plist_stream == NULL)
112 if (!CFReadStreamOpen(version_plist_stream)) {
113 CFRelease(version_plist_stream);
116 * Try SystemVersion.plist.
118 version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
119 system_version_plist_path, sizeof system_version_plist_path - 1,
121 if (version_plist_file_url == NULL)
123 version_plist_stream = CFReadStreamCreateWithFile(NULL,
124 version_plist_file_url);
125 CFRelease(version_plist_file_url);
126 if (version_plist_stream == NULL)
128 if (!CFReadStreamOpen(version_plist_stream)) {
129 CFRelease(version_plist_stream);
133 #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM
134 version_dict = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL,
135 version_plist_stream, 0, kCFPropertyListImmutable,
138 version_dict = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL,
139 version_plist_stream, 0, kCFPropertyListImmutable,
142 if (version_dict == NULL) {
143 CFRelease(version_plist_stream);
146 if (CFGetTypeID(version_dict) != CFDictionaryGetTypeID()) {
147 /* This is *supposed* to be a dictionary. Punt. */
148 CFRelease(version_dict);
149 CFReadStreamClose(version_plist_stream);
150 CFRelease(version_plist_stream);
153 /* Get the product name string. */
154 string = get_string_from_dictionary(version_dict,
155 CFSTR("ProductName"));
156 if (string == NULL) {
157 CFRelease(version_dict);
158 CFReadStreamClose(version_plist_stream);
159 CFRelease(version_plist_stream);
162 g_string_append_printf(str, "%s", string);
165 /* Get the OS version string. */
166 string = get_string_from_dictionary(version_dict,
167 CFSTR("ProductUserVisibleVersion"));
168 if (string == NULL) {
169 CFRelease(version_dict);
170 CFReadStreamClose(version_plist_stream);
171 CFRelease(version_plist_stream);
174 g_string_append_printf(str, " %s", string);
177 /* Get the build string */
178 string = get_string_from_dictionary(version_dict,
179 CFSTR("ProductBuildVersion"));
180 if (string == NULL) {
181 CFRelease(version_dict);
182 CFReadStreamClose(version_plist_stream);
183 CFRelease(version_plist_stream);
186 g_string_append_printf(str, ", build %s", string);
188 CFRelease(version_dict);
189 CFReadStreamClose(version_plist_stream);
190 CFRelease(version_plist_stream);
196 * Get the OS version, and append it to the GString
199 get_os_version_info(GString *str)
202 HMODULE kernel_dll_handle;
203 OSVERSIONINFOEX info;
204 SYSTEM_INFO system_info;
205 nativesi_func_ptr nativesi_func;
206 #elif defined(HAVE_SYS_UTSNAME_H)
214 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
216 * for more than you ever wanted to know about determining the
217 * flavor of Windows on which you're running. Implementing more
218 * of that is left as an exercise to the reader - who should
219 * check any copyright information about code samples on MSDN
220 * before cutting and pasting into Wireshark.
222 * They should also note that you need an OSVERSIONINFOEX structure
223 * to get some of that information, and that not only is that
224 * structure not supported on older versions of Windows, you might
225 * not even be able to compile code that *uses* that structure with
226 * older versions of the SDK.
229 memset(&info, '\0', sizeof info);
230 info.dwOSVersionInfoSize = sizeof info;
231 if (!GetVersionEx((OSVERSIONINFO *)&info)) {
233 * XXX - get the failure reason.
235 g_string_append(str, "unknown Windows version");
239 memset(&system_info, '\0', sizeof system_info);
240 /* Look for and use the GetNativeSystemInfo() function if available to get the correct processor
241 * architecture even when running 32-bit Wireshark in WOW64 (x86 emulation on 64-bit Windows) */
242 kernel_dll_handle = GetModuleHandle(_T("kernel32.dll"));
243 if (kernel_dll_handle == NULL) {
245 * XXX - get the failure reason.
247 g_string_append(str, "unknown Windows version");
250 nativesi_func = (nativesi_func_ptr)GetProcAddress(kernel_dll_handle, "GetNativeSystemInfo");
252 nativesi_func(&system_info);
254 GetSystemInfo(&system_info);
256 switch (info.dwPlatformId) {
258 case VER_PLATFORM_WIN32s:
260 g_string_append_printf(str, "Windows 3.1 with Win32s");
263 case VER_PLATFORM_WIN32_WINDOWS:
265 switch (info.dwMajorVersion) {
268 /* 3 cheers for Microsoft marketing! */
269 switch (info.dwMinorVersion) {
272 g_string_append_printf(str, "Windows 95");
276 g_string_append_printf(str, "Windows 98");
280 g_string_append_printf(str, "Windows Me");
284 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
285 info.dwMajorVersion, info.dwMinorVersion);
291 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
292 info.dwMajorVersion, info.dwMinorVersion);
297 case VER_PLATFORM_WIN32_NT:
299 switch (info.dwMajorVersion) {
303 g_string_append_printf(str, "Windows NT %lu.%lu",
304 info.dwMajorVersion, info.dwMinorVersion);
308 /* 3 cheers for Microsoft marketing! */
309 switch (info.dwMinorVersion) {
312 g_string_append_printf(str, "Windows 2000");
316 g_string_append_printf(str, "Windows XP");
320 if ((info.wProductType == VER_NT_WORKSTATION) &&
321 (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
322 g_string_append_printf(str, "Windows XP Professional x64 Edition");
324 g_string_append_printf(str, "Windows Server 2003");
325 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
326 g_string_append_printf(str, " x64 Edition");
331 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
332 info.dwMajorVersion, info.dwMinorVersion);
338 gboolean is_nt_workstation;
340 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
341 g_string_append(str, "64-bit ");
342 else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
343 g_string_append(str, "32-bit ");
344 #ifndef VER_NT_WORKSTATION
345 #define VER_NT_WORKSTATION 0x01
346 is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
348 is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
350 switch (info.dwMinorVersion) {
352 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
355 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
358 g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
361 g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
364 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
365 info.dwMajorVersion, info.dwMinorVersion);
372 gboolean is_nt_workstation;
374 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
375 g_string_append(str, "64-bit ");
376 else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
377 g_string_append(str, "32-bit ");
378 is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
379 switch (info.dwMinorVersion) {
381 g_string_append_printf(str, is_nt_workstation ? "Windows 10" : "Windows Server 2016");
384 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
385 info.dwMajorVersion, info.dwMinorVersion);
392 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
393 info.dwMajorVersion, info.dwMinorVersion);
395 } /* info.dwMajorVersion */
399 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
400 info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
403 if (info.szCSDVersion[0] != '\0')
404 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
405 g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
406 #elif defined(HAVE_SYS_UTSNAME_H)
408 * We have <sys/utsname.h>, so we assume we have "uname()".
410 if (uname(&name) < 0) {
411 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
416 if (strcmp(name.sysname, "AIX") == 0) {
418 * Yay, IBM! Thanks for doing something different
419 * from most of the other UNIXes out there, and
420 * making "name.version" apparently be the major
421 * version number and "name.release" be the minor
424 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
428 * XXX - get "version" on any other platforms?
430 * On Digital/Tru64 UNIX, it's something unknown.
431 * On Solaris, it's some kind of build information.
432 * On HP-UX, it appears to be some sort of subrevision
434 * On *BSD and Darwin/macOS, it's a long string giving
435 * a build date, config file name, etc., etc., etc..
437 #ifdef HAVE_OS_X_FRAMEWORKS
439 * On macOS, report the macOS version number as the OS
440 * version if we can, and put the Darwin information
443 if (get_os_x_version_info(str)) {
444 /* Success - append the Darwin information. */
445 g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
447 /* Failure - just use the Darwin information. */
448 g_string_append_printf(str, "%s %s", name.sysname, name.release);
450 #else /* HAVE_OS_X_FRAMEWORKS */
452 * XXX - on Linux, are there any APIs to get the distribution
453 * name and version number? I think some distributions have
456 * At least on Linux Standard Base-compliant distributions,
457 * there's an "lsb_release" command. However:
459 * http://forums.fedoraforum.org/showthread.php?t=220885
461 * seems to suggest that if you don't have the redhat-lsb
462 * package installed, you don't have lsb_release, and that
463 * /etc/fedora-release has the release information on
466 * http://linux.die.net/man/1/lsb_release
468 * suggests that there's an /etc/distrib-release file, but
469 * it doesn't indicate whether "distrib" is literally
470 * "distrib" or is the name for the distribution, and
471 * also speaks of an /etc/debian_version file.
473 * "lsb_release" apparently parses /etc/lsb-release, which
474 * has shell-style assignments, assigning to, among other
475 * values, DISTRIB_ID (distributor/distribution name),
476 * DISTRIB_RELEASE (release number of the distribution),
477 * DISTRIB_DESCRIPTION (*might* be name followed by version,
478 * but the manpage for lsb_release seems to indicate that's
479 * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
480 * "licentious" for the Ubuntu Licentious Lemur release).
481 * the lsb_release man page also speaks of the distrib-release
482 * file, but Debian doesn't have one, and Ubuntu 7's
483 * lsb_release command doesn't look for one.
485 * I've seen references to /etc/redhat-release as well.
487 * At least on my Ubuntu 7 system, /etc/debian_version
488 * doesn't contain anything interesting (just some Debian
493 * http://bugs.python.org/issue1322
495 * http://www.novell.com/coolsolutions/feature/11251.html
497 * http://linuxmafia.com/faq/Admin/release-files.html
499 * and the Lib/Platform.py file in recent Python 2.x
502 g_string_append_printf(str, "%s %s", name.sysname, name.release);
503 #endif /* HAVE_OS_X_FRAMEWORKS */
506 g_string_append(str, "an unknown OS");
512 * Get the Windows major OS version.
514 * XXX - Should this return the minor version as well, e.g. 0x00050002?
516 * XXX - I think Microsoft have now stuck it at 6 forever, so it really
517 * distinguishes, on the versions of Windows we currently support and
518 * future Windows versions between Windows 2000/XP (major version 5) and
519 * everything after it (major version 6).
522 get_windows_major_version(void)
526 info.dwOSVersionInfoSize = sizeof info;
527 if (GetVersionEx(&info)) {
528 return info.dwMajorVersion;
535 * Editor modelines - http://www.wireshark.org/tools/modelines.html
540 * indent-tabs-mode: t
543 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
544 * :indentSize=8:tabSize=8:noTabs=false: