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/report_message.h>
34 #include <wsutil/wslog.h>
36 #include <wsutil/ws_getopt.h>
37 #include <wsutil/version_info.h>
39 #include "randpkt_core/randpkt_core.h"
41 /* Additional exit codes */
42 #define INVALID_TYPE 2
46 list_capture_types(void) {
47 GArray
*writable_type_subtypes
;
49 cmdarg_err("The available capture file types for the \"-F\" flag are:\n");
50 writable_type_subtypes
= wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME
);
51 for (guint i
= 0; i
< writable_type_subtypes
->len
; i
++) {
52 int ft
= g_array_index(writable_type_subtypes
, int, i
);
53 fprintf(stderr
, " %s - %s\n", wtap_file_type_subtype_name(ft
),
54 wtap_file_type_subtype_description(ft
));
56 g_array_free(writable_type_subtypes
, TRUE
);
60 * Report an error in command-line arguments.
63 randpkt_cmdarg_err(const char *msg_format
, va_list ap
)
65 fprintf(stderr
, "randpkt: ");
66 vfprintf(stderr
, msg_format
, ap
);
67 fprintf(stderr
, "\n");
71 * Report additional information for an error in command-line arguments.
74 randpkt_cmdarg_err_cont(const char *msg_format
, va_list ap
)
76 vfprintf(stderr
, msg_format
, ap
);
77 fprintf(stderr
, "\n");
80 /* Print usage statement and exit program */
82 usage(gboolean is_error
)
96 fprintf(output
, "Usage: randpkt [options] <outfile>\n");
97 fprintf(output
, "\n");
98 fprintf(output
, "Options:\n");
99 fprintf(output
, " -b maximum bytes per packet (default: 5000)\n");
100 fprintf(output
, " -c packet count (default: 1000)\n");
101 fprintf(output
, " -F output file type (default: pcapng)\n");
102 fprintf(output
, " an empty \"-F\" option will list the file types\n");
103 fprintf(output
, " -r select a different random type for each packet\n");
104 fprintf(output
, " -t packet type\n");
105 fprintf(output
, " -h, --help display this help and exit.\n");
106 fprintf(output
, " -v, --version print version information and exit.\n");
107 fprintf(output
, "\n");
108 fprintf(output
, "Types:\n");
110 /* Get the examples list */
111 randpkt_example_list(&abbrev_list
, &longname_list
);
112 while (abbrev_list
[i
] && longname_list
[i
]) {
113 fprintf(output
, "\t%-16s%s\n", abbrev_list
[i
], longname_list
[i
]);
117 g_strfreev(abbrev_list
);
118 g_strfreev(longname_list
);
120 fprintf(output
, "\nIf type is not specified, a random packet type will be chosen\n\n");
124 main(int argc
, char *argv
[])
126 char *configuration_init_error
;
127 static const struct report_message_routines randpkt_report_routines
= {
130 open_failure_message
,
131 read_failure_message
,
132 write_failure_message
,
133 cfile_open_failure_message
,
134 cfile_dump_open_failure_message
,
135 cfile_read_failure_message
,
136 cfile_write_failure_message
,
137 cfile_close_failure_message
140 int produce_type
= -1;
141 char *produce_filename
= NULL
;
142 int produce_max_bytes
= 5000;
143 int produce_count
= 1000;
144 int file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
;
145 randpkt_example
*example
;
147 int allrandom
= FALSE
;
148 wtap_dumper
*savedump
;
149 int ret
= EXIT_SUCCESS
;
150 static const struct ws_option long_options
[] = {
151 {"help", ws_no_argument
, NULL
, 'h'},
152 {"version", ws_no_argument
, NULL
, 'v'},
156 cmdarg_err_init(randpkt_cmdarg_err
, randpkt_cmdarg_err_cont
);
158 /* Initialize log handler early so we can have proper logging during startup. */
159 ws_log_init("randpkt", vcmdarg_err
);
161 /* Early logging command-line initialization. */
162 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, WS_EXIT_INVALID_OPTION
);
164 ws_noisy("Finished log init and parsing command line log arguments");
167 * Get credential information for later use.
169 init_process_policies();
172 * Attempt to get the pathname of the directory containing the
175 configuration_init_error
= configuration_init(argv
[0], NULL
);
176 if (configuration_init_error
!= NULL
) {
178 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
179 configuration_init_error
);
180 g_free(configuration_init_error
);
183 init_report_message("randpkt", &randpkt_report_routines
);
188 create_app_running_mutex();
191 ws_init_version_info("Randpkt", NULL
, NULL
);
193 while ((opt
= ws_getopt_long(argc
, argv
, "b:c:F:ht:rv", long_options
, NULL
)) != -1) {
195 case 'b': /* max bytes */
196 produce_max_bytes
= get_positive_int(ws_optarg
, "max bytes");
197 if (produce_max_bytes
> 65536) {
198 cmdarg_err("max bytes is > 65536");
199 ret
= WS_EXIT_INVALID_OPTION
;
204 case 'c': /* count */
205 produce_count
= get_positive_int(ws_optarg
, "count");
209 file_type_subtype
= wtap_name_to_file_type_subtype(ws_optarg
);
210 if (file_type_subtype
< 0) {
211 cmdarg_err("\"%s\" isn't a valid capture file type", ws_optarg
);
212 list_capture_types();
213 return WS_EXIT_INVALID_OPTION
;
217 case 't': /* type of packet to produce */
218 type
= g_strdup(ws_optarg
);
222 show_help_header(NULL
);
239 list_capture_types();
240 return WS_EXIT_INVALID_OPTION
;
247 ret
= WS_EXIT_INVALID_OPTION
;
253 /* any more command line parameters? */
254 if (argc
> ws_optind
) {
255 produce_filename
= argv
[ws_optind
];
258 ret
= WS_EXIT_INVALID_OPTION
;
262 if (file_type_subtype
== WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
) {
263 file_type_subtype
= wtap_pcapng_file_type_subtype();
267 produce_type
= randpkt_parse_type(type
);
270 example
= randpkt_find_example(produce_type
);
272 ret
= WS_EXIT_INVALID_OPTION
;
276 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
277 if (ret
!= EXIT_SUCCESS
)
279 randpkt_loop(example
, produce_count
, 0);
282 fprintf(stderr
, "Can't set type in random mode\n");
287 produce_type
= randpkt_parse_type(NULL
);
288 example
= randpkt_find_example(produce_type
);
290 ret
= WS_EXIT_INVALID_OPTION
;
293 ret
= randpkt_example_init(example
, produce_filename
, produce_max_bytes
, file_type_subtype
);
294 if (ret
!= EXIT_SUCCESS
)
297 while (produce_count
-- > 0) {
298 randpkt_loop(example
, 1, 0);
299 produce_type
= randpkt_parse_type(NULL
);
301 savedump
= example
->dump
;
303 example
= randpkt_find_example(produce_type
);
305 ret
= WS_EXIT_INVALID_OPTION
;
308 example
->dump
= savedump
;
309 example
->filename
= produce_filename
;
312 if (!randpkt_example_close(example
)) {