1 /* packet_list_record.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "packet_list_record.h"
26 #include <epan/epan_dissect.h>
27 #include <epan/column-info.h>
28 #include <epan/column.h>
29 #include <epan/conversation.h>
30 #include <epan/wmem/wmem.h>
32 #include <epan/color_filters.h>
34 #include "frame_tvbuff.h"
36 #include <QStringList>
38 class ColumnTextList : public QList<const char *> {
40 // Allocate our records using wmem.
41 static void *operator new(size_t size) {
42 return wmem_alloc(wmem_file_scope(), size);
45 static void operator delete(void *) {}
48 QMap<int, int> PacketListRecord::cinfo_column_;
49 unsigned PacketListRecord::col_data_ver_ = 1;
51 PacketListRecord::PacketListRecord(frame_data *frameData) :
55 line_count_changed_(false),
62 void *PacketListRecord::operator new(size_t size)
64 return wmem_alloc(wmem_file_scope(), size);
67 // We might want to return a const char * instead. This would keep us from
68 // creating excessive QByteArrays, e.g. in PacketListModel::recordLessThan.
69 const QByteArray PacketListRecord::columnString(capture_file *cap_file, int column, bool colorized)
71 // packet_list_store.c:packet_list_get_value
74 if (!cap_file || column < 0 || column > cap_file->cinfo.num_cols) {
78 bool dissect_color = colorized && !colorized_;
79 if (!col_text_ || column >= col_text_->size() || !col_text_->at(column) || data_ver_ != col_data_ver_ || dissect_color) {
80 dissect(cap_file, dissect_color);
83 return col_text_->value(column, QByteArray());
86 void PacketListRecord::resetColumns(column_info *cinfo)
94 cinfo_column_.clear();
96 for (i = 0, j = 0; i < cinfo->num_cols; i++) {
97 if (!col_based_on_frame_data(cinfo, i)) {
104 void PacketListRecord::resetColorized()
109 void PacketListRecord::dissect(capture_file *cap_file, bool dissect_color)
111 // packet_list_store.c:packet_list_dissect_and_cache_record
113 column_info *cinfo = NULL;
114 gboolean create_proto_tree;
115 struct wtap_pkthdr phdr; /* Packet header */
116 Buffer buf; /* Packet data */
118 if (!col_text_) col_text_ = new ColumnTextList;
119 gboolean dissect_columns = col_text_->isEmpty() || data_ver_ != col_data_ver_;
125 memset(&phdr, 0, sizeof(struct wtap_pkthdr));
127 if (dissect_columns) {
128 cinfo = &cap_file->cinfo;
131 ws_buffer_init(&buf, 1500);
132 if (!cf_read_record_r(cap_file, fdata_, &phdr, &buf)) {
134 * Error reading the record.
136 * Don't set the color filter for now (we might want
137 * to colorize it in some fashion to warn that the
138 * row couldn't be filled in or colorized), and
139 * set the columns to placeholder values, except
140 * for the Info column, where we'll put in an
143 if (dissect_columns) {
144 col_fill_in_error(cinfo, fdata_, FALSE, FALSE /* fill_fd_columns */);
146 cacheColumnStrings(cinfo);
149 fdata_->color_filter = NULL;
152 ws_buffer_free(&buf);
153 return; /* error reading the record */
156 create_proto_tree = ((dissect_color && color_filters_used()) ||
157 (dissect_columns && (have_custom_cols(cinfo) ||
158 have_field_extractors())));
160 epan_dissect_init(&edt, cap_file->epan,
162 FALSE /* proto_tree_visible */);
164 /* Re-color when the coloring rules are changed via the UI. */
166 color_filters_prime_edt(&edt);
167 fdata_->flags.need_colorize = 1;
170 col_custom_prime_edt(&edt, cinfo);
173 * XXX - need to catch an OutOfMemoryError exception and
174 * attempt to recover from it.
176 epan_dissect_run(&edt, cap_file->cd_t, &phdr, frame_tvbuff_new_buffer(fdata_, &buf), fdata_, cinfo);
178 if (dissect_columns) {
179 /* "Stringify" non frame_data vals */
180 epan_dissect_fill_in_columns(&edt, FALSE, FALSE /* fill_fd_columns */);
181 cacheColumnStrings(cinfo);
187 data_ver_ = col_data_ver_;
189 packet_info *pi = &edt.pi;
190 conv_ = find_conversation(pi->num, &pi->src, &pi->dst, pi->ptype,
191 pi->srcport, pi->destport, 0);
193 epan_dissect_cleanup(&edt);
194 ws_buffer_free(&buf);
197 // This assumes only one packet list. We might want to move this to
198 // PacketListModel (or replace this with a wmem allocator).
199 struct _GStringChunk *PacketListRecord::string_pool_ = g_string_chunk_new(1 * 1024 * 1024);
200 void PacketListRecord::clearStringPool()
202 g_string_chunk_clear(string_pool_);
205 //#define MINIMIZE_STRING_COPYING 1
206 void PacketListRecord::cacheColumnStrings(column_info *cinfo)
208 // packet_list_store.c:packet_list_change_record(PacketList *packet_list, PacketListRecord *record, gint col, column_info *cinfo)
216 col_text_ = new ColumnTextList;
219 line_count_changed_ = false;
221 for (int column = 0; column < cinfo->num_cols; ++column) {
224 #ifdef MINIMIZE_STRING_COPYING
225 int text_col = cinfo_column_.value(column, -1);
227 /* Column based on frame_data or it already contains a value */
229 col_fill_in_frame_data(fdata_, cinfo, column, FALSE);
230 col_text_->append(cinfo->columns[column].col_data);
234 switch (cinfo->col_fmt[column]) {
239 case COL_8021Q_VLAN_ID:
242 if (cinfo->columns[column].col_data && cinfo->columns[column].col_data != cinfo->columns[column].col_buf) {
243 /* This is a constant string, so we don't have to copy it */
244 // XXX - ui/gtk/packet_list_store.c uses G_MAXUSHORT. We don't do proper UTF8
245 // truncation in either case.
246 int col_text_len = MIN(qstrlen(cinfo->col_data[column]) + 1, COL_MAX_INFO_LEN);
247 col_text_->append(QByteArray::fromRawData(cinfo->columns[column].col_data, col_text_len));
250 /* !! FALL-THROUGH!! */
253 case COL_RES_SRC: /* COL_DEF_SRC is currently just like COL_RES_SRC */
257 case COL_UNRES_DL_SRC:
258 case COL_DEF_NET_SRC:
259 case COL_RES_NET_SRC:
260 case COL_UNRES_NET_SRC:
262 case COL_RES_DST: /* COL_DEF_DST is currently just like COL_RES_DST */
266 case COL_UNRES_DL_DST:
267 case COL_DEF_NET_DST:
268 case COL_RES_NET_DST:
269 case COL_UNRES_NET_DST:
271 if (!get_column_resolved(column) && cinfo->col_expr.col_expr_val[column]) {
272 /* Use the unresolved value in col_expr_val */
273 // XXX Use QContiguousCache?
274 col_text_->append(cinfo->col_expr.col_expr_val[column]);
276 col_text_->append(cinfo->columns[column].col_data);
280 #else // MINIMIZE_STRING_COPYING
282 if (!get_column_resolved(column) && cinfo->col_expr.col_expr_val[column]) {
283 /* Use the unresolved value in col_expr_val */
284 col_str = cinfo->col_expr.col_expr_val[column];
286 int text_col = cinfo_column_.value(column, -1);
289 col_fill_in_frame_data(fdata_, cinfo, column, FALSE);
291 col_str = cinfo->columns[column].col_data;
293 // g_string_chunk_insert_const manages a hash table of pointers to
295 // https://git.gnome.org/browse/glib/tree/glib/gstringchunk.c
296 // We might be better off adding the equivalent functionality to
298 col_text_->append(g_string_chunk_insert_const(string_pool_, col_str));
299 for (int i = 0; col_str[i]; i++) {
300 if (col_str[i] == '\n') col_lines++;
302 if (col_lines > lines_) {
304 line_count_changed_ = true;
306 #endif // MINIMIZE_STRING_COPYING
316 * indent-tabs-mode: nil
319 * ex: set shiftwidth=4 tabstop=8 expandtab:
320 * :indentSize=4:tabSize=8:noTabs=true: