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 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <wsutil/clopts_common.h>
31 #include <wsutil/cmdarg_err.h>
32 #include <wsutil/unicode-utils.h>
33 #include <wsutil/file_util.h>
34 #include <wsutil/filesystem.h>
35 #include <wsutil/privileges.h>
38 #include <wsutil/plugins.h>
41 #include <wsutil/report_err.h>
46 #ifndef HAVE_GETOPT_LONG
47 #include "wsutil/wsgetopt.h"
50 #include "randpkt_core/randpkt_core.h"
53 * General errors are reported with an console message in randpkt.
56 failure_message(const char *msg_format, va_list ap)
58 fprintf(stderr, "randpkt: ");
59 vfprintf(stderr, msg_format, ap);
60 fprintf(stderr, "\n");
64 * Report additional information for an error in command-line arguments.
67 failure_message_cont(const char *msg_format, va_list ap)
69 vfprintf(stderr, msg_format, ap);
70 fprintf(stderr, "\n");
73 /* Print usage statement and exit program */
75 usage(gboolean is_error)
89 fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
90 fprintf(output, "Default max bytes (per packet) is 5000\n");
91 fprintf(output, "Default count is 1000.\n");
92 fprintf(output, "-r: random packet type selection\n");
93 fprintf(output, "\n");
94 fprintf(output, "Types:\n");
96 /* Get the examples list */
97 randpkt_example_list(&abbrev_list, &longname_list);
98 while (abbrev_list[i] && longname_list[i]) {
99 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
103 g_strfreev(abbrev_list);
104 g_strfreev(longname_list);
106 fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
108 exit(is_error ? 1 : 0);
111 main(int argc, char **argv)
113 char *init_progfile_dir_error;
115 int produce_type = -1;
116 char *produce_filename = NULL;
117 int produce_max_bytes = 5000;
118 int produce_count = 1000;
119 randpkt_example *example;
121 int allrandom = FALSE;
122 wtap_dumper *savedump;
123 static const struct option long_options[] = {
124 {"help", no_argument, NULL, 'h'},
129 * Get credential information for later use.
131 init_process_policies();
134 * Attempt to get the pathname of the directory containing the
137 init_progfile_dir_error = init_progfile_dir(argv[0], main);
138 if (init_progfile_dir_error != NULL) {
140 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
141 init_progfile_dir_error);
142 g_free(init_progfile_dir_error);
147 cmdarg_err_init(failure_message, failure_message_cont);
150 arg_list_utf_16to8(argc, argv);
151 create_app_running_mutex();
155 /* Register wiretap plugins */
156 init_report_err(failure_message,NULL,NULL,NULL);
158 /* Scan for plugins. This does *not* call their registration routines;
161 Don't report failures to load plugins because most
162 (non-wiretap) plugins *should* fail to load (because
163 we're not linked against libwireshark and dissector
164 plugins need libwireshark). */
165 scan_plugins(DONT_REPORT_LOAD_FAILURE);
167 /* Register all libwiretap plugin modules. */
168 register_all_wiretap_modules();
171 while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
173 case 'b': /* max bytes */
174 produce_max_bytes = get_positive_int(optarg, "max bytes");
175 if (produce_max_bytes > 65536) {
176 cmdarg_err("max bytes is > 65536");
181 case 'c': /* count */
182 produce_count = get_positive_int(optarg, "count");
185 case 't': /* type of packet to produce */
186 type = g_strdup(optarg);
203 /* any more command line parameters? */
205 produce_filename = argv[optind];
212 produce_type = randpkt_parse_type(type);
215 example = randpkt_find_example(produce_type);
219 randpkt_example_init(example, produce_filename, produce_max_bytes);
220 randpkt_loop(example, produce_count);
223 fprintf(stderr, "Can't set type in random mode\n");
227 produce_type = randpkt_parse_type(NULL);
228 example = randpkt_find_example(produce_type);
231 randpkt_example_init(example, produce_filename, produce_max_bytes);
233 while (produce_count-- > 0) {
234 randpkt_loop(example, 1);
235 produce_type = randpkt_parse_type(NULL);
237 savedump = example->dump;
239 example = randpkt_find_example(produce_type);
242 example->dump = savedump;
245 if (!randpkt_example_close(example))
252 * Editor modelines - http://www.wireshark.org/tools/modelines.html
257 * indent-tabs-mode: t
260 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
261 * :indentSize=8:tabSize=8:noTabs=false: