Skip to content

Commit

Permalink
Have the seek-read routines take a Buffer rather than a guint8 pointer
Browse files Browse the repository at this point in the history
as the "where to put the packet data" argument.

This lets more of the libwiretap code be common between the read and
seek-read code paths, and also allows for more flexibility in the "fill
in the data" path - we can expand the buffer as needed in both cases.

svn path=/trunk/; revision=49949
  • Loading branch information
guyharris committed Jun 16, 2013
1 parent 3846abe commit 8c9edf1
Show file tree
Hide file tree
Showing 65 changed files with 758 additions and 1,066 deletions.
2 changes: 1 addition & 1 deletion cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ typedef struct _capture_file {
gboolean search_in_progress; /* TRUE if user just clicked OK in the Find dialog or hit <control>N/B */
/* packet data */
struct wtap_pkthdr phdr; /* Packet header */
guint8 pd[WTAP_MAX_PACKET_SIZE]; /* Packet data */
Buffer buf; /* Packet data */
/* frames */
frame_data_sequence *frames; /* Sequence of frames, if we're keeping that information */
guint32 first_displayed; /* Frame number of first frame displayed */
Expand Down
58 changes: 39 additions & 19 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
and fill in the information for this file. */
cf_close(cf);

/* XXX - we really want to initialize this after we've read all
the packets, so we know how much we'll ultimately need. */
buffer_init(&cf->buf, 1500);

/* Cleanup all data structures used for dissection. */
cleanup_dissection();
/* Initialize all data structures used for dissection. */
Expand Down Expand Up @@ -432,6 +436,9 @@ cf_reset_state(capture_file *cf)
/* ...which means we have no changes to that file to save. */
cf->unsaved_changes = FALSE;

/* Free up the packet buffer. */
buffer_free(&cf->buf);

dfilter_free(cf->rfcode);
cf->rfcode = NULL;
if (cf->frames != NULL) {
Expand Down Expand Up @@ -1109,8 +1116,7 @@ void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
static int
add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
dfilter_t *dfcode, gboolean create_proto_tree, column_info *cinfo,
struct wtap_pkthdr *phdr, const guchar *buf,
gboolean add_to_packet_list)
struct wtap_pkthdr *phdr, const guint8 *buf, gboolean add_to_packet_list)
{
epan_dissect_t edt;
gint row = -1;
Expand Down Expand Up @@ -1186,7 +1192,7 @@ read_packet(capture_file *cf, dfilter_t *dfcode,
gboolean create_proto_tree, column_info *cinfo, gint64 offset)
{
struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
const guchar *buf = wtap_buf_ptr(cf->wth);
const guint8 *buf = wtap_buf_ptr(cf->wth);
frame_data fdlocal;
guint32 framenum;
frame_data *fdata;
Expand Down Expand Up @@ -1680,7 +1686,7 @@ cf_redissect_packets(capture_file *cf)

gboolean
cf_read_frame_r(capture_file *cf, frame_data *fdata,
struct wtap_pkthdr *phdr, guint8 *pd)
struct wtap_pkthdr *phdr, Buffer *buf)
{
int err;
gchar *err_info;
Expand All @@ -1702,7 +1708,7 @@ cf_read_frame_r(capture_file *cf, frame_data *fdata,
}
#endif

if (!wtap_seek_read(cf->wth, fdata->file_off, phdr, pd,
if (!wtap_seek_read(cf->wth, fdata->file_off, phdr, buf,
fdata->cap_len, &err, &err_info)) {
display_basename = g_filename_display_basename(cf->filename);
switch (err) {
Expand Down Expand Up @@ -1734,7 +1740,7 @@ cf_read_frame_r(capture_file *cf, frame_data *fdata,
gboolean
cf_read_frame(capture_file *cf, frame_data *fdata)
{
return cf_read_frame_r(cf, fdata, &cf->phdr, cf->pd);
return cf_read_frame_r(cf, fdata, &cf->phdr, &cf->buf);
}

/* Rescan the list of packets, reconstructing the CList.
Expand Down Expand Up @@ -1937,7 +1943,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
preceding_frame = prev_frame;
}
add_packet_to_packet_list(fdata, cf, dfcode, create_proto_tree,
cinfo, &cf->phdr, cf->pd,
cinfo, &cf->phdr,
buffer_start_ptr(&cf->buf),
add_to_packet_list);

/* If this frame is displayed, and this is the first frame we've
Expand Down Expand Up @@ -2155,7 +2162,7 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
{
guint32 framenum;
frame_data *fdata;
guint8 pd[WTAP_MAX_PACKET_SIZE+1];
Buffer buf;
psp_return_t ret = PSP_FINISHED;

progdlg_t *progbar = NULL;
Expand All @@ -2169,6 +2176,8 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
range_process_e process_this;
struct wtap_pkthdr phdr;

buffer_init(&buf, 1500);

/* Update the progress bar when it gets to this value. */
progbar_nextstep = 0;
/* When we reach the value that triggers a progress bar update,
Expand Down Expand Up @@ -2246,13 +2255,13 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
}

/* Get the packet */
if (!cf_read_frame_r(cf, fdata, &phdr, pd)) {
if (!cf_read_frame_r(cf, fdata, &phdr, &buf)) {
/* Attempt to get the packet failed. */
ret = PSP_FAILED;
break;
}
/* Process the packet */
if (!callback(cf, fdata, &phdr, pd, callback_args)) {
if (!callback(cf, fdata, &phdr, buffer_start_ptr(&buf), callback_args)) {
/* Callback failed. We assume it reported the error appropriately. */
ret = PSP_FAILED;
break;
Expand All @@ -2264,6 +2273,8 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
if (progbar != NULL)
destroy_progress_dlg(progbar);

buffer_free(&buf);

return ret;
}

Expand Down Expand Up @@ -2986,7 +2997,7 @@ match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
/* Construct the protocol tree, including the displayed text */
epan_dissect_init(&edt, TRUE, TRUE);
/* We don't need the column information */
epan_dissect_run(&edt, &cf->phdr, cf->pd, fdata, NULL);
epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata, NULL);

/* Iterate through all the nodes, seeing if they have text that matches. */
mdata->cf = cf;
Expand Down Expand Up @@ -3090,7 +3101,8 @@ match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
/* Don't bother constructing the protocol tree */
epan_dissect_init(&edt, FALSE, FALSE);
/* Get the column information */
epan_dissect_run(&edt, &cf->phdr, cf->pd, fdata, &cf->cinfo);
epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata,
&cf->cinfo);

/* Find the Info column */
for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
Expand Down Expand Up @@ -3173,6 +3185,7 @@ match_narrow_and_wide(capture_file *cf, frame_data *fdata, void *criterion)
size_t textlen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd;
guint32 i;
guint8 c_char;
size_t c_match = 0;
Expand All @@ -3185,9 +3198,10 @@ match_narrow_and_wide(capture_file *cf, frame_data *fdata, void *criterion)

result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
pd = buffer_start_ptr(&cf->buf);
i = 0;
while (i < buf_len) {
c_char = cf->pd[i];
c_char = pd[i];
if (cf->case_type)
c_char = toupper(c_char);
if (c_char != '\0') {
Expand All @@ -3214,6 +3228,7 @@ match_narrow_and_wide(capture_file *cf, frame_data *fdata, void *criterion)
static match_result
match_narrow(capture_file *cf, frame_data *fdata, void *criterion)
{
guint8 *pd;
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
Expand All @@ -3231,9 +3246,10 @@ match_narrow(capture_file *cf, frame_data *fdata, void *criterion)

result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
pd = buffer_start_ptr(&cf->buf);
i = 0;
while (i < buf_len) {
c_char = cf->pd[i];
c_char = pd[i];
if (cf->case_type)
c_char = toupper(c_char);
if (c_char == ascii_text[c_match]) {
Expand Down Expand Up @@ -3264,6 +3280,7 @@ match_wide(capture_file *cf, frame_data *fdata, void *criterion)
size_t textlen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd;
guint32 i;
guint8 c_char;
size_t c_match = 0;
Expand All @@ -3276,9 +3293,10 @@ match_wide(capture_file *cf, frame_data *fdata, void *criterion)

result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
pd = buffer_start_ptr(&cf->buf);
i = 0;
while (i < buf_len) {
c_char = cf->pd[i];
c_char = pd[i];
if (cf->case_type)
c_char = toupper(c_char);
if (c_char == ascii_text[c_match]) {
Expand Down Expand Up @@ -3309,6 +3327,7 @@ match_binary(capture_file *cf, frame_data *fdata, void *criterion)
size_t datalen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd;
guint32 i;
size_t c_match = 0;

Expand All @@ -3320,9 +3339,10 @@ match_binary(capture_file *cf, frame_data *fdata, void *criterion)

result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
pd = buffer_start_ptr(&cf->buf);
i = 0;
while (i < buf_len) {
if (cf->pd[i] == binary_data[c_match]) {
if (pd[i] == binary_data[c_match]) {
c_match += 1;
if (c_match == datalen) {
result = MR_MATCHED;
Expand Down Expand Up @@ -3389,7 +3409,7 @@ match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)

epan_dissect_init(&edt, TRUE, FALSE);
epan_dissect_prime_dfilter(&edt, sfcode);
epan_dissect_run(&edt, &cf->phdr, cf->pd, fdata, NULL);
epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata, NULL);
result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
epan_dissect_cleanup(&edt);
return result;
Expand Down Expand Up @@ -3722,8 +3742,8 @@ cf_select_packet(capture_file *cf, int row)
cf->edt = epan_dissect_new(TRUE, TRUE);

tap_build_interesting(cf->edt);
epan_dissect_run(cf->edt, &cf->phdr, cf->pd, cf->current_frame,
NULL);
epan_dissect_run(cf->edt, &cf->phdr, buffer_start_ptr(&cf->buf),
cf->current_frame, NULL);

dfilter_macro_build_ftv_cache(cf->edt->tree);

Expand Down
6 changes: 3 additions & 3 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ cf_read_status_t cf_read(capture_file *cf, gboolean from_save);
* @param fdata the frame_data structure for the packet in question
* @param phdr pointer to a wtap_pkthdr structure to contain the
* packet's pseudo-header and other metadata
* @param pd a guin8 array into which to read the packet's raw data
* @param buf a Buffer into which to read the packet's raw data
* @return TRUE if the read succeeded, FALSE if there was an error
*/
gboolean cf_read_frame_r(capture_file *cf, frame_data *fdata,
struct wtap_pkthdr *phdr, guint8 *pd);
struct wtap_pkthdr *phdr, Buffer *buf);

/**
* Read the pseudo-header and raw data for a packet into a
* capture_file structure's pseudo_header and pd members.
* capture_file structure's pseudo_header and buf members.
* It will pop up an alert box if there's an error.
*
* @param cf the capture file from which to read the packet
Expand Down
8 changes: 5 additions & 3 deletions proto_hier_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,19 @@ process_frame(frame_data *frame, column_info *cinfo, ph_stats_t* ps)
{
epan_dissect_t edt;
struct wtap_pkthdr phdr;
guint8 pd[WTAP_MAX_PACKET_SIZE];
Buffer buf;
double cur_time;

/* Load the frame from the capture file */
if (!cf_read_frame_r(&cfile, frame, &phdr, pd))
buffer_init(&buf, 1500);
if (!cf_read_frame_r(&cfile, frame, &phdr, &buf))
return FALSE; /* failure */

/* Dissect the frame tree not visible */
epan_dissect_init(&edt, TRUE, FALSE);
/* Don't fake protocols. We need them for the protocol hierarchy */
epan_dissect_fake_protocols(&edt, FALSE);
epan_dissect_run(&edt, &phdr, pd, frame, cinfo);
epan_dissect_run(&edt, &phdr, buffer_start_ptr(&buf), frame, cinfo);

/* Get stats from this protocol tree */
process_tree(edt.tree, ps, frame->pkt_len);
Expand All @@ -168,6 +169,7 @@ process_frame(frame_data *frame, column_info *cinfo, ph_stats_t* ps)

/* Free our memory. */
epan_dissect_cleanup(&edt);
buffer_free(&buf);

return TRUE; /* success */
}
Expand Down
11 changes: 7 additions & 4 deletions reordercap.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ typedef struct FrameRecord_t {


static void
frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh)
frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf)
{
int err;
gchar *errinfo;
struct wtap_pkthdr phdr;
guint8 buf[65535];

DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n",
frame->offset, frame->length);


/* Re-read the first frame from the stored location */
wtap_seek_read(wth,
frame->offset,
Expand All @@ -107,7 +107,7 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh)
phdr.ts = frame->time;

/* Dump frame to outfile */
if (!wtap_dump(pdh, &phdr, buf, &err)) {
if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
printf("Error (%s) writing frame to outfile\n", wtap_strerror(err));
exit(1);
}
Expand Down Expand Up @@ -155,6 +155,7 @@ int main(int argc, char *argv[])
{
wtap *wth = NULL;
wtap_dumper *pdh = NULL;
Buffer buf;
int err;
gchar *err_info;
gint64 data_offset;
Expand Down Expand Up @@ -239,15 +240,17 @@ int main(int argc, char *argv[])
}

/* Write out each sorted frame in turn */
buffer_init(&buf, 1500);
for (i = 0; i < frames->len; i++) {
FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];

/* Avoid writing if already sorted and configured to */
if (write_output_regardless || (wrong_order_count > 0)) {
frame_write(frame, wth, pdh);
frame_write(frame, wth, pdh, &buf);
}
g_slice_free(FrameRecord_t, frame);
}
buffer_free(&buf);

if (!write_output_regardless && (wrong_order_count == 0)) {
printf("Not writing output file because input file is already in order!\n");
Expand Down
Loading

0 comments on commit 8c9edf1

Please sign in to comment.