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 OS X (we want the OS X 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 OS X 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 OS X, report the OS X 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 OSVERSIONINFOEX info;
203 SYSTEM_INFO system_info;
204 nativesi_func_ptr nativesi_func;
205 #elif defined(HAVE_SYS_UTSNAME_H)
213 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
215 * for more than you ever wanted to know about determining the
216 * flavor of Windows on which you're running. Implementing more
217 * of that is left as an exercise to the reader - who should
218 * check any copyright information about code samples on MSDN
219 * before cutting and pasting into Wireshark.
221 * They should also note that you need an OSVERSIONINFOEX structure
222 * to get some of that information, and that not only is that
223 * structure not supported on older versions of Windows, you might
224 * not even be able to compile code that *uses* that structure with
225 * older versions of the SDK.
228 memset(&info, '\0', sizeof info);
229 info.dwOSVersionInfoSize = sizeof info;
230 if (!GetVersionEx((OSVERSIONINFO *)&info)) {
232 * XXX - get the failure reason.
234 g_string_append(str, "unknown Windows version");
238 memset(&system_info, '\0', sizeof system_info);
239 /* Look for and use the GetNativeSystemInfo() function if available to get the correct processor
240 * architecture even when running 32-bit Wireshark in WOW64 (x86 emulation on 64-bit Windows) */
241 nativesi_func = (nativesi_func_ptr)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetNativeSystemInfo");
243 nativesi_func(&system_info);
245 GetSystemInfo(&system_info);
247 switch (info.dwPlatformId) {
249 case VER_PLATFORM_WIN32s:
251 g_string_append_printf(str, "Windows 3.1 with Win32s");
254 case VER_PLATFORM_WIN32_WINDOWS:
256 switch (info.dwMajorVersion) {
259 /* 3 cheers for Microsoft marketing! */
260 switch (info.dwMinorVersion) {
263 g_string_append_printf(str, "Windows 95");
267 g_string_append_printf(str, "Windows 98");
271 g_string_append_printf(str, "Windows Me");
275 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
276 info.dwMajorVersion, info.dwMinorVersion);
282 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
283 info.dwMajorVersion, info.dwMinorVersion);
288 case VER_PLATFORM_WIN32_NT:
290 switch (info.dwMajorVersion) {
294 g_string_append_printf(str, "Windows NT %lu.%lu",
295 info.dwMajorVersion, info.dwMinorVersion);
299 /* 3 cheers for Microsoft marketing! */
300 switch (info.dwMinorVersion) {
303 g_string_append_printf(str, "Windows 2000");
307 g_string_append_printf(str, "Windows XP");
311 if ((info.wProductType == VER_NT_WORKSTATION) &&
312 (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
313 g_string_append_printf(str, "Windows XP Professional x64 Edition");
315 g_string_append_printf(str, "Windows Server 2003");
316 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
317 g_string_append_printf(str, " x64 Edition");
322 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
323 info.dwMajorVersion, info.dwMinorVersion);
329 gboolean is_nt_workstation;
331 if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
332 g_string_append(str, "64-bit ");
333 else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
334 g_string_append(str, "32-bit ");
335 #ifndef VER_NT_WORKSTATION
336 #define VER_NT_WORKSTATION 0x01
337 is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
339 is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
341 switch (info.dwMinorVersion) {
343 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
346 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
349 g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
352 g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
355 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
356 info.dwMajorVersion, info.dwMinorVersion);
362 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
363 info.dwMajorVersion, info.dwMinorVersion);
365 } /* info.dwMajorVersion */
369 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
370 info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
373 if (info.szCSDVersion[0] != '\0')
374 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
375 g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
376 #elif defined(HAVE_SYS_UTSNAME_H)
378 * We have <sys/utsname.h>, so we assume we have "uname()".
380 if (uname(&name) < 0) {
381 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
386 if (strcmp(name.sysname, "AIX") == 0) {
388 * Yay, IBM! Thanks for doing something different
389 * from most of the other UNIXes out there, and
390 * making "name.version" apparently be the major
391 * version number and "name.release" be the minor
394 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
398 * XXX - get "version" on any other platforms?
400 * On Digital/Tru64 UNIX, it's something unknown.
401 * On Solaris, it's some kind of build information.
402 * On HP-UX, it appears to be some sort of subrevision
404 * On *BSD and Darwin/OS X, it's a long string giving
405 * a build date, config file name, etc., etc., etc..
407 #ifdef HAVE_OS_X_FRAMEWORKS
409 * On Mac OS X, report the Mac OS X version number as
410 * the OS version if we can, and put the Darwin information
413 if (get_os_x_version_info(str)) {
414 /* Success - append the Darwin information. */
415 g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
417 /* Failure - just use the Darwin information. */
418 g_string_append_printf(str, "%s %s", name.sysname, name.release);
420 #else /* HAVE_OS_X_FRAMEWORKS */
422 * XXX - on Linux, are there any APIs to get the distribution
423 * name and version number? I think some distributions have
426 * At least on Linux Standard Base-compliant distributions,
427 * there's an "lsb_release" command. However:
429 * http://forums.fedoraforum.org/showthread.php?t=220885
431 * seems to suggest that if you don't have the redhat-lsb
432 * package installed, you don't have lsb_release, and that
433 * /etc/fedora-release has the release information on
436 * http://linux.die.net/man/1/lsb_release
438 * suggests that there's an /etc/distrib-release file, but
439 * it doesn't indicate whether "distrib" is literally
440 * "distrib" or is the name for the distribution, and
441 * also speaks of an /etc/debian_version file.
443 * "lsb_release" apparently parses /etc/lsb-release, which
444 * has shell-style assignments, assigning to, among other
445 * values, DISTRIB_ID (distributor/distribution name),
446 * DISTRIB_RELEASE (release number of the distribution),
447 * DISTRIB_DESCRIPTION (*might* be name followed by version,
448 * but the manpage for lsb_release seems to indicate that's
449 * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
450 * "licentious" for the Ubuntu Licentious Lemur release).
451 * the lsb_release man page also speaks of the distrib-release
452 * file, but Debian doesn't have one, and Ubuntu 7's
453 * lsb_release command doesn't look for one.
455 * I've seen references to /etc/redhat-release as well.
457 * At least on my Ubuntu 7 system, /etc/debian_version
458 * doesn't contain anything interesting (just some Debian
463 * http://bugs.python.org/issue1322
465 * http://www.novell.com/coolsolutions/feature/11251.html
467 * http://linuxmafia.com/faq/Admin/release-files.html
469 * and the Lib/Platform.py file in recent Python 2.x
472 g_string_append_printf(str, "%s %s", name.sysname, name.release);
473 #endif /* HAVE_OS_X_FRAMEWORKS */
476 g_string_append(str, "an unknown OS");
482 * Get the Windows major OS version.
484 * XXX - Should this return the minor version as well, e.g. 0x00050002?
486 * XXX - I think Microsoft have now stuck it at 6 forever, so it really
487 * distinguishes, on the versions of Windows we currently support and
488 * future Windows versions between Windows 2000/XP (major version 5) and
489 * everything after it (major version 6).
492 get_windows_major_version(void)
496 info.dwOSVersionInfoSize = sizeof info;
497 if (GetVersionEx(&info)) {
498 return info.dwMajorVersion;
505 * Editor modelines - http://www.wireshark.org/tools/modelines.html
510 * indent-tabs-mode: t
513 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
514 * :indentSize=8:tabSize=8:noTabs=false: