Avoid adding -pie on older cmake versions
[metze/wireshark/wip.git] / randpkt.c
index 2d92d95ea688b3700f9a8957e91b6b8b8a018017..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$
- *
  * 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])
 
@@ -484,7 +490,7 @@ pkt_example examples[] = {
 
 
 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);
@@ -495,21 +501,31 @@ 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:ht:")) != -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);
@@ -529,8 +545,10 @@ main(int argc, char **argv)
                                break;
 
                        case 'h':
+                               usage(FALSE);
+                               break;
                        default:
-                               usage();
+                               usage(TRUE);
                                break;
                }
        }
@@ -540,13 +558,13 @@ main(int argc, char **argv)
                produce_filename = argv[optind];
        }
        else {
-               usage();
+               usage(TRUE);
        }
 
        example = find_example(produce_type);
 
 
-       dump = wtap_dump_open(produce_filename, WTAP_FILE_PCAP,
+       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,
@@ -570,14 +588,15 @@ main(int argc, char **argv)
        }
 
        memset(&pkthdr, 0, sizeof(pkthdr));
-       memset(&ps_header, 0, sizeof(ps_header));
        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);
+               memcpy(ps_header, example->pseudo_buffer, example->pseudo_length);
 
        /* Load the sample into our buffer */
        if (example->sample_buffer)
@@ -598,8 +617,8 @@ main(int argc, char **argv)
                pkthdr.len = len_this_pkt;
                pkthdr.ts.secs = i; /* just for variety */
 
-               for (j = example->pseudo_length; j < (int) sizeof(ps_header); j++) {
-                       ((guint8*)&ps_header)[j] = (rand() % 0x100);
+               for (j = example->pseudo_length; j < (int) sizeof(*ps_header); j++) {
+                       ((guint8*)ps_header)[j] = (rand() % 0x100);
                }
 
                for (j = example->sample_length; j < len_this_pkt; j++) {
@@ -612,7 +631,11 @@ main(int argc, char **argv)
                        }
                }
 
-               wtap_dump(dump, &pkthdr, &ps_header, &buffer[0], &err);
+               /* 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);
@@ -622,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 */
@@ -684,42 +715,45 @@ void
 seed(void)
 {
        unsigned int    randomness;
-       time_t now;
+       time_t          now;
 #ifndef _WIN32
        int             fd;
        ssize_t         ret;
 
+#define RANDOM_DEV "/dev/urandom"
+
        /*
-        * Assume it's at least worth trying /dev/random on UN*X.
+        * Assume it's at least worth trying /dev/urandom on UN*X.
         * If it doesn't exist, fall back on time().
         *
-        * XXX - does Windows have a system source of entropy?
+        * XXX - Use CryptGenRandom on Windows?
         */
-       fd = open("/dev/random", O_RDONLY);
+       fd = ws_open(RANDOM_DEV, O_RDONLY);
        if (fd == -1) {
                if (errno != ENOENT) {
                        fprintf(stderr,
-                           "randpkt: Could not open /dev/random for reading: %s\n",
-                           strerror(errno));
+                           "randpkt: Could not open " RANDOM_DEV " for reading: %s\n",
+                           g_strerror(errno));
                        exit(2);
                }
                goto fallback;
        }
 
-       ret = read(fd, &randomness, sizeof randomness);
+       ret = ws_read(fd, &randomness, sizeof randomness);
        if (ret == -1) {
                fprintf(stderr,
-                   "randpkt: Could not read from /dev/random: %s\n",
-                   strerror(errno));
+                   "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 /dev/random, got %ld\n",
+                   "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:
@@ -729,3 +763,16 @@ fallback:
 
        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:
+ */