2 * Routines for packet disassembly
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <epan/epan.h>
17 #include <wiretap/wtap.h>
18 #include <epan/frame_data.h>
19 #include <epan/column-utils.h>
20 #include <epan/timestamp.h>
22 #define COMPARE_FRAME_NUM() ((fdata1->num < fdata2->num) ? -1 : \
23 (fdata1->num > fdata2->num) ? 1 : \
26 #define COMPARE_NUM(f) ((fdata1->f < fdata2->f) ? -1 : \
27 (fdata1->f > fdata2->f) ? 1 : \
30 /* Compare time stamps.
31 A packet whose time is a reference time is considered to have
32 a lower time stamp than any frame with a non-reference time;
33 if both packets' times are reference times, we compare the
34 times of the packets. */
35 #define COMPARE_TS_REAL(time1, time2) \
36 ((fdata1->flags.ref_time && !fdata2->flags.ref_time) ? -1 : \
37 (!fdata1->flags.ref_time && fdata2->flags.ref_time) ? 1 : \
38 ((time1).secs < (time2).secs) ? -1 : \
39 ((time1).secs > (time2).secs) ? 1 : \
40 ((time1).nsecs < (time2).nsecs) ? -1 :\
41 ((time1).nsecs > (time2).nsecs) ? 1 : \
44 #define COMPARE_TS(ts) COMPARE_TS_REAL(fdata1->ts, fdata2->ts)
47 frame_delta_abs_time(const struct epan_session *epan, const frame_data *fdata, guint32 prev_num, nstime_t *delta)
49 const nstime_t *prev_abs_ts = (prev_num) ? epan_get_frame_ts(epan, prev_num) : NULL;
52 nstime_delta(delta, &fdata->abs_ts, prev_abs_ts);
54 /* If we don't have the time stamp of the previous packet,
55 it's because we have no displayed/captured packets prior to this.
56 Set the delta time to zero. */
57 nstime_set_zero(delta);
62 frame_data_time_delta_compare(const struct epan_session *epan, const frame_data *fdata1, const frame_data *fdata2)
64 nstime_t del_cap_ts1, del_cap_ts2;
66 frame_delta_abs_time(epan, fdata1, fdata1->num - 1, &del_cap_ts1);
67 frame_delta_abs_time(epan, fdata2, fdata2->num - 1, &del_cap_ts2);
69 return COMPARE_TS_REAL(del_cap_ts1, del_cap_ts2);
73 frame_data_time_delta_rel_compare(const struct epan_session *epan, const frame_data *fdata1, const frame_data *fdata2)
75 nstime_t del_rel_ts1, del_rel_ts2;
77 frame_delta_abs_time(epan, fdata1, fdata1->frame_ref_num, &del_rel_ts1);
78 frame_delta_abs_time(epan, fdata2, fdata2->frame_ref_num, &del_rel_ts2);
80 return COMPARE_TS_REAL(del_rel_ts1, del_rel_ts2);
84 frame_data_time_delta_dis_compare(const struct epan_session *epan, const frame_data *fdata1, const frame_data *fdata2)
86 nstime_t del_dis_ts1, del_dis_ts2;
88 frame_delta_abs_time(epan, fdata1, fdata1->prev_dis_num, &del_dis_ts1);
89 frame_delta_abs_time(epan, fdata2, fdata2->prev_dis_num, &del_dis_ts2);
91 return COMPARE_TS_REAL(del_dis_ts1, del_dis_ts2);
95 frame_data_compare(const struct epan_session *epan, const frame_data *fdata1, const frame_data *fdata2, int field)
99 return COMPARE_FRAME_NUM();
102 switch (timestamp_get_type()) {
104 case TS_ABSOLUTE_WITH_YMD:
105 case TS_ABSOLUTE_WITH_YDOY:
107 case TS_UTC_WITH_YMD:
108 case TS_UTC_WITH_YDOY:
110 return COMPARE_TS(abs_ts);
113 return frame_data_time_delta_rel_compare(epan, fdata1, fdata2);
116 return frame_data_time_delta_compare(epan, fdata1, fdata2);
119 return frame_data_time_delta_dis_compare(epan, fdata1, fdata2);
127 case COL_ABS_YMD_TIME:
128 case COL_ABS_YDOY_TIME:
130 case COL_UTC_YMD_TIME:
131 case COL_UTC_YDOY_TIME:
132 return COMPARE_TS(abs_ts);
135 return frame_data_time_delta_rel_compare(epan, fdata1, fdata2);
138 return frame_data_time_delta_compare(epan, fdata1, fdata2);
140 case COL_DELTA_TIME_DIS:
141 return frame_data_time_delta_dis_compare(epan, fdata1, fdata2);
143 case COL_PACKET_LENGTH:
144 return COMPARE_NUM(pkt_len);
146 case COL_CUMULATIVE_BYTES:
147 return COMPARE_NUM(cum_bytes);
150 g_return_val_if_reached(0);
154 frame_data_init(frame_data *fdata, guint32 num, const wtap_rec *rec,
155 gint64 offset, guint32 cum_bytes)
159 fdata->file_off = offset;
161 fdata->flags.passed_dfilter = 0;
162 fdata->flags.dependent_of_displayed = 0;
163 fdata->flags.encoding = PACKET_CHAR_ENC_CHAR_ASCII;
164 fdata->flags.visited = 0;
165 fdata->flags.marked = 0;
166 fdata->flags.ref_time = 0;
167 fdata->flags.ignored = 0;
168 fdata->flags.has_ts = (rec->presence_flags & WTAP_HAS_TS) ? 1 : 0;
169 switch (rec->rec_type) {
171 case REC_TYPE_PACKET:
172 fdata->pkt_len = rec->rec_header.packet_header.len;
173 fdata->cum_bytes = cum_bytes + rec->rec_header.packet_header.len;
174 fdata->cap_len = rec->rec_header.packet_header.caplen;
177 case REC_TYPE_FT_SPECIFIC_EVENT:
178 case REC_TYPE_FT_SPECIFIC_REPORT:
186 case REC_TYPE_SYSCALL:
188 * XXX - is cum_bytes supposed to count non-packet bytes?
190 fdata->pkt_len = rec->rec_header.syscall_header.event_len;
191 fdata->cum_bytes = cum_bytes + rec->rec_header.syscall_header.event_len;
192 fdata->cap_len = rec->rec_header.syscall_header.event_filelen;
196 /* To save some memory, we coerce it into a gint16 */
197 g_assert(rec->tsprec <= G_MAXINT16);
198 fdata->tsprec = (gint16)rec->tsprec;
199 fdata->abs_ts = rec->ts;
200 fdata->flags.has_phdr_comment = (rec->opt_comment != NULL);
201 fdata->flags.has_user_comment = 0;
202 fdata->flags.need_colorize = 0;
203 fdata->color_filter = NULL;
204 fdata->shift_offset.secs = 0;
205 fdata->shift_offset.nsecs = 0;
206 fdata->frame_ref_num = 0;
207 fdata->prev_dis_num = 0;
211 frame_data_set_before_dissect(frame_data *fdata,
212 nstime_t *elapsed_time,
213 const frame_data **frame_ref,
214 const frame_data *prev_dis)
218 /* Don't have the reference frame, set to current */
219 if (*frame_ref == NULL)
222 /* if this frames is marked as a reference time frame,
223 set reference frame this frame */
224 if(fdata->flags.ref_time)
227 /* Get the time elapsed between the first packet and this packet. */
228 nstime_delta(&rel_ts, &fdata->abs_ts, &(*frame_ref)->abs_ts);
230 /* If it's greater than the current elapsed time, set the elapsed time
231 to it (we check for "greater than" so as not to be confused by
232 time moving backwards). */
233 if ((gint32)elapsed_time->secs < rel_ts.secs
234 || ((gint32)elapsed_time->secs == rel_ts.secs && (gint32)elapsed_time->nsecs < rel_ts.nsecs)) {
235 *elapsed_time = rel_ts;
238 fdata->frame_ref_num = (*frame_ref != fdata) ? (*frame_ref)->num : 0;
239 fdata->prev_dis_num = (prev_dis) ? prev_dis->num : 0;
243 frame_data_set_after_dissect(frame_data *fdata,
246 /* This frame either passed the display filter list or is marked as
247 a time reference frame. All time reference frames are displayed
248 even if they don't pass the display filter */
249 if(fdata->flags.ref_time){
250 /* if this was a TIME REF frame we should reset the cul bytes field */
251 *cum_bytes = fdata->pkt_len;
252 fdata->cum_bytes = *cum_bytes;
254 /* increase cum_bytes with this packets length */
255 *cum_bytes += fdata->pkt_len;
256 fdata->cum_bytes = *cum_bytes;
261 frame_data_reset(frame_data *fdata)
263 fdata->flags.visited = 0;
267 g_slist_free(fdata->pfd);
273 frame_data_destroy(frame_data *fdata)
276 g_slist_free(fdata->pfd);
282 * Editor modelines - http://www.wireshark.org/tools/modelines.html
287 * indent-tabs-mode: nil
290 * vi: set shiftwidth=2 tabstop=8 expandtab:
291 * :indentSize=2:tabSize=8:noTabs=true: