Add to the Info column for the stuff in COTP CR and CC PDUs.
[metze/wireshark/wip.git] / captype.c
1 /* captype.c
2  * Reports capture file type
3  *
4  * Based on capinfos.c
5  * Copyright 2004 Ian Schorr
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <locale.h>
33 #include <errno.h>
34
35 #ifdef HAVE_GETOPT_H
36 #include <getopt.h>
37 #endif
38
39 #ifdef HAVE_LIBZ
40 #include <zlib.h>     /* to get the libz version number */
41 #endif
42
43 #include <glib.h>
44
45 #include <wiretap/wtap.h>
46
47 #include <wsutil/crash_info.h>
48 #include <wsutil/file_util.h>
49 #include <wsutil/filesystem.h>
50 #include <wsutil/privileges.h>
51 #include <wsutil/ws_diag_control.h>
52 #include <wsutil/ws_version_info.h>
53
54 #ifdef HAVE_PLUGINS
55 #include <wsutil/plugins.h>
56 #endif
57
58 #include <wsutil/report_err.h>
59 #include <wsutil/str_util.h>
60
61 #ifdef _WIN32
62 #include <wsutil/unicode-utils.h>
63 #endif /* _WIN32 */
64
65 #ifndef HAVE_GETOPT_LONG
66 #include "wsutil/wsgetopt.h"
67 #endif
68
69 static void
70 print_usage(FILE *output)
71 {
72   fprintf(output, "\n");
73   fprintf(output, "Usage: captype <infile> ...\n");
74 }
75
76 #ifdef HAVE_PLUGINS
77 /*
78  *  Don't report failures to load plugins because most (non-wiretap) plugins
79  *  *should* fail to load (because we're not linked against libwireshark and
80  *  dissector plugins need libwireshark).
81  */
82 static void
83 failure_message(const char *msg_format _U_, va_list ap _U_)
84 {
85   return;
86 }
87 #endif
88
89 static void
90 get_captype_compiled_info(GString *str)
91 {
92   /* LIBZ */
93   g_string_append(str, ", ");
94 #ifdef HAVE_LIBZ
95   g_string_append(str, "with libz ");
96 #ifdef ZLIB_VERSION
97   g_string_append(str, ZLIB_VERSION);
98 #else /* ZLIB_VERSION */
99   g_string_append(str, "(version unknown)");
100 #endif /* ZLIB_VERSION */
101 #else /* HAVE_LIBZ */
102   g_string_append(str, "without libz");
103 #endif /* HAVE_LIBZ */
104 }
105
106 static void
107 get_captype_runtime_info(GString *str)
108 {
109   /* zlib */
110 #if defined(HAVE_LIBZ) && !defined(_WIN32)
111   g_string_append_printf(str, ", with libz %s", zlibVersion());
112 #endif
113 }
114
115 int
116 main(int argc, char *argv[])
117 {
118   GString *comp_info_str;
119   GString *runtime_info_str;
120   wtap  *wth;
121   int    err;
122   gchar *err_info;
123   int    i;
124   int    opt;
125   int    overall_error_status;
126   static const struct option long_options[] = {
127       {"help", no_argument, NULL, 'h'},
128       {"version", no_argument, NULL, 'v'},
129       {0, 0, 0, 0 }
130   };
131
132 #ifdef HAVE_PLUGINS
133   char  *init_progfile_dir_error;
134 #endif
135
136   /* Set the C-language locale to the native environment. */
137   setlocale(LC_ALL, "");
138
139   /* Get the compile-time version information string */
140   comp_info_str = get_compiled_version_info(NULL, get_captype_compiled_info);
141
142   /* Get the run-time version information string */
143   runtime_info_str = get_runtime_version_info(get_captype_runtime_info);
144
145   /* Add it to the information to be reported on a crash. */
146   ws_add_crash_info("Captype (Wireshark) %s\n"
147          "\n"
148          "%s"
149          "\n"
150          "%s",
151       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
152
153 #ifdef _WIN32
154   arg_list_utf_16to8(argc, argv);
155   create_app_running_mutex();
156 #endif /* _WIN32 */
157
158   /*
159    * Get credential information for later use.
160    */
161   init_process_policies();
162   init_open_routines();
163
164 #ifdef HAVE_PLUGINS
165   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
166     g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
167     g_free(init_progfile_dir_error);
168   } else {
169     /* Register all the plugin types we have. */
170     wtap_register_plugin_types(); /* Types known to libwiretap */
171
172     init_report_err(failure_message,NULL,NULL,NULL);
173
174     /* Scan for plugins.  This does *not* call their registration routines;
175        that's done later. */
176     scan_plugins();
177
178     /* Register all libwiretap plugin modules. */
179     register_all_wiretap_modules();
180   }
181 #endif
182
183   /* Process the options */
184   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
185
186     switch (opt) {
187
188       case 'h':
189         printf("Captype (Wireshark) %s\n"
190                "Print the file types of capture files.\n"
191                "See https://www.wireshark.org for more information.\n",
192                get_ws_vcs_version_info());
193         print_usage(stdout);
194         exit(0);
195         break;
196
197       case 'v':
198         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
199         g_string_free(comp_info_str, TRUE);
200         g_string_free(runtime_info_str, TRUE);
201         exit(0);
202         break;
203
204       case '?':              /* Bad flag - print usage message */
205         print_usage(stderr);
206         exit(1);
207         break;
208     }
209   }
210
211   if (argc < 2) {
212     print_usage(stderr);
213     return 1;
214   }
215
216   overall_error_status = 0;
217
218   for (i = 1; i < argc; i++) {
219     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
220
221     if(wth) {
222       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
223       wtap_close(wth);
224     } else {
225       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
226         printf("%s: unknown\n", argv[i]);
227       else {
228         fprintf(stderr, "captype: Can't open %s: %s\n", argv[i],
229                 wtap_strerror(err));
230         if (err_info != NULL) {
231           fprintf(stderr, "(%s)\n", err_info);
232           g_free(err_info);
233         }
234         overall_error_status = 1; /* remember that an error has occurred */
235       }
236     }
237
238   }
239
240   return overall_error_status;
241 }
242
243 /*
244  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
245  *
246  * Local variables:
247  * c-basic-offset: 2
248  * tab-width: 8
249  * indent-tabs-mode: nil
250  * End:
251  *
252  * vi: set shiftwidth=2 tabstop=8 expandtab:
253  * :indentSize=2:tabSize=8:noTabs=true:
254  */