int main(int argc, char **argv) { wtap_dumper *dump; struct wtap_pkthdr pkthdr; 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; 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; static const struct option long_options[] = { {(char *)"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:", long_options, NULL)) != -1) { switch (opt) { case 'b': /* max bytes */ produce_max_bytes = atoi(optarg); if (produce_max_bytes > 65536) { fprintf(stderr, "randpkt: Max bytes is 65536\n"); exit(1); } break; case 'c': /* count */ produce_count = atoi(optarg); break; case 't': /* type of packet to produce */ produce_type = parse_type(optarg); break; case 'h': usage(FALSE); break; default: usage(TRUE); break; } } /* any more command line parameters? */ if (argc > optind) { produce_filename = argv[optind]; } else { usage(TRUE); } example = find_example(produce_type); 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) { 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); /* 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; } len_this_pkt = example->sample_length + len_random; pkthdr.caplen = len_this_pkt; 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->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); return 0; }
int main(int argc, char **argv) { wtap_dumper *dump; struct wtap_pkthdr pkthdr; union wtap_pseudo_header ps_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; while ((opt = getopt(argc, argv, "b:c:ht:")) != -1) { switch (opt) { case 'b': /* max bytes */ produce_max_bytes = atoi(optarg); if (produce_max_bytes > 65536) { fprintf(stderr, "randpkt: Max bytes is 65536\n"); exit(1); } break; case 'c': /* count */ produce_count = atoi(optarg); break; case 't': /* type of packet to produce */ produce_type = parse_type(optarg); break; case 'h': default: usage(); break; } } /* any more command line parameters? */ if (argc > optind) { produce_filename = argv[optind]; } else { usage(); } example = find_example(produce_type); dump = wtap_dump_open(produce_filename, WTAP_FILE_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) { 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(&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); /* Load the sample into our buffer */ if (example->sample_buffer) memcpy(&buffer[0], example->sample_buffer, example->sample_length); /* 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; } len_this_pkt = example->sample_length + len_random; pkthdr.caplen = len_this_pkt; 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->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); } } wtap_dump(dump, &pkthdr, &ps_header, &buffer[0], &err); } wtap_dump_close(dump, &err); return 0; }