Revert "TODO: smb2: simplify preauth_hash calculation..."
[wireshark-sm.git] / randpkt.c
blob77e2d8843da86490d2335ce2db0674fce80d486d
1 /*
2 * randpkt.c
3 * ---------
4 * Creates random packet traces. Useful for debugging sniffers by testing
5 * assumptions about the veracity of the data found in the packet.
7 * Copyright (C) 1999 by Gilbert Ramirez <gram@alumni.rice.edu>
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <config.h>
13 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
15 #include <glib.h>
17 #include <stdio.h>
18 #include <stdlib.h>
20 #include <ws_exit_codes.h>
21 #include <wsutil/clopts_common.h>
22 #include <ui/failure_message.h>
23 #include <wsutil/cmdarg_err.h>
24 #include <wsutil/file_util.h>
25 #include <wsutil/filesystem.h>
26 #include <wsutil/privileges.h>
27 #include <cli_main.h>
29 #ifdef HAVE_PLUGINS
30 #include <wsutil/plugins.h>
31 #endif
33 #include <wsutil/report_message.h>
34 #include <wsutil/wslog.h>
36 #include <wsutil/ws_getopt.h>
37 #include <wsutil/version_info.h>
39 #include "randpkt_core/randpkt_core.h"
41 /* Additional exit codes */
42 #define INVALID_TYPE 2
43 #define CLOSE_ERROR 2
45 static void
46 list_capture_types(void) {
47 GArray *writable_type_subtypes;
49 cmdarg_err("The available capture file types for the \"-F\" flag are:\n");
50 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
51 for (guint i = 0; i < writable_type_subtypes->len; i++) {
52 int ft = g_array_index(writable_type_subtypes, int, i);
53 fprintf(stderr, " %s - %s\n", wtap_file_type_subtype_name(ft),
54 wtap_file_type_subtype_description(ft));
56 g_array_free(writable_type_subtypes, TRUE);
60 * Report an error in command-line arguments.
62 static void
63 randpkt_cmdarg_err(const char *msg_format, va_list ap)
65 fprintf(stderr, "randpkt: ");
66 vfprintf(stderr, msg_format, ap);
67 fprintf(stderr, "\n");
71 * Report additional information for an error in command-line arguments.
73 static void
74 randpkt_cmdarg_err_cont(const char *msg_format, va_list ap)
76 vfprintf(stderr, msg_format, ap);
77 fprintf(stderr, "\n");
80 /* Print usage statement and exit program */
81 static void
82 usage(gboolean is_error)
84 FILE *output;
85 char** abbrev_list;
86 char** longname_list;
87 unsigned i = 0;
89 if (!is_error) {
90 output = stdout;
92 else {
93 output = stderr;
96 fprintf(output, "Usage: randpkt [options] <outfile>\n");
97 fprintf(output, "\n");
98 fprintf(output, "Options:\n");
99 fprintf(output, " -b maximum bytes per packet (default: 5000)\n");
100 fprintf(output, " -c packet count (default: 1000)\n");
101 fprintf(output, " -F output file type (default: pcapng)\n");
102 fprintf(output, " an empty \"-F\" option will list the file types\n");
103 fprintf(output, " -r select a different random type for each packet\n");
104 fprintf(output, " -t packet type\n");
105 fprintf(output, " -h, --help display this help and exit.\n");
106 fprintf(output, " -v, --version print version information and exit.\n");
107 fprintf(output, "\n");
108 fprintf(output, "Types:\n");
110 /* Get the examples list */
111 randpkt_example_list(&abbrev_list, &longname_list);
112 while (abbrev_list[i] && longname_list[i]) {
113 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
114 i++;
117 g_strfreev(abbrev_list);
118 g_strfreev(longname_list);
120 fprintf(output, "\nIf type is not specified, a random packet type will be chosen\n\n");
124 main(int argc, char *argv[])
126 char *configuration_init_error;
127 static const struct report_message_routines randpkt_report_routines = {
128 failure_message,
129 failure_message,
130 open_failure_message,
131 read_failure_message,
132 write_failure_message,
133 cfile_open_failure_message,
134 cfile_dump_open_failure_message,
135 cfile_read_failure_message,
136 cfile_write_failure_message,
137 cfile_close_failure_message
139 int opt;
140 int produce_type = -1;
141 char *produce_filename = NULL;
142 int produce_max_bytes = 5000;
143 int produce_count = 1000;
144 int file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
145 randpkt_example *example;
146 guint8* type = NULL;
147 int allrandom = FALSE;
148 wtap_dumper *savedump;
149 int ret = EXIT_SUCCESS;
150 static const struct ws_option long_options[] = {
151 {"help", ws_no_argument, NULL, 'h'},
152 {"version", ws_no_argument, NULL, 'v'},
153 {0, 0, 0, 0 }
156 cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont);
158 /* Initialize log handler early so we can have proper logging during startup. */
159 ws_log_init("randpkt", vcmdarg_err);
161 /* Early logging command-line initialization. */
162 ws_log_parse_args(&argc, argv, vcmdarg_err, WS_EXIT_INVALID_OPTION);
164 ws_noisy("Finished log init and parsing command line log arguments");
167 * Get credential information for later use.
169 init_process_policies();
172 * Attempt to get the pathname of the directory containing the
173 * executable file.
175 configuration_init_error = configuration_init(argv[0], NULL);
176 if (configuration_init_error != NULL) {
177 fprintf(stderr,
178 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
179 configuration_init_error);
180 g_free(configuration_init_error);
183 init_report_message("randpkt", &randpkt_report_routines);
185 wtap_init(TRUE);
187 #ifdef _WIN32
188 create_app_running_mutex();
189 #endif /* _WIN32 */
191 ws_init_version_info("Randpkt", NULL, NULL);
193 while ((opt = ws_getopt_long(argc, argv, "b:c:F:ht:rv", long_options, NULL)) != -1) {
194 switch (opt) {
195 case 'b': /* max bytes */
196 produce_max_bytes = get_positive_int(ws_optarg, "max bytes");
197 if (produce_max_bytes > 65536) {
198 cmdarg_err("max bytes is > 65536");
199 ret = WS_EXIT_INVALID_OPTION;
200 goto clean_exit;
202 break;
204 case 'c': /* count */
205 produce_count = get_positive_int(ws_optarg, "count");
206 break;
208 case 'F':
209 file_type_subtype = wtap_name_to_file_type_subtype(ws_optarg);
210 if (file_type_subtype < 0) {
211 cmdarg_err("\"%s\" isn't a valid capture file type", ws_optarg);
212 list_capture_types();
213 return WS_EXIT_INVALID_OPTION;
215 break;
217 case 't': /* type of packet to produce */
218 type = g_strdup(ws_optarg);
219 break;
221 case 'h':
222 show_help_header(NULL);
223 usage(FALSE);
224 goto clean_exit;
225 break;
227 case 'r':
228 allrandom = TRUE;
229 break;
231 case 'v':
232 show_version();
233 goto clean_exit;
234 break;
236 case '?':
237 switch(ws_optopt) {
238 case 'F':
239 list_capture_types();
240 return WS_EXIT_INVALID_OPTION;
241 break;
243 /* FALLTHROUGH */
245 default:
246 usage(TRUE);
247 ret = WS_EXIT_INVALID_OPTION;
248 goto clean_exit;
249 break;
253 /* any more command line parameters? */
254 if (argc > ws_optind) {
255 produce_filename = argv[ws_optind];
256 } else {
257 usage(TRUE);
258 ret = WS_EXIT_INVALID_OPTION;
259 goto clean_exit;
262 if (file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_UNKNOWN) {
263 file_type_subtype = wtap_pcapng_file_type_subtype();
266 if (!allrandom) {
267 produce_type = randpkt_parse_type(type);
268 g_free(type);
270 example = randpkt_find_example(produce_type);
271 if (!example) {
272 ret = WS_EXIT_INVALID_OPTION;
273 goto clean_exit;
276 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
277 if (ret != EXIT_SUCCESS)
278 goto clean_exit;
279 randpkt_loop(example, produce_count, 0);
280 } else {
281 if (type) {
282 fprintf(stderr, "Can't set type in random mode\n");
283 ret = INVALID_TYPE;
284 goto clean_exit;
287 produce_type = randpkt_parse_type(NULL);
288 example = randpkt_find_example(produce_type);
289 if (!example) {
290 ret = WS_EXIT_INVALID_OPTION;
291 goto clean_exit;
293 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
294 if (ret != EXIT_SUCCESS)
295 goto clean_exit;
297 while (produce_count-- > 0) {
298 randpkt_loop(example, 1, 0);
299 produce_type = randpkt_parse_type(NULL);
301 savedump = example->dump;
303 example = randpkt_find_example(produce_type);
304 if (!example) {
305 ret = WS_EXIT_INVALID_OPTION;
306 goto clean_exit;
308 example->dump = savedump;
309 example->filename = produce_filename;
312 if (!randpkt_example_close(example)) {
313 ret = CLOSE_ERROR;
316 clean_exit:
317 wtap_cleanup();
318 return ret;