void SCTPChunkStatisticsDialog::on_pushButton_clicked() { FILE* fp; pref_t *pref = prefs_find_preference(prefs_find_module("sctp"),"statistics_chunk_types"); if (!pref) { g_log(NULL, G_LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); return; } uat_t *uat = prefs_get_uat_value(pref); gchar* fname = uat_get_actual_filename(uat,TRUE); if (!fname) { return; } fp = ws_fopen(fname,"w"); if (!fp && errno == ENOENT) { gchar *pf_dir_path = NULL; if (create_persconffile_dir(&pf_dir_path) != 0) { g_free (pf_dir_path); return; } fp = ws_fopen(fname,"w"); } if (!fp) { return; } g_free (fname); fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n"); char str[40]; struct chunkTypes tempChunk; for (int i = 0; i < chunks.size(); i++) { tempChunk = chunks.value(i); g_snprintf(str, sizeof str, "\"%d\",\"%s\",\"%s\"\n", tempChunk.id, tempChunk.name, tempChunk.hide==0?"Show":"Hide"); fputs(str, fp); void *rec = g_malloc0(uat->record_size); uat_add_record(uat, rec, TRUE); if (uat->free_cb) { uat->free_cb(rec); } g_free(rec); } fclose(fp); }
gboolean uat_save(uat_t* uat, char** error) { guint i; gchar* fname = uat_get_actual_filename(uat,TRUE); FILE* fp; if (! fname ) return FALSE; fp = ws_fopen(fname,"w"); if (!fp && errno == ENOENT) { /* Parent directory does not exist, try creating first */ gchar *pf_dir_path = NULL; if (create_persconffile_dir(&pf_dir_path) != 0) { *error = ep_strdup_printf("uat_save: error creating '%s'", pf_dir_path); g_free (pf_dir_path); return FALSE; } fp = ws_fopen(fname,"w"); } if (!fp) { *error = ep_strdup_printf("uat_save: error opening '%s': %s",fname,g_strerror(errno)); return FALSE; } *error = NULL; g_free (fname); fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n"); for ( i = 0 ; i < uat->user_data->len ; i++ ) { void* rec = uat->user_data->data + (uat->record_size * i); uat_field_t* f; guint j; f = uat->fields; for( j=0 ; j < uat->ncols ; j++ ) { putfld(fp, rec, &(f[j])); fputs((j == uat->ncols - 1) ? "\n" : "," ,fp); } } fclose(fp); uat->changed = FALSE; return TRUE; }
/* read filters from the filter file */ gboolean color_filters_read_globals(gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb) { gchar *path; FILE *f; int ret; /* decide what file to open (from dfilter code) */ path = get_datafile_path("colorfilters"); if ((f = ws_fopen(path, "r")) == NULL) { if (errno != ENOENT) { *err_msg = g_strdup_printf("Could not open global filter file\n\"%s\": %s.", path, g_strerror(errno)); } g_free(path); return FALSE; } ret = read_filters_file(path, f, user_data, add_cb); if (ret != 0) { *err_msg = g_strdup_printf("Error reading global filter file\n\"%s\": %s.", path, g_strerror(errno)); fclose(f); g_free(path); return FALSE; } fclose(f); g_free(path); return TRUE; }
/* check the version of the wireshark program */ static update_info_t * update_check_wireshark(const char *local_file) { FILE *pf; update_info_t *update_info = update_info_new(); update_info->version_installed = g_strdup(VERSION); update_info->prefix = "wireshark.setup."; pf = ws_fopen(local_file, "r"); if(pf != NULL) { /* read in update_info of Wireshark */ read_prefs_file(local_file, pf, update_pref, update_info); fclose(pf); /* check if Wireshark needs an update */ if(update_info->version_installed && update_info->version_recommended && strcmp(update_info->version_installed, update_info->version_recommended) != 0) { update_info->needs_update = TRUE; } } else { g_warning("Could not open %s", local_file); } return update_info; }
/* read filters from the user's filter file */ static gboolean read_users_filters(GSList **cfl) { gchar *path; FILE *f; gboolean ret; /* decide what file to open (from dfilter code) */ path = get_persconffile_path("colorfilters", TRUE); if ((f = ws_fopen(path, "r")) == NULL) { if (errno != ENOENT) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open filter file\n\"%s\": %s.", path, g_strerror(errno)); } g_free(path); return FALSE; } g_free(path); path = NULL; ret = read_filters_file(f, cfl); fclose(f); return ret; }
/* read filters from the filter file */ gboolean color_filters_read_globals(gpointer user_data) { gchar *path; FILE *f; gboolean ret; /* decide what file to open (from dfilter code) */ path = get_datafile_path("colorfilters"); if ((f = ws_fopen(path, "r")) == NULL) { if (errno != ENOENT) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open global filter file\n\"%s\": %s.", path, g_strerror(errno)); } g_free(path); return FALSE; } g_free(path); path = NULL; ret = read_filters_file(f, user_data); fclose(f); return ret; }
/* save filters in users filter file */ gboolean color_filters_write(GSList *cfl) { gchar *pf_dir_path; gchar *path; FILE *f; /* Create the directory that holds personal configuration files, if necessary. */ if (create_persconffile_dir(&pf_dir_path) == -1) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't create directory\n\"%s\"\nfor color files: %s.", pf_dir_path, g_strerror(errno)); g_free(pf_dir_path); return FALSE; } path = get_persconffile_path("colorfilters", TRUE); if ((f = ws_fopen(path, "w+")) == NULL) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open\n%s\nfor writing: %s.", path, g_strerror(errno)); g_free(path); return FALSE; } g_free(path); write_filters_file(cfl, f, FALSE); fclose(f); return TRUE; }
/* read filters from the user's filter file */ static gboolean read_users_filters(GSList **cfl, gchar** err_msg, color_filter_add_cb_func add_cb) { gchar *path; FILE *f; int ret; /* decide what file to open (from dfilter code) */ path = get_persconffile_path("colorfilters", TRUE); if ((f = ws_fopen(path, "r")) == NULL) { if (errno != ENOENT) { *err_msg = g_strdup_printf("Could not open filter file\n\"%s\": %s.", path, g_strerror(errno)); } g_free(path); return FALSE; } ret = read_filters_file(path, f, cfl, add_cb); if (ret != 0) { *err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.", path, g_strerror(errno)); fclose(f); g_free(path); return FALSE; } fclose(f); g_free(path); return TRUE; }
static FILE_T wtap_dump_file_open(wtap_dumper *wdh, const char *filename) { if(wdh->compressed) { return gzopen(filename, "wb"); } else { return ws_fopen(filename, "wb"); } }
static FILE * open_print_dest(int to_file, const char *dest) { FILE *fh; /* Open the file or command for output */ if (to_file) fh = ws_fopen(dest, "w"); else fh = popen(dest, "w"); return fh; }
/* check the version of winpcap */ static update_info_t * update_check_winpcap(const char *local_file) { FILE *pf; update_info_t * update_info = update_info_new(); GString *pcap_version_tmp; char *pcap_version = NULL; char *pcap_vstart; char *pcap_vend; update_info->prefix = "winpcap."; pf = ws_fopen(local_file, "r"); if(pf != NULL) { /* read in update_info of WinPcap */ read_prefs_file(local_file, pf, update_pref, update_info); fclose(pf); /* get WinPcap version */ /* XXX - what's the "approved" method to get the WinPcap version? */ pcap_version_tmp = g_string_new(""); get_runtime_pcap_version(pcap_version_tmp); /* cut out real version from "combined" version string */ pcap_vstart = strstr(pcap_version_tmp->str, "with WinPcap version "); if(pcap_vstart != NULL) { pcap_vstart += sizeof("with WinPcap version"); pcap_vend = strstr(pcap_vstart, " "); if(pcap_vend != NULL) { pcap_vend[0] = 0; pcap_version = g_strdup(pcap_vstart); } } update_info->version_installed = g_strdup(pcap_version); if(pcap_version && update_info->version_recommended && strcmp(pcap_version, update_info->version_recommended) != 0) { update_info->needs_update = TRUE; } } else { g_warning("Could not open %s", local_file); } g_string_free(pcap_version_tmp, TRUE); g_free(pcap_version); return update_info; }
/* save filters in some other filter file (export) */ gboolean color_filters_export(const gchar *path, GSList *cfl, gboolean only_marked, gchar** err_msg) { FILE *f; if ((f = ws_fopen(path, "w+")) == NULL) { *err_msg = g_strdup_printf("Could not open\n%s\nfor writing: %s.", path, g_strerror(errno)); return FALSE; } write_filters_file(cfl, f, only_marked); fclose(f); return TRUE; }
void dfilter_macro_save(const gchar* filename, gchar** error) { FILE* f = ws_fopen(filename,"w"); if (!f) { *error = ep_strdup_printf("Could not open file: '%s', error: %s\n", filename, g_strerror(errno) ); return; } dfilter_macro_foreach(macro_fprint, f); fclose(f); return; }
/* save filters in some other filter file (export) */ gboolean color_filters_export(const gchar *path, const GSList *cfl, gboolean only_marked) { FILE *f; if ((f = ws_fopen(path, "w+")) == NULL) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open\n%s\nfor writing: %s.", path, g_strerror(errno)); return FALSE; } write_filters_file(cfl, f, only_marked); fclose(f); return TRUE; }
/* save rtp dump of stream_fwd */ gboolean rtpstream_save(rtpstream_tapinfo_t *tapinfo, capture_file *cap_file, rtp_stream_info_t* stream, const gchar *filename) { gboolean was_registered; if (!tapinfo) { return FALSE; } was_registered = tapinfo->is_registered; /* open file for saving */ tapinfo->save_file = ws_fopen(filename, "wb"); if (tapinfo->save_file==NULL) { open_failure_alert_box(filename, errno, TRUE); return FALSE; } rtp_write_header(stream, tapinfo->save_file); if (ferror(tapinfo->save_file)) { write_failure_alert_box(filename, errno); fclose(tapinfo->save_file); return FALSE; } if (!tapinfo->is_registered) register_tap_listener_rtp_stream(tapinfo); tapinfo->mode = TAP_SAVE; tapinfo->filter_stream_fwd = stream; cf_retap_packets(cap_file); tapinfo->mode = TAP_ANALYSE; if (!was_registered) remove_tap_listener_rtp_stream(tapinfo); if (ferror(tapinfo->save_file)) { write_failure_alert_box(filename, errno); fclose(tapinfo->save_file); return FALSE; } if (fclose(tapinfo->save_file) == EOF) { write_failure_alert_box(filename, errno); return FALSE; } return TRUE; }
/* read filters from some other filter file (import) */ gboolean color_filters_import(const gchar *path, const gpointer user_data) { FILE *f; gboolean ret; if ((f = ws_fopen(path, "r")) == NULL) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open\n%s\nfor reading: %s.", path, g_strerror(errno)); return FALSE; } ret = read_filters_file(f, user_data); fclose(f); return ret; }
/* * Put the complete text file into a text page. */ static void text_page_set_text(GtkWidget *page, const char *absolute_path) { FILE *text_file; char line[4096+1]; /* XXX - size? */ text_file = ws_fopen(absolute_path, "r"); if (text_file != NULL) { while (fgets(line, sizeof line, text_file) != NULL) { text_page_insert(page, line, (int) strlen(line)); } if(ferror(text_file)) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s", absolute_path, g_strerror(errno)); } fclose(text_file); } else { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s", absolute_path, g_strerror(errno)); } }
/* * add all etch symbols from file to our symbol cache */ static void add_symbols_of_file(const char *filename) { FILE *pFile; pFile = ws_fopen(filename, "r"); if (pFile != NULL) { char line[256]; while (fgets(line, sizeof line, pFile) != NULL) { unsigned int hash; size_t length, pos; length = strlen(line); /* Must at least have a hash, else skip line */ if (length < 10) continue; pos = length - 1; while (pos > 0 && (line[pos] == 0xD || line[pos] == 0xA)) { pos--; } line[pos + 1] = '\0'; /* Parse the Hash */ if (sscanf(&line[0], "%x", &hash) != 1) continue; /* didn't find a valid hex value at the beginning of the line */ /* And read the symbol */ pos = strcspn(line, ","); if ((line[pos] != '\0') && (line[pos+1] !='\0')) /* require at least 1 char in symbol */ gbl_symbols_array_append(hash, g_strdup_printf("%." ETCH_MAX_SYMBOL_LENGTH "s", &line[pos+1])); } fclose(pFile); } }
/* read filters from some other filter file (import) */ gboolean color_filters_import(const gchar *path, gpointer user_data, gchar **err_msg, color_filter_add_cb_func add_cb) { FILE *f; int ret; if ((f = ws_fopen(path, "r")) == NULL) { *err_msg = g_strdup_printf("Could not open filter file\n%s\nfor reading: %s.", path, g_strerror(errno)); return FALSE; } ret = read_filters_file(path, f, user_data, add_cb); if (ret != 0) { *err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.", path, g_strerror(errno)); fclose(f); return FALSE; } fclose(f); return TRUE; }
/* Attempt to Write out profile "recent" to the user's profile recent file. If we got an error report it with a dialog box and return FALSE, otherwise return TRUE. */ gboolean write_profile_recent(void) { char *pf_dir_path; char *rf_path; FILE *rf; /* To do: * - Split output lines longer than MAX_VAL_LEN * - Create a function for the preference directory check/creation * so that duplication can be avoided with filter.c */ /* Create the directory that holds personal configuration files, if necessary. */ if (create_persconffile_dir(&pf_dir_path) == -1) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't create directory\n\"%s\"\nfor recent file: %s.", pf_dir_path, g_strerror(errno)); g_free(pf_dir_path); return FALSE; } rf_path = get_persconffile_path(RECENT_FILE_NAME, TRUE); if ((rf = ws_fopen(rf_path, "w")) == NULL) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't open recent file\n\"%s\": %s.", rf_path, g_strerror(errno)); g_free(rf_path); return FALSE; } g_free(rf_path); fputs("# Recent settings file for Wireshark " VERSION ".\n" "#\n" "# This file is regenerated each time Wireshark is quit\n" "# and when changing configuration profile.\n" "# So be careful, if you want to make manual changes here.\n" "\n", rf); fprintf(rf, "\n# Main Toolbar show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_MAIN_TOOLBAR_SHOW ": %s\n", recent.main_toolbar_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Filter Toolbar show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_FILTER_TOOLBAR_SHOW ": %s\n", recent.filter_toolbar_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Wireless Settings Toolbar show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_WIRELESS_TOOLBAR_SHOW ": %s\n", recent.wireless_toolbar_show == TRUE ? "TRUE" : "FALSE"); #ifdef HAVE_AIRPCAP fprintf(rf, "\n# Show (hide) old AirPcap driver warning dialog box.\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_DRIVER_CHECK_SHOW ": %s\n", recent.airpcap_driver_check_show == TRUE ? "TRUE" : "FALSE"); #endif fprintf(rf, "\n# Packet list show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_PACKET_LIST_SHOW ": %s\n", recent.packet_list_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Tree view show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_TREE_VIEW_SHOW ": %s\n", recent.tree_view_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Byte view show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_BYTE_VIEW_SHOW ": %s\n", recent.byte_view_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Statusbar show (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_STATUSBAR_SHOW ": %s\n", recent.statusbar_show == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Packet list colorize (hide).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_PACKET_LIST_COLORIZE ": %s\n", recent.packet_list_colorize == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Timestamp display format.\n"); fprintf(rf, "# One of: RELATIVE, ABSOLUTE, ABSOLUTE_WITH_DATE, DELTA, DELTA_DIS, EPOCH, UTC, UTC_WITH_DATE\n"); fprintf(rf, RECENT_GUI_TIME_FORMAT ": %s\n", ts_type_text[recent.gui_time_format]); fprintf(rf, "\n# Timestamp display precision.\n"); fprintf(rf, "# One of: AUTO, SEC, DSEC, CSEC, MSEC, USEC, NSEC\n"); fprintf(rf, RECENT_GUI_TIME_PRECISION ": %s\n", ts_precision_text[recent.gui_time_precision]); fprintf(rf, "\n# Seconds display format.\n"); fprintf(rf, "# One of: SECONDS, HOUR_MIN_SEC\n"); fprintf(rf, RECENT_GUI_SECONDS_FORMAT ": %s\n", ts_seconds_text[recent.gui_seconds_format]); fprintf(rf, "\n# Zoom level.\n"); fprintf(rf, "# A decimal number.\n"); fprintf(rf, RECENT_GUI_ZOOM_LEVEL ": %d\n", recent.gui_zoom_level); fprintf(rf, "\n# Bytes view.\n"); fprintf(rf, "# A decimal number.\n"); fprintf(rf, RECENT_GUI_BYTES_VIEW ": %d\n", recent.gui_bytes_view); fprintf(rf, "\n# Main window upper (or leftmost) pane size.\n"); fprintf(rf, "# Decimal number.\n"); if (recent.gui_geometry_main_upper_pane != 0) { fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_UPPER_PANE ": %d\n", recent.gui_geometry_main_upper_pane); } fprintf(rf, "\n# Main window middle pane size.\n"); fprintf(rf, "# Decimal number.\n"); if (recent.gui_geometry_main_lower_pane != 0) { fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_LOWER_PANE ": %d\n", recent.gui_geometry_main_lower_pane); } fprintf(rf, "\n# Packet list column pixel widths.\n"); fprintf(rf, "# Each pair of strings consists of a column format and its pixel width.\n"); packet_list_recent_write_all(rf); if (get_last_open_dir() != NULL) { fprintf(rf, "\n# Last directory navigated to in File Open dialog.\n"); if(u3_active()) fprintf(rf, RECENT_GUI_FILEOPEN_REMEMBERED_DIR ": %s\n", u3_contract_device_path(get_last_open_dir())); else fprintf(rf, RECENT_GUI_FILEOPEN_REMEMBERED_DIR ": %s\n", get_last_open_dir()); } fclose(rf); /* XXX - catch I/O errors (e.g. "ran out of disk space") and return an error indication, or maybe write to a new recent file and rename that file on top of the old one only if there are not I/O errors. */ return TRUE; }
gboolean uat_save(uat_t* uat, char** error) { guint i; gchar* fname = uat_get_actual_filename(uat,TRUE); FILE* fp; if (! fname ) return FALSE; fp = ws_fopen(fname,"w"); if (!fp && errno == ENOENT) { /* Parent directory does not exist, try creating first */ gchar *pf_dir_path = NULL; if (create_persconffile_dir(&pf_dir_path) != 0) { *error = g_strdup_printf("uat_save: error creating '%s'", pf_dir_path); g_free (pf_dir_path); return FALSE; } fp = ws_fopen(fname,"w"); } if (!fp) { *error = g_strdup_printf("uat_save: error opening '%s': %s",fname,g_strerror(errno)); return FALSE; } *error = NULL; g_free (fname); /* Ensure raw_data is synced with user_data and all "good" entries have been accounted for */ /* Start by clearing current user_data */ for ( i = 0 ; i < uat->user_data->len ; i++ ) { if (uat->free_cb) { uat->free_cb(UAT_USER_INDEX_PTR(uat,i)); } } g_array_set_size(uat->user_data,0); *((uat)->user_ptr) = NULL; *((uat)->nrows_p) = 0; /* Now copy "good" raw_data entries to user_data */ for ( i = 0 ; i < uat->raw_data->len ; i++ ) { void *rec = UAT_INDEX_PTR(uat, i); gboolean* valid = (gboolean*)(uat->valid_data->data + sizeof(gboolean)*i); if (*valid) { g_array_append_vals(uat->user_data, rec, 1); if (uat->copy_cb) { uat->copy_cb(UAT_USER_INDEX_PTR(uat, uat->user_data->len - 1), rec, (unsigned int) uat->record_size); } UAT_UPDATE(uat); } } fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n"); for ( i = 0 ; i < uat->user_data->len ; i++ ) { void* rec = uat->user_data->data + (uat->record_size * i); uat_field_t* f; guint j; f = uat->fields; for( j=0 ; j < uat->ncols ; j++ ) { putfld(fp, rec, &(f[j])); fputs((j == uat->ncols - 1) ? "\n" : "," ,fp); } } fclose(fp); uat->changed = FALSE; return TRUE; }
/* * Write out a list of filters. * * On success, "*pref_path_return" is set to NULL. * On error, "*pref_path_return" is set to point to the pathname of * the file we tried to read - it should be freed by our caller - * and "*errno_return" is set to the error. */ void save_filter_list(filter_list_type_t list_type, char **pref_path_return, int *errno_return) { const gchar *ff_name; gchar *ff_path, *ff_path_new; GList *fl; GList *flpp; filter_def *filt; FILE *ff; guchar *p, c; *pref_path_return = NULL; /* assume no error */ switch (list_type) { case CFILTER_LIST: ff_name = CFILTER_FILE_NAME; fl = capture_filters; break; case DFILTER_LIST: ff_name = DFILTER_FILE_NAME; fl = display_filters; break; default: g_assert_not_reached(); return; } ff_path = get_persconffile_path(ff_name, TRUE); /* Write to "XXX.new", and rename if that succeeds. That means we don't trash the file if we fail to write it out completely. */ ff_path_new = g_strdup_printf("%s.new", ff_path); if ((ff = ws_fopen(ff_path_new, "w")) == NULL) { *pref_path_return = ff_path; *errno_return = errno; g_free(ff_path_new); return; } flpp = g_list_first(fl); while (flpp) { filt = (filter_def *) flpp->data; /* Write out the filter name as a quoted string; escape any quotes or backslashes. */ putc('"', ff); for (p = (guchar *)filt->name; (c = *p) != '\0'; p++) { if (c == '"' || c == '\\') putc('\\', ff); putc(c, ff); } putc('"', ff); /* Separate the filter name and value with a space. */ putc(' ', ff); /* Write out the filter expression and a newline. */ fprintf(ff, "%s\n", filt->strval); if (ferror(ff)) { *pref_path_return = ff_path; *errno_return = errno; fclose(ff); ws_unlink(ff_path_new); g_free(ff_path_new); return; } flpp = flpp->next; } if (fclose(ff) == EOF) { *pref_path_return = ff_path; *errno_return = errno; ws_unlink(ff_path_new); g_free(ff_path_new); return; } #ifdef _WIN32 /* ANSI C doesn't say whether "rename()" removes the target if it exists; the Win32 call to rename files doesn't do so, which I infer is the reason why the MSVC++ "rename()" doesn't do so. We must therefore remove the target file first, on Windows. XXX - ws_rename() should be ws_stdio_rename() on Windows, and ws_stdio_rename() uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING, so it should remove the target if it exists, so this stuff shouldn't be necessary. Perhaps it dates back to when we were calling rename(), with that being a wrapper around Microsoft's _rename(), which didn't remove the target. */ if (ws_remove(ff_path) < 0 && errno != ENOENT) { /* It failed for some reason other than "it's not there"; if it's not there, we don't need to remove it, so we just drive on. */ *pref_path_return = ff_path; *errno_return = errno; ws_unlink(ff_path_new); g_free(ff_path_new); return; } #endif if (ws_rename(ff_path_new, ff_path) < 0) { *pref_path_return = ff_path; *errno_return = errno; ws_unlink(ff_path_new); g_free(ff_path_new); return; } g_free(ff_path_new); g_free(ff_path); }
void read_filter_list(filter_list_type_t list_type, char **pref_path_return, int *errno_return) { const char *ff_name; char *ff_path; FILE *ff; GList **flpp; int c; char *filt_name, *filt_expr; int filt_name_len, filt_expr_len; int filt_name_index, filt_expr_index; int line = 1; *pref_path_return = NULL; /* assume no error */ switch (list_type) { case CFILTER_LIST: ff_name = CFILTER_FILE_NAME; flpp = &capture_filters; break; case DFILTER_LIST: ff_name = DFILTER_FILE_NAME; flpp = &display_filters; break; default: g_assert_not_reached(); return; } /* try to open personal "cfilters"/"dfilters" file */ ff_path = get_persconffile_path(ff_name, TRUE); if ((ff = ws_fopen(ff_path, "r")) == NULL) { /* * Did that fail because the file didn't exist? */ if (errno != ENOENT) { /* * No. Just give up. */ *pref_path_return = ff_path; *errno_return = errno; return; } /* * Yes. See if there's an "old style" personal "filters" file; if so, read it. * This means that a user will start out with their capture and * display filter lists being identical; each list may contain * filters that don't belong in that list. The user can edit * the filter lists, and delete the ones that don't belong in * a particular list. */ g_free(ff_path); ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE); if ((ff = ws_fopen(ff_path, "r")) == NULL) { /* * Did that fail because the file didn't exist? */ if (errno != ENOENT) { /* * No. Just give up. */ *pref_path_return = ff_path; *errno_return = errno; return; } /* * Try to open the global "cfilters/dfilters" file */ g_free(ff_path); ff_path = get_datafile_path(ff_name); if ((ff = ws_fopen(ff_path, "r")) == NULL) { /* * Well, that didn't work, either. Just give up. * Return an error if the file existed but we couldn't open it. */ if (errno != ENOENT) { *pref_path_return = ff_path; *errno_return = errno; } else { g_free(ff_path); } return; } } } /* If we already have a list of filters, discard it. */ /* this should never happen - this function is called only once for each list! */ while(*flpp) { *flpp = remove_filter_entry(*flpp, g_list_first(*flpp)); } /* Allocate the filter name buffer. */ filt_name_len = INIT_BUF_SIZE; filt_name = (char *)g_malloc(filt_name_len + 1); filt_expr_len = INIT_BUF_SIZE; filt_expr = (char *)g_malloc(filt_expr_len + 1); for (line = 1; ; line++) { /* Lines in a filter file are of the form "name" expression where "name" is a name, in quotes - backslashes in the name escape the next character, so quotes and backslashes can appear in the name - and "expression" is a filter expression, not in quotes, running to the end of the line. */ /* Skip over leading white space, if any. */ c = skip_whitespace(ff); if (c == EOF) break; /* Nothing more to read */ if (c == '\n') continue; /* Blank line. */ /* "c" is the first non-white-space character. If it's not a quote, it's an error. */ if (c != '"') { g_warning("'%s' line %d doesn't have a quoted filter name.", ff_path, line); while (c != '\n') c = getc(ff); /* skip to the end of the line */ continue; } /* Get the name of the filter. */ filt_name_index = 0; for (;;) { c = getc_crlf(ff); if (c == EOF || c == '\n') break; /* End of line - or end of file */ if (c == '"') { /* Closing quote. */ if (filt_name_index >= filt_name_len) { /* Filter name buffer isn't long enough; double its length. */ filt_name_len *= 2; filt_name = (char *)g_realloc(filt_name, filt_name_len + 1); } filt_name[filt_name_index] = '\0'; break; } if (c == '\\') { /* Next character is escaped */ c = getc_crlf(ff); if (c == EOF || c == '\n') break; /* End of line - or end of file */ } /* Add this character to the filter name string. */ if (filt_name_index >= filt_name_len) { /* Filter name buffer isn't long enough; double its length. */ filt_name_len *= 2; filt_name = (char *)g_realloc(filt_name, filt_name_len + 1); } filt_name[filt_name_index] = c; filt_name_index++; } if (c == EOF) { if (!ferror(ff)) { /* EOF, not error; no newline seen before EOF */ g_warning("'%s' line %d doesn't have a newline.", ff_path, line); } break; /* nothing more to read */ } if (c != '"') { /* No newline seen before end-of-line */ g_warning("'%s' line %d doesn't have a closing quote.", ff_path, line); continue; } /* Skip over separating white space, if any. */ c = skip_whitespace(ff); if (c == EOF) { if (!ferror(ff)) { /* EOF, not error; no newline seen before EOF */ g_warning("'%s' line %d doesn't have a newline.", ff_path, line); } break; /* nothing more to read */ } if (c == '\n') { /* No filter expression */ g_warning("'%s' line %d doesn't have a filter expression.", ff_path, line); continue; } /* "c" is the first non-white-space character; it's the first character of the filter expression. */ filt_expr_index = 0; for (;;) { /* Add this character to the filter expression string. */ if (filt_expr_index >= filt_expr_len) { /* Filter expressioin buffer isn't long enough; double its length. */ filt_expr_len *= 2; filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1); } filt_expr[filt_expr_index] = c; filt_expr_index++; /* Get the next character. */ c = getc_crlf(ff); if (c == EOF || c == '\n') break; } if (c == EOF) { if (!ferror(ff)) { /* EOF, not error; no newline seen before EOF */ g_warning("'%s' line %d doesn't have a newline.", ff_path, line); } break; /* nothing more to read */ } /* We saw the ending newline; terminate the filter expression string */ if (filt_expr_index >= filt_expr_len) { /* Filter expressioin buffer isn't long enough; double its length. */ filt_expr_len *= 2; filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1); } filt_expr[filt_expr_index] = '\0'; /* Add the new filter to the list of filters */ *flpp = add_filter_entry(*flpp, filt_name, filt_expr); } if (ferror(ff)) { *pref_path_return = ff_path; *errno_return = errno; } else g_free(ff_path); fclose(ff); g_free(filt_name); g_free(filt_expr); /* init the corresponding edited list */ switch (list_type) { case CFILTER_LIST: copy_filter_list(CFILTER_EDITED_LIST, CFILTER_LIST); break; case DFILTER_LIST: copy_filter_list(DFILTER_EDITED_LIST, DFILTER_LIST); break; default: g_assert_not_reached(); return; } }
wtap_open_return_val k12_open(wtap *wth, int *err, gchar **err_info) { k12_src_desc_t* rec; guint8 header_buffer[K12_FILE_HDR_LEN]; guint8* read_buffer; guint32 type; long offset; long len; guint port_type; guint32 rec_len; guint32 hwpart_len; guint32 name_len; guint32 stack_len; guint i; k12_t* file_data; #ifdef DEBUG_K12 gchar* env_level = getenv("K12_DEBUG_LEVEL"); env_file = getenv("K12_DEBUG_FILENAME"); if ( env_file ) { dbg_out = ws_fopen(env_file,"w"); if (dbg_out == NULL) { dbg_out = stderr; K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing! Logging to standard error")); } } else dbg_out = stderr; if ( env_level ) debug_level = (unsigned int)strtoul(env_level,NULL,10); K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level)); #endif if ( !wtap_read_bytes(wth->fh,header_buffer,K12_FILE_HDR_LEN,err,err_info) ) { K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR")); if (*err != WTAP_ERR_SHORT_READ) { return WTAP_OPEN_ERROR; } return WTAP_OPEN_NOT_MINE; } if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) { K12_DBG(1,("k12_open: BAD MAGIC")); return WTAP_OPEN_NOT_MINE; } offset = K12_FILE_HDR_LEN; file_data = new_k12_file_data(); file_data->file_len = pntoh32( header_buffer + 0x8); if (memiszero(header_buffer + 0x10, K12_FILE_HDR_LEN - 0x10)) { /* * The rest of the file header is all zeroes. That means * this is a file written by the old Wireshark code, and * a count of records in the file is at an offset of 0x0C. */ file_data->num_of_records = pntoh32( header_buffer + 0x0C ); } else { /* * There's at least one non-zero byte in the rest of the * header. The value 8192 is at 0xC (page size?), and * what appears to be the number of records in the file * is at an offset of 0x24 and at an offset of 0x2c. * * If the two values are not the same, we fail; if that's * the case, we need to see the file to figure out which * of those two values, if any, is the count. */ file_data->num_of_records = pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_1 ); if ( file_data->num_of_records != pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_2 ) ) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12: two different record counts, %u at 0x%02x and %u at 0x%02x", file_data->num_of_records, K12_FILE_HDR_RECORD_COUNT_1, pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_2 ), K12_FILE_HDR_RECORD_COUNT_2 ); return WTAP_OPEN_ERROR; } } K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i", offset, file_data->file_len, file_data->num_of_records )); do { if ( file_data->num_of_records == 0 ) { *err = WTAP_ERR_SHORT_READ; destroy_k12_file_data(file_data); return WTAP_OPEN_ERROR; } len = get_record(file_data, wth->fh, offset, FALSE, err, err_info); if ( len < 0 ) { K12_DBG(1,("k12_open: BAD HEADER RECORD",len)); destroy_k12_file_data(file_data); return WTAP_OPEN_ERROR; } if ( len == 0 ) { K12_DBG(1,("k12_open: BAD HEADER RECORD",len)); *err = WTAP_ERR_SHORT_READ; destroy_k12_file_data(file_data); return WTAP_OPEN_ERROR; } read_buffer = file_data->seq_read_buff; rec_len = pntoh32( read_buffer + K12_RECORD_LEN ); if (rec_len < K12_RECORD_TYPE + 4) { /* Record isn't long enough to have a type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: record length %u < %u", rec_len, K12_RECORD_TYPE + 4); return WTAP_OPEN_ERROR; } type = pntoh32( read_buffer + K12_RECORD_TYPE ); if ( (type & K12_MASK_PACKET) == K12_REC_PACKET || (type & K12_MASK_PACKET) == K12_REC_D0020) { /* * we are at the first packet record, rewind and leave. */ if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) { destroy_k12_file_data(file_data); return WTAP_OPEN_ERROR; } K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset)); break; } switch (type) { case K12_REC_SRCDSC: case K12_REC_SRCDSC2: rec = g_new0(k12_src_desc_t,1); if (rec_len < K12_SRCDESC_HWPART) { /* * Record isn't long enough to have the fixed-length portion * of the source descriptor field. */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_HWPART); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } port_type = read_buffer[K12_SRCDESC_PORT_TYPE]; hwpart_len = pntoh16( read_buffer + K12_SRCDESC_HWPARTLEN ); name_len = pntoh16( read_buffer + K12_SRCDESC_NAMELEN ); stack_len = pntoh16( read_buffer + K12_SRCDESC_STACKLEN ); rec->input = pntoh32( read_buffer + K12_RECORD_SRC_ID ); K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input)); if (name_len == 0) { K12_DBG(5,("k12_open: failed (name_len == 0 in source description")); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_NOT_MINE; } if (stack_len == 0) { K12_DBG(5,("k12_open: failed (stack_len == 0 in source description")); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_NOT_MINE; } if (rec_len < K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len) { /* * Record isn't long enough to have the full source descriptor * field, including the variable-length parts. */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u (%u + %u + %u + %u)", rec_len, K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len, K12_SRCDESC_HWPART, hwpart_len, name_len, stack_len); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } if (hwpart_len) { if (hwpart_len < 4) { /* Hardware part isn't long enough to have a type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor hardware part length %u < 4", hwpart_len); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } switch(( rec->input_type = pntoh32( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_HWPARTTYPE ) )) { case K12_PORT_DS0S: /* This appears to be variable-length */ rec->input_info.ds0mask = 0x00000000; if (hwpart_len > K12_SRCDESC_DS0_MASK) { for (i = 0; i < hwpart_len - K12_SRCDESC_DS0_MASK; i++) { rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 1U<<(31-i) : 0x0; } } break; case K12_PORT_ATMPVC: if (hwpart_len < K12_SRCDESC_ATM_VCI + 2) { /* Hardware part isn't long enough to have ATM information */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor hardware part length %u < %u", hwpart_len, K12_SRCDESC_ATM_VCI + 2); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } rec->input_info.atm.vp = pntoh16( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_ATM_VPI ); rec->input_info.atm.vc = pntoh16( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_ATM_VCI ); break; default: break; } } else { /* Record viewer generated files don't have this information */ if (port_type >= 0x14 && port_type <= 0x17) { /* For ATM2_E1DS1, ATM2_E3DS3, ATM2_STM1EL and ATM2_STM1OP */ rec->input_type = K12_PORT_ATMPVC; rec->input_info.atm.vp = 0; rec->input_info.atm.vc = 0; } } if (read_buffer[K12_SRCDESC_HWPART + hwpart_len + name_len - 1] != '\0') { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup("k12_open: source descriptor record contains non-null-terminated link-layer name"); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } if (read_buffer[K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len - 1] != '\0') { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup("k12_open: source descriptor record contains non-null-terminated stack path"); destroy_k12_file_data(file_data); g_free(rec); return WTAP_OPEN_ERROR; } rec->input_name = (gchar *)g_memdup(read_buffer + K12_SRCDESC_HWPART + hwpart_len, name_len); rec->stack_file = (gchar *)g_memdup(read_buffer + K12_SRCDESC_HWPART + hwpart_len + name_len, stack_len); ascii_strdown_inplace (rec->stack_file); g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec); g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec); break; case K12_REC_STK_FILE: K12_DBG(1,("k12_open: K12_REC_STK_FILE")); K12_DBG(1,("Field 1: 0x%08x",pntoh32( read_buffer + 0x08 ))); K12_DBG(1,("Field 2: 0x%08x",pntoh32( read_buffer + 0x0c ))); K12_ASCII_DUMP(1, read_buffer, rec_len, 16); break; default: K12_DBG(1,("k12_open: RECORD TYPE 0x%08x",type)); break; } offset += len; file_data->num_of_records--; } while(1); wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_K12; wth->file_encap = WTAP_ENCAP_K12; wth->snapshot_length = 0; wth->subtype_read = k12_read; wth->subtype_seek_read = k12_seek_read; wth->subtype_close = k12_close; wth->priv = (void *)file_data; wth->file_tsprec = WTAP_TSPREC_NSEC; return WTAP_OPEN_MINE; }
/* Drawing on the DC */ static void print_file(const char *file_name, HDC hdc, int width, int height) { #define MAX_BUF_SIZE 1024 /* An arbitrary maximum */ #define X_OFFSET 5 #define Y_OFFSET 5 FILE* fh1; size_t results; int cnt=0, y_pos = Y_OFFSET, y_cnt = 0; char buf[MAX_BUF_SIZE]; char ch; TEXTMETRIC tm; int max_chars_per_line, max_lines_per_page; SetMapMode(hdc, MM_LOMETRIC); GetTextMetrics(hdc, &tm); max_chars_per_line = MIN(width / (tm.tmMaxCharWidth + 1), MAX_BUF_SIZE); max_lines_per_page = height / (tm.tmHeight + 1); SetMapMode(hdc, MM_TEXT); GetTextMetrics(hdc, &tm); fh1 = ws_fopen(file_name, "r"); if (!fh1) { MessageBox(NULL, "Open failed on input file", "Error", MB_APPLMODAL | MB_OK); return; } while ((results = fread(&ch, 1, 1, fh1)) != 0) { /* end of page (form feed)? */ if (ch == 0x0c) { /* send buffer */ buf[cnt] = 0; TextOut(hdc, X_OFFSET,y_pos, buf, (int) strlen(buf)); y_pos += tm.tmHeight; cnt = 0; /* reset page */ EndPage(hdc); StartPage(hdc); y_pos = Y_OFFSET; y_cnt = 0; continue; } /* end of line (line feed)? */ if (ch == 0x0a) { /* send buffer */ buf[cnt] = 0; TextOut(hdc, X_OFFSET,y_pos, buf, (int) strlen(buf)); y_pos += tm.tmHeight; cnt = 0; /* last line on page? -> reset page */ if (++y_cnt == max_lines_per_page) { EndPage(hdc); StartPage(hdc); y_pos = Y_OFFSET; y_cnt = 0; } continue; } /* buffer full? */ if (cnt == (max_chars_per_line - 1)) { /* send buffer */ buf[cnt] = 0; TextOut(hdc, X_OFFSET, y_pos, buf, (int) strlen(buf)); y_pos += tm.tmHeight; cnt = 0; /* last line on page? -> reset page */ if (++y_cnt == max_lines_per_page) { EndPage(hdc); StartPage(hdc); y_pos = Y_OFFSET; y_cnt = 0; } } buf[cnt++] = ch; } /* while */ /* Print the remaining text if needed */ if (cnt > 0) { buf[cnt] = 0; TextOut(hdc, 0,y_pos, buf, (int) strlen(buf)); } fclose(fh1); }
/* * XXX - the routine pointed to by "print_line_fcn_p" doesn't get handed lines, * it gets handed bufferfuls. That's fine for "follow_write_raw()" * and "follow_add_to_gtk_text()", but, as "follow_print_text()" calls * the "print_line()" routine from "print.c", and as that routine might * genuinely expect to be handed a line (if, for example, it's using * some OS or desktop environment's printing API, and that API expects * to be handed lines), "follow_print_text()" should probably accumulate * lines in a buffer and hand them "print_line()". (If there's a * complete line in a buffer - i.e., there's nothing of the line in * the previous buffer or the next buffer - it can just hand that to * "print_line()" after filtering out non-printables, as an * optimization.) * * This might or might not be the reason why C arrays display * correctly but get extra blank lines very other line when printed. */ frs_return_t FollowStreamDialog::readTcpStream() { FILE *data_out_fp; tcp_stream_chunk sc; size_t bcount; size_t bytes_read; int iplen; guint8 client_addr[MAX_IPADDR_LEN]; guint16 client_port = 0; gboolean is_server; guint32 global_client_pos = 0, global_server_pos = 0; guint32 *global_pos; gboolean skip; char buffer[FLT_BUF_SIZE+1]; /* +1 to fix ws bug 1043 */ size_t nchars; frs_return_t frs_return; iplen = (follow_info_.is_ipv6) ? 16 : 4; data_out_fp = ws_fopen(data_out_filename_.toUtf8().constData(), "rb"); if (data_out_fp == NULL) { QMessageBox::critical(this, "Error", "Could not open temporary file %1: %2", data_out_filename_, g_strerror(errno)); return FRS_OPEN_ERROR; } while ((nchars=fread(&sc, 1, sizeof(sc), data_out_fp))) { if (nchars != sizeof(sc)) { QMessageBox::critical(this, "Error", QString(tr("Short read from temporary file %1: expected %2, got %3")) .arg(data_out_filename_) .arg(sizeof(sc)) .arg(nchars)); fclose(data_out_fp); data_out_fp = NULL; return FRS_READ_ERROR; } if (client_port == 0) { memcpy(client_addr, sc.src_addr, iplen); client_port = sc.src_port; } skip = FALSE; if (memcmp(client_addr, sc.src_addr, iplen) == 0 && client_port == sc.src_port) { is_server = FALSE; global_pos = &global_client_pos; if (follow_info_.show_stream == FROM_SERVER) { skip = TRUE; } } else { is_server = TRUE; global_pos = &global_server_pos; if (follow_info_.show_stream == FROM_CLIENT) { skip = TRUE; } } bytes_read = 0; while (bytes_read < sc.dlen) { bcount = ((sc.dlen-bytes_read) < FLT_BUF_SIZE) ? (sc.dlen-bytes_read) : FLT_BUF_SIZE; nchars = fread(buffer, 1, bcount, data_out_fp); if (nchars == 0) break; /* XXX - if we don't get "bcount" bytes, is that an error? */ bytes_read += nchars; if (!skip) { frs_return = showBuffer(buffer, nchars, is_server, sc.packet_num, global_pos); if(frs_return == FRS_PRINT_ERROR) { fclose(data_out_fp); data_out_fp = NULL; return frs_return; } } } } if (ferror(data_out_fp)) { QMessageBox::critical(this, tr("Error reading temporary file"), QString("%1: %2").arg(data_out_filename_).arg(g_strerror(errno))); fclose(data_out_fp); data_out_fp = NULL; return FRS_READ_ERROR; } fclose(data_out_fp); data_out_fp = NULL; return FRS_OK; }
int k12_open(wtap *wth, int *err, gchar **err_info) { k12_src_desc_t* rec; guint8 header_buffer[0x200]; guint8* read_buffer; guint32 type; long offset; long len; guint32 rec_len; guint32 extra_len; guint32 name_len; guint32 stack_len; guint i; k12_t* file_data; #ifdef DEBUG_K12 gchar* env_level = getenv("K12_DEBUG_LEVEL"); env_file = getenv("K12_DEBUG_FILENAME"); if ( env_file ) { dbg_out = ws_fopen(env_file,"w"); if (dbg_out == NULL) { dbg_out = stderr; K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing! Logging to standard error")); } } else dbg_out = stderr; if ( env_level ) debug_level = (unsigned int)strtoul(env_level,NULL,10); K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level)); #endif if ( file_read(header_buffer,0x200,wth->fh) != 0x200 ) { K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR")); *err = file_error(wth->fh, err_info); if (*err != 0 && *err != WTAP_ERR_SHORT_READ) { return -1; } return 0; } else { if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) { K12_DBG(1,("k12_open: BAD MAGIC")); return 0; } } offset = 0x200; file_data = new_k12_file_data(); file_data->file_len = pntoh32( header_buffer + 0x8); file_data->num_of_records = pntoh32( header_buffer + 0xC ); K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i", offset, file_data->file_len, file_data->num_of_records )); do { len = get_record(file_data, wth->fh, offset, FALSE, err, err_info); if ( len < 0 ) { K12_DBG(1,("k12_open: BAD HEADER RECORD",len)); destroy_k12_file_data(file_data); return -1; } if (len == 0) { K12_DBG(1,("k12_open: BAD HEADER RECORD",len)); *err = WTAP_ERR_SHORT_READ; destroy_k12_file_data(file_data); return -1; } if (len == 0) { K12_DBG(1,("k12_open: BAD HEADER RECORD",len)); *err = WTAP_ERR_SHORT_READ; destroy_k12_file_data(file_data); return -1; } read_buffer = file_data->seq_read_buff; rec_len = pntoh32( read_buffer + K12_RECORD_LEN ); if (rec_len < K12_RECORD_TYPE + 4) { /* Record isn't long enough to have a type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: record length %u < %u", rec_len, K12_RECORD_TYPE + 4); return -1; } type = pntoh32( read_buffer + K12_RECORD_TYPE ); if ( (type & K12_MASK_PACKET) == K12_REC_PACKET || (type & K12_MASK_PACKET) == K12_REC_D0020) { /* * we are at the first packet record, rewind and leave. */ if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) { destroy_k12_file_data(file_data); return -1; } K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset)); break; } else if (type == K12_REC_SRCDSC || type == K12_REC_SRCDSC2 ) { rec = g_new0(k12_src_desc_t,1); if (rec_len < K12_SRCDESC_STACKLEN + 2) { /* Record isn't long enough to have a stack length field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_STACKLEN + 2); return -1; } extra_len = pntoh16( read_buffer + K12_SRCDESC_EXTRALEN ); name_len = pntoh16( read_buffer + K12_SRCDESC_NAMELEN ); stack_len = pntoh16( read_buffer + K12_SRCDESC_STACKLEN ); rec->input = pntoh32( read_buffer + K12_RECORD_SRC_ID ); K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input)); if (name_len == 0 || stack_len == 0 || 0x20 + extra_len + name_len + stack_len > rec_len ) { g_free(rec); K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 " "|| 0x20 + extra_len + name_len + stack_len > rec_len) extra_len=%i name_len=%i stack_len=%i")); destroy_k12_file_data(file_data); return 0; } if (extra_len) { if (rec_len < K12_SRCDESC_EXTRATYPE + 4) { /* Record isn't long enough to have a source descriptor extra type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_EXTRATYPE + 4); return -1; } switch(( rec->input_type = pntoh32( read_buffer + K12_SRCDESC_EXTRATYPE ) )) { case K12_PORT_DS0S: if (rec_len < K12_SRCDESC_DS0_MASK + 32) { /* Record isn't long enough to have a source descriptor extra type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_DS0_MASK + 12); return -1; } rec->input_info.ds0mask = 0x00000000; for (i = 0; i < 32; i++) { rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0; } break; case K12_PORT_ATMPVC: if (rec_len < K12_SRCDESC_ATM_VCI + 2) { /* Record isn't long enough to have a source descriptor extra type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_DS0_MASK + 12); return -1; } rec->input_info.atm.vp = pntoh16( read_buffer + K12_SRCDESC_ATM_VPI ); rec->input_info.atm.vc = pntoh16( read_buffer + K12_SRCDESC_ATM_VCI ); break; default: break; } } else { /* Record viewer generated files don't have this information */ if (rec_len < K12_SRCDESC_PORT_TYPE + 1) { /* Record isn't long enough to have a source descriptor extra type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_DS0_MASK + 12); return -1; } if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14 && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17) { /* For ATM2_E1DS1, ATM2_E3DS3, ATM2_STM1EL and ATM2_STM1OP */ rec->input_type = K12_PORT_ATMPVC; rec->input_info.atm.vp = 0; rec->input_info.atm.vc = 0; } } /* XXX - this is assumed, in a number of places (not just in the ascii_strdown_inplace() call below) to be null-terminated; is that guaranteed (even with a corrupt file)? Obviously not, as a corrupt file could contain anything here; the Tektronix document says the strings "must end with \0", but a bad file could fail to add the \0. */ if (rec_len < K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len) { /* Record isn't long enough to have a source descriptor extra type field */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u", rec_len, K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len); return -1; } rec->input_name = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len); rec->stack_file = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len); ascii_strdown_inplace (rec->stack_file); g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec); g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec); offset += len; continue; } else if (type == K12_REC_STK_FILE) { K12_DBG(1,("k12_open: K12_REC_STK_FILE")); K12_DBG(1,("Field 1: 0x%08x",pntoh32( read_buffer + 0x08 ))); K12_DBG(1,("Field 2: 0x%08x",pntoh32( read_buffer + 0x0c ))); K12_ASCII_DUMP(1, read_buffer, rec_len, 0x10); offset += len; continue; } else { K12_DBG(1,("k12_open: RECORD TYPE 0x%08x",type)); offset += len; continue; } } while(1); wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_K12; wth->file_encap = WTAP_ENCAP_K12; wth->snapshot_length = 0; wth->subtype_read = k12_read; wth->subtype_seek_read = k12_seek_read; wth->subtype_close = k12_close; wth->priv = (void *)file_data; wth->tsprecision = WTAP_FILE_TSPREC_NSEC; return 1; }
/* Attempt to Write out "recent common" to the user's recent common file. If we got an error report it with a dialog box and return FALSE, otherwise return TRUE. */ gboolean write_recent(void) { char *pf_dir_path; char *rf_path; FILE *rf; /* To do: * - Split output lines longer than MAX_VAL_LEN * - Create a function for the preference directory check/creation * so that duplication can be avoided with filter.c */ /* Create the directory that holds personal configuration files, if necessary. */ if (create_persconffile_dir(&pf_dir_path) == -1) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't create directory\n\"%s\"\nfor recent file: %s.", pf_dir_path, g_strerror(errno)); g_free(pf_dir_path); return FALSE; } rf_path = get_persconffile_path(RECENT_COMMON_FILE_NAME, FALSE); if ((rf = ws_fopen(rf_path, "w")) == NULL) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't open recent file\n\"%s\": %s.", rf_path, g_strerror(errno)); g_free(rf_path); return FALSE; } g_free(rf_path); fputs("# Recent settings file for Wireshark " VERSION ".\n" "#\n" "# This file is regenerated each time Wireshark is quit.\n" "# So be careful, if you want to make manual changes here.\n" "\n" "######## Recent capture files (latest last), cannot be altered through command line ########\n" "\n", rf); menu_recent_file_write_all(rf); fputs("\n" "######## Recent capture filters (latest last), cannot be altered through command line ########\n" "\n", rf); cfilter_recent_write_all(rf); fputs("\n" "######## Recent display filters (latest last), cannot be altered through command line ########\n" "\n", rf); dfilter_recent_combo_write_all(rf); #ifdef HAVE_PCAP_REMOTE fputs("\n" "######## Recent remote hosts, cannot be altered through command line ########\n" "\n", rf); capture_remote_combo_recent_write_all(rf); #endif fprintf(rf, "\n# Main window geometry.\n"); fprintf(rf, "# Decimal numbers.\n"); fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_X ": %d\n", recent.gui_geometry_main_x); fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_Y ": %d\n", recent.gui_geometry_main_y); fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_WIDTH ": %d\n", recent.gui_geometry_main_width); fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_HEIGHT ": %d\n", recent.gui_geometry_main_height); fprintf(rf, "\n# Main window maximized.\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_GUI_GEOMETRY_MAIN_MAXIMIZED ": %s\n", recent.gui_geometry_main_maximized == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Statusbar left pane size.\n"); fprintf(rf, "# Decimal number.\n"); if (recent.gui_geometry_status_pane_left != 0) { fprintf(rf, RECENT_GUI_GEOMETRY_STATUS_PANE_LEFT ": %d\n", recent.gui_geometry_status_pane_left); } fprintf(rf, "\n# Statusbar middle pane size.\n"); fprintf(rf, "# Decimal number.\n"); if (recent.gui_geometry_status_pane_right != 0) { fprintf(rf, RECENT_GUI_GEOMETRY_STATUS_PANE_RIGHT ": %d\n", recent.gui_geometry_status_pane_right); } fprintf(rf, "\n# Last used Configuration Profile.\n"); fprintf(rf, RECENT_LAST_USED_PROFILE ": %s\n", get_profile_name()); fprintf(rf, "\n# WLAN statistics upper pane size.\n"); fprintf(rf, "# Decimal number.\n"); fprintf(rf, RECENT_GUI_GEOMETRY_WLAN_STATS_PANE ": %d\n", recent.gui_geometry_wlan_stats_pane); fprintf(rf, "\n# Warn if running with elevated permissions (e.g. as root).\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_PRIVS_WARN_IF_ELEVATED ": %s\n", recent.privs_warn_if_elevated == TRUE ? "TRUE" : "FALSE"); fprintf(rf, "\n# Warn if npf.sys isn't loaded on Windows >= 6.0.\n"); fprintf(rf, "# TRUE or FALSE (case-insensitive).\n"); fprintf(rf, RECENT_KEY_PRIVS_WARN_IF_NO_NPF ": %s\n", recent.privs_warn_if_no_npf == TRUE ? "TRUE" : "FALSE"); window_geom_recent_write_all(rf); fclose(rf); /* XXX - catch I/O errors (e.g. "ran out of disk space") and return an error indication, or maybe write to a new recent file and rename that file on top of the old one only if there are not I/O errors. */ return TRUE; }
void SCTPChunkStatisticsDialog::fillTable(bool all) { FILE* fp; pref_t *pref = prefs_find_preference(prefs_find_module("sctp"),"statistics_chunk_types"); uat_t *uat = pref->varp.uat; gchar* fname = uat_get_actual_filename(uat,TRUE); bool init = false; if (!fname ) { init = true; } else { fp = ws_fopen(fname,"r"); if (!fp && errno == ENOENT) { init = true; } } g_free (fname); if (init || all) { int j = 0; for (int i = 0; i < chunks.size(); i++) { if (!chunks.value(i).hide) { ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1); ui->tableWidget->setVerticalHeaderItem(j, new QTableWidgetItem(QString("%1").arg(chunks.value(i).name))); ui->tableWidget->setItem(j,0, new QTableWidgetItem(QString("%1").arg(selected_assoc->chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,1, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep1_chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,2, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep2_chunk_count[chunks.value(i).id]))); j++; } } for (int i = 0; i < chunks.size(); i++) { if (chunks.value(i).hide) { ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1); ui->tableWidget->setVerticalHeaderItem(j, new QTableWidgetItem(QString("%1").arg(chunks.value(i).name))); ui->tableWidget->setItem(j,0, new QTableWidgetItem(QString("%1").arg(selected_assoc->chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,1, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep1_chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,2, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep2_chunk_count[chunks.value(i).id]))); ui->tableWidget->hideRow(j); j++; } } } else { char line[100]; size_t cap = 100; char *token, id[5]; int i = 0, j = 0; struct chunkTypes temp; while (fgets(line, cap, fp)) { if (line[0] == '#') continue; token = strtok(line, ","); /* Get rid of the quotation marks */ QString ch = QString(token).mid(1, (int)strlen(token)-2); strcpy(id, qPrintable(ch)); temp.id = atoi(id); while(token != NULL) { token = strtok(NULL, ","); if (token) { if ((strstr(token, "Hide"))) { temp.hide = 1; } else if ((strstr(token, "Show"))) { temp.hide = 0; } else { QString ch = QString(token).mid(1, (int)strlen(token)-2); strcpy(temp.name, qPrintable(ch)); } } } if (!temp.hide) { ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1); ui->tableWidget->setVerticalHeaderItem(j, new QTableWidgetItem(QString("%1").arg(temp.name))); ui->tableWidget->setItem(j,0, new QTableWidgetItem(QString("%1").arg(selected_assoc->chunk_count[temp.id]))); ui->tableWidget->setItem(j,1, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep1_chunk_count[temp.id]))); ui->tableWidget->setItem(j,2, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep2_chunk_count[temp.id]))); j++; } chunks.insert(i, temp); i++; } j = ui->tableWidget->rowCount(); for (int i = 0; i < chunks.size(); i++) { if (chunks.value(i).hide) { ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1); ui->tableWidget->setVerticalHeaderItem(j, new QTableWidgetItem(QString("%1").arg(chunks.value(i).name))); ui->tableWidget->setItem(j,0, new QTableWidgetItem(QString("%1").arg(selected_assoc->chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,1, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep1_chunk_count[chunks.value(i).id]))); ui->tableWidget->setItem(j,2, new QTableWidgetItem(QString("%1").arg(selected_assoc->ep2_chunk_count[chunks.value(i).id]))); ui->tableWidget->hideRow(j); j++; } } fclose(fp); } }
/** * loal_from_file: * @param filename the file containing a loals text representation. * * Given a filename it will attempt to load a loal containing a copy of * the avpls represented in the file. * * Return value: if successful a pointer to the new populated loal, else NULL. * **/ extern LoAL* loal_from_file(gchar* filename) { FILE *fp = NULL; gchar c; int i = 0; guint32 linenum = 1; gchar linenum_buf[MAX_ITEM_LEN]; gchar name[MAX_ITEM_LEN]; gchar value[MAX_ITEM_LEN]; gchar op = '?'; LoAL *loal = new_loal(filename); AVPL* curr = NULL; AVP* avp; enum _load_loal_states { START, BEFORE_NAME, IN_NAME, IN_VALUE, MY_IGNORE } state; #ifndef _WIN32 if (! getuid()) { return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root"); } #endif state = START; if (( fp = ws_fopen(filename,"r") )) { while(( c = (gchar) fgetc(fp) )){ if ( feof(fp) ) { if ( ferror(fp) ) { report_read_failure(filename,errno); return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename); } break; } if ( c == '\n' ) { linenum++; } if ( i >= MAX_ITEM_LEN - 1 ) { return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded"); } switch(state) { case MY_IGNORE: switch (c) { case '\n': state = START; i = 0; continue; default: continue; } case START: switch (c) { case ' ': case '\t': /* ignore whitespace at line start */ continue; case '\n': /* ignore empty lines */ i = 0; continue; case AVP_NAME_CHAR: state = IN_NAME; i = 0; name[i++] = c; name[i] = '\0'; g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum); curr = new_avpl(linenum_buf); continue; case '#': state = MY_IGNORE; continue; default: return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c); } case BEFORE_NAME: i = 0; name[0] = '\0'; switch (c) { case '\\': c = (gchar) fgetc(fp); if (c != '\n') ungetc(c,fp); continue; case ' ': case '\t': continue; case AVP_NAME_CHAR: state = IN_NAME; name[i++] = c; name[i] = '\0'; continue; case '\n': loal_append(loal,curr); state = START; continue; default: return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c); } case IN_NAME: switch (c) { case ';': state = BEFORE_NAME; op = '?'; name[i] = '\0'; value[0] = '\0'; i = 0; avp = new_avp(name,value,op); if (! insert_avp(curr,avp) ) { delete_avp(avp); } continue; case AVP_OP_CHAR: name[i] = '\0'; i = 0; op = c; state = IN_VALUE; continue; case AVP_NAME_CHAR: name[i++] = c; continue; case '\n': return load_loal_error(fp,loal,curr,linenum,"operator expected found new line"); default: return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c); } case IN_VALUE: switch (c) { case '\\': value[i++] = (gchar) fgetc(fp); continue; case ';': state = BEFORE_NAME; value[i] = '\0'; i = 0; avp = new_avp(name,value,op); if (! insert_avp(curr,avp) ) { delete_avp(avp); } continue; case '\n': return load_loal_error(fp,loal,curr,linenum,"';' expected found new line"); default: value[i++] = c; continue; } } } fclose (fp); return loal; } else { report_open_failure(filename,errno,FALSE); return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename); } }