forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captype.c
202 lines (168 loc) · 4.95 KB
/
captype.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* captype.c
* Reports capture file type
*
* Based on capinfos.c
* Copyright 2004 Ian Schorr
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <locale.h>
#include <errno.h>
#include <wsutil/ws_getopt.h>
#include <glib.h>
#include <wiretap/wtap.h>
#include <ui/cmdarg_err.h>
#include <wsutil/file_util.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
#include <cli_main.h>
#include <ui/version_info.h>
#ifdef HAVE_PLUGINS
#include <wsutil/plugins.h>
#endif
#include <wsutil/report_message.h>
#include <wsutil/str_util.h>
#include <wsutil/wslog.h>
#include "ui/failure_message.h"
static void
print_usage(FILE *output)
{
fprintf(output, "\n");
fprintf(output, "Usage: captype [options] <infile> ...\n");
fprintf(output, "\n");
fprintf(output, "Miscellaneous:\n");
fprintf(output, " -h, --help display this help and exit\n");
fprintf(output, " -v, --version display version info and exit\n");
}
/*
* Report an error in command-line arguments.
*/
static void
captype_cmdarg_err(const char *msg_format, va_list ap)
{
fprintf(stderr, "captype: ");
vfprintf(stderr, msg_format, ap);
fprintf(stderr, "\n");
}
/*
* Report additional information for an error in command-line arguments.
*/
static void
captype_cmdarg_err_cont(const char *msg_format, va_list ap)
{
vfprintf(stderr, msg_format, ap);
fprintf(stderr, "\n");
}
int
main(int argc, char *argv[])
{
char *configuration_init_error;
static const struct report_message_routines captype_report_routines = {
failure_message,
failure_message,
open_failure_message,
read_failure_message,
write_failure_message,
cfile_open_failure_message,
cfile_dump_open_failure_message,
cfile_read_failure_message,
cfile_write_failure_message,
cfile_close_failure_message
};
wtap *wth;
int err;
gchar *err_info;
int i;
int opt;
int overall_error_status;
static const struct ws_option long_options[] = {
{"help", ws_no_argument, NULL, 'h'},
{"version", ws_no_argument, NULL, 'v'},
{0, 0, 0, 0 }
};
/*
* Set the C-language locale to the native environment and set the
* code page to UTF-8 on Windows.
*/
#ifdef _WIN32
setlocale(LC_ALL, ".UTF-8");
#else
setlocale(LC_ALL, "");
#endif
cmdarg_err_init(captype_cmdarg_err, captype_cmdarg_err_cont);
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("captype", vcmdarg_err);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
/* Initialize the version information. */
ws_init_version_info("Captype", NULL, NULL);
#ifdef _WIN32
create_app_running_mutex();
#endif /* _WIN32 */
/*
* Get credential information for later use.
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
configuration_init_error = configuration_init(argv[0], NULL);
if (configuration_init_error != NULL) {
fprintf(stderr,
"captype: Can't get pathname of directory containing the captype program: %s.\n",
configuration_init_error);
g_free(configuration_init_error);
}
init_report_message("captype", &captype_report_routines);
wtap_init(TRUE);
/* Process the options */
while ((opt = ws_getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
switch (opt) {
case 'h':
show_help_header("Print the file types of capture files.");
print_usage(stdout);
exit(0);
break;
case 'v':
show_version();
exit(0);
break;
case '?': /* Bad flag - print usage message */
print_usage(stderr);
exit(1);
break;
}
}
if (argc < 2) {
print_usage(stderr);
return 1;
}
overall_error_status = 0;
for (i = 1; i < argc; i++) {
wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
if(wth) {
printf("%s: %s\n", argv[i], wtap_file_type_subtype_name(wtap_file_type_subtype(wth)));
wtap_close(wth);
} else {
if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
printf("%s: unknown\n", argv[i]);
else {
cfile_open_failure_message(argv[i], err, err_info);
overall_error_status = 2; /* remember that an error has occurred */
}
}
}
wtap_cleanup();
free_progdirs();
return overall_error_status;
}