TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / randpkt.c
blobdfe8601be377c79123d471ea160156ae21749192
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/wslog.h>
35 #include <wsutil/ws_getopt.h>
36 #include <wsutil/version_info.h>
38 #include "randpkt_core/randpkt_core.h"
40 /* Additional exit codes */
41 #define INVALID_TYPE 2
42 #define CLOSE_ERROR 2
44 static void
45 list_capture_types(void) {
46 GArray *writable_type_subtypes;
48 cmdarg_err("The available capture file types for the \"-F\" flag are:\n");
49 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
50 for (unsigned i = 0; i < writable_type_subtypes->len; i++) {
51 int ft = g_array_index(writable_type_subtypes, int, i);
52 fprintf(stderr, " %s - %s\n", wtap_file_type_subtype_name(ft),
53 wtap_file_type_subtype_description(ft));
55 g_array_free(writable_type_subtypes, TRUE);
58 /* Print usage statement and exit program */
59 static void
60 usage(bool is_error)
62 FILE *output;
63 char** abbrev_list;
64 char** longname_list;
65 unsigned i = 0;
67 if (!is_error) {
68 output = stdout;
70 else {
71 output = stderr;
74 fprintf(output, "Usage: randpkt [options] <outfile>\n");
75 fprintf(output, "\n");
76 fprintf(output, "Options:\n");
77 fprintf(output, " -b maximum bytes per packet (default: 5000)\n");
78 fprintf(output, " -c packet count (default: 1000)\n");
79 fprintf(output, " -F output file type (default: pcapng)\n");
80 fprintf(output, " an empty \"-F\" option will list the file types\n");
81 fprintf(output, " -r select a different random type for each packet\n");
82 fprintf(output, " -t packet type\n");
83 fprintf(output, " -h, --help display this help and exit.\n");
84 fprintf(output, " -v, --version print version information and exit.\n");
85 fprintf(output, "\n");
86 fprintf(output, "Types:\n");
88 /* Get the examples list */
89 randpkt_example_list(&abbrev_list, &longname_list);
90 while (abbrev_list[i] && longname_list[i]) {
91 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
92 i++;
95 g_strfreev(abbrev_list);
96 g_strfreev(longname_list);
98 fprintf(output, "\nIf type is not specified, a random packet type will be chosen\n\n");
102 main(int argc, char *argv[])
104 char *configuration_init_error;
105 int opt;
106 int produce_type = -1;
107 char *produce_filename = NULL;
108 int produce_max_bytes = 5000;
109 int produce_count = 1000;
110 int file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
111 randpkt_example *example;
112 uint8_t* type = NULL;
113 bool allrandom = false;
114 wtap_dumper *savedump;
115 int ret = EXIT_SUCCESS;
116 static const struct ws_option long_options[] = {
117 {"help", ws_no_argument, NULL, 'h'},
118 {"version", ws_no_argument, NULL, 'v'},
119 {0, 0, 0, 0 }
122 /* Set the program name. */
123 g_set_prgname("randpkt");
125 cmdarg_err_init(stderr_cmdarg_err, stderr_cmdarg_err_cont);
127 /* Initialize log handler early so we can have proper logging during startup. */
128 ws_log_init(vcmdarg_err);
130 /* Early logging command-line initialization. */
131 ws_log_parse_args(&argc, argv, vcmdarg_err, WS_EXIT_INVALID_OPTION);
133 ws_noisy("Finished log init and parsing command line log arguments");
136 * Get credential information for later use.
138 init_process_policies();
141 * Attempt to get the pathname of the directory containing the
142 * executable file.
144 configuration_init_error = configuration_init(argv[0]);
145 if (configuration_init_error != NULL) {
146 fprintf(stderr,
147 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
148 configuration_init_error);
149 g_free(configuration_init_error);
152 init_report_failure_message("randpkt");
154 wtap_init(true);
156 #ifdef _WIN32
157 create_app_running_mutex();
158 #endif /* _WIN32 */
160 ws_init_version_info("Randpkt", NULL, NULL);
162 while ((opt = ws_getopt_long(argc, argv, "b:c:F:ht:rv", long_options, NULL)) != -1) {
163 switch (opt) {
164 case 'b': /* max bytes */
165 produce_max_bytes = get_positive_int(ws_optarg, "max bytes");
166 if (produce_max_bytes > 65536) {
167 cmdarg_err("max bytes is > 65536");
168 ret = WS_EXIT_INVALID_OPTION;
169 goto clean_exit;
171 break;
173 case 'c': /* count */
174 produce_count = get_positive_int(ws_optarg, "count");
175 break;
177 case 'F':
178 file_type_subtype = wtap_name_to_file_type_subtype(ws_optarg);
179 if (file_type_subtype < 0) {
180 cmdarg_err("\"%s\" isn't a valid capture file type", ws_optarg);
181 list_capture_types();
182 return WS_EXIT_INVALID_OPTION;
184 break;
186 case 't': /* type of packet to produce */
187 type = g_strdup(ws_optarg);
188 break;
190 case 'h':
191 show_help_header(NULL);
192 usage(false);
193 goto clean_exit;
194 break;
196 case 'r':
197 allrandom = true;
198 break;
200 case 'v':
201 show_version();
202 goto clean_exit;
203 break;
205 case '?':
206 switch(ws_optopt) {
207 case 'F':
208 list_capture_types();
209 return WS_EXIT_INVALID_OPTION;
211 /* FALLTHROUGH */
213 default:
214 usage(true);
215 ret = WS_EXIT_INVALID_OPTION;
216 goto clean_exit;
217 break;
221 /* any more command line parameters? */
222 if (argc > ws_optind) {
223 produce_filename = argv[ws_optind];
224 } else {
225 usage(true);
226 ret = WS_EXIT_INVALID_OPTION;
227 goto clean_exit;
230 if (file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_UNKNOWN) {
231 file_type_subtype = wtap_pcapng_file_type_subtype();
234 if (!allrandom) {
235 produce_type = randpkt_parse_type(type);
236 g_free(type);
238 example = randpkt_find_example(produce_type);
239 if (!example) {
240 ret = WS_EXIT_INVALID_OPTION;
241 goto clean_exit;
244 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
245 if (ret != EXIT_SUCCESS)
246 goto clean_exit;
247 randpkt_loop(example, produce_count, 0);
248 } else {
249 if (type) {
250 fprintf(stderr, "Can't set type in random mode\n");
251 ret = INVALID_TYPE;
252 goto clean_exit;
255 produce_type = randpkt_parse_type(NULL);
256 example = randpkt_find_example(produce_type);
257 if (!example) {
258 ret = WS_EXIT_INVALID_OPTION;
259 goto clean_exit;
261 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
262 if (ret != EXIT_SUCCESS)
263 goto clean_exit;
265 while (produce_count-- > 0) {
266 randpkt_loop(example, 1, 0);
267 produce_type = randpkt_parse_type(NULL);
269 savedump = example->dump;
271 example = randpkt_find_example(produce_type);
272 if (!example) {
273 ret = WS_EXIT_INVALID_OPTION;
274 goto clean_exit;
276 example->dump = savedump;
277 example->filename = produce_filename;
280 if (!randpkt_example_close(example)) {
281 ret = CLOSE_ERROR;
284 clean_exit:
285 wtap_cleanup();
286 return ret;