Add ability to follow UDP stream by index
[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_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_GETOPT_H
40 #include <getopt.h>
41 #endif
42
43 #ifdef HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46
47 #ifdef HAVE_LIBZ
48 #include <zlib.h>     /* to get the libz version number */
49 #endif
50
51 #include <glib.h>
52
53 #include <wsutil/privileges.h>
54 #include <wsutil/filesystem.h>
55 #include <wsutil/file_util.h>
56 #include <wsutil/crash_info.h>
57 #include <wsutil/ws_version_info.h>
58
59 #ifdef HAVE_PLUGINS
60 #include <wsutil/plugins.h>
61 #endif
62
63 #include "wtap.h"
64 #include <wsutil/report_err.h>
65 #include <wsutil/str_util.h>
66
67 #ifdef _WIN32
68 #include <wsutil/unicode-utils.h>
69 #endif /* _WIN32 */
70
71 #ifndef HAVE_GETOPT_LONG
72 #include "wsutil/wsgetopt.h"
73 #endif
74
75 #include "version_info.h"
76
77 static void
78 print_usage(FILE *output)
79 {
80   fprintf(output, "\n");
81   fprintf(output, "Usage: captype <infile> ...\n");
82 }
83
84 #ifdef HAVE_PLUGINS
85 /*
86  *  Don't report failures to load plugins because most (non-wiretap) plugins
87  *  *should* fail to load (because we're not linked against libwireshark and
88  *  dissector plugins need libwireshark).
89  */
90 static void
91 failure_message(const char *msg_format _U_, va_list ap _U_)
92 {
93   return;
94 }
95 #endif
96
97 static void
98 get_captype_compiled_info(GString *str)
99 {
100   /* LIBZ */
101   g_string_append(str, ", ");
102 #ifdef HAVE_LIBZ
103   g_string_append(str, "with libz ");
104 #ifdef ZLIB_VERSION
105   g_string_append(str, ZLIB_VERSION);
106 #else /* ZLIB_VERSION */
107   g_string_append(str, "(version unknown)");
108 #endif /* ZLIB_VERSION */
109 #else /* HAVE_LIBZ */
110   g_string_append(str, "without libz");
111 #endif /* HAVE_LIBZ */
112 }
113
114 static void
115 get_captype_runtime_info(GString *str)
116 {
117   /* zlib */
118 #if defined(HAVE_LIBZ) && !defined(_WIN32)
119   g_string_append_printf(str, ", with libz %s", zlibVersion());
120 #endif
121 }
122
123 int
124 main(int argc, char *argv[])
125 {
126   GString *comp_info_str;
127   GString *runtime_info_str;
128   wtap  *wth;
129   int    err;
130   gchar *err_info;
131   int    i;
132   int    opt;
133   int    overall_error_status;
134   static const struct option long_options[] = {
135       {(char *)"help", no_argument, NULL, 'h'},
136       {(char *)"version", no_argument, NULL, 'v'},
137       {0, 0, 0, 0 }
138   };
139
140 #ifdef HAVE_PLUGINS
141   char  *init_progfile_dir_error;
142 #endif
143
144   /* Assemble the compile-time version information string */
145   comp_info_str = g_string_new("Compiled ");
146   get_compiled_version_info(comp_info_str, NULL, get_captype_compiled_info);
147
148   /* Assemble the run-time version information string */
149   runtime_info_str = g_string_new("Running ");
150   get_runtime_version_info(runtime_info_str, get_captype_runtime_info);
151
152   /* Add it to the information to be reported on a crash. */
153   ws_add_crash_info("Captype (Wireshark) %s\n"
154          "\n"
155          "%s"
156          "\n"
157          "%s",
158       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
159
160 #ifdef _WIN32
161   arg_list_utf_16to8(argc, argv);
162   create_app_running_mutex();
163 #endif /* _WIN32 */
164
165   /*
166    * Get credential information for later use.
167    */
168   init_process_policies();
169   init_open_routines();
170
171 #ifdef HAVE_PLUGINS
172   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
173     g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
174     g_free(init_progfile_dir_error);
175   } else {
176     /* Register all the plugin types we have. */
177     wtap_register_plugin_types(); /* Types known to libwiretap */
178
179     init_report_err(failure_message,NULL,NULL,NULL);
180
181     /* Scan for plugins.  This does *not* call their registration routines;
182        that's done later. */
183     scan_plugins();
184
185     /* Register all libwiretap plugin modules. */
186     register_all_wiretap_modules();
187   }
188 #endif
189
190   /* Process the options */
191   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
192
193     switch (opt) {
194
195       case 'h':
196         printf("Captype (Wireshark) %s\n"
197                "Print the file types of capture files.\n"
198                "See http://www.wireshark.org for more information.\n",
199                get_ws_vcs_version_info());
200         print_usage(stdout);
201         exit(0);
202         break;
203
204       case 'v':
205         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
206         g_string_free(comp_info_str, TRUE);
207         g_string_free(runtime_info_str, TRUE);
208         exit(0);
209         break;
210
211       case '?':              /* Bad flag - print usage message */
212         print_usage(stderr);
213         exit(1);
214         break;
215     }
216   }
217
218   /* Set the C-language locale to the native environment. */
219   setlocale(LC_ALL, "");
220
221   if (argc < 2) {
222     print_usage(stderr);
223     return 1;
224   }
225
226   overall_error_status = 0;
227
228   for (i = 1; i < argc; i++) {
229     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
230
231     if(wth) {
232       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
233       wtap_close(wth);
234     } else {
235       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
236         printf("%s: unknown\n", argv[i]);
237       else {
238         fprintf(stderr, "captype: Can't open %s: %s\n", argv[i],
239                 wtap_strerror(err));
240         if (err_info != NULL) {
241           fprintf(stderr, "(%s)\n", err_info);
242           g_free(err_info);
243         }
244         overall_error_status = 1; /* remember that an error has occurred */
245       }
246     }
247
248   }
249
250   return overall_error_status;
251 }
252
253 /*
254  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
255  *
256  * Local variables:
257  * c-basic-offset: 2
258  * tab-width: 8
259  * indent-tabs-mode: nil
260  * End:
261  *
262  * vi: set shiftwidth=2 tabstop=8 expandtab:
263  * :indentSize=2:tabSize=8:noTabs=true:
264  */