Skip to content

Commit

Permalink
Handle "I can't map this for that file format" better.
Browse files Browse the repository at this point in the history
For cases where record (meta)data is something that can't be written out
in a particular file format, return WTAP_ERR_UNWRITABLE_REC_DATA along
with an err_info string.

Report (and free) that err_info string in cases where
WTAP_ERR_UNWRITABLE_REC_DATA is returned.

Clean up some other error reporting cases, and flag with an XXX some
cases where we aren't reporting errors at all, while we're at it.

Change-Id: I91d02093af0d42c24ec4634c2c773b30f3d39ab3
Reviewed-on: https://code.wireshark.org/review/5823
Reviewed-by: Guy Harris <[email protected]>
  • Loading branch information
guyharris committed Dec 18, 2014
1 parent 8ce0f12 commit 51522b3
Show file tree
Hide file tree
Showing 37 changed files with 333 additions and 73 deletions.
2 changes: 2 additions & 0 deletions capinfos.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ process_cap_file(wtap *wth, const char *filename)
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_DECOMPRESS:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
/* fallthrough */
Expand Down Expand Up @@ -1488,6 +1489,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_DECOMPRESS:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down
1 change: 1 addition & 0 deletions captype.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down
36 changes: 33 additions & 3 deletions editcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down Expand Up @@ -1638,7 +1639,7 @@ main(int argc, char *argv[])
}
}

if (!wtap_dump(pdh, phdr, buf, &err)) {
if (!wtap_dump(pdh, phdr, buf, &err, &err_info)) {
switch (err) {
case WTAP_ERR_UNWRITABLE_ENCAP:
/*
Expand All @@ -1648,7 +1649,7 @@ main(int argc, char *argv[])
* and file type/subtype.
*/
fprintf(stderr,
"editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file\n.",
"editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n",
read_count, argv[optind],
wtap_file_type_subtype_string(out_file_type_subtype));
break;
Expand All @@ -1661,11 +1662,39 @@ main(int argc, char *argv[])
* and file type/subtype.
*/
fprintf(stderr,
"editcap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
"editcap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
read_count, argv[optind],
wtap_file_type_subtype_string(out_file_type_subtype));
break;

case WTAP_ERR_REC_TYPE_UNSUPPORTED:
/*
* 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 record number
* and file type/subtype.
*/
fprintf(stderr,
"editcap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
read_count, argv[optind],
wtap_file_type_subtype_string(out_file_type_subtype));
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 record number
* and file type/subtype.
*/
fprintf(stderr,
"editcap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
read_count, argv[optind],
wtap_file_type_subtype_string(out_file_type_subtype),
err_info);
g_free(err_info);
break;

default:
fprintf(stderr, "editcap: Error writing to %s: %s\n",
filename, wtap_strerror(err));
Expand All @@ -1691,6 +1720,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down
36 changes: 30 additions & 6 deletions epan/wslua/wslua_dumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ WSLUA_METHOD Dumper_dump(lua_State* L) {
struct wtap_pkthdr pkthdr;
double ts;
int err;
gchar *err_info;

if (!d) return 0;

Expand Down Expand Up @@ -335,9 +336,20 @@ WSLUA_METHOD Dumper_dump(lua_State* L) {
/* TODO: Can we get access to pinfo->pkt_comment here somehow? We
* should be copying it to pkthdr.opt_comment if we can. */

if (! wtap_dump(d, &pkthdr, ba->data, &err)) {
luaL_error(L,"error while dumping: %s",
wtap_strerror(err));
if (! wtap_dump(d, &pkthdr, ba->data, &err, &err_info)) {
switch (err) {

case WTAP_ERR_UNWRITABLE_REC_DATA:
luaL_error(L,"error while dumping: %s (%s)",
wtap_strerror(err), err_info);
g_free(err_info);
break;

default:
luaL_error(L,"error while dumping: %s",
wtap_strerror(err));
break;
}
}

return 0;
Expand Down Expand Up @@ -402,6 +414,7 @@ WSLUA_METHOD Dumper_dump_current(lua_State* L) {
tvbuff_t* tvb;
struct data_source *data_src;
int err = 0;
gchar *err_info;

if (!d) return 0;

Expand Down Expand Up @@ -432,9 +445,20 @@ WSLUA_METHOD Dumper_dump_current(lua_State* L) {

data = (const guchar *)tvb_memdup(wmem_packet_scope(),tvb,0,pkthdr.caplen);

if (! wtap_dump(d, &pkthdr, data, &err)) {
luaL_error(L,"error while dumping: %s",
wtap_strerror(err));
if (! wtap_dump(d, &pkthdr, data, &err, &err_info)) {
switch (err) {

case WTAP_ERR_UNWRITABLE_REC_DATA:
luaL_error(L,"error while dumping: %s (%s)",
wtap_strerror(err), err_info);
g_free(err_info);
break;

default:
luaL_error(L,"error while dumping: %s",
wtap_strerror(err));
break;
}
}

return 0;
Expand Down
4 changes: 2 additions & 2 deletions epan/wslua/wslua_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ wslua_filehandler_can_write_encap(int encap, void* data)
/* some declarations */
static gboolean
wslua_filehandler_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
const guint8 *pd, int *err);
const guint8 *pd, int *err, gchar **err_info);
static gboolean
wslua_filehandler_dump_close(wtap_dumper *wdh, int *err);

Expand Down Expand Up @@ -2019,7 +2019,7 @@ wslua_filehandler_dump_open(wtap_dumper *wdh, int *err)
*/
static gboolean
wslua_filehandler_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
const guint8 *pd, int *err)
const guint8 *pd, int *err, gchar **err_info _U_)
{
FileHandler fh = (FileHandler)(wdh->wslua_data);
int retval = -1;
Expand Down
61 changes: 58 additions & 3 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
int out_fd;
wtap_dumper *pdh;
int open_err, read_err, write_err, close_err;
gchar *err_info;
gchar *err_info, *write_err_info;
int err_fileno;
int i;
gboolean got_read_error = FALSE, got_write_error = FALSE;
Expand Down Expand Up @@ -1491,7 +1491,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
phdr->presence_flags = phdr->presence_flags | WTAP_HAS_INTERFACE_ID;
}
if (!wtap_dump(pdh, wtap_phdr(in_file->wth),
wtap_buf_ptr(in_file->wth), &write_err)) {
wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) {
got_write_error = TRUE;
break;
}
Expand Down Expand Up @@ -1599,6 +1599,36 @@ cf_merge_files(char **out_filenamep, int in_file_count,
g_free(display_basename);
break;

case WTAP_ERR_REC_TYPE_UNSUPPORTED:
/*
* 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 record number and file type/subtype.
*/
display_basename = g_filename_display_basename(in_file ? in_file->filename : "UNKNOWN");
simple_error_message_box(
"Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.",
in_file ? in_file->packet_num : 0, display_basename,
wtap_file_type_subtype_string(file_type));
g_free(display_basename);
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 frame number and file type/subtype.
*/
display_basename = g_filename_display_basename(in_file ? in_file->filename : "UNKNOWN");
simple_error_message_box(
"Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)",
in_file ? in_file->packet_num : 0, display_basename,
wtap_file_type_subtype_string(file_type),
write_err_info);
g_free(write_err_info);
g_free(display_basename);
break;

default:
display_basename = g_filename_display_basename(out_filename);
simple_error_message_box(
Expand Down Expand Up @@ -4098,6 +4128,7 @@ save_record(capture_file *cf, frame_data *fdata,
save_callback_args_t *args = (save_callback_args_t *)argsp;
struct wtap_pkthdr hdr;
int err;
gchar *err_info;
gchar *display_basename;
const char *pkt_comment;

Expand Down Expand Up @@ -4145,7 +4176,7 @@ save_record(capture_file *cf, frame_data *fdata,
hdr.pack_flags = /* XXX - 0 for now (any value for "we don't have it"?) */
#endif
/* and save the packet */
if (!wtap_dump(args->pdh, &hdr, pd, &err)) {
if (!wtap_dump(args->pdh, &hdr, pd, &err, &err_info)) {
if (err < 0) {
/* Wiretap error. */
switch (err) {
Expand All @@ -4172,6 +4203,30 @@ save_record(capture_file *cf, frame_data *fdata,
fdata->num, wtap_file_type_subtype_string(args->file_type));
break;

case WTAP_ERR_REC_TYPE_UNSUPPORTED:
/*
* 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 record number and file type/subtype.
*/
simple_error_message_box(
"Record %u has a record type that can't be saved in a \"%s\" file.",
fdata->num, wtap_file_type_subtype_string(args->file_type));
break;

case WTAP_ERR_UNWRITABLE_REC_DATA:
/*
* 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 frame number and file type/subtype.
*/
simple_error_message_box(
"Record %u has data that can't be saved in a \"%s\" file.\n(%s)",
fdata->num, wtap_file_type_subtype_string(args->file_type),
err_info);
g_free(err_info);
break;

default:
display_basename = g_filename_display_basename(args->fname);
simple_error_message_box(
Expand Down
32 changes: 29 additions & 3 deletions mergecap.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ main(int argc, char *argv[])
struct wtap_pkthdr *phdr, snap_phdr;
wtap_dumper *pdh;
int open_err, read_err = 0, write_err, close_err;
gchar *err_info;
gchar *err_info, *write_err_info;
int err_fileno;
char *out_filename = NULL;
gboolean got_read_error = FALSE, got_write_error = FALSE;
Expand Down Expand Up @@ -379,6 +379,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down Expand Up @@ -515,7 +516,7 @@ main(int argc, char *argv[])
phdr = &snap_phdr;
}

if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err)) {
if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) {
got_write_error = TRUE;
break;
}
Expand Down Expand Up @@ -548,6 +549,7 @@ main(int argc, char *argv[])
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNWRITABLE_ENCAP:
case WTAP_ERR_BAD_FILE:
case WTAP_ERR_UNWRITABLE_REC_DATA:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
Expand Down Expand Up @@ -576,11 +578,35 @@ main(int argc, char *argv[])
* the file type and subtype we're wwriting; note that, and
* report the frame number and file type/subtype.
*/
fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
wtap_file_type_subtype_string(file_type));
break;

case WTAP_ERR_REC_TYPE_UNSUPPORTED:
/*
* This is a problem with the particular record we're writing and
* the file type and subtype we're wwriting; note that, and
* report the record number and file type/subtype.
*/
fprintf(stderr, "mergecap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
wtap_file_type_subtype_string(file_type));
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 wwriting; note that, and
* report the record number and file type/subtype.
*/
fprintf(stderr, "mergecap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
wtap_file_type_subtype_string(file_type),
write_err_info);
g_free(write_err_info);
break;

default:
fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
wtap_strerror(write_err));
Expand Down
14 changes: 13 additions & 1 deletion randpkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ main(int argc, char **argv)
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;
Expand Down Expand Up @@ -619,7 +620,18 @@ main(int argc, char **argv)
}
}

wtap_dump(dump, &pkthdr, &buffer[0], &err);
/* XXX - report errors! */
if (!wtap_dump(dump, &pkthdr, &buffer[0], &err, &err_info)) {
switch (err) {

case WTAP_ERR_UNWRITABLE_REC_DATA:
g_free(err_info);
break;

default:
break;
}
}
}

wtap_dump_close(dump, &err);
Expand Down
Loading

0 comments on commit 51522b3

Please sign in to comment.