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_MACOS_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_MACOS_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.
77 * XXX - this gives the OS name as "Mac OS X" even if Apple called/calls
78 * it "OS X" or "macOS".
81 get_macos_version_info(GString *str)
83 static const UInt8 server_version_plist_path[] =
84 "/System/Library/CoreServices/ServerVersion.plist";
85 static const UInt8 system_version_plist_path[] =
86 "/System/Library/CoreServices/SystemVersion.plist";
87 CFURLRef version_plist_file_url;
88 CFReadStreamRef version_plist_stream;
89 CFDictionaryRef version_dict;
93 * On macOS, report the macOS version number as the OS, and put
94 * the Darwin information in parentheses.
96 * Alas, Gestalt() is deprecated in Mountain Lion, so the build
97 * fails if you treat deprecation warnings as fatal. I don't
98 * know of any replacement API, so we fall back on reading
99 * /System/Library/CoreServices/ServerVersion.plist if it
100 * exists, otherwise /System/Library/CoreServices/SystemVersion.plist,
101 * and using ProductUserVisibleVersion. We also get the build
102 * version from ProductBuildVersion and the product name from
105 version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
106 server_version_plist_path, sizeof server_version_plist_path - 1,
108 if (version_plist_file_url == NULL)
110 version_plist_stream = CFReadStreamCreateWithFile(NULL,
111 version_plist_file_url);
112 CFRelease(version_plist_file_url);
113 if (version_plist_stream == NULL)
115 if (!CFReadStreamOpen(version_plist_stream)) {
116 CFRelease(version_plist_stream);
119 * Try SystemVersion.plist.
121 version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
122 system_version_plist_path, sizeof system_version_plist_path - 1,
124 if (version_plist_file_url == NULL)
126 version_plist_stream = CFReadStreamCreateWithFile(NULL,
127 version_plist_file_url);
128 CFRelease(version_plist_file_url);
129 if (version_plist_stream == NULL)
131 if (!CFReadStreamOpen(version_plist_stream)) {
132 CFRelease(version_plist_stream);
136 #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM
137 version_dict = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL,
138 version_plist_stream, 0, kCFPropertyListImmutable,
141 version_dict = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL,
142 version_plist_stream, 0, kCFPropertyListImmutable,
145 if (version_dict == NULL) {
146 CFRelease(version_plist_stream);
149 if (CFGetTypeID(version_dict) != CFDictionaryGetTypeID()) {
150 /* This is *supposed* to be a dictionary. Punt. */
151 CFRelease(version_dict);
152 CFReadStreamClose(version_plist_stream);
153 CFRelease(version_plist_stream);
156 /* Get the product name string. */
157 string = get_string_from_dictionary(version_dict,
158 CFSTR("ProductName"));
159 if (string == NULL) {
160 CFRelease(version_dict);
161 CFReadStreamClose(version_plist_stream);
162 CFRelease(version_plist_stream);
165 g_string_append_printf(str, "%s", string);
168 /* Get the OS version string. */
169 string = get_string_from_dictionary(version_dict,
170 CFSTR("ProductUserVisibleVersion"));
171 if (string == NULL) {
172 CFRelease(version_dict);
173 CFReadStreamClose(version_plist_stream);
174 CFRelease(version_plist_stream);
177 g_string_append_printf(str, " %s", string);
180 /* Get the build string */
181 string = get_string_from_dictionary(version_dict,
182 CFSTR("ProductBuildVersion"));
183 if (string == NULL) {
184 CFRelease(version_dict);
185 CFReadStreamClose(version_plist_stream);
186 CFRelease(version_plist_stream);
189 g_string_append_printf(str, ", build %s", string);
191 CFRelease(version_dict);
192 CFReadStreamClose(version_plist_stream);
193 CFRelease(version_plist_stream);
199 * Get the OS version, and append it to the GString
202 get_os_version_info(GString *str)
205 HMODULE kernel_dll_handle;
206 OSVERSIONINFOEX info;
207 SYSTEM_INFO system_info;
208 nativesi_func_ptr nativesi_func;
209 #elif defined(HAVE_SYS_UTSNAME_H)
217 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
219 * for more than you ever wanted to know about determining the
220 * flavor of Windows on which you're running. Implementing more
221 * of that is left as an exercise to the reader - who should
222 * check any copyright information about code samples on MSDN
223 * before cutting and pasting into Wireshark.
225 * They should also note that you need an OSVERSIONINFOEX structure
226 * to get some of that information, and that not only is that
227 * structure not supported on older versions of Windows, you might
228 * not even be able to compile code that *uses* that structure with
229 * older versions of the SDK.
232 memset(&info, '\0', sizeof info);
233 info.dwOSVersionInfoSize = sizeof info;
234 if (!GetVersionEx((OSVERSIONINFO *)&info)) {
236 * XXX - get the failure reason.
238 g_string_append(str, "unknown Windows version");
242 memset(&system_info, '\0', sizeof system_info);
243 /* Look for and use the GetNativeSystemInfo() function if available to get the correct processor
244 * architecture even when running 32-bit Wireshark in WOW64 (x86 emulation on 64-bit Windows) */
245 kernel_dll_handle = GetModuleHandle(_T("kernel32.dll"));
246 if (kernel_dll_handle == NULL) {
248 * XXX - get the failure reason.
250 g_string_append(str, "unknown Windows version");
253 nativesi_func = (nativesi_func_ptr)GetProcAddress(kernel_dll_handle, "GetNativeSystemInfo");
255 nativesi_func(&system_info);
257 GetSystemInfo(&system_info);
259 switch (info.dwPlatformId) {
261 case VER_PLATFORM_WIN32s:
263 g_string_append_printf(str, "Windows 3.1 with Win32s");
266 case VER_PLATFORM_WIN32_WINDOWS:
268 switch (info.dwMajorVersion) {
271 /* 3 cheers for Microsoft marketing! */
272 switch (info.dwMinorVersion) {
275 g_string_append_printf(str, "Windows 95");
279 g_string_append_printf(str, "Windows 98");
283 g_string_append_printf(str, "Windows Me");
287 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
288 info.dwMajorVersion, info.dwMinorVersion);
294 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
295 info.dwMajorVersion, info.dwMinorVersion);
300 case VER_PLATFORM_WIN32_NT:
302 switch (info.dwMajorVersion) {
306 g_string_append_printf(str, "Windows NT %lu.%lu",
307 info.dwMajorVersion, info.dwMinorVersion);
311 /* 3 cheers for Microsoft marketing! */
312 switch (info.dwMinorVersion) {
315 g_string_append_printf(str, "Windows 2000");
319 g_string_append_printf(str, "Windows XP");
323 if ((info.wProductType == VER_NT_WORKSTATION) &&
324 (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
325 g_string_append_printf(str, "Windows XP Professional x64 Edition");
327 g_string_append_printf(str, "Windows Server 2003");
328 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
329 g_string_append_printf(str, " x64 Edition");
334 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
335 info.dwMajorVersion, info.dwMinorVersion);
341 gboolean is_nt_workstation;
343 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
344 g_string_append(str, "64-bit ");
345 else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
346 g_string_append(str, "32-bit ");
347 #ifndef VER_NT_WORKSTATION
348 #define VER_NT_WORKSTATION 0x01
349 is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
351 is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
353 switch (info.dwMinorVersion) {
355 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
358 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
361 g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
364 g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
367 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
368 info.dwMajorVersion, info.dwMinorVersion);
375 gboolean is_nt_workstation;
377 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
378 g_string_append(str, "64-bit ");
379 else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
380 g_string_append(str, "32-bit ");
381 is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
382 switch (info.dwMinorVersion) {
384 g_string_append_printf(str, is_nt_workstation ? "Windows 10" : "Windows Server 2016");
387 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
388 info.dwMajorVersion, info.dwMinorVersion);
395 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
396 info.dwMajorVersion, info.dwMinorVersion);
398 } /* info.dwMajorVersion */
402 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
403 info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
406 if (info.szCSDVersion[0] != '\0')
407 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
408 g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
409 #elif defined(HAVE_SYS_UTSNAME_H)
411 * We have <sys/utsname.h>, so we assume we have "uname()".
413 if (uname(&name) < 0) {
414 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
419 if (strcmp(name.sysname, "AIX") == 0) {
421 * Yay, IBM! Thanks for doing something different
422 * from most of the other UNIXes out there, and
423 * making "name.version" apparently be the major
424 * version number and "name.release" be the minor
427 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
431 * XXX - get "version" on any other platforms?
433 * On Digital/Tru64 UNIX, it's something unknown.
434 * On Solaris, it's some kind of build information.
435 * On HP-UX, it appears to be some sort of subrevision
437 * On *BSD and Darwin/macOS, it's a long string giving
438 * a build date, config file name, etc., etc., etc..
440 #ifdef HAVE_MACOS_FRAMEWORKS
442 * On macOS, report the macOS version number as the OS
443 * version if we can, and put the Darwin information
446 if (get_macos_version_info(str)) {
447 /* Success - append the Darwin information. */
448 g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
450 /* Failure - just use the Darwin information. */
451 g_string_append_printf(str, "%s %s", name.sysname, name.release);
453 #else /* HAVE_MACOS_FRAMEWORKS */
455 * XXX - on Linux, are there any APIs to get the distribution
456 * name and version number? I think some distributions have
459 * At least on Linux Standard Base-compliant distributions,
460 * there's an "lsb_release" command. However:
462 * http://forums.fedoraforum.org/showthread.php?t=220885
464 * seems to suggest that if you don't have the redhat-lsb
465 * package installed, you don't have lsb_release, and that
466 * /etc/fedora-release has the release information on
469 * http://linux.die.net/man/1/lsb_release
471 * suggests that there's an /etc/distrib-release file, but
472 * it doesn't indicate whether "distrib" is literally
473 * "distrib" or is the name for the distribution, and
474 * also speaks of an /etc/debian_version file.
476 * "lsb_release" apparently parses /etc/lsb-release, which
477 * has shell-style assignments, assigning to, among other
478 * values, DISTRIB_ID (distributor/distribution name),
479 * DISTRIB_RELEASE (release number of the distribution),
480 * DISTRIB_DESCRIPTION (*might* be name followed by version,
481 * but the manpage for lsb_release seems to indicate that's
482 * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
483 * "licentious" for the Ubuntu Licentious Lemur release).
484 * the lsb_release man page also speaks of the distrib-release
485 * file, but Debian doesn't have one, and Ubuntu 7's
486 * lsb_release command doesn't look for one.
488 * I've seen references to /etc/redhat-release as well.
490 * At least on my Ubuntu 7 system, /etc/debian_version
491 * doesn't contain anything interesting (just some Debian
496 * http://bugs.python.org/issue1322
498 * http://www.novell.com/coolsolutions/feature/11251.html
500 * http://linuxmafia.com/faq/Admin/release-files.html
502 * and the Lib/Platform.py file in recent Python 2.x
505 g_string_append_printf(str, "%s %s", name.sysname, name.release);
506 #endif /* HAVE_MACOS_FRAMEWORKS */
509 g_string_append(str, "an unknown OS");
515 * Get the Windows major OS version.
517 * XXX - Should this return the minor version as well, e.g. 0x00050002?
519 * XXX - I think Microsoft have now stuck it at 6 forever, so it really
520 * distinguishes, on the versions of Windows we currently support and
521 * future Windows versions between Windows 2000/XP (major version 5) and
522 * everything after it (major version 6).
525 get_windows_major_version(void)
529 info.dwOSVersionInfoSize = sizeof info;
530 if (GetVersionEx(&info)) {
531 return info.dwMajorVersion;
538 * Editor modelines - http://www.wireshark.org/tools/modelines.html
543 * indent-tabs-mode: t
546 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
547 * :indentSize=8:tabSize=8:noTabs=false: