Avoid adding -pie on older cmake versions
[metze/wireshark/wip.git] / randpkt.c
index f88cac0925a036095c783cdcd6d05b49296e66c5..bf391be2bbe1843d2fbd95fbcea32ec7aaae63a5 100644 (file)
--- a/randpkt.c
+++ b/randpkt.c
@@ -4,8 +4,6 @@
  * 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.19 2004/02/23 14:37:59 jmayer Exp $
- *
  * Copyright (C) 1999 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
  * This program is free software; you can redistribute it and/or
  *
  * 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"
+#include <config.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
 #endif
 
-#ifdef NEED_GETOPT_H
-#include "getopt.h"
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#ifndef HAVE_GETOPT_LONG
+#include "wsutil/wsgetopt.h"
 #endif
 
 #ifdef HAVE_FCNTL_H
 #include <string.h>
 #include <glib.h>
 #include "wiretap/wtap.h"
+#include "wsutil/file_util.h"
+#include <wsutil/ws_diag_control.h>
+
+#ifdef _WIN32
+#include <wsutil/unicode-utils.h>
+#endif /* _WIN32 */
 
 #define array_length(x)        (sizeof x / sizeof x[0])
 
 /* Types of produceable packets */
 enum {
        PKT_ARP,
+       PKT_BGP,
+       PKT_BVLC,
        PKT_DNS,
        PKT_ETHERNET,
        PKT_FDDI,
+       PKT_GIOP,
        PKT_ICMP,
        PKT_IP,
        PKT_LLC,
+       PKT_M2M,
+       PKT_MEGACO,
        PKT_NBNS,
+       PKT_NCP2222,
+       PKT_SCTP,
        PKT_SYSLOG,
        PKT_TCP,
+       PKT_TDS,
        PKT_TR,
        PKT_UDP,
-       PKT_BVLC,
-       PKT_NCP2222,
-       PKT_GIOP,
-       PKT_BGP,
-       PKT_TDS
+       PKT_USB,
+       PKT_USB_LINUX
 };
 
 typedef struct {
-       char    *abbrev;
-       char    *longname;
-       int     produceable_type;
-       guint8  *sample_buffer;
-       int     sample_wtap_encap;
-       int     sample_length;
+       const char      *abbrev;
+       const char      *longname;
+       int             produceable_type;
+       int             sample_wtap_encap;
+       guint8          *sample_buffer;
+       int             sample_length;
+       guint8          *pseudo_buffer;
+       int             pseudo_length;
 } pkt_example;
 
 /* Ethernet, indicating ARP */
@@ -102,7 +115,7 @@ guint8 pkt_dns[] = {
        0x0a, 0x01, 0x01, 0x63,
 
        0x05, 0xe8, 0x00, 0x35,
-       0x00, 0x00, 0x2a, 0xb9,
+       0xff, 0xff, 0x2a, 0xb9,
        0x30
 };
 
@@ -136,6 +149,14 @@ guint8 pkt_llc[] = {
        0xc4, 0x67
 };
 
+/* Ethernet, indicating WiMAX M2M */
+guint8 pkt_m2m[] = {
+       0xff, 0xff, 0xff, 0xff,
+       0xff, 0xff, 0x00, 0x00,
+       0x32, 0x25, 0x0f, 0xff,
+       0x08, 0xf0
+};
+
 /* Ethernet+IP+UDP, indicating NBNS */
 guint8 pkt_nbns[] = {
        0xff, 0xff, 0xff, 0xff,
@@ -315,65 +336,161 @@ guint8 pkt_tds[] = {
        0x0f, 0xda, 0x00, 0x00,
 };
 
+/* Ethernet+IP, indicating SCTP */
+guint8 pkt_sctp[] = {
+       0x00, 0xa0, 0x80, 0x00,
+       0x5e, 0x46, 0x08, 0x00,
+       0x03, 0x4a, 0x00, 0x35,
+       0x08, 0x00,
+
+       0x45, 0x00, 0x00, 0x7c,
+       0x14, 0x1c, 0x00, 0x00,
+       0x3b, 0x84, 0x4a, 0x54,
+       0x0a, 0x1c, 0x06, 0x2b,
+       0x0a, 0x1c, 0x06, 0x2c,
+};
+
+
+/* Ethernet+IP+SCTP, indicating MEGACO */
+guint8 pkt_megaco[] = {
+       0x00, 0xa0, 0x80, 0x00,
+       0x5e, 0x46, 0x08, 0x00,
+       0x03, 0x4a, 0x00, 0x35,
+       0x08, 0x00,
+
+       0x45, 0x00, 0x00, 0x7c,
+       0x14, 0x1c, 0x00, 0x00,
+       0x3b, 0x84, 0x4a, 0x54,
+       0x0a, 0x1c, 0x06, 0x2b,
+       0x0a, 0x1c, 0x06, 0x2c,
+
+       0x40, 0x00, 0x0b, 0x80,
+       0x00, 0x01, 0x6f, 0x0a,
+       0x6d, 0xb0, 0x18, 0x82,
+       0x00, 0x03, 0x00, 0x5b,
+       0x28, 0x02, 0x43, 0x45,
+       0x00, 0x00, 0xa0, 0xbd,
+       0x00, 0x00, 0x00, 0x07,
+};
+
 /* 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) },
+               PKT_ARP,        WTAP_ENCAP_ETHERNET,
+               pkt_arp,        array_length(pkt_arp),
+               NULL,           0 },
+
+       { "bgp", "Border Gateway Protocol",
+               PKT_BGP,        WTAP_ENCAP_ETHERNET,
+               pkt_bgp,        array_length(pkt_bgp),
+               NULL,           0 },
+
+       { "bvlc", "BACnet Virtual Link Control",
+               PKT_BVLC,       WTAP_ENCAP_ETHERNET,
+               pkt_bvlc,       array_length(pkt_bvlc),
+               NULL,           0 },
 
        { "dns", "Domain Name Service",
-               PKT_DNS,        pkt_dns,        WTAP_ENCAP_ETHERNET,    array_length(pkt_dns) },
+               PKT_DNS,        WTAP_ENCAP_ETHERNET,
+               pkt_dns,        array_length(pkt_dns),
+               NULL,           0 },
 
        { "eth", "Ethernet",
-               PKT_ETHERNET,   NULL,           WTAP_ENCAP_ETHERNET,    0 },
+               PKT_ETHERNET,   WTAP_ENCAP_ETHERNET,
+               NULL,           0,
+               NULL,           0 },
 
        { "fddi", "Fiber Distributed Data Interface",
-               PKT_FDDI,       NULL,           WTAP_ENCAP_FDDI,        0 },
+               PKT_FDDI,       WTAP_ENCAP_FDDI,
+               NULL,           0,
+               NULL,           0 },
+
+       { "giop", "General Inter-ORB Protocol",
+               PKT_GIOP,       WTAP_ENCAP_ETHERNET,
+               pkt_giop,       array_length(pkt_giop),
+               NULL,           0 },
 
        { "icmp", "Internet Control Message Protocol",
-               PKT_ICMP,       pkt_icmp,       WTAP_ENCAP_ETHERNET,    array_length(pkt_icmp) },
+               PKT_ICMP,       WTAP_ENCAP_ETHERNET,
+               pkt_icmp,       array_length(pkt_icmp),
+               NULL,           0 },
 
        { "ip", "Internet Protocol",
-               PKT_IP,         pkt_ip,         WTAP_ENCAP_ETHERNET,    array_length(pkt_ip) },
+               PKT_IP,         WTAP_ENCAP_ETHERNET,
+               pkt_ip,         array_length(pkt_ip),
+               NULL,           0 },
 
        { "llc", "Logical Link Control",
-               PKT_LLC,        pkt_llc,        WTAP_ENCAP_TOKEN_RING,  array_length(pkt_llc) },
+               PKT_LLC,        WTAP_ENCAP_TOKEN_RING,
+               pkt_llc,        array_length(pkt_llc),
+               NULL,           0 },
+
+       { "m2m", "WiMAX M2M Encapsulation Protocol",
+               PKT_M2M,        WTAP_ENCAP_ETHERNET,
+               pkt_m2m,        array_length(pkt_m2m),
+               NULL,           0 },
+
+       { "megaco", "MEGACO",
+               PKT_MEGACO,     WTAP_ENCAP_ETHERNET,
+               pkt_megaco,     array_length(pkt_megaco),
+               NULL,           0 },
 
        { "nbns", "NetBIOS-over-TCP Name Service",
-               PKT_NBNS,       pkt_nbns,       WTAP_ENCAP_ETHERNET,    array_length(pkt_nbns) },
+               PKT_NBNS,       WTAP_ENCAP_ETHERNET,
+               pkt_nbns,       array_length(pkt_nbns),
+               NULL,           0 },
+
+       { "ncp2222", "NetWare Core Protocol",
+               PKT_NCP2222,    WTAP_ENCAP_TOKEN_RING,
+               pkt_ncp2222,    array_length(pkt_ncp2222),
+               NULL,           0 },
+
+       { "sctp", "Stream Control Transmission Protocol",
+               PKT_SCTP,       WTAP_ENCAP_ETHERNET,
+               pkt_sctp,       array_length(pkt_sctp),
+               NULL,           0 },
 
        { "syslog", "Syslog message",
-               PKT_SYSLOG,     pkt_syslog,     WTAP_ENCAP_ETHERNET,    array_length(pkt_syslog) },
+               PKT_SYSLOG,     WTAP_ENCAP_ETHERNET,
+               pkt_syslog,     array_length(pkt_syslog),
+               NULL,           0 },
+
+       { "tds", "TDS NetLib",
+               PKT_TDS,        WTAP_ENCAP_ETHERNET,
+               pkt_tds,        array_length(pkt_tds),
+               NULL,           0 },
 
        { "tcp", "Transmission Control Protocol",
-               PKT_TCP,        pkt_tcp,        WTAP_ENCAP_TOKEN_RING,  array_length(pkt_tcp) },
+               PKT_TCP,        WTAP_ENCAP_TOKEN_RING,
+               pkt_tcp,        array_length(pkt_tcp),
+               NULL,           0 },
 
        { "tr",  "Token-Ring",
-               PKT_TR,         NULL,           WTAP_ENCAP_TOKEN_RING,  0 },
+               PKT_TR,         WTAP_ENCAP_TOKEN_RING,
+               NULL,           0,
+               NULL,           0 },
 
        { "udp", "User Datagram Protocol",
-               PKT_UDP,        pkt_udp,        WTAP_ENCAP_ETHERNET,    array_length(pkt_udp) },
-
-       { "bvlc", "BACnet Virtual Link Control",
-               PKT_BVLC,       pkt_bvlc,       WTAP_ENCAP_ETHERNET,    array_length(pkt_bvlc) },
+               PKT_UDP,        WTAP_ENCAP_ETHERNET,
+               pkt_udp,        array_length(pkt_udp),
+               NULL,           0 },
 
-       { "ncp2222", "NetWare Core Protocol",
-               PKT_NCP2222,    pkt_ncp2222,    WTAP_ENCAP_TOKEN_RING,  array_length(pkt_ncp2222) },
+       { "usb", "Universal Serial Bus",
+               PKT_USB,        WTAP_ENCAP_USB,
+               NULL,           0,
+               NULL,           0 },
 
-       { "giop", "General Inter-ORB Protocol",
-               PKT_GIOP,       pkt_giop,       WTAP_ENCAP_ETHERNET,    array_length(pkt_giop) },
-
-       { "bgp", "Border Gateway Protocol",
-               PKT_BGP,        pkt_bgp,        WTAP_ENCAP_ETHERNET,    array_length(pkt_bgp) },
-
-       { "tds", "TDS NetLib",
-               PKT_TDS,        pkt_tds,        WTAP_ENCAP_ETHERNET,    array_length(pkt_tds) },
+       { "usb-linux", "Universal Serial Bus with Linux specific header",
+               PKT_USB_LINUX,  WTAP_ENCAP_USB_LINUX,
+               NULL,           0,
+               NULL,           0 },
 
 };
 
 
 
 static int parse_type(char *string);
-static void usage(void);
+static void usage(gboolean is_error);
 static void seed(void);
 
 static pkt_example* find_example(int type);
@@ -384,27 +501,38 @@ main(int argc, char **argv)
 
        wtap_dumper             *dump;
        struct wtap_pkthdr      pkthdr;
-       union wtap_pseudo_header        ps_header;
+       union wtap_pseudo_header *ps_header = &pkthdr.pseudo_header;
        int                     i, j, len_this_pkt, len_random, err;
+       gchar                   *err_info;
        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;
        char                    *produce_filename = NULL;
        int                     produce_max_bytes = 5000;
        pkt_example             *example;
-
-       while ((opt = getopt(argc, argv, "b:c:t:")) != -1) {
+DIAG_OFF(cast-qual)
+       static const struct option long_options[] = {
+               {(char *)"help", no_argument, NULL, 'h'},
+               {0, 0, 0, 0 }
+       };
+DIAG_ON(cast-qual)
+
+#ifdef _WIN32
+       arg_list_utf_16to8(argc, argv);
+       create_app_running_mutex();
+#endif /* _WIN32 */
+
+       while ((opt = getopt_long(argc, argv, "b:c:ht:", 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");
+                                       exit(1);
                                }
                                break;
 
@@ -416,8 +544,11 @@ main(int argc, char **argv)
                                produce_type = parse_type(optarg);
                                break;
 
+                       case 'h':
+                               usage(FALSE);
+                               break;
                        default:
-                               usage();
+                               usage(TRUE);
                                break;
                }
        }
@@ -427,30 +558,46 @@ main(int argc, char **argv)
                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;
 
-       dump = wtap_dump_open(produce_filename, WTAP_FILE_PCAP,
-               example->sample_wtap_encap, produce_max_bytes, &err);
+       dump = wtap_dump_open(produce_filename, WTAP_FILE_TYPE_SUBTYPE_PCAP,
+               example->sample_wtap_encap, produce_max_bytes, FALSE /* compressed */, &err);
+       if (!dump) {
+               fprintf(stderr,
+                   "randpkt: Error writing to %s\n", produce_filename);
+               exit(2);
+       }
 
        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);
+               fprintf(stderr,
+                   "randpkt: Sample packet length is %d, which is greater than or equal to\n",
+                   example->sample_length);
+               fprintf(stderr, "your requested max_bytes value of %d\n",
+                   produce_max_bytes);
+               exit(1);
        }
        else {
                produce_max_bytes -= example->sample_length;
        }
 
+       memset(&pkthdr, 0, sizeof(pkthdr));
+       memset(buffer, 0, sizeof(buffer));
+
+       pkthdr.rec_type = REC_TYPE_PACKET;
+       pkthdr.presence_flags = WTAP_HAS_TS;
+       pkthdr.pkt_encap = example->sample_wtap_encap;
+
+       /* Load the sample pseudoheader into our pseudoheader buffer */
+       if (example->pseudo_buffer)
+               memcpy(ps_header, example->pseudo_buffer, example->pseudo_length);
+
        /* Load the sample into our buffer */
        if (example->sample_buffer)
                memcpy(&buffer[0], example->sample_buffer, example->sample_length);
@@ -468,13 +615,27 @@ main(int argc, char **argv)
 
                pkthdr.caplen = len_this_pkt;
                pkthdr.len = len_this_pkt;
-               pkthdr.ts.tv_sec = i; /* just for variety */
+               pkthdr.ts.secs = i; /* just for variety */
 
-               for (j = example->sample_length; j < len_random; j++) {
-                       buffer[j] = (rand() % 0x100);
+               for (j = example->pseudo_length; j < (int) sizeof(*ps_header); j++) {
+                       ((guint8*)ps_header)[j] = (rand() % 0x100);
                }
 
-               wtap_dump(dump, &pkthdr, &ps_header, &buffer[0], &err);
+               for (j = example->sample_length; j < len_this_pkt; j++) {
+                       /* Add format strings here and there */
+                       if ((int) (100.0*rand()/(RAND_MAX+1.0)) < 3 && j < (len_random - 3)) {
+                               memcpy(&buffer[j], "%s", 3);
+                               j += 2;
+                       } else {
+                               buffer[j] = (rand() % 0x100);
+                       }
+               }
+
+               /* XXX - report errors! */
+               if (!wtap_dump(dump, &pkthdr, &buffer[0], &err, &err_info)) {
+                       if (err_info != NULL)
+                               g_free(err_info);
+               }
        }
 
        wtap_dump_close(dump, &err);
@@ -484,24 +645,32 @@ main(int argc, char **argv)
 }
 
 /* Print usage statement and exit program */
-static
-void usage(void)
+static void
+usage(gboolean is_error)
 {
-       int     num_entries = array_length(examples);
-       int     i;
+       FILE    *output;
+       int      num_entries = array_length(examples);
+       int      i;
+
+       if (!is_error) {
+               output = stdout;
+       }
+       else {
+               output = stderr;
+       }
 
-       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");
+       fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] filename\n");
+       fprintf(output, "Default max bytes (per packet) is 5000\n");
+       fprintf(output, "Default count is 1000.\n");
+       fprintf(output, "Types:\n");
 
        for (i = 0; i < num_entries; i++) {
-               printf("\t%s\t%s\n", examples[i].abbrev, examples[i].longname);
+               fprintf(output, "\t%-16s%s\n", examples[i].abbrev, examples[i].longname);
        }
 
