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
);
59 * Report an error in command-line arguments.
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.
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 */
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
]);
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
;
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'},
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
162 configuration_init_error
= configuration_init(argv
[0], NULL
);
163 if (configuration_init_error
!= NULL
) {
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");
175 create_app_running_mutex();
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) {
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
;
191 case 'c': /* count */
192 produce_count
= get_positive_int(ws_optarg
, "count");
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
;
204 case 't': /* type of packet to produce */
205 type
= g_strdup(ws_optarg
);
209 show_help_header(NULL
);
226 list_capture_types();
227 return WS_EXIT_INVALID_OPTION
;
233 ret
= WS_EXIT_INVALID_OPTION
;
239 /* any more command line parameters? */
240 if (argc
> ws_optind
) {
241 produce_filename
= argv
[ws_optind
];
244 ret
= WS_EXIT_INVALID_OPTION
;
248 if (file_type_subtype
== WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
) {
249 file_type_subtype
= wtap_pcapng_file_type_subtype();
253 produce_type
= randpkt_parse_type(type
);
256 example
= randpkt_find_example(produce_type
);
258 ret
= WS_EXIT_INVALID_OPTION
;
262 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
263 if (ret
!= EXIT_SUCCESS
)
265 randpkt_loop(example
, produce_count
, 0);
268 fprintf(stderr
, "Can't set type in random mode\n");
273 produce_type
= randpkt_parse_type(NULL
);
274 example
= randpkt_find_example(produce_type
);
276 ret
= WS_EXIT_INVALID_OPTION
;
279 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
280 if (ret
!= EXIT_SUCCESS
)
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
);
291 ret
= WS_EXIT_INVALID_OPTION
;
294 example
->dump
= savedump
;
295 example
->filename
= produce_filename
;
298 if (!randpkt_example_close(example
)) {