* Creates random packet traces. Useful for debugging sniffers by testing
* assumptions about the veracity of the data found in the packet.
*
- * $Id: randpkt.c,v 1.4 1999/10/06 20:29:26 gram Exp $
+ * Copyright (C) 1999 by Gilbert Ramirez <gram@alumni.rice.edu>
*
- * Copyright (C) 1999 by Gilbert Ramirez <gram@xiexie.org>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
*/
-#ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
+
+#include <glib.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <wsutil/clopts_common.h>
+#include <wsutil/cmdarg_err.h>
+#include <wsutil/unicode-utils.h>
+#include <wsutil/file_util.h>
+#include <wsutil/filesystem.h>
+#include <wsutil/privileges.h>
+
+#ifdef HAVE_PLUGINS
+#include <wsutil/plugins.h>
#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#include <wsutil/report_message.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
#endif
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
+#ifndef HAVE_GETOPT_LONG
+#include "wsutil/wsgetopt.h"
#endif
-#include <time.h>
-#include <errno.h>
+#include "randpkt_core/randpkt_core.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <glib.h>
-#include "wiretap/wtap.h"
-
-#define array_length(x) (sizeof x / sizeof x[0])
-
-/* Types of produceable packets */
-enum {
- PKT_ARP,
- PKT_DNS,
- PKT_ETHERNET,
- PKT_FDDI,
- PKT_ICMP,
- PKT_IP,
- PKT_LLC,
- PKT_TCP,
- PKT_TR,
- PKT_UDP
-};
-
-typedef struct {
- char *abbrev;
- char *longname;
- int produceable_type;
- guint8 *sample_buffer;
- int sample_wtap_encap;
- int sample_length;
-} pkt_example;
-
-/* Ethernet, indicating ARP */
-guint8 pkt_arp[] = {
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x00, 0x00,
- 0x32, 0x25, 0x0f, 0xff,
- 0x08, 0x06
-};
-
-/* Ethernet+IP+UP, indicating DNS */
-guint8 pkt_dns[] = {
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01,
- 0x08, 0x00,
-
- 0x45, 0x00, 0x00, 0x3c,
- 0xc5, 0x9e, 0x40, 0x00,
- 0xff, 0x11, 0xd7, 0xe0,
- 0xd0, 0x15, 0x02, 0xb8,
- 0x0a, 0x01, 0x01, 0x63,
-
- 0x05, 0xe8, 0x00, 0x35,
- 0x00, 0x00, 0x2a, 0xb9,
- 0x30
-};
-
-/* Ethernet+IP, indicating ICMPP */
-guint8 pkt_icmp[] = {
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01,
- 0x08, 0x00,
-
- 0x45, 0x00, 0x00, 0x54,
- 0x8f, 0xb3, 0x40, 0x00,
- 0xfd, 0x01, 0x8a, 0x99,
- 0xcc, 0xfc, 0x66, 0x0b,
- 0xce, 0x41, 0x62, 0x12
-};
-
-/* Ethernet, indicating IP */
-guint8 pkt_ip[] = {
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01,
- 0x08, 0x00
-};
-
-/* TR, indicating LLC */
-guint8 pkt_llc[] = {
- 0x10, 0x40, 0x68, 0x00,
- 0x19, 0x69, 0x95, 0x8b,
- 0x00, 0x01, 0xfa, 0x68,
- 0xc4, 0x67
-};
-
-/* TR+LLC+IP, indicating TCP */
-guint8 pkt_tcp[] = {
- 0x10, 0x40, 0x68, 0x00,
- 0x19, 0x69, 0x95, 0x8b,
- 0x00, 0x01, 0xfa, 0x68,
- 0xc4, 0x67,
-
- 0xaa, 0xaa, 0x03, 0x00,
- 0x00, 0x00, 0x08, 0x00,
-
- 0x45, 0x00, 0x00, 0x28,
- 0x0b, 0x0b, 0x40, 0x00,
- 0x20, 0x06, 0x85, 0x37,
- 0xc0, 0xa8, 0x27, 0x01,
- 0xc0, 0xa8, 0x22, 0x3c
-};
-
-/* Ethernet+IP, indicating UDP */
-guint8 pkt_udp[] = {
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01,
- 0x08, 0x00,
-
- 0x45, 0x00, 0x00, 0x3c,
- 0xc5, 0x9e, 0x40, 0x00,
- 0xff, 0x11, 0xd7, 0xe0,
- 0xd0, 0x15, 0x02, 0xb8,
- 0x0a, 0x01, 0x01, 0x63
-};
-
-/* This little data table drives the whole program */
-pkt_example examples[] = {
- { "arp", "Address Resolution Protocol",
- PKT_ARP, pkt_arp, WTAP_ENCAP_ETHERNET, array_length(pkt_arp) },
-
- { "dns", "Domain Name Service",
- PKT_DNS, pkt_dns, WTAP_ENCAP_ETHERNET, array_length(pkt_dns) },
-
- { "eth", "Ethernet",
- PKT_ETHERNET, NULL, WTAP_ENCAP_ETHERNET, 0 },
-
- { "fddi", "Fiber Distributed Data Interface",
- PKT_FDDI, NULL, WTAP_ENCAP_FDDI, 0 },
-
- { "icmp", "Internet Control Message Protocol",
- PKT_ICMP, pkt_icmp, WTAP_ENCAP_ETHERNET, array_length(pkt_icmp) },
-
- { "ip", "Internet Protocol",
- PKT_IP, pkt_ip, WTAP_ENCAP_ETHERNET, array_length(pkt_ip) },
-
- { "llc", "Logical Link Control",
- PKT_LLC, pkt_llc, WTAP_ENCAP_TR, array_length(pkt_llc) },
-
- { "tcp", "Transmission Control Protocol",
- PKT_TCP, pkt_tcp, WTAP_ENCAP_TR, array_length(pkt_tcp) },
-
- { "tr", "Token-Ring",
- PKT_TR, NULL, WTAP_ENCAP_TR, 0 },
-
- { "udp", "User Datagram Protocol",
- PKT_UDP, pkt_udp, WTAP_ENCAP_ETHERNET, array_length(pkt_udp) }
-};
-
-
-
-static int parse_type(char *string);
-static void usage(void);
-static void seed(void);
-
-static pkt_example* find_example(int type);
+#define INVALID_OPTION 1
+#define INVALID_TYPE 2
+#define CLOSE_ERROR 2
-int
-main(int argc, char **argv)
+/*
+ * General errors and warnings are reported with an console message
+ * in randpkt.
+ */
+static void
+failure_warning_message(const char *msg_format, va_list ap)
{
+ fprintf(stderr, "randpkt: ");
+ vfprintf(stderr, msg_format, ap);
+ fprintf(stderr, "\n");
+}
- wtap_dumper *dump;
- struct wtap_pkthdr pkthdr;
- int i, j, len_this_pkt, len_random, err;
- guint8 buffer[65536];
+/*
+ * Report additional information for an error in command-line arguments.
+ */
+static void
+failure_message_cont(const char *msg_format, va_list ap)
+{
+ vfprintf(stderr, msg_format, ap);
+ fprintf(stderr, "\n");
+}
- int opt;
- extern char *optarg;
- extern int optind;
+/* Print usage statement and exit program */
+static void
+usage(gboolean is_error)
+{
+ FILE *output;
+ char** abbrev_list;
+ char** longname_list;
+ unsigned i = 0;
+
+ if (!is_error) {
+ output = stdout;
+ }
+ else {
+ output = stderr;
+ }
+
+ fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
+ fprintf(output, "Default max bytes (per packet) is 5000\n");
+ fprintf(output, "Default count is 1000.\n");
+ fprintf(output, "-r: random packet type selection\n");
+ fprintf(output, "\n");
+ fprintf(output, "Types:\n");
+
+ /* Get the examples list */
+ randpkt_example_list(&abbrev_list, &longname_list);
+ while (abbrev_list[i] && longname_list[i]) {
+ fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
+ i++;
+ }
+
+ g_strfreev(abbrev_list);
+ g_strfreev(longname_list);
- int produce_count = 1000; /* number of pkts to produce */
- int produce_type = PKT_ETHERNET;
+ fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
+}
+
+static int
+real_main(int argc, char **argv)
+{
+ char *init_progfile_dir_error;
+ int opt;
+ int produce_type = -1;
char *produce_filename = NULL;
int produce_max_bytes = 5000;
- pkt_example *example;
+ int produce_count = 1000;
+ randpkt_example *example;
+ guint8* type = NULL;
+ int allrandom = FALSE;
+ wtap_dumper *savedump;
+ int ret = EXIT_SUCCESS;
+ static const struct option long_options[] = {
+ {"help", no_argument, NULL, 'h'},
+ {0, 0, 0, 0 }
+ };
+
+ /*
+ * Get credential information for later use.
+ */
+ init_process_policies();
+
+ /*
+ * Attempt to get the pathname of the directory containing the
+ * executable file.
+ */
+ init_progfile_dir_error = init_progfile_dir(argv[0]);
+ if (init_progfile_dir_error != NULL) {
+ fprintf(stderr,
+ "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
+ init_progfile_dir_error);
+ g_free(init_progfile_dir_error);
+ }
+
+ init_report_message(failure_warning_message, failure_warning_message,
+ NULL, NULL, NULL);
- while ((opt = getopt(argc, argv, "b:c:t:")) != EOF) {
+ wtap_init(TRUE);
+
+ cmdarg_err_init(failure_warning_message, failure_message_cont);
+
+#ifdef _WIN32
+ create_app_running_mutex();
+#endif /* _WIN32 */
+
+ while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
switch (opt) {
case 'b': /* max bytes */
- produce_max_bytes = atoi(optarg);
+ produce_max_bytes = get_positive_int(optarg, "max bytes");
if (produce_max_bytes > 65536) {
- printf("Max bytes is 65536\n");
- exit(0);
+ cmdarg_err("max bytes is > 65536");
+ ret = INVALID_OPTION;
+ goto clean_exit;
}
break;
case 'c': /* count */
- produce_count = atoi(optarg);
+ produce_count = get_positive_int(optarg, "count");
break;
case 't': /* type of packet to produce */
- produce_type = parse_type(optarg);
+ type = g_strdup(optarg);
+ break;
+
+ case 'h':
+ usage(FALSE);
+ goto clean_exit;
+ break;
+
+ case 'r':
+ allrandom = TRUE;
break;
default:
- usage();
+ usage(TRUE);
+ ret = INVALID_OPTION;
+ goto clean_exit;
break;
}
}
produce_filename = argv[optind];
}
else {
- usage();
- }
-
- example = find_example(produce_type);
-
- pkthdr.ts.tv_sec = 0;
- pkthdr.ts.tv_usec = 0;
- pkthdr.pkt_encap = example->sample_wtap_encap;
-
- dump = wtap_dump_open(produce_filename, WTAP_FILE_PCAP,
- example->sample_wtap_encap, produce_max_bytes, &err);
-
- seed();
-
- /* reduce max_bytes by # of bytes already in sample */
- if (produce_max_bytes <= example->sample_length) {
- printf("Sample packet length is %d, which is greater than or equal to\n", example->sample_length);
- printf("your requested max_bytes value of %d\n", produce_max_bytes);
- exit(0);
- }
- else {
- produce_max_bytes -= example->sample_length;
+ usage(TRUE);
+ ret = INVALID_OPTION;
+ goto clean_exit;
}
- /* Load the sample into our buffer */
- if (example->sample_buffer)
- memcpy(&buffer[0], example->sample_buffer, example->sample_length);
+ if (!allrandom) {
+ produce_type = randpkt_parse_type(type);
+ g_free(type);
- /* Produce random packets */
- for (i = 0; i < produce_count; i++) {
- if (produce_max_bytes > 0) {
- len_random = (rand() % produce_max_bytes + 1);
- }
- else {
- len_random = 0;
+ example = randpkt_find_example(produce_type);
+ if (!example) {
+ ret = INVALID_OPTION;
+ goto clean_exit;
}
- len_this_pkt = example->sample_length + len_random;
-
- pkthdr.caplen = len_this_pkt;
- pkthdr.len = len_this_pkt;
- pkthdr.ts.tv_sec = i; /* just for variety */
-
- for (j = example->sample_length; j < len_random; j++) {
- buffer[j] = (rand() % 0x100);
+ ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
+ if (ret != EXIT_SUCCESS)
+ goto clean_exit;
+ randpkt_loop(example, produce_count, 0);
+ } else {
+ if (type) {
+ fprintf(stderr, "Can't set type in random mode\n");
+ ret = INVALID_TYPE;
+ goto clean_exit;
}
- wtap_dump(dump, &pkthdr, &buffer[0], &err);
+ produce_type = randpkt_parse_type(NULL);
+ example = randpkt_find_example(produce_type);
+ if (!example) {
+ ret = INVALID_OPTION;
+ goto clean_exit;
+ }
+ ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
+ if (ret != EXIT_SUCCESS)
+ goto clean_exit;
+
+ while (produce_count-- > 0) {
+ randpkt_loop(example, 1, 0);
+ produce_type = randpkt_parse_type(NULL);
+
+ savedump = example->dump;
+
+ example = randpkt_find_example(produce_type);
+ if (!example) {
+ ret = INVALID_OPTION;
+ goto clean_exit;
+ }
+ example->dump = savedump;
+ }
}
-
- wtap_dump_close(dump, &err);
-
- return 0;
-
-}
-
-/* Print usage statement and exit program */
-static
-void usage(void)
-{
- int num_entries = array_length(examples);
- int i;
-
- printf("Usage: randpkt [-b maxbytes] [-c count] [-t type] filename\n");
- printf("Default max bytes (per packet) is 5000\n");
- printf("Default count is 1000.\n");
- printf("Types:\n");
-
- for (i = 0; i < num_entries; i++) {
- printf("\t%s\t%s\n", examples[i].abbrev, examples[i].longname);
+ if (!randpkt_example_close(example)) {
+ ret = CLOSE_ERROR;
}
- printf("\n");
-
- exit(0);
+clean_exit:
+ wtap_cleanup();
+ return ret;
}
-/* Parse command-line option "type" and return enum type */
-static
-int parse_type(char *string)
+#ifdef _WIN32
+int
+wmain(int argc, wchar_t **wc_argv)
{
- int num_entries = array_length(examples);
- int i;
-
- for (i = 0; i < num_entries; i++) {
- if (strcmp(examples[i].abbrev, string) == 0) {
- return examples[i].produceable_type;
- }
- }
+ char **argv;
- /* default type */
- return PKT_ETHERNET;
+ argv = arg_list_utf_16to8(argc, wc_argv);
+ return real_main(argc, argv);
}
-
-/* Find pkt_example record and return pointer to it */
-static
-pkt_example* find_example(int type)
+#else
+int
+main(int argc, char **argv)
{
- int num_entries = array_length(examples);
- int i;
-
- for (i = 0; i < num_entries; i++) {
- if (examples[i].produceable_type == type) {
- return &examples[i];
- }
- }
-
- printf("Internal error. Type %d has no entry in examples table.\n", type);
- exit(0);
+ return real_main(argc, argv);
}
-
-/* Seed the random-number generator */
-void
-seed(void)
-{
- unsigned int randomness;
-
-#if defined(linux)
- /* Okay, I should use #ifdef HAVE_DEV_RANDOM, but this is a quick hack */
- int fd;
-
- fd = open("/dev/random", O_RDONLY);
- if (fd < 0) {
- printf("Could not open /dev/random for reading: %s\n", strerror(errno));
- exit(0);
- }
-
- read(fd, &randomness, sizeof(randomness));
-#else
- time_t now;
-
- now = time(NULL);
- randomness = (unsigned int) now;
#endif
- srand(randomness);
-}
+/*
+ * Editor modelines - http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */