From Richard Brodie via bug 4606:
[metze/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_PYTHON
37 #include <Python.h> /* to get the Python version number (PY_VERSION) */
38 #endif
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_LIBGCRYPT
49 #include <gcrypt.h>
50 #endif /* HAVE_LIBGCRYPT */
51
52 #ifdef HAVE_LIBGNUTLS
53 #include <gnutls/gnutls.h>
54 #endif /* HAVE_LIBGNUTLS */
55
56 #ifdef HAVE_SYS_UTSNAME_H
57 #include <sys/utsname.h>
58 #endif
59
60 #include "version_info.h"
61 #include "capture-pcap-util.h"
62 #include <wsutil/unicode-utils.h>
63
64 #include "svnversion.h"
65
66 #ifdef HAVE_WINDOWS_H
67 #include <windows.h>
68 #endif
69
70 #ifdef HAVE_C_ARES
71 #include <ares_version.h>
72 #endif
73
74 #ifdef HAVE_LUA_5_1
75 #include <lua.h>
76 #endif
77
78 #ifdef HAVE_LIBSMI
79 #include <smi.h>
80 #endif
81
82 #ifdef HAVE_OS_X_FRAMEWORKS
83 #include <CoreServices/CoreServices.h>
84 #endif
85
86 #ifdef HAVE_LIBCAP
87 # include <sys/capability.h>
88 #endif
89
90 #ifdef HAVE_GEOIP
91 #include <epan/geoip_db.h>
92 #endif
93
94 #ifdef SVNVERSION
95         const char *wireshark_svnversion = " (" SVNVERSION " from " SVNPATH ")";
96 #else
97         const char *wireshark_svnversion = "";
98 #endif
99
100 /*
101  * If the string doesn't end with a newline, append one.
102  * Then word-wrap it to 80 columns.
103  */
104 static void
105 end_string(GString *str)
106 {
107         size_t point;
108         char *p, *q;
109
110         point = strlen(str->str);
111         if (point == 0 || str->str[point - 1] != '\n')
112                 g_string_append(str, "\n");
113         p = str->str;
114         while (*p != '\0') {
115                 q = strchr(p, '\n');
116                 if (q - p > 80) {
117                         /*
118                          * Break at or before this point.
119                          */
120                         q = p + 80;
121                         while (q > p && *q != ' ')
122                                 q--;
123                         if (q != p)
124                                 *q = '\n';
125                 }
126                 p = q + 1;
127         }
128 }
129
130 /*
131  * Get various library compile-time versions and append them to
132  * the specified GString.
133  *
134  * "additional_info" is called at the end to append any additional
135  * information; this is required in order to, for example, put the
136  * Portaudio information at the end of the string, as we currently
137  * don't use Portaudio in TShark.
138  */
139 void
140 get_compiled_version_info(GString *str, void (*additional_info)(GString *))
141 {
142         /* GLIB */
143         g_string_append(str, "with ");
144         g_string_append_printf(str,
145 #ifdef GLIB_MAJOR_VERSION
146             "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
147             GLIB_MICRO_VERSION);
148 #else
149             "GLib (version unknown)");
150 #endif
151
152         /* Libpcap */
153         g_string_append(str, ", ");
154         get_compiled_pcap_version(str);
155
156         /* LIBZ */
157         g_string_append(str, ", ");
158 #ifdef HAVE_LIBZ
159         g_string_append(str, "with libz ");
160 #ifdef ZLIB_VERSION
161         g_string_append(str, ZLIB_VERSION);
162 #else /* ZLIB_VERSION */
163         g_string_append(str, "(version unknown)");
164 #endif /* ZLIB_VERSION */
165 #else /* HAVE_LIBZ */
166         g_string_append(str, "without libz");
167 #endif /* HAVE_LIBZ */
168
169         /* LIBCAP */
170         g_string_append(str, ", ");
171 #ifdef HAVE_LIBCAP
172         g_string_append(str, "with POSIX capabilities");
173 #ifdef _LINUX_CAPABILITY_VERSION
174         g_string_append(str, " (Linux)");
175 #endif /* _LINUX_CAPABILITY_VERSION */
176 #else /* HAVE_LIBCAP */
177         g_string_append(str, "without POSIX capabilities");
178 #endif /* HAVE_LIBCAP */
179
180         /* Additional application-dependent information */
181         if (additional_info)
182                 (*additional_info)(str);
183         g_string_append(str, ".");
184
185 #if !defined(HAVE_LIBPCRE) && !GLIB_CHECK_VERSION(2,14,0)
186         g_string_append(str,
187         "\nNOTE: this build doesn't support the \"matches\" operator for Wireshark filter syntax");
188         g_string_append(str, ".");
189 #endif  /* HAVE_LIBPCRE */
190
191         end_string(str);
192 }
193
194 /*
195  * Get compile-time information used only by applications that use
196  * libwireshark.
197  */
198 void
199 get_epan_compiled_version_info(GString *str)
200 {
201         /* PCRE */
202         g_string_append(str, ", ");
203 #ifdef HAVE_LIBPCRE
204         g_string_append(str, "with libpcre ");
205 #ifdef PCRE_MAJOR
206 #ifdef PCRE_MINOR
207         g_string_append_printf(str, "%u.%u", PCRE_MAJOR, PCRE_MINOR);
208 #else                   /* PCRE_MINOR */
209         g_string_append_printf(str, "%u", PCRE_MAJOR);
210 #endif                  /* PCRE_MINOR */
211 #else           /* PCRE_MAJOR */
212         g_string_append(str, "(version unknown)");
213 #endif          /* PCRE_MAJOR */
214 #else   /* HAVE_LIBPCRE */
215         g_string_append(str, "without libpcre");
216 #endif  /* HAVE_LIBPCRE */
217
218         /* SNMP */
219         g_string_append(str, ", ");
220 #ifdef HAVE_LIBSMI
221         g_string_append(str, "with SMI " SMI_VERSION_STRING);
222 #else /* no SNMP library */
223         g_string_append(str, "without SMI");
224 #endif /* _SMI_H */
225
226         /* c-ares */
227         g_string_append(str, ", ");
228 #ifdef HAVE_C_ARES
229         g_string_append(str, "with c-ares " ARES_VERSION_STR);
230 #else
231         g_string_append(str, "without c-ares");
232
233         /* ADNS - only add if no c-ares */
234         g_string_append(str, ", ");
235 #ifdef HAVE_GNU_ADNS
236         g_string_append(str, "with ADNS");
237 #else
238         g_string_append(str, "without ADNS");
239 #endif /* HAVE_GNU_ADNS */
240 #endif /* HAVE_C_ARES */
241
242         /* LUA */
243         g_string_append(str, ", ");
244 #ifdef HAVE_LUA_5_1
245         g_string_append(str, "with ");
246         g_string_append(str, LUA_VERSION);
247 #else
248         g_string_append(str, "without Lua");
249 #endif /* HAVE_LUA_5_1 */
250
251         g_string_append(str, ", ");
252 #ifdef HAVE_PYTHON
253         g_string_append(str, "with Python");
254 #ifdef PY_VERSION
255         g_string_append(str, " " PY_VERSION);
256 #endif /* PY_VERSION */
257 #else
258         g_string_append(str, "without Python");
259 #endif /* HAVE_PYTHON */
260
261         /* GnuTLS */
262         g_string_append(str, ", ");
263 #ifdef HAVE_LIBGNUTLS
264         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
265 #else
266         g_string_append(str, "without GnuTLS");
267 #endif /* HAVE_LIBGNUTLS */
268
269         /* Gcrypt */
270         g_string_append(str, ", ");
271 #ifdef HAVE_LIBGCRYPT
272         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
273 #else
274         g_string_append(str, "without Gcrypt");
275 #endif /* HAVE_LIBGCRYPT */
276
277         /* Kerberos */
278         /* XXX - I don't see how to get the version number, at least for KfW */
279         g_string_append(str, ", ");
280 #ifdef HAVE_KERBEROS
281 #ifdef HAVE_MIT_KERBEROS
282         g_string_append(str, "with MIT Kerberos");
283 #else
284         /* HAVE_HEIMDAL_KERBEROS */
285         g_string_append(str, "with Heimdal Kerberos");
286 #endif
287 #else
288         g_string_append(str, "without Kerberos");
289 #endif /* HAVE_KERBEROS */
290
291         /* GeoIP */
292         g_string_append(str, ", ");
293 #ifdef HAVE_GEOIP
294         g_string_append(str, "with GeoIP");
295 #else
296         g_string_append(str, "without GeoIP");
297 #endif /* HAVE_GEOIP */
298
299 }
300
301 /*
302  * Get various library run-time versions, and the OS version, and append
303  * them to the specified GString.
304  */
305 void
306 get_runtime_version_info(GString *str, void (*additional_info)(GString *))
307 {
308 #if defined(_WIN32)
309         OSVERSIONINFOEX info;
310         SYSTEM_INFO system_info;
311 #elif defined(HAVE_SYS_UTSNAME_H)
312         struct utsname name;
313 #endif
314 #if HAVE_OS_X_FRAMEWORKS
315         SInt32 macosx_ver, macosx_major_ver, macosx_minor_ver, macosx_bugfix_ver;
316 #endif
317
318         g_string_append(str, "on ");
319
320 #if defined(_WIN32)
321         /*
322          * See
323          *
324          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
325          *
326          * for more than you ever wanted to know about determining the
327          * flavor of Windows on which you're running.  Implementing more
328          * of that is left as an exercise to the reader - who should
329          * check any copyright information about code samples on MSDN
330          * before cutting and pasting into Wireshark.
331          *
332          * They should also note that you need an OSVERSIONINFOEX structure
333          * to get some of that information, and that not only is that
334          * structure not supported on older versions of Windows, you might
335          * not even be able to compile code that *uses* that structure with
336          * older versions of the SDK.
337          */
338
339         memset(&info, '\0', sizeof info);
340         info.dwOSVersionInfoSize = sizeof info;
341         if (!GetVersionEx((OSVERSIONINFO *)&info)) {
342                 /*
343                  * XXX - get the failure reason.
344                  */
345                 g_string_append(str, "unknown Windows version");
346                 return;
347         }
348
349         memset(&system_info, '\0', sizeof system_info);
350         GetSystemInfo(&system_info);    /* only for W2K or greater .... (which is what we support) */
351
352         switch (info.dwPlatformId) {
353
354         case VER_PLATFORM_WIN32s:
355                 /* Shyeah, right. */
356                 g_string_append_printf(str, "Windows 3.1 with Win32s");
357                 break;
358
359         case VER_PLATFORM_WIN32_WINDOWS:
360                 /* Windows OT */
361                 switch (info.dwMajorVersion) {
362
363                 case 4:
364                         /* 3 cheers for Microsoft marketing! */
365                         switch (info.dwMinorVersion) {
366
367                         case 0:
368                                 g_string_append_printf(str, "Windows 95");
369                                 break;
370
371                         case 10:
372                                 g_string_append_printf(str, "Windows 98");
373                                 break;
374
375                         case 90:
376                                 g_string_append_printf(str, "Windows Me");
377                                 break;
378
379                         default:
380                                 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
381                                     info.dwMajorVersion, info.dwMinorVersion);
382                                 break;
383                         }
384                         break;
385
386                 default:
387                         g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
388                             info.dwMajorVersion, info.dwMinorVersion);
389                         break;
390                 }
391                 break;
392
393         case VER_PLATFORM_WIN32_NT:
394                 /* Windows NT */
395                 switch (info.dwMajorVersion) {
396
397                 case 3:
398                 case 4:
399                         g_string_append_printf(str, "Windows NT %lu.%lu",
400                             info.dwMajorVersion, info.dwMinorVersion);
401                         break;
402
403                 case 5:
404                         /* 3 cheers for Microsoft marketing! */
405                         switch (info.dwMinorVersion) {
406
407                         case 0:
408                                 g_string_append_printf(str, "Windows 2000");
409                                 break;
410
411                         case 1:
412                                 g_string_append_printf(str, "Windows XP");
413                                 break;
414
415                         case 2:
416                                 if ((info.wProductType == VER_NT_WORKSTATION) &&
417                                     (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
418                                         g_string_append_printf(str, "Windows XP Professional x64 Edition");
419                                 } else {
420                                         g_string_append_printf(str, "Windows Server 2003");
421                                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
422                                                 g_string_append_printf(str, " x64 Edition");
423                                 }
424                                 break;
425
426                         default:
427                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
428                                                        info.dwMajorVersion, info.dwMinorVersion);
429                                 break;
430                         }
431                         break;
432
433                 case 6: {
434                         gboolean is_nt_workstation;
435
436                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
437                                 g_string_append(str, "64-bit ");
438                         else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
439                                 g_string_append(str, "32-bit ");
440 #ifndef VER_NT_WORKSTATION
441 #define VER_NT_WORKSTATION 0x01
442                         is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
443 #else
444                         is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
445 #endif
446                         switch (info.dwMinorVersion) {
447                         case 0:
448                                 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
449                                 break;
450                         case 1:
451                                 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
452                                 break;
453                         default:
454                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
455                                                        info.dwMajorVersion, info.dwMinorVersion);
456                                 break;
457                         }
458                         break;
459                 }  /* case 6 */
460                 default:
461                         g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
462                             info.dwMajorVersion, info.dwMinorVersion);
463                         break;
464                 } /* info.dwMajorVersion */
465                 break;
466
467         default:
468                 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
469                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
470                 break;
471         }
472         if (info.szCSDVersion[0] != '\0')
473                 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
474         g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
475 #elif defined(HAVE_SYS_UTSNAME_H)
476         /*
477          * We have <sys/utsname.h>, so we assume we have "uname()".
478          */
479         if (uname(&name) < 0) {
480                 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
481                     strerror(errno));
482                 return;
483         }
484
485         if (strcmp(name.sysname, "AIX") == 0) {
486                 /*
487                  * Yay, IBM!  Thanks for doing something different
488                  * from most of the other UNIXes out there, and
489                  * making "name.version" apparently be the major
490                  * version number and "name.release" be the minor
491                  * version number.
492                  */
493                 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
494                     name.release);
495         } else {
496                 /*
497                  * XXX - get "version" on any other platforms?
498                  *
499                  * On Digital/Tru64 UNIX, it's something unknown.
500                  * On Solaris, it's some kind of build information.
501                  * On HP-UX, it appears to be some sort of subrevision
502                  * thing.
503                  */
504                 g_string_append_printf(str, "%s %s", name.sysname, name.release);
505 #ifdef HAVE_OS_X_FRAMEWORKS
506                 Gestalt(gestaltSystemVersion, &macosx_ver);
507
508                 /* The following functions are only available in MacOS 10.4+ */
509                 if(macosx_ver >= 0x1040) {
510                         Gestalt(gestaltSystemVersionMajor, &macosx_major_ver);
511                         Gestalt(gestaltSystemVersionMinor, &macosx_minor_ver);
512                         Gestalt(gestaltSystemVersionBugFix, &macosx_bugfix_ver);
513
514                         g_string_append_printf(str, " (MacOS %ld.%ld.%ld)",
515                                           (long)macosx_major_ver,
516                                           (long)macosx_minor_ver,
517                                           (long)macosx_bugfix_ver);
518                 } else {
519                         g_string_append_printf(str, " (MacOS X < 10.4 [%lx])",
520                                           (long)macosx_ver);
521                         /* See Apple's Gestalt Manager Reference for meanings
522                          * of the macosx_ver values. */
523                 }
524 #endif /* HAVE_OS_X_FRAMEWORKS */
525         }
526 #else
527         g_string_append(str, "an unknown OS");
528 #endif
529
530         /* Libpcap */
531         g_string_append(str, ", ");
532         get_runtime_pcap_version(str);
533
534         /* zlib */
535 #ifdef HAVE_LIBZ
536         g_string_append_printf(str, ", with libz %s", zlibVersion());
537 #endif
538
539     /* GnuTLS */
540 #ifdef HAVE_LIBGNUTLS
541         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
542 #endif /* HAVE_LIBGNUTLS */
543
544         /* Gcrypt */
545 #ifdef HAVE_LIBGCRYPT
546         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
547 #endif /* HAVE_LIBGCRYPT */
548
549         /* Additional application-dependent information */
550         if (additional_info)
551                 (*additional_info)(str);
552
553         g_string_append(str, ".");
554
555         /* Compiler info */
556
557         /*
558          * See http://predef.sourceforge.net/precomp.html for
559          * information on various defined strings.
560          *
561          * GCC's __VERSION__ is a nice text string for humans to
562          * read.  The page at predef.sourceforge.net largely
563          * describes numeric #defines that encode the version;
564          * if the compiler doesn't also offer a nice printable
565          * string, we should probably prettify the number somehow.
566          */
567 #if defined(__GNUC__) && defined(__VERSION__)
568         g_string_append_printf(str, "\n\nBuilt using gcc %s.\n", __VERSION__);
569 #elif defined(__HP_aCC)
570         g_string_append_printf(str, "\n\nBuilt using HP aCC %d.\n", __HP_aCC);
571 #elif defined(__xlC__)
572         g_string_append_printf(str, "\n\nBuilt using IBM XL C %d.%d\n",
573             (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
574 #ifdef __IBMC__
575         if ((__IBMC__ % 10) != 0)
576                 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
577 #endif /* __IBMC__ */
578         g_string_append_printf(str, "\n");
579 #elif defined(__INTEL_COMPILER)
580         g_string_append_printf(str, "\n\nBuilt using Intel C %d.%d",
581             __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
582         if ((__INTEL_COMPILER % 10) != 0)
583                 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
584 #ifdef __INTEL_COMPILER_BUILD_DATE
585         g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
586             __INTEL_COMPILER_BUILD_DATE / 10000,
587             (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
588             __INTEL_COMPILER_BUILD_DATE % 100);
589 #endif /* __INTEL_COMPILER_BUILD_DATE */
590         g_string_append_printf(str, "\n");
591 #elif defined(_MSC_FULL_VER)
592         if (_MSC_FULL_VER > 99999999) {
593                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
594                     (_MSC_FULL_VER / 10000000) - 6,
595                     (_MSC_FULL_VER / 100000) % 100);
596                 if ((_MSC_FULL_VER % 100000) != 0)
597                         g_string_append_printf(str, " build %d",
598                             _MSC_FULL_VER % 100000);
599         } else {
600                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
601                     (_MSC_FULL_VER / 1000000) - 6,
602                     (_MSC_FULL_VER / 10000) % 100);
603                 if ((_MSC_FULL_VER % 10000) != 0)
604                         g_string_append_printf(str, " build %d",
605                             _MSC_FULL_VER % 10000);
606         }
607         g_string_append_printf(str, "\n");
608 #elif defined(_MSC_VER)
609         /* _MSC_FULL_VER not defined, but _MSC_VER defined */
610         g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d\n",
611             (_MSC_VER / 100) - 6, _MSC_VER % 100);
612 #elif defined(__SUNPRO_C)
613         g_string_append_printf(str, "\n\nBuilt using Sun C %d.%d",
614             (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
615         if ((__SUNPRO_C & 0xF) != 0)
616                 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
617         g_string_append_printf(str, "\n");
618 #endif
619
620         end_string(str);
621 }
622
623 /*
624  * Get copyright information.
625  */
626 const char *
627 get_copyright_info(void)
628 {
629         return
630 "Copyright 1998-2010 Gerald Combs <gerald@wireshark.org> and contributors.\n"
631 "This is free software; see the source for copying conditions. There is NO\n"
632 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
633 }
634
635 #if defined(_WIN32)
636 /*
637  * Get the major OS version.
638  */
639 /* XXX - Should this return the minor version as well, e.g. 0x00050002? */
640 guint32
641 get_os_major_version()
642 {
643         OSVERSIONINFO info;
644         info.dwOSVersionInfoSize = sizeof info;
645         if (GetVersionEx(&info)) {
646                 return info.dwMajorVersion;
647         }
648         return 0;
649 }
650 #endif
651
652 /*
653  * Editor modelines
654  *
655  * Local Variables:
656  * c-basic-offset: 8
657  * tab-width: 8
658  * indent-tabs-mode: t
659  * End:
660  *
661  * ex: set shiftwidth=8 tabstop=8 noexpandtab
662  * :indentSize=8:tabSize=8:noTabs=false:
663  */