Cope with NT 6.0, and with the x86-64 version of XP being NT 5.2 rather
[obnox/wireshark/wip.git] / version_info.c
1 /* version_info.c
2  * Routines to report version information for stuff used by Ethereal
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_LIBPCAP
30 #include <pcap.h>
31 #endif /* HAVE_LIBPCAP */
32
33 #include <glib.h>
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #ifdef HAVE_LIBZ
40 #include <zlib.h>       /* to get the libz version number */
41 #endif
42
43 #ifdef HAVE_LIBPCRE
44 #include <pcre.h>       /* to get the libpcre version number */
45 #endif /* HAVE_LIBPCRE */
46
47 /*
48  * This has to come after the include of <pcap.h>, as the include of
49  * <pcap.h> might cause <winsock2.h> to be included, and if we've
50  * already included <winsock.h> as a result of including <windows.h>,
51  * we get a bunch of redefinitions.
52  */
53 #ifdef HAVE_WINDOWS_H
54 #include <windows.h>
55 #endif
56
57 #ifdef HAVE_SOME_SNMP
58
59 #ifdef HAVE_NET_SNMP
60 #include <net-snmp/version.h>
61 #endif /* HAVE_NET_SNMP */
62
63 #ifdef HAVE_UCD_SNMP
64 #include <ucd-snmp/version.h>
65 #endif /* HAVE_UCD_SNMP */
66
67 #endif /* HAVE_SOME_SNMP */
68
69 #ifdef HAVE_SYS_UTSNAME_H
70 #include <sys/utsname.h>
71 #endif
72
73 #include "version_info.h"
74 #include "pcap-util.h"
75
76 #include "svnversion.h"
77
78 #ifdef SVNVERSION
79         const char *svnversion = " (" SVNVERSION ")";
80 #else
81         const char *svnversion = "";
82 #endif
83
84
85 /*
86  * See whether the last line in the string goes past column 80; if so,
87  * replace the blank at the specified point with a newline.
88  */
89 static void
90 do_word_wrap(GString *str, gint point)
91 {
92         char *line_begin;
93
94         line_begin = strrchr(str->str, '\n');
95         if (line_begin == NULL)
96                 line_begin = str->str;
97         else
98                 line_begin++;
99         if (strlen(line_begin) > 80) {
100                 g_assert(str->str[point] == ' ');
101                 str->str[point] = '\n';
102         }
103 }       
104
105 /*
106  * Get various library compile-time versions and append them to
107  * the specified GString.
108  */
109 void
110 get_compiled_version_info(GString *str)
111 {
112         gint break_point;
113
114         g_string_append(str, "with ");
115         g_string_sprintfa(str,
116 #ifdef GLIB_MAJOR_VERSION
117             "GLib %d.%d.%d,", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
118             GLIB_MICRO_VERSION);
119 #else
120             "GLib (version unknown),");
121 #endif
122
123         g_string_append(str, " ");
124         break_point = str->len - 1;
125         get_compiled_pcap_version(str);
126         g_string_append(str, ",");
127         do_word_wrap(str, break_point);
128
129         g_string_append(str, " ");
130         break_point = str->len - 1;
131 #ifdef HAVE_LIBZ
132         g_string_append(str, "with libz ");
133 #ifdef ZLIB_VERSION
134         g_string_append(str, ZLIB_VERSION);
135 #else /* ZLIB_VERSION */
136         g_string_append(str, "(version unknown)");
137 #endif /* ZLIB_VERSION */
138 #else /* HAVE_LIBZ */
139         g_string_append(str, "without libz");
140 #endif /* HAVE_LIBZ */
141         g_string_append(str, ",");
142         do_word_wrap(str, break_point);
143
144         g_string_append(str, " ");
145         break_point = str->len - 1;
146 #ifdef HAVE_LIBPCRE
147         g_string_append(str, "with libpcre ");
148 #ifdef PCRE_MAJOR
149 #ifdef PCRE_MINOR
150         g_string_sprintfa(str, "%u.%u", PCRE_MAJOR, PCRE_MINOR);
151 #else                   /* PCRE_MINOR */
152         g_string_sprintfa(str, "%u", PCRE_MAJOR);
153 #endif                  /* PCRE_MINOR */
154 #else           /* PCRE_MAJOR */
155         g_string_append(str, "(version unknown)");
156 #endif          /* PCRE_MAJOR */
157 #else   /* HAVE_LIBPCRE */
158         g_string_append(str, "without libpcre");
159 #endif  /* HAVE_LIBPCRE */
160
161         g_string_append(str, ",");
162         do_word_wrap(str, break_point);
163
164 /* Oh, this is pretty. */
165 /* Oh, ha.  you think that was pretty.  Try this:! --Wes */
166         g_string_append(str, " ");
167         break_point = str->len - 1;
168 #ifdef HAVE_SOME_SNMP
169
170 #ifdef HAVE_UCD_SNMP
171         g_string_append(str, "with UCD-SNMP ");
172         g_string_append(str, VersionInfo);
173 #endif /* HAVE_UCD_SNMP */
174
175 #ifdef HAVE_NET_SNMP
176         g_string_append(str, "with Net-SNMP ");
177         g_string_append(str, netsnmp_get_version());
178 #endif /* HAVE_NET_SNMP */
179
180 #else /* no SNMP library */
181         g_string_append(str, "without UCD-SNMP or Net-SNMP");
182 #endif /* HAVE_SOME_SNMP */
183         g_string_append(str, ",");
184         do_word_wrap(str, break_point);
185
186         g_string_append(str, " ");
187         break_point = str->len - 1;
188 #ifdef HAVE_GNU_ADNS
189         g_string_append(str, "with ADNS");
190 #else
191         g_string_append(str, "without ADNS");
192 #endif /* HAVE_GNU_ADNS */
193
194         g_string_append(str, ".");
195         do_word_wrap(str, break_point);
196
197 #ifndef HAVE_LIBPCRE
198         break_point = str->len - 1;
199         g_string_append(str,
200                         "\nNOTE: this build doesn't support the \"matches\" operator for Ethereal filter"
201                         "\nsyntax.");
202         do_word_wrap(str, break_point);
203 #endif  /* HAVE_LIBPCRE */
204 }
205
206 /*
207  * Get various library run-time versions, and the OS version, and append
208  * them to the specified GString.
209  */
210 void
211 get_runtime_version_info(GString *str)
212 {
213 #if defined(_WIN32)
214         OSVERSIONINFO info;
215 #elif defined(HAVE_SYS_UTSNAME_H)
216         struct utsname name;
217 #endif
218
219         get_runtime_pcap_version(str);
220
221         g_string_append(str, "on ");
222 #if defined(_WIN32)
223         /*
224          * See
225          *
226          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
227          *
228          * for more than you ever wanted to know about determining the
229          * flavor of Windows on which you're running.  Implementing more
230          * of that is left as an exercise to the reader - who should
231          * check any copyright information about code samples on MSDN
232          * before cutting and pasting into Ethereal.
233          */
234         info.dwOSVersionInfoSize = sizeof info;
235         if (!GetVersionEx(&info)) {
236                 /*
237                  * XXX - get the failure reason.
238                  */
239                 g_string_append(str, "unknown Windows version");
240                 return;
241         }
242         switch (info.dwPlatformId) {
243
244         case VER_PLATFORM_WIN32s:
245                 /* Shyeah, right. */
246                 g_string_sprintfa(str, "Windows 3.1 with Win32s");
247                 break;
248
249         case VER_PLATFORM_WIN32_WINDOWS:
250                 /* Windows OT */
251                 switch (info.dwMajorVersion) {
252
253                 case 4:
254                         /* 3 cheers for Microsoft marketing! */
255                         switch (info.dwMinorVersion) {
256
257                         case 0:
258                                 g_string_sprintfa(str, "Windows 95");
259                                 break;
260
261                         case 10:
262                                 g_string_sprintfa(str, "Windows 98");
263                                 break;
264
265                         case 90:
266                                 g_string_sprintfa(str, "Windows Me");
267                                 break;
268
269                         default:
270                                 g_string_sprintfa(str, "Windows OT, unknown version %lu.%lu",
271                                     info.dwMajorVersion, info.dwMinorVersion);
272                                 break;
273                         }
274                         break;
275
276                 default:
277                         g_string_sprintfa(str, "Windows OT, unknown version %lu.%lu",
278                             info.dwMajorVersion, info.dwMinorVersion);
279                         break;
280                 }
281                 break;
282
283         case VER_PLATFORM_WIN32_NT:
284                 /* Windows NT */
285                 switch (info.dwMajorVersion) {
286
287                 case 3:
288                 case 4:
289                         g_string_sprintfa(str, "Windows NT %lu.%lu",
290                             info.dwMajorVersion, info.dwMinorVersion);
291                         break;
292
293                 case 5:
294                         /* 3 cheers for Microsoft marketing! */
295                         switch (info.dwMinorVersion) {
296
297                         case 0:
298                                 g_string_sprintfa(str, "Windows 2000");
299                                 break;
300
301                         case 1:
302                                 g_string_sprintfa(str, "Windows XP");
303                                 break;
304
305                         case 2:
306                                 /*
307                                  * The 64-bit version of XP is, I guess,
308                                  * built on the NT 5.2 code base.
309                                  */
310                                 if (info.wProductType == VER_NT_WORKSTATION)
311                                         g_string_sprintfa(str, "Windows XP");
312                                 else
313                                         g_string_sprintfa(str, "Windows Server 2003");
314                                 break;
315
316                         default:
317                                 g_string_sprintfa(str, "Windows NT, unknown version %lu.%lu",
318                                     info.dwMajorVersion, info.dwMinorVersion);
319                                 break;
320                         }
321                         break;
322
323                 case 6:
324                         if (info.wProductType == VER_NT_WORKSTATION)
325                                 g_string_sprintfa(str, "Windows Vista");
326                         else
327                                 g_string_sprintfa(str, "Windows Server \"Longhorn\"");
328                         break;
329
330                 default:
331                         g_string_sprintfa(str, "Windows NT, unknown version %lu.%lu",
332                             info.dwMajorVersion, info.dwMinorVersion);
333                         break;
334                 }
335                 break;
336
337         default:
338                 g_string_sprintfa(str, "Unknown Windows platform %lu version %lu.%lu",
339                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
340                 break;
341         }
342         if (info.szCSDVersion[0] != '\0')
343                 g_string_sprintfa(str, " %s", info.szCSDVersion);
344         g_string_sprintfa(str, ", build %lu", info.dwBuildNumber);
345 #elif defined(HAVE_SYS_UTSNAME_H)
346         /*
347          * We have <sys/utsname.h>, so we assume we have "uname()".
348          */
349         if (uname(&name) < 0) {
350                 g_string_sprintfa(str, "unknown OS version (uname failed - %s)",
351                     strerror(errno));
352                 return;
353         }
354
355         if (strcmp(name.sysname, "AIX") == 0) {
356                 /*
357                  * Yay, IBM!  Thanks for doing something different
358                  * from most of the other UNIXes out there, and
359                  * making "name.version" apparently be the major
360                  * version number and "name.release" be the minor
361                  * version number.
362                  */
363                 g_string_sprintfa(str, "%s %s.%s", name.sysname, name.version,
364                     name.release);
365         } else {
366                 /*
367                  * XXX - get "version" on any other platforms?
368                  *
369                  * On Digital/Tru65 UNIX, it's something unknown.
370                  * On Solaris, it's some kind of build information.
371                  * On HP-UX, it appears to be some sort of subrevision
372                  * thing.
373                  */
374                 g_string_sprintfa(str, "%s %s", name.sysname, name.release);
375         }
376 #else
377         g_string_append(str, "an unknown OS");
378 #endif
379         g_string_append(str, ".");
380 }