int get_natural_int(const char *string, const char *name) { gint32 number; if (!ws_strtoi32(string, NULL, &number)) { if (errno == EINVAL) { cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); exit(1); } if (number < 0) { cmdarg_err("The specified %s \"%s\" is a negative number", name, string); exit(1); } cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", name, string, number); exit(1); } if (number < 0) { cmdarg_err("The specified %s \"%s\" is a negative number", name, string); exit(1); } return (int)number; }
static gboolean xml_get_int(int *val, const char *str, const char *pattern, int *err, gchar **err_info) { const char *ptr, *endptr; char *start, *end; char buf[32]; ptr = strstr(str, pattern); if (ptr == NULL) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("dct3trace: %s not found", pattern); return FALSE; } /* * XXX - should we just skip past the pattern and check for ="? */ start = strchr(ptr, '"'); if (start == NULL) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("dct3trace: opening quote for %s not found", pattern); return FALSE; } start++; /* * XXX - should we just use ws_strtoi32() and check whether * the character following the number is a "? */ end = strchr(start, '"'); if (end == NULL) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("dct3trace: closing quote for %s not found", pattern); return FALSE; } if (end - start > 31) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("dct3trace: %s value is too long", pattern); return FALSE; } memcpy(buf, start, end - start); buf[end - start] = '\0'; /* * XXX - should we allow negative numbers in all cases? Or are * there cases where the number is unsigned? */ if (!ws_strtoi32(buf, &endptr, val)) { *err = WTAP_ERR_BAD_FILE; if (errno == ERANGE) { if (*val < 0) *err_info = g_strdup_printf("dct3trace: %s value is too small, minimum is %d", pattern, *val); else *err_info = g_strdup_printf("dct3trace: %s value is too large, maximum is %d", pattern, *val); } else *err_info = g_strdup_printf("dct3trace: %s value \"%s\" not a number", pattern, buf); return FALSE; } if (*endptr != '\0') { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("dct3trace: %s value \"%s\" not a number", pattern, buf); return FALSE; } return TRUE; }
void SCTPChunkStatisticsDialog::fillTable(bool all) { FILE* fp = NULL; 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); bool init = false; if (!fname) { init = true; } else { fp = ws_fopen(fname,"r"); if (!fp) { if (errno == ENOENT) { init = true; } else { g_log(NULL, G_LOG_LEVEL_ERROR, "Can't open %s: %s", fname, g_strerror(errno)); return; } } } g_free (fname); if (init || all) { int i, j = 0; for (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 (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]; char *token, id[5]; int i = 0, j = 0; struct chunkTypes temp; while (fgets(line, (int)sizeof line, fp)) { if (line[0] == '#') continue; token = strtok(line, ","); if (!token) continue; /* Get rid of the quotation marks */ QString ch = QString(token).mid(1, (int)strlen(token)-2); g_strlcpy(id, qPrintable(ch), sizeof id); if (!ws_strtoi32(id, NULL, &temp.id)) continue; temp.hide = 0; temp.name[0] = '\0'; 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 ch2 = QString(token).mid(1, (int)strlen(token)-2); g_strlcpy(temp.name, qPrintable(ch2), sizeof temp.name); } } } 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 (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++; } } } if (fp) fclose(fp); }
/* classic wtap: open capture file. Return WTAP_OPEN_MINE on success, * WTAP_OPEN_NOT_MINE on normal failure like malformed format, * WTAP_OPEN_ERROR on bad error like file system */ wtap_open_return_val ipfix_open(wtap *wth, int *err, gchar **err_info) { gint i, n, records_for_ipfix_check = RECORDS_FOR_IPFIX_CHECK; gchar *s; guint16 checked_len = 0; ipfix_message_header_t msg_hdr; ipfix_set_header_t set_hdr; ipfix_debug("ipfix_open: opening file"); /* number of records to scan before deciding if this really is IPFIX */ if ((s = getenv("IPFIX_RECORDS_TO_CHECK")) != NULL) { if (ws_strtoi32(s, NULL, &n) && n > 0 && n < 101) { records_for_ipfix_check = n; } } /* * IPFIX is a little hard because there's no magic number; we look at * the first few records and see if they look enough like IPFIX * records. */ for (i = 0; i < records_for_ipfix_check; i++) { /* read first message header to check version */ if (!ipfix_read_message_header(&msg_hdr, wth->fh, err, err_info)) { ipfix_debug("ipfix_open: couldn't read message header #%d with err code #%d (%s)", i, *err, *err_info); if (*err == WTAP_ERR_BAD_FILE) { *err = 0; /* not actually an error in this case */ g_free(*err_info); *err_info = NULL; return WTAP_OPEN_NOT_MINE; } if (*err != 0 && *err != WTAP_ERR_SHORT_READ) return WTAP_OPEN_ERROR; /* real failure */ /* else it's EOF */ if (i < 1) { /* we haven't seen enough to prove this is a ipfix file */ return WTAP_OPEN_NOT_MINE; } /* * If we got here, it's EOF and we haven't yet seen anything * that doesn't look like an IPFIX record - i.e. everything * we've seen looks like an IPFIX record - so we assume this * is an IPFIX file. */ break; } if (file_seek(wth->fh, IPFIX_MSG_HDR_SIZE, SEEK_CUR, err) == -1) { ipfix_debug("ipfix_open: failed seek to next message in file, %d bytes away", msg_hdr.message_length); return WTAP_OPEN_NOT_MINE; } checked_len = IPFIX_MSG_HDR_SIZE; /* check each Set in IPFIX Message for sanity */ while (checked_len < msg_hdr.message_length) { if (!wtap_read_bytes(wth->fh, &set_hdr, IPFIX_SET_HDR_SIZE, err, err_info)) { if (*err == WTAP_ERR_SHORT_READ) { /* Not a valid IPFIX Set, so not an IPFIX file. */ ipfix_debug("ipfix_open: error %d reading set", *err); return WTAP_OPEN_NOT_MINE; } /* A real I/O error; fail. */ return WTAP_OPEN_ERROR; } set_hdr.set_length = g_ntohs(set_hdr.set_length); if ((set_hdr.set_length < IPFIX_SET_HDR_SIZE) || ((set_hdr.set_length + checked_len) > msg_hdr.message_length)) { ipfix_debug("ipfix_open: found invalid set_length of %d", set_hdr.set_length); return WTAP_OPEN_NOT_MINE; } if (file_seek(wth->fh, set_hdr.set_length - IPFIX_SET_HDR_SIZE, SEEK_CUR, err) == -1) { ipfix_debug("ipfix_open: failed seek to next set in file, %d bytes away", set_hdr.set_length - IPFIX_SET_HDR_SIZE); return WTAP_OPEN_ERROR; } checked_len += set_hdr.set_length; } } /* go back to beginning of file */ if (file_seek (wth->fh, 0, SEEK_SET, err) != 0) { return WTAP_OPEN_ERROR; } /* all's good, this is a IPFIX file */ wth->file_encap = WTAP_ENCAP_RAW_IPFIX; wth->snapshot_length = 0; wth->file_tsprec = WTAP_TSPREC_SEC; wth->subtype_read = ipfix_read; wth->subtype_seek_read = ipfix_seek_read; wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_IPFIX; return WTAP_OPEN_MINE; }