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
18 #include <ui/clopts_common.h>
19 #include <ui/cmdarg_err.h>
20 #include <wsutil/file_util.h>
21 #include <wsutil/filesystem.h>
22 #include <wsutil/privileges.h>
26 #include <wsutil/plugins.h>
29 #include <wsutil/report_message.h>
34 #ifndef HAVE_GETOPT_LONG
35 #include "wsutil/wsgetopt.h"
38 #include "randpkt_core/randpkt_core.h"
40 #define INVALID_OPTION 1
41 #define INVALID_TYPE 2
45 * General errors and warnings are reported with an console message
49 failure_warning_message(const char *msg_format, va_list ap)
51 fprintf(stderr, "randpkt: ");
52 vfprintf(stderr, msg_format, ap);
53 fprintf(stderr, "\n");
57 * Report additional information for an error in command-line arguments.
60 failure_message_cont(const char *msg_format, va_list ap)
62 vfprintf(stderr, msg_format, ap);
63 fprintf(stderr, "\n");
66 /* Print usage statement and exit program */
68 usage(gboolean is_error)
82 fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
83 fprintf(output, "Default max bytes (per packet) is 5000\n");
84 fprintf(output, "Default count is 1000.\n");
85 fprintf(output, "-r: random packet type selection\n");
86 fprintf(output, "\n");
87 fprintf(output, "Types:\n");
89 /* Get the examples list */
90 randpkt_example_list(&abbrev_list, &longname_list);
91 while (abbrev_list[i] && longname_list[i]) {
92 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
96 g_strfreev(abbrev_list);
97 g_strfreev(longname_list);
99 fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
103 main(int argc, char *argv[])
105 char *init_progfile_dir_error;
107 int produce_type = -1;
108 char *produce_filename = NULL;
109 int produce_max_bytes = 5000;
110 int produce_count = 1000;
111 randpkt_example *example;
113 int allrandom = FALSE;
114 wtap_dumper *savedump;
115 int ret = EXIT_SUCCESS;
116 static const struct option long_options[] = {
117 {"help", no_argument, NULL, 'h'},
122 * Get credential information for later use.
124 init_process_policies();
127 * Attempt to get the pathname of the directory containing the
130 init_progfile_dir_error = init_progfile_dir(argv[0]);
131 if (init_progfile_dir_error != NULL) {
133 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
134 init_progfile_dir_error);
135 g_free(init_progfile_dir_error);
138 init_report_message(failure_warning_message, failure_warning_message,
143 cmdarg_err_init(failure_warning_message, failure_message_cont);
146 create_app_running_mutex();
149 while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
151 case 'b': /* max bytes */
152 produce_max_bytes = get_positive_int(optarg, "max bytes");
153 if (produce_max_bytes > 65536) {
154 cmdarg_err("max bytes is > 65536");
155 ret = INVALID_OPTION;
160 case 'c': /* count */
161 produce_count = get_positive_int(optarg, "count");
164 case 't': /* type of packet to produce */
165 type = g_strdup(optarg);
179 ret = INVALID_OPTION;
185 /* any more command line parameters? */
187 produce_filename = argv[optind];
191 ret = INVALID_OPTION;
196 produce_type = randpkt_parse_type(type);
199 example = randpkt_find_example(produce_type);
201 ret = INVALID_OPTION;
205 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
206 if (ret != EXIT_SUCCESS)
208 randpkt_loop(example, produce_count, 0);
211 fprintf(stderr, "Can't set type in random mode\n");
216 produce_type = randpkt_parse_type(NULL);
217 example = randpkt_find_example(produce_type);
219 ret = INVALID_OPTION;
222 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
223 if (ret != EXIT_SUCCESS)
226 while (produce_count-- > 0) {
227 randpkt_loop(example, 1, 0);
228 produce_type = randpkt_parse_type(NULL);
230 savedump = example->dump;
232 example = randpkt_find_example(produce_type);
234 ret = INVALID_OPTION;
237 example->dump = savedump;
240 if (!randpkt_example_close(example)) {
250 * Editor modelines - http://www.wireshark.org/tools/modelines.html
255 * indent-tabs-mode: t
258 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
259 * :indentSize=8:tabSize=8:noTabs=false: