954b05379e02bb72b60be657b87c780e42873585
[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 "capture-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          * They should also note that you need an OSVERSIONINFOEX structure
235          * to get some of that information, and that not only is that
236          * structure not supported on older versions of Windows, you might
237          * not even be able to compile code that *uses* that structure with
238          * older versions of the SDK.
239          */
240         info.dwOSVersionInfoSize = sizeof info;
241         if (!GetVersionEx(&info)) {
242                 /*
243                  * XXX - get the failure reason.
244                  */
245                 g_string_append(str, "unknown Windows version");
246                 return;
247         }
248         switch (info.dwPlatformId) {
249
250         case VER_PLATFORM_WIN32s:
251                 /* Shyeah, right. */
252                 g_string_sprintfa(str, "Windows 3.1 with Win32s");
253                 break;
254
255         case VER_PLATFORM_WIN32_WINDOWS:
256                 /* Windows OT */
257                 switch (info.dwMajorVersion) {
258
259                 case 4:
260                         /* 3 cheers for Microsoft marketing! */
261                         switch (info.dwMinorVersion) {
262
263                         case 0:
264                                 g_string_sprintfa(str, "Windows 95");
265                                 break;
266
267                         case 10:
268                                 g_string_sprintfa(str, "Windows 98");
269                                 break;
270
271                         case 90:
272                                 g_string_sprintfa(str, "Windows Me");
273                                 break;
274
275                         default:
276                                 g_string_sprintfa(str, "Windows OT, unknown version %lu.%lu",
277                                     info.dwMajorVersion, info.dwMinorVersion);
278                                 break;
279                         }
280                         break;
281
282                 default:
283                         g_string_sprintfa(str, "Windows OT, unknown version %lu.%lu",
284                             info.dwMajorVersion, info.dwMinorVersion);
285                         break;
286                 }
287                 break;
288
289         case VER_PLATFORM_WIN32_NT:
290                 /* Windows NT */
291                 switch (info.dwMajorVersion) {
292
293                 case 3:
294                 case 4:
295                         g_string_sprintfa(str, "Windows NT %lu.%lu",
296                             info.dwMajorVersion, info.dwMinorVersion);
297                         break;
298
299                 case 5:
300                         /* 3 cheers for Microsoft marketing! */
301                         switch (info.dwMinorVersion) {
302
303                         case 0:
304                                 g_string_sprintfa(str, "Windows 2000");
305                                 break;
306
307                         case 1:
308                                 g_string_sprintfa(str, "Windows XP");
309                                 break;
310
311                         case 2:
312                                 g_string_sprintfa(str, "Windows Server 2003");
313                                 break;
314
315                         default:
316                                 g_string_sprintfa(str, "Windows NT, unknown version %lu.%lu",
317                                     info.dwMajorVersion, info.dwMinorVersion);
318                                 break;
319                         }
320                         break;
321
322                 case 6:
323                         g_string_sprintfa(str, "Windows Vista");
324                         break;
325
326                 default:
327                         g_string_sprintfa(str, "Windows NT, unknown version %lu.%lu",
328                             info.dwMajorVersion, info.dwMinorVersion);
329                         break;
330                 }
331                 break;
332
333         default:
334                 g_string_sprintfa(str, "Unknown Windows platform %lu version %lu.%lu",
335                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
336                 break;
337         }
338         if (info.szCSDVersion[0] != '\0')
339                 g_string_sprintfa(str, " %s", info.szCSDVersion);
340         g_string_sprintfa(str, ", build %lu", info.dwBuildNumber);
341 #elif defined(HAVE_SYS_UTSNAME_H)
342         /*
343          * We have <sys/utsname.h>, so we assume we have "uname()".
344          */
345         if (uname(&name) < 0) {
346                 g_string_sprintfa(str, "unknown OS version (uname failed - %s)",
347                     strerror(errno));
348                 return;
349         }
350
351         if (strcmp(name.sysname, "AIX") == 0) {
352                 /*
353                  * Yay, IBM!  Thanks for doing something different
354                  * from most of the other UNIXes out there, and
355                  * making "name.version" apparently be the major
356                  * version number and "name.release" be the minor
357                  * version number.
358                  */
359                 g_string_sprintfa(str, "%s %s.%s", name.sysname, name.version,
360                     name.release);
361         } else {
362                 /*
363                  * XXX - get "version" on any other platforms?
364                  *
365                  * On Digital/Tru65 UNIX, it's something unknown.
366                  * On Solaris, it's some kind of build information.
367                  * On HP-UX, it appears to be some sort of subrevision
368                  * thing.
369                  */
370                 g_string_sprintfa(str, "%s %s", name.sysname, name.release);
371         }
372 #else
373         g_string_append(str, "an unknown OS");
374 #endif
375         g_string_append(str, ".");
376 }