Update E.212 list to Operational Bulletin No. 1038 (15.X.2013)
[metze/wireshark/wip.git] / randpkt.c
index 941b267643d72935f24468d5dc8b8abe117cc12c..2316e71af6415143817dd6ae220175813b4ad871 100644 (file)
--- a/randpkt.c
+++ b/randpkt.c
  *
  * 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_GETOPT_H
-#include <getopt.h>
-#else
-#include "wsutil/wsgetopt.h"
-#endif
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
+#ifndef HAVE_GETOPT
+#include "wsutil/wsgetopt.h"
+#endif
+
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 #include <string.h>
 #include <glib.h>
 #include "wiretap/wtap.h"
+#include "wsutil/file_util.h"
+
+#ifdef _WIN32
+#include <wsutil/unicode-utils.h>
+#endif /* _WIN32 */
 
 #define array_length(x)        (sizeof x / sizeof x[0])
 
@@ -497,17 +498,23 @@ 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;
        guint8                  buffer[65536];
 
        int                     opt;
+
        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;
 
+#ifdef _WIN32
+       arg_list_utf_16to8(argc, argv);
+       create_app_running_mutex();
+#endif /* _WIN32 */
+
        while ((opt = getopt(argc, argv, "b:c:ht:")) != -1) {
                switch (opt) {
                        case 'b':       /* max bytes */
@@ -569,14 +576,13 @@ main(int argc, char **argv)
        }
 
        memset(&pkthdr, 0, sizeof(pkthdr));
-       memset(&ps_header, 0, sizeof(ps_header));
        memset(buffer, 0, sizeof(buffer));
 
        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)
@@ -597,8 +603,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++) {
@@ -611,7 +617,7 @@ main(int argc, char **argv)
                        }
                }
 
-               wtap_dump(dump, &pkthdr, &ps_header, &buffer[0], &err);
+               wtap_dump(dump, &pkthdr, &buffer[0], &err);
        }
 
        wtap_dump_close(dump, &err);
@@ -633,7 +639,7 @@ void usage(void)
        printf("Types:\n");
 
        for (i = 0; i < num_entries; i++) {
-               printf("\t%s\t%s\n", examples[i].abbrev, examples[i].longname);
+               printf("\t%-16s%s\n", examples[i].abbrev, examples[i].longname);
        }
 
        printf("\n");
@@ -696,22 +702,22 @@ seed(void)
         *
         * XXX - Use CryptGenRandom on Windows?
         */
-       fd = open(RANDOM_DEV, O_RDONLY);
+       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",
-                           strerror(errno));
+                           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 " RANDOM_DEV ": %s\n",
-                   strerror(errno));
+                   g_strerror(errno));
                exit(2);
        }
        if ((size_t)ret != sizeof randomness) {
@@ -721,6 +727,7 @@ seed(void)
                exit(2);
        }
        srand(randomness);
+       ws_close(fd);
        return;
 
 fallback: