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
13 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
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>
30 #include <wsutil/plugins.h>
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
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 */
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
]);
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
;
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'},
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
144 configuration_init_error
= configuration_init(argv
[0]);
145 if (configuration_init_error
!= NULL
) {
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");
157 create_app_running_mutex();
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) {
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
;
173 case 'c': /* count */
174 produce_count
= get_positive_int(ws_optarg
, "count");
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
;
186 case 't': /* type of packet to produce */
187 type
= g_strdup(ws_optarg
);
191 show_help_header(NULL
);
208 list_capture_types();
209 return WS_EXIT_INVALID_OPTION
;
215 ret
= WS_EXIT_INVALID_OPTION
;
221 /* any more command line parameters? */
222 if (argc
> ws_optind
) {
223 produce_filename
= argv
[ws_optind
];
226 ret
= WS_EXIT_INVALID_OPTION
;
230 if (file_type_subtype
== WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
) {
231 file_type_subtype
= wtap_pcapng_file_type_subtype();
235 produce_type
= randpkt_parse_type(type
);
238 example
= randpkt_find_example(produce_type
);
240 ret
= WS_EXIT_INVALID_OPTION
;
244 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
245 if (ret
!= EXIT_SUCCESS
)
247 randpkt_loop(example
, produce_count
, 0);
250 fprintf(stderr
, "Can't set type in random mode\n");
255 produce_type
= randpkt_parse_type(NULL
);
256 example
= randpkt_find_example(produce_type
);
258 ret
= WS_EXIT_INVALID_OPTION
;
261 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
262 if (ret
!= EXIT_SUCCESS
)
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
);
273 ret
= WS_EXIT_INVALID_OPTION
;
276 example
->dump
= savedump
;
277 example
->filename
= produce_filename
;
280 if (!randpkt_example_close(example
)) {