void randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes) { int err; if (strcmp(produce_filename, "-") == 0) { /* Write to the standard output. */ example->dump = wtap_dump_open_stdout(WTAP_FILE_TYPE_SUBTYPE_PCAP, example->sample_wtap_encap, produce_max_bytes, FALSE /* compressed */, &err); example->filename = "the standard output"; } else { example->dump = wtap_dump_open(produce_filename, WTAP_FILE_TYPE_SUBTYPE_PCAP, example->sample_wtap_encap, produce_max_bytes, FALSE /* compressed */, &err); example->filename = produce_filename; } if (!example->dump) { fprintf(stderr, "randpkt: Error writing to %s\n", example->filename); exit(2); } /* 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 { example->produce_max_bytes = produce_max_bytes - example->sample_length; } }
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; }
/* * Merges the files to an output file whose name is supplied as an argument, * based on given input, and invokes callback during execution. Returns * MERGE_OK on success, or a MERGE_ERR_XXX on failure. */ merge_result merge_files(const gchar* out_filename, const int file_type, const char *const *in_filenames, const guint in_file_count, const gboolean do_append, const idb_merge_mode mode, guint snaplen, const gchar *app_name, merge_progress_callback_t* cb, int *err, gchar **err_info, guint *err_fileno, guint32 *err_framenum) { merge_in_file_t *in_files = NULL; int frame_type = WTAP_ENCAP_PER_PACKET; merge_result status = MERGE_OK; wtap_dumper *pdh; GArray *shb_hdrs = NULL; wtapng_iface_descriptions_t *idb_inf = NULL; g_assert(out_filename != NULL); g_assert(in_file_count > 0); g_assert(in_filenames != NULL); g_assert(err != NULL); g_assert(err_info != NULL); g_assert(err_fileno != NULL); g_assert(err_framenum != NULL); /* if a callback was given, it has to have a callback function ptr */ g_assert((cb != NULL) ? (cb->callback_func != NULL) : TRUE); merge_debug("merge_files: begin"); /* open the input files */ if (!merge_open_in_files(in_file_count, in_filenames, &in_files, cb, err, err_info, err_fileno)) { merge_debug("merge_files: merge_open_in_files() failed with err=%d", *err); *err_framenum = 0; return MERGE_ERR_CANT_OPEN_INFILE; } if (snaplen == 0) { /* Snapshot length not specified - default to the maximum. */ snaplen = WTAP_MAX_PACKET_SIZE_STANDARD; } /* * This doesn't tell us that much. It tells us what to set the outfile's * encap type to, but that's all - for example, it does *not* tells us * whether the input files had the same number of IDBs, for the same exact * interfaces, and only one IDB each, so it doesn't actually tell us * whether we can merge IDBs into one or not. */ frame_type = merge_select_frame_type(in_file_count, in_files); merge_debug("merge_files: got frame_type=%d", frame_type); if (cb) cb->callback_func(MERGE_EVENT_FRAME_TYPE_SELECTED, frame_type, in_files, in_file_count, cb->data); /* prepare the outfile */ if (file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG) { shb_hdrs = create_shb_header(in_files, in_file_count, app_name); merge_debug("merge_files: SHB created"); idb_inf = generate_merged_idb(in_files, in_file_count, mode); merge_debug("merge_files: IDB merge operation complete, got %u IDBs", idb_inf ? idb_inf->interface_data->len : 0); pdh = wtap_dump_open_ng(out_filename, file_type, frame_type, snaplen, FALSE /* compressed */, shb_hdrs, idb_inf, NULL, err); } else { pdh = wtap_dump_open(out_filename, file_type, frame_type, snaplen, FALSE /* compressed */, err); } if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); wtap_block_array_free(shb_hdrs); wtap_free_idb_info(idb_inf); *err_framenum = 0; return MERGE_ERR_CANT_OPEN_OUTFILE; } if (cb) cb->callback_func(MERGE_EVENT_READY_TO_MERGE, 0, in_files, in_file_count, cb->data); status = merge_process_packets(pdh, file_type, in_files, in_file_count, do_append, snaplen, cb, err, err_info, err_fileno, err_framenum); g_free(in_files); wtap_block_array_free(shb_hdrs); wtap_free_idb_info(idb_inf); return status; }
int main(int argc, char *argv[]) { wtap *wth = NULL; wtap_dumper *pdh = NULL; int err; gchar *err_info; gint64 data_offset; const struct wtap_pkthdr *phdr; guint wrong_order_count = 0; gboolean write_output_regardless = TRUE; guint i; GPtrArray *frames; FrameRecord_t *prevFrame = NULL; int opt; int file_count; char *infile; char *outfile; /* Process the options first */ while ((opt = getopt(argc, argv, "n")) != -1) { switch (opt) { case 'n': write_output_regardless = FALSE; break; case '?': usage(); exit(1); } } /* Remaining args are file names */ file_count = argc - optind; if (file_count == 2) { infile = argv[optind]; outfile = argv[optind+1]; } else { usage(); exit(1); } /* Open infile */ wth = wtap_open_offline(infile, &err, &err_info, TRUE); if (wth == NULL) { printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err)); exit(1); } DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth)); /* Open outfile (same filetype/encap as input file) */ pdh = wtap_dump_open(outfile, wtap_file_type(wth), wtap_file_encap(wth), 65535, FALSE, &err); if (pdh == NULL) { printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err)); exit(1); } /* Allocate the array of frame pointers. */ frames = g_ptr_array_new(); /* Read each frame from infile */ while (wtap_read(wth, &err, &err_info, &data_offset)) { FrameRecord_t *newFrameRecord; phdr = wtap_phdr(wth); newFrameRecord = g_slice_new(FrameRecord_t); newFrameRecord->num = frames->len + 1; newFrameRecord->offset = data_offset; newFrameRecord->length = phdr->len; newFrameRecord->time = phdr->ts; if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) { wrong_order_count++; } g_ptr_array_add(frames, newFrameRecord); prevFrame = newFrameRecord; } printf("%u frames, %u out of order\n", frames->len, wrong_order_count); /* Sort the frames */ if (wrong_order_count > 0) { g_ptr_array_sort(frames, frames_compare); } /* Write out each sorted frame in turn */ for (i = 0; i < frames->len; i++) { FrameRecord_t *frame = frames->pdata[i]; /* Avoid writing if already sorted and configured to */ if (write_output_regardless || (wrong_order_count > 0)) { frame_write(frame, wth, pdh); } g_slice_free(FrameRecord_t, frame); } if (!write_output_regardless && (wrong_order_count == 0)) { printf("Not writing output file because input file is already in order!\n"); } /* Free the whole array */ g_ptr_array_free(frames, TRUE); /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { printf("Error closing %s: %s\n", outfile, wtap_strerror(err)); exit(1); } /* Finally, close infile */ wtap_fdclose(wth); 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; }