-       printf("\n");
+       fprintf(output, "\n");
 
-       exit(0);
+       exit(is_error ? 1 : 0);
 }
 
 /* Parse command-line option "type" and return enum type */
@@ -518,7 +687,7 @@ int parse_type(char *string)
        }
 
        /* Complain */
-       printf("Type %s not known.\n", string);
+       fprintf(stderr, "randpkt: Type %s not known.\n", string);
        exit(1);
 }
 
@@ -535,7 +704,9 @@ pkt_example* find_example(int type)
                }
        }
 
-       printf("Internal error. Type %d has no entry in examples table.\n", type);
+       fprintf(stderr,
+           "randpkt: Internal error. Type %d has no entry in examples table.\n",
+           type);
        exit(1);
 }
 
@@ -544,24 +715,64 @@ void
 seed(void)
 {
        unsigned int    randomness;
-
-#if defined(linux)
-       /* Okay, I should use #ifdef HAVE_DEV_RANDOM, but this is a quick hack */
+       time_t          now;
+#ifndef _WIN32
        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);
+       ssize_t         ret;
+
+#define RANDOM_DEV "/dev/urandom"
+
+       /*
+        * Assume it's at least worth trying /dev/urandom on UN*X.
+        * If it doesn't exist, fall back on time().
+        *
+        * XXX - Use CryptGenRandom on Windows?
+        */
+       fd = ws_open(RANDOM_DEV, O_RDONLY);
+       if (fd == -1) {
+               if (errno != ENOENT) {
+                       fprintf(stderr,
+                           "randpkt: Could not open " RANDOM_DEV " for reading: %s\n",
+                           g_strerror(errno));
+                       exit(2);
+               }
+               goto fallback;
        }
 
-       read(fd, &randomness, sizeof(randomness));
-#else
-       time_t now;
+       ret = ws_read(fd, &randomness, sizeof randomness);
+       if (ret == -1) {
+               fprintf(stderr,
+                   "randpkt: Could not read from " RANDOM_DEV ": %s\n",
+                   g_strerror(errno));
+               exit(2);
+       }
+       if ((size_t)ret != sizeof randomness) {
+               fprintf(stderr,
+                   "randpkt: Tried to read %lu bytes from " RANDOM_DEV ", got %ld\n",
+                   (unsigned long)sizeof randomness, (long)ret);
+               exit(2);
+       }
+       srand(randomness);
+       ws_close(fd);
+       return;
 
+fallback:
+#endif
        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:
+ */