static void list_capture_types(void) { int i; struct string_elem *captypes; GSList *list = NULL; captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES); fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n"); for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) { if (wtap_dump_can_open(i)) { captypes[i].sstr = wtap_file_type_subtype_short_string(i); captypes[i].lstr = wtap_file_type_subtype_string(i); list = g_slist_insert_sorted(list, &captypes[i], string_compare); } } g_slist_foreach(list, string_elem_print, NULL); g_slist_free(list); g_free(captypes); }
void randpkt_loop(randpkt_example* example, guint64 produce_count) { guint i; int j; int err; int len_random; int len_this_pkt; gchar* err_info; union wtap_pseudo_header* ps_header; guint8 buffer[65536]; struct wtap_pkthdr* pkthdr; pkthdr = g_new0(struct wtap_pkthdr, 1); pkthdr->rec_type = REC_TYPE_PACKET; pkthdr->presence_flags = WTAP_HAS_TS; pkthdr->pkt_encap = example->sample_wtap_encap; memset(pkthdr, 0, sizeof(struct wtap_pkthdr)); memset(buffer, 0, sizeof(buffer)); ps_header = &pkthdr->pseudo_header; /* 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, example->sample_buffer, example->sample_length); /* Produce random packets */ for (i = 0; i < produce_count; i++) { if (example->produce_max_bytes > 0) { len_random = (rand() % example->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); } } if (!wtap_dump(example->dump, pkthdr, buffer, &err, &err_info)) { fprintf(stderr, "randpkt: Error writing to %s: %s\n", example->filename, wtap_strerror(err)); switch (err) { case WTAP_ERR_UNWRITABLE_ENCAP: /* * This is a problem with the particular * frame we're writing and the file type * and subtype we're writing; note that, * and report the file type/subtype. */ fprintf(stderr, "Frame has a network type that can't be saved in a \"%s\" file.\n", wtap_file_type_subtype_short_string(WTAP_FILE_TYPE_SUBTYPE_PCAP)); break; case WTAP_ERR_PACKET_TOO_LARGE: /* * This is a problem with the particular * frame we're writing and the file type * and subtype we're writing; note that, * and report the file type/subtype. */ fprintf(stderr, "Frame is too large for a \"%s\" file.\n", wtap_file_type_subtype_short_string(WTAP_FILE_TYPE_SUBTYPE_PCAP)); break; case WTAP_ERR_UNWRITABLE_REC_TYPE: /* * This is a problem with the particular * record we're writing and the file type * and subtype we're writing; note that, * and report the file type/subtype. */ fprintf(stderr, "Record has a record type that can't be saved in a \"%s\" file.\n", wtap_file_type_subtype_short_string(WTAP_FILE_TYPE_SUBTYPE_PCAP)); break; case WTAP_ERR_UNWRITABLE_REC_DATA: /* * This is a problem with the particular * record we're writing and the file type * and subtype we're writing; note that, * and report the file type/subtype. */ fprintf(stderr, "Record has data that can't be saved in a \"%s\" file.\n(%s)\n", wtap_file_type_subtype_short_string(WTAP_FILE_TYPE_SUBTYPE_PCAP), err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; default: break; } } } g_free(pkthdr); }
int main(int argc, char *argv[]) { GString *comp_info_str; GString *runtime_info_str; wtap *wth; int err; gchar *err_info; int i; int opt; int overall_error_status; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; #ifdef HAVE_PLUGINS char *init_progfile_dir_error; #endif /* Set the C-language locale to the native environment. */ setlocale(LC_ALL, ""); /* Get the compile-time version information string */ comp_info_str = get_compiled_version_info(NULL, NULL); /* Get the run-time version information string */ runtime_info_str = get_runtime_version_info(NULL); /* Add it to the information to be reported on a crash. */ ws_add_crash_info("Captype (Wireshark) %s\n" "\n" "%s" "\n" "%s", get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); #ifdef _WIN32 arg_list_utf_16to8(argc, argv); create_app_running_mutex(); #endif /* _WIN32 */ /* * Get credential information for later use. */ init_process_policies(); init_open_routines(); #ifdef HAVE_PLUGINS if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) { g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error); g_free(init_progfile_dir_error); } else { /* Register all the plugin types we have. */ wtap_register_plugin_types(); /* Types known to libwiretap */ init_report_err(failure_message,NULL,NULL,NULL); /* Scan for plugins. This does *not* call their registration routines; that's done later. Don't report failures to load plugins because most (non-wiretap) plugins *should* fail to load (because we're not linked against libwireshark and dissector plugins need libwireshark). */ scan_plugins(DONT_REPORT_LOAD_FAILURE); /* Register all libwiretap plugin modules. */ register_all_wiretap_modules(); } #endif /* Process the options */ while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) { switch (opt) { case 'h': printf("Captype (Wireshark) %s\n" "Print the file types of capture files.\n" "See https://www.wireshark.org for more information.\n", get_ws_vcs_version_info()); print_usage(stdout); exit(0); break; case 'v': comp_info_str = get_compiled_version_info(NULL, NULL); runtime_info_str = get_runtime_version_info(NULL); show_version("Captype (Wireshark)", comp_info_str, runtime_info_str); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); exit(0); break; case '?': /* Bad flag - print usage message */ print_usage(stderr); exit(1); break; } } if (argc < 2) { print_usage(stderr); return 1; } overall_error_status = 0; for (i = 1; i < argc; i++) { wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE); if(wth) { printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth))); wtap_close(wth); } else { if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT) printf("%s: unknown\n", argv[i]); else { fprintf(stderr, "captype: Can't open %s: %s\n", argv[i], wtap_strerror(err)); if (err_info != NULL) { fprintf(stderr, "(%s)\n", err_info); g_free(err_info); } overall_error_status = 1; /* remember that an error has occurred */ } } } return overall_error_status; }