diff options
author | Guy Harris <guy@alum.mit.edu> | 2013-06-02 22:17:37 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2013-06-02 22:17:37 +0000 |
commit | fb2cb6c9a8fbf063079358a998f6341a0fd85592 (patch) | |
tree | 57b0cc519033709f1117ae9c783a2799a3b15a99 /wiretap/packetlogger.c | |
parent | b0646c07565f82366ce781aeadce24dd4fc6c834 (diff) | |
download | wireshark-fb2cb6c9a8fbf063079358a998f6341a0fd85592.tar.gz |
Pull code to read process packet headers into a
packetlogger_process_header() routine, used in both the read and
seek-read routines.
svn path=/trunk/; revision=49699
Diffstat (limited to 'wiretap/packetlogger.c')
-rw-r--r-- | wiretap/packetlogger.c | 88 |
1 files changed, 49 insertions, 39 deletions
diff --git a/wiretap/packetlogger.c b/wiretap/packetlogger.c index a4aef3f717..12d8409f67 100644 --- a/wiretap/packetlogger.c +++ b/wiretap/packetlogger.c @@ -54,7 +54,8 @@ static gboolean packetlogger_seek_read(wtap *wth, gint64 seek_off, gchar **err_info); static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err, gchar **err_info); - +static gboolean packetlogger_process_header(FILE_T fh, struct wtap_pkthdr *phdr, + int *err, gchar **err_info); int packetlogger_open(wtap *wth, int *err, gchar **err_info) { @@ -97,35 +98,18 @@ int packetlogger_open(wtap *wth, int *err, gchar **err_info) static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) { - packetlogger_header_t pl_hdr; guint bytes_read; *data_offset = file_tell(wth->fh); - if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info)) + if(!packetlogger_process_header(wth->fh, &wth->phdr, err, err_info)) return FALSE; - if (pl_hdr.len < 8) { - *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len); - return FALSE; - } - if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) { - /* - * Probably a corrupt capture file; don't blow up trying - * to allocate space for an immensely-large packet. - */ - *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u", - pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE); - return FALSE; - } - - buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8); + buffer_assure_space(wth->frame_buffer, wth->phdr.caplen); bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), - pl_hdr.len - 8, + wth->phdr.caplen, wth->fh); - if(bytes_read != pl_hdr.len - 8) { + if(bytes_read != wth->phdr.caplen) { *err = file_error(wth->fh, err_info); if(*err == 0) *err = WTAP_ERR_SHORT_READ; @@ -133,43 +117,33 @@ packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) return FALSE; } - wth->phdr.presence_flags = WTAP_HAS_TS; - - wth->phdr.len = pl_hdr.len - 8; - wth->phdr.caplen = pl_hdr.len - 8; - - wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32); - wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000); - return TRUE; } static gboolean -packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr _U_, - guint8 *pd, int length, int *err, - gchar **err_info) +packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, + guint8 *pd, int length, int *err, gchar **err_info) { - packetlogger_header_t pl_hdr; guint bytes_read; if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) return FALSE; - if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err, err_info)) { + if(!packetlogger_process_header(wth->random_fh, phdr, err, err_info)) { if(*err == 0) *err = WTAP_ERR_SHORT_READ; return FALSE; } - if(length != (int)pl_hdr.len - 8) { + if((guint32)length != phdr->caplen) { *err = WTAP_ERR_BAD_FILE; - *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length); + *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", phdr->caplen, length); return FALSE; } - bytes_read = file_read(pd, pl_hdr.len - 8, wth->random_fh); - if(bytes_read != (pl_hdr.len - 8)) { + bytes_read = file_read(pd, phdr->caplen, wth->random_fh); + if(bytes_read != phdr->caplen) { *err = file_error(wth->random_fh, err_info); if(*err == 0) *err = WTAP_ERR_SHORT_READ; @@ -193,3 +167,39 @@ packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err, return TRUE; } + +static gboolean +packetlogger_process_header(FILE_T fh, struct wtap_pkthdr *phdr, + int *err, gchar **err_info) +{ + packetlogger_header_t pl_hdr; + + if(!packetlogger_read_header(&pl_hdr, fh, err, err_info)) + return FALSE; + + if (pl_hdr.len < 8) { + *err = WTAP_ERR_BAD_FILE; + *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len); + return FALSE; + } + if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) { + /* + * Probably a corrupt capture file; don't blow up trying + * to allocate space for an immensely-large packet. + */ + *err = WTAP_ERR_BAD_FILE; + *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u", + pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE); + return FALSE; + } + + phdr->presence_flags = WTAP_HAS_TS; + + phdr->len = pl_hdr.len - 8; + phdr->caplen = pl_hdr.len - 8; + + phdr->ts.secs = (time_t) (pl_hdr.ts >> 32); + phdr->ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000); + + return TRUE; +} |