2 * Implements a tvbuff for frame
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/packet.h>
30 #include <epan/tvbuff-int.h>
31 #include <epan/tvbuff.h>
33 #include "frame_tvbuff.h"
36 #include "wtap-int.h" /* for ->random_fh */
41 Buffer *buf; /* Packet data */
43 wtap *wth; /**< Wiretap session */
44 gint64 file_off; /**< File offset */
50 frame_read(struct tvb_frame *frame_tvb, struct wtap_pkthdr *phdr, Buffer *buf)
55 /* sanity check, capture file was closed? */
56 if (cfile.wth != frame_tvb->wth)
59 if (!wtap_seek_read(frame_tvb->wth, frame_tvb->file_off, phdr, buf, frame_tvb->tvb.length, &err, &err_info)) {
61 case WTAP_ERR_UNSUPPORTED_ENCAP:
62 case WTAP_ERR_BAD_FILE:
73 frame_invalidate(struct tvb_frame *frame_tvb)
75 struct wtap_pkthdr phdr; /* Packet header */
77 if (frame_tvb->buf == NULL) {
78 frame_tvb->buf = (struct Buffer *) g_malloc(sizeof(struct Buffer));
80 /* XXX, register frame_tvb to some list which frees from time to time not used buffers :] */
81 buffer_init(frame_tvb->buf, frame_tvb->tvb.length);
83 if (!frame_read(frame_tvb, &phdr, frame_tvb->buf))
84 { /* TODO: THROW(???); */ }
87 frame_tvb->tvb.real_data = buffer_start_ptr(frame_tvb->buf);
91 frame_free(tvbuff_t *tvb)
93 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
96 buffer_free(frame_tvb->buf);
98 g_free(frame_tvb->buf);
102 static const guint8 *
103 frame_get_ptr(tvbuff_t *tvb, guint abs_offset, guint abs_length _U_)
105 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
107 frame_invalidate(frame_tvb);
109 return tvb->real_data + abs_offset;
113 frame_memcpy(tvbuff_t *tvb, void *target, guint abs_offset, guint abs_length)
115 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
117 frame_invalidate(frame_tvb);
119 return memcpy(target, tvb->real_data + abs_offset, abs_length);
123 frame_find_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
125 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
126 const guint8 *result;
128 frame_invalidate(frame_tvb);
130 result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
132 return (gint) (result - tvb->real_data);
138 frame_pbrk_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *needles, guchar *found_needle)
140 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
142 frame_invalidate(frame_tvb);
144 return tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
150 return sizeof(struct tvb_frame);
154 frame_offset(const tvbuff_t *tvb _U_, const guint counter)
156 /* XXX: frame_tvb->offset */
160 static tvbuff_t *frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length);
162 static const struct tvb_ops tvb_frame_ops = {
163 frame_sizeof, /* size */
164 frame_free, /* free */
165 frame_offset, /* offset */
166 frame_get_ptr, /* get_ptr */
167 frame_memcpy, /* memcpy */
168 frame_find_guint8, /* find_guint8 */
169 frame_pbrk_guint8, /* pbrk_guint8 */
170 frame_clone, /* clone */
173 /* based on tvb_new_real_data() */
175 frame_tvbuff_new(const frame_data *fd, const guint8 *buf)
177 struct tvb_frame *frame_tvb;
180 tvb = tvb_new(&tvb_frame_ops);
183 * XXX - currently, the length arguments in
184 * tvbuff structure are signed, but the captured
185 * and reported length values are unsigned; this means
186 * that length values > 2^31 - 1 will appear as
189 * Captured length values that large will already
190 * have been filtered out by the Wiretap modules
191 * (the file will be reported as corrupted), to
192 * avoid trying to allocate large chunks of data.
194 * Reported length values will not have been
195 * filtered out, and should not be filtered out,
196 * as those lengths are not necessarily invalid.
198 * For now, we clip the reported length at G_MAXINT
200 * (XXX, is this still a problem?) There was an exception when we call
201 * tvb_new_real_data() now there's no one
204 tvb->real_data = buf;
205 tvb->length = fd->cap_len;
206 tvb->reported_length = fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len;
207 tvb->initialized = TRUE;
210 * This is the top-level real tvbuff for this data source,
211 * so its data source tvbuff is itself.
215 frame_tvb = (struct tvb_frame *) tvb;
217 /* XXX, how to handle fd->file_off == -1 (edited packet) ?? */
218 /* don't care, reassemble code was doing whole copy of data, so it'll work the same */
220 /* XXX, wtap_can_seek() */
221 if (fd && cfile.wth && cfile.wth->random_fh) {
222 frame_tvb->wth = cfile.wth;
223 frame_tvb->file_off = fd->file_off;
224 frame_tvb->offset = 0;
226 frame_tvb->wth = NULL;
228 frame_tvb->buf = NULL;
234 frame_tvbuff_new_buffer(const frame_data *fd, Buffer *buf)
236 return frame_tvbuff_new(fd, buffer_start_ptr(buf));
240 frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length)
242 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
244 tvbuff_t *cloned_tvb;
245 struct tvb_frame *cloned_frame_tvb;
247 /* file not seekable */
251 abs_offset += frame_tvb->offset;
253 cloned_tvb = tvb_new(&tvb_frame_ops);
255 /* data will be read when needed */
256 cloned_tvb->real_data = NULL;
257 cloned_tvb->length = abs_length;
258 cloned_tvb->reported_length = abs_length; /* XXX? */
259 cloned_tvb->initialized = TRUE;
262 * This is the top-level real tvbuff for this data source,
263 * so its data source tvbuff is itself.
265 cloned_tvb->ds_tvb = cloned_tvb;
267 cloned_frame_tvb = (struct tvb_frame *) cloned_tvb;
268 cloned_frame_tvb->wth = frame_tvb->wth;
269 cloned_frame_tvb->file_off = frame_tvb->file_off;
270 cloned_frame_tvb->offset = abs_offset;
271 cloned_frame_tvb->buf = NULL;