If we don't have any of the pcap_datalink_XXX_to_YYY routines,
[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 #if (defined(HAVE_LIBGCRYPT) || defined(HAVE_LIBGNUTLS)) && defined(_WIN32)
45 #include <winposixtype.h>
46 #endif
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 ")";
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 #ifndef HAVE_LIBPCRE
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         /* GnuTLS */
252         g_string_append(str, ", ");
253 #ifdef HAVE_LIBGNUTLS
254         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
255 #else
256         g_string_append(str, "without GnuTLS");
257 #endif /* HAVE_LIBGNUTLS */
258
259         /* Gcrypt */
260         g_string_append(str, ", ");
261 #ifdef HAVE_LIBGCRYPT
262         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
263 #else
264         g_string_append(str, "without Gcrypt");
265 #endif /* HAVE_LIBGCRYPT */
266
267         /* Kerberos */
268         /* XXX - I don't see how to get the version number, at least for KfW */
269         g_string_append(str, ", ");
270 #ifdef HAVE_KERBEROS
271 #ifdef HAVE_MIT_KERBEROS
272         g_string_append(str, "with MIT Kerberos");
273 #else
274         /* HAVE_HEIMDAL_KERBEROS */
275         g_string_append(str, "with Heimdal Kerberos");
276 #endif
277 #else
278         g_string_append(str, "without Kerberos");
279 #endif /* HAVE_KERBEROS */
280
281         /* GeoIP */
282         g_string_append(str, ", ");
283 #ifdef HAVE_GEOIP
284         g_string_append(str, "with GeoIP");
285 #else
286         g_string_append(str, "without GeoIP");
287 #endif /* HAVE_GEOIP */
288
289 }
290
291 /*
292  * Get various library run-time versions, and the OS version, and append
293  * them to the specified GString.
294  */
295 void
296 get_runtime_version_info(GString *str, void (*additional_info)(GString *))
297 {
298 #if defined(_WIN32)
299         OSVERSIONINFOEX info;
300 #elif defined(HAVE_SYS_UTSNAME_H)
301         struct utsname name;
302 #endif
303 #if HAVE_OS_X_FRAMEWORKS
304         long macosx_ver, macosx_major_ver, macosx_minor_ver, macosx_bugfix_ver;
305 #endif
306
307         g_string_append(str, "on ");
308
309 #if defined(_WIN32)
310         /*
311          * See
312          *
313          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
314          *
315          * for more than you ever wanted to know about determining the
316          * flavor of Windows on which you're running.  Implementing more
317          * of that is left as an exercise to the reader - who should
318          * check any copyright information about code samples on MSDN
319          * before cutting and pasting into Wireshark.
320          *
321          * They should also note that you need an OSVERSIONINFOEX structure
322          * to get some of that information, and that not only is that
323          * structure not supported on older versions of Windows, you might
324          * not even be able to compile code that *uses* that structure with
325          * older versions of the SDK.
326          */
327         info.dwOSVersionInfoSize = sizeof info;
328         if (!GetVersionEx((OSVERSIONINFO *)&info)) {
329                 /*
330                  * XXX - get the failure reason.
331                  */
332                 g_string_append(str, "unknown Windows version");
333                 return;
334         }
335         switch (info.dwPlatformId) {
336
337         case VER_PLATFORM_WIN32s:
338                 /* Shyeah, right. */
339                 g_string_append_printf(str, "Windows 3.1 with Win32s");
340                 break;
341
342         case VER_PLATFORM_WIN32_WINDOWS:
343                 /* Windows OT */
344                 switch (info.dwMajorVersion) {
345
346                 case 4:
347                         /* 3 cheers for Microsoft marketing! */
348                         switch (info.dwMinorVersion) {
349
350                         case 0:
351                                 g_string_append_printf(str, "Windows 95");
352                                 break;
353
354                         case 10:
355                                 g_string_append_printf(str, "Windows 98");
356                                 break;
357
358                         case 90:
359                                 g_string_append_printf(str, "Windows Me");
360                                 break;
361
362                         default:
363                                 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
364                                     info.dwMajorVersion, info.dwMinorVersion);
365                                 break;
366                         }
367                         break;
368
369                 default:
370                         g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
371                             info.dwMajorVersion, info.dwMinorVersion);
372                         break;
373                 }
374                 break;
375
376         case VER_PLATFORM_WIN32_NT:
377                 /* Windows NT */
378                 switch (info.dwMajorVersion) {
379
380                 case 3:
381                 case 4:
382                         g_string_append_printf(str, "Windows NT %lu.%lu",
383                             info.dwMajorVersion, info.dwMinorVersion);
384                         break;
385
386                 case 5:
387                         /* 3 cheers for Microsoft marketing! */
388                         switch (info.dwMinorVersion) {
389
390                         case 0:
391                                 g_string_append_printf(str, "Windows 2000");
392                                 break;
393
394                         case 1:
395                                 g_string_append_printf(str, "Windows XP");
396                                 break;
397
398                         case 2:
399                                 g_string_append_printf(str, "Windows Server 2003");
400                                 break;
401
402                         default:
403                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
404                                     info.dwMajorVersion, info.dwMinorVersion);
405                                 break;
406                         }
407                         break;
408
409                 case 6:
410                         if (info.wProductType == VER_NT_WORKSTATION)
411                                 g_string_append_printf(str, "Windows Vista");
412                         else
413                                 g_string_append_printf(str, "Windows Server 2008");
414                         break;
415
416                 default:
417                         g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
418                             info.dwMajorVersion, info.dwMinorVersion);
419                         break;
420                 }
421                 break;
422
423         default:
424                 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
425                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
426                 break;
427         }
428         if (info.szCSDVersion[0] != '\0')
429                 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
430         g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
431 #elif defined(HAVE_SYS_UTSNAME_H)
432         /*
433          * We have <sys/utsname.h>, so we assume we have "uname()".
434          */
435         if (uname(&name) < 0) {
436                 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
437                     strerror(errno));
438                 return;
439         }
440
441         if (strcmp(name.sysname, "AIX") == 0) {
442                 /*
443                  * Yay, IBM!  Thanks for doing something different
444                  * from most of the other UNIXes out there, and
445                  * making "name.version" apparently be the major
446                  * version number and "name.release" be the minor
447                  * version number.
448                  */
449                 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
450                     name.release);
451         } else {
452                 /*
453                  * XXX - get "version" on any other platforms?
454                  *
455                  * On Digital/Tru64 UNIX, it's something unknown.
456                  * On Solaris, it's some kind of build information.
457                  * On HP-UX, it appears to be some sort of subrevision
458                  * thing.
459                  */
460                 g_string_append_printf(str, "%s %s", name.sysname, name.release);
461 #ifdef HAVE_OS_X_FRAMEWORKS
462                 Gestalt(gestaltSystemVersion, &macosx_ver);
463
464                 /* The following functions are only available in MacOS 10.4+ */
465                 if(macosx_ver >= 0x1040) {
466                         Gestalt(gestaltSystemVersionMajor, &macosx_major_ver);
467                         Gestalt(gestaltSystemVersionMinor, &macosx_minor_ver);
468                         Gestalt(gestaltSystemVersionBugFix, &macosx_bugfix_ver);
469
470                         g_string_append_printf(str, " (MacOS %ld.%ld.%ld)",
471                                           macosx_major_ver,
472                                           macosx_minor_ver,
473                                           macosx_bugfix_ver);
474                 } else {
475                         g_string_append_printf(str, " (MacOS X < 10.4 [%lx])",
476                                           macosx_ver);
477                         /* See Apple's Gestalt Manager Reference for meanings
478                          * of the macosx_ver values. */
479                 }
480 #endif /* HAVE_OS_X_FRAMEWORKS */
481         }
482 #else
483         g_string_append(str, "an unknown OS");
484 #endif
485
486         /* Libpcap */
487         g_string_append(str, ", ");
488         get_runtime_pcap_version(str);
489
490         /* Additional application-dependent information */
491         if (additional_info)
492                 (*additional_info)(str);
493
494         g_string_append(str, ".");
495
496         /* Compiler info */
497
498         /*
499          * See http://predef.sourceforge.net/precomp.html for
500          * information on various defined strings.
501          *
502          * GCC's __VERSION__ is a nice text string for humans to
503          * read.  The page at predef.sourceforge.net largely
504          * describes numeric #defines that encode the version;
505          * if the compiler doesn't also offer a nice printable
506          * string, we should probably prettify the number somehow.
507          */
508 #if defined(__GNUC__) && defined(__VERSION__)
509         g_string_append_printf(str, "\n\nBuilt using gcc %s.\n", __VERSION__);
510 #elif defined(__HP_aCC)
511         g_string_append_printf(str, "\n\nBuilt using HP aCC %d.\n", __HP_aCC);
512 #elif defined(__xlC__)
513         g_string_append_printf(str, "\n\nBuilt using IBM XL C %d.%d\n",
514             (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
515 #ifdef __IBMC__
516         if ((__IBMC__ % 10) != 0)
517                 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
518 #endif /* __IBMC__ */
519         g_string_append_printf(str, "\n");
520 #elif defined(__INTEL_COMPILER)
521         g_string_append_printf(str, "\n\nBuilt using Intel C %d.%d",
522             __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
523         if ((__INTEL_COMPILER % 10) != 0)
524                 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
525 #ifdef __INTEL_COMPILER_BUILD_DATE
526         g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
527             __INTEL_COMPILER_BUILD_DATE / 10000,
528             (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
529             __INTEL_COMPILER_BUILD_DATE % 100);
530 #endif /* __INTEL_COMPILER_BUILD_DATE */
531         g_string_append_printf(str, "\n");
532 #elif defined(_MSC_FULL_VER)
533         if (_MSC_FULL_VER > 99999999) {
534                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
535                     (_MSC_FULL_VER / 10000000) - 6,
536                     (_MSC_FULL_VER / 100000) % 100);
537                 if ((_MSC_FULL_VER % 100000) != 0)
538                         g_string_append_printf(str, " build %d",
539                             _MSC_FULL_VER % 100000);
540         } else {
541                 g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
542                     (_MSC_FULL_VER / 1000000) - 6,
543                     (_MSC_FULL_VER / 10000) % 100);
544                 if ((_MSC_FULL_VER % 10000) != 0)
545                         g_string_append_printf(str, " build %d",
546                             _MSC_FULL_VER % 10000);
547         }
548         g_string_append_printf(str, "\n");
549 #elif defined(_MSC_VER)
550         /* _MSC_FULL_VER not defined, but _MSC_VER defined */
551         g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d\n",
552             (_MSC_VER / 100) - 6, _MSC_VER % 100);
553 #elif defined(__SUNPRO_C)
554         g_string_append_printf(str, "\n\nBuilt using Sun C %d.%d",
555             (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
556         if ((__SUNPRO_C & 0xF) != 0)
557                 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
558         g_string_append_printf(str, "\n");
559 #endif
560
561         end_string(str);
562 }
563
564 /*
565  * Get copyright information.
566  */
567 const char *
568 get_copyright_info(void)
569 {
570         return
571 "Copyright 1998-2008 Gerald Combs <gerald@wireshark.org> and contributors.\n"
572 "This is free software; see the source for copying conditions. There is NO\n"
573 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
574 }
575
576 #if defined(_WIN32)
577 /*
578  * Get the major OS version.
579  */
580 /* XXX - Should this return the minor version as well, e.g. 0x00050002? */
581 guint32
582 get_os_major_version()
583 {
584         OSVERSIONINFO info;
585         info.dwOSVersionInfoSize = sizeof info;
586         if (GetVersionEx(&info)) {
587                 return info.dwMajorVersion;
588         }
589         return 0;
590 }
591 #endif
592
593 /*
594  * Editor modelines
595  *
596  * Local Variables:
597  * c-basic-offset: 8
598  * tab-width: 8
599  * indent-tabs-mode: t
600  * End:
601  *
602  * ex: set shiftwidth=8 tabstop=8 noexpandtab
603  * :indentSize=8:tabSize=8:noTabs=false:
604  */