Revert "drsuapi_dissect_element_DsGetNCChangesCtr6TS_ctr6 dissect_krb5_PAC_NDRHEADERBLOB"
[wireshark-sm.git] / randpkt.c
blob413155ceacf3dcb9f1e5de5e16e9954c65ecf4fa
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);
59 * Report an error in command-line arguments.
61 static void
62 randpkt_cmdarg_err(const char *msg_format, va_list ap)
64 fprintf(stderr, "randpkt: ");
65 vfprintf(stderr, msg_format, ap);
66 fprintf(stderr, "\n");
70 * Report additional information for an error in command-line arguments.
72 static void
73 randpkt_cmdarg_err_cont(const char *msg_format, va_list ap)
75 vfprintf(stderr, msg_format, ap);
76 fprintf(stderr, "\n");
79 /* Print usage statement and exit program */
80 static void
81 usage(bool is_error)
83 FILE *output;
84 char** abbrev_list;
85 char** longname_list;
86 unsigned i = 0;
88 if (!is_error) {
89 output = stdout;
91 else {
92 output = stderr;
95 fprintf(output, "Usage: randpkt [options] <outfile>\n");
96 fprintf(output, "\n");
97 fprintf(output, "Options:\n");
98 fprintf(output, " -b maximum bytes per packet (default: 5000)\n");
99 fprintf(output, " -c packet count (default: 1000)\n");
100 fprintf(output, " -F output file type (default: pcapng)\n");
101 fprintf(output, " an empty \"-F\" option will list the file types\n");
102 fprintf(output, " -r select a different random type for each packet\n");
103 fprintf(output, " -t packet type\n");
104 fprintf(output, " -h, --help display this help and exit.\n");
105 fprintf(output, " -v, --version print version information and exit.\n");
106 fprintf(output, "\n");
107 fprintf(output, "Types:\n");
109 /* Get the examples list */
110 randpkt_example_list(&abbrev_list, &longname_list);
111 while (abbrev_list[i] && longname_list[i]) {
112 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
113 i++;
116 g_strfreev(abbrev_list);
117 g_strfreev(longname_list);
119 fprintf(output, "\nIf type is not specified, a random packet type will be chosen\n\n");
123 main(int argc, char *argv[])
125 char *configuration_init_error;
126 int opt;
127 int produce_type = -1;
128 char *produce_filename = NULL;
129 int produce_max_bytes = 5000;
130 int produce_count = 1000;
131 int file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
132 randpkt_example *example;
133 uint8_t* type = NULL;
134 bool allrandom = false;
135 wtap_dumper *savedump;
136 int ret = EXIT_SUCCESS;
137 static const struct ws_option long_options[] = {
138 {"help", ws_no_argument, NULL, 'h'},
139 {"version", ws_no_argument, NULL, 'v'},
140 {0, 0, 0, 0 }
143 cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont);
145 /* Initialize log handler early so we can have proper logging during startup. */
146 ws_log_init("randpkt", vcmdarg_err);
148 /* Early logging command-line initialization. */
149 ws_log_parse_args(&argc, argv, vcmdarg_err, WS_EXIT_INVALID_OPTION);
151 ws_noisy("Finished log init and parsing command line log arguments");
154 * Get credential information for later use.
156 init_process_policies();
159 * Attempt to get the pathname of the directory containing the
160 * executable file.
162 configuration_init_error = configuration_init(argv[0], NULL);
163 if (configuration_init_error != NULL) {
164 fprintf(stderr,
165 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
166 configuration_init_error);
167 g_free(configuration_init_error);
170 init_report_failure_message("randpkt");
172 wtap_init(true);
174 #ifdef _WIN32
175 create_app_running_mutex();
176 #endif /* _WIN32 */
178 ws_init_version_info("Randpkt", NULL, NULL);
180 while ((opt = ws_getopt_long(argc, argv, "b:c:F:ht:rv", long_options, NULL)) != -1) {
181 switch (opt) {
182 case 'b': /* max bytes */
183 produce_max_bytes = get_positive_int(ws_optarg, "max bytes");
184 if (produce_max_bytes > 65536) {
185 cmdarg_err("max bytes is > 65536");
186 ret = WS_EXIT_INVALID_OPTION;
187 goto clean_exit;
189 break;
191 case 'c': /* count */
192 produce_count = get_positive_int(ws_optarg, "count");
193 break;
195 case 'F':
196 file_type_subtype = wtap_name_to_file_type_subtype(ws_optarg);
197 if (file_type_subtype < 0) {
198 cmdarg_err("\"%s\" isn't a valid capture file type", ws_optarg);
199 list_capture_types();
200 return WS_EXIT_INVALID_OPTION;
202 break;
204 case 't': /* type of packet to produce */
205 type = g_strdup(ws_optarg);
206 break;
208 case 'h':
209 show_help_header(NULL);
210 usage(false);
211 goto clean_exit;
212 break;
214 case 'r':
215 allrandom = true;
216 break;
218 case 'v':
219 show_version();
220 goto clean_exit;
221 break;
223 case '?':
224 switch(ws_optopt) {
225 case 'F':
226 list_capture_types();
227 return WS_EXIT_INVALID_OPTION;
229 /* FALLTHROUGH */
231 default:
232 usage(true);
233 ret = WS_EXIT_INVALID_OPTION;
234 goto clean_exit;
235 break;
239 /* any more command line parameters? */
240 if (argc > ws_optind) {
241 produce_filename = argv[ws_optind];
242 } else {
243 usage(true);
244 ret = WS_EXIT_INVALID_OPTION;
245 goto clean_exit;
248 if (file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_UNKNOWN) {
249 file_type_subtype = wtap_pcapng_file_type_subtype();
252 if (!allrandom) {
253 produce_type = randpkt_parse_type(type);
254 g_free(type);
256 example = randpkt_find_example(produce_type);
257 if (!example) {
258 ret = WS_EXIT_INVALID_OPTION;
259 goto clean_exit;
262 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
263 if (ret != EXIT_SUCCESS)
264 goto clean_exit;
265 randpkt_loop(example, produce_count, 0);
266 } else {
267 if (type) {
268 fprintf(stderr, "Can't set type in random mode\n");
269 ret = INVALID_TYPE;
270 goto clean_exit;
273 produce_type = randpkt_parse_type(NULL);
274 example = randpkt_find_example(produce_type);
275 if (!example) {
276 ret = WS_EXIT_INVALID_OPTION;
277 goto clean_exit;
279 ret = randpkt_example_init(example, produce_filename, produce_max_bytes, file_type_subtype);
280 if (ret != EXIT_SUCCESS)
281 goto clean_exit;
283 while (produce_count-- > 0) {
284 randpkt_loop(example, 1, 0);
285 produce_type = randpkt_parse_type(NULL);
287 savedump = example->dump;
289 example = randpkt_find_example(produce_type);
290 if (!example) {
291 ret = WS_EXIT_INVALID_OPTION;
292 goto clean_exit;
294 example->dump = savedump;
295 example->filename = produce_filename;
298 if (!randpkt_example_close(example)) {
299 ret = CLOSE_ERROR;
302 clean_exit:
303 wtap_cleanup();
304 return ret;