* 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.1 1999/09/10 05:15:10 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-
-#include <time.h>
-#include <errno.h>
+#include "randpkt-core.h"
+#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <glib.h>
-#include <wtap.h>
-
-#define array_length(x) (sizeof x / sizeof x[0])
+#include <wsutil/ws_diag_control.h>
+#include <wsutil/unicode-utils.h>
+#include <wsutil/file_util.h>
-/* Types of produceable packets */
-enum {
- PKT_ARP,
- PKT_ETHERNET,
- PKT_FDDI,
- PKT_LLC,
- PKT_TR
-};
-
-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
-};
-
-/* TR, indicating LLC */
-guint8 pkt_llc[] = {
- 0x10, 0x40, 0x68, 0x00,
- 0x19, 0x69, 0x95, 0x8b,
- 0x00, 0x01, 0xfa, 0x68,
- 0xc4, 0x67
-};
-
-/* 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) },
-
- { "eth", "Ethernet",
- PKT_ETHERNET, NULL, WTAP_ENCAP_ETHERNET, 0 },
-
- { "fddi", "Fiber Distributed Data Interface",
- PKT_FDDI, NULL, WTAP_ENCAP_FDDI, 0 },
-
- { "llc", "Logical Link Control",
- PKT_LLC, pkt_llc, WTAP_ENCAP_TR, array_length(pkt_llc) },
-
- { "tr", "Token-Ring",
- PKT_TR, NULL, WTAP_ENCAP_TR, 0 }
-};
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+#ifndef HAVE_GETOPT_LONG
+#include "wsutil/wsgetopt.h"
+#endif
+/* Print usage statement and exit program */
+static void
+usage(gboolean is_error)
+{
+ FILE *output;
+ const char** abbrev_list;
+ const char** longname_list;
+ unsigned list_num;
+ unsigned i;
+
+ if (!is_error) {
+ output = stdout;
+ }
+ else {
+ output = stderr;
+ }
-static int parse_type(char *string);
-static void usage(void);
-static void seed(void);
+ 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, &list_num);
+ for (i = 0; i < list_num; i++) {
+ fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
+ }
+ g_free((char**)abbrev_list);
+ g_free((char**)longname_list);
-static pkt_example* find_example(int type);
+ fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
+ exit(is_error ? 1 : 0);
+}
int
main(int argc, char **argv)
{
-
- wtap_dumper *dump;
- struct wtap_pkthdr pkthdr;
- int i, j, len_this_pkt, len_random, err;
- guint8 buffer[65536];
-
int opt;
- extern char *optarg;
- extern int optind;
-
- int produce_count = 1000; /* number of pkts to produce */
- int produce_type = PKT_ETHERNET;
+ int produce_type = -1;
char *produce_filename = NULL;
int produce_max_bytes = 5000;
- pkt_example *example;
-
- while ((opt = getopt(argc, argv, "b:c:t:")) != EOF) {
+ int produce_count = 1000;
+ randpkt_example *example;
+ guint8* type = NULL;
+ int allrandom = FALSE;
+ wtap_dumper *savedump;
+ static const struct option long_options[] = {
+ {"help", no_argument, NULL, 'h'},
+ {0, 0, 0, 0 }
+ };
+
+#ifdef _WIN32
+ arg_list_utf_16to8(argc, argv);
+ 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);
if (produce_max_bytes > 65536) {
- printf("Max bytes is 65536\n");
- exit(0);
+ fprintf(stderr, "randpkt: Max bytes is 65536\n");
+ return 1;
}
break;
break;
case 't': /* type of packet to produce */
- produce_type = parse_type(optarg);
+ type = g_strdup(optarg);
+ break;
+
+ case 'h':
+ usage(FALSE);
+ break;
+
+ case 'r':
+ allrandom = TRUE;
break;
default:
- usage();
+ usage(TRUE);
break;
}
}
produce_filename = argv[optind];
}
else {
- usage();
+ usage(TRUE);
}
- example = find_example(produce_type);
-
- pkthdr.ts.tv_sec = 0;
- pkthdr.ts.tv_usec = 0;
- pkthdr.pkt_encap = example->sample_wtap_encap;
+ randpkt_seed();
- 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;
- }
+ if (!allrandom) {
+ produce_type = randpkt_parse_type(type);
+ g_free(type);
- /* Load the sample into our buffer */
- if (example->sample_buffer)
- memcpy(&buffer[0], example->sample_buffer, example->sample_length);
+ example = randpkt_find_example(produce_type);
+ if (!example)
+ return 1;
- /* Produce random packets */
- for (i = 0; i < produce_count; i++) {
- if (produce_max_bytes > 0) {
- len_random = (rand() % produce_max_bytes + 1);
+ randpkt_example_init(example, produce_filename, produce_max_bytes);
+ randpkt_loop(example, produce_count);
+ } else {
+ if (type) {
+ fprintf(stderr, "Can't set type in random mode\n");
+ return 2;
}
- else {
- len_random = 0;
- }
-
- 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);
- }
+ produce_type = randpkt_parse_type(NULL);
+ example = randpkt_find_example(produce_type);
+ if (!example)
+ return 1;
+ randpkt_example_init(example, produce_filename, produce_max_bytes);
- wtap_dump(dump, &pkthdr, &buffer[0], &err);
- }
+ while (produce_count-- > 0) {
+ randpkt_loop(example, 1);
+ produce_type = randpkt_parse_type(NULL);
- wtap_dump_close(dump, &err);
+ savedump = example->dump;
- 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);
- }
-
- printf("\n");
-
- exit(0);
-}
-
-/* Parse command-line option "type" and return enum type */
-static
-int parse_type(char *string)
-{
- 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;
- }
- }
-
- /* default type */
- return PKT_ETHERNET;
-}
-
-/* Find pkt_example record and return pointer to it */
-static
-pkt_example* find_example(int type)
-{
- int num_entries = array_length(examples);
- int i;
-
- for (i = 0; i < num_entries; i++) {
- if (examples[i].produceable_type == type) {
- return &examples[i];
+ example = randpkt_find_example(produce_type);
+ if (!example)
+ return 1;
+ example->dump = savedump;
}
}
+ if (!randpkt_example_close(example))
+ return 2;
+ return 0;
- printf("Internal error. Type %d has no entry in examples table.\n", type);
- exit(0);
}
-/* 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:
+ */