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