26a444e02b2e06658e03e7d32de413fbc549c9f5
[metze/wireshark/wip.git] / ui / qt / packet_list_record.cpp
1 /* packet_list_record.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "packet_list_record.h"
23
24 #include <file.h>
25
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>
31
32 #include <epan/color_filters.h>
33
34 #include "frame_tvbuff.h"
35
36 #include <QStringList>
37
38 class ColumnTextList : public QList<const char *> {
39 public:
40     // Allocate our records using wmem.
41     static void *operator new(size_t size) {
42         return wmem_alloc(wmem_file_scope(), size);
43     }
44
45     static void operator delete(void *) {}
46 };
47
48 QMap<int, int> PacketListRecord::cinfo_column_;
49 unsigned PacketListRecord::col_data_ver_ = 1;
50
51 PacketListRecord::PacketListRecord(frame_data *frameData) :
52     col_text_(0),
53     fdata_(frameData),
54     lines_(1),
55     line_count_changed_(false),
56     data_ver_(0),
57     colorized_(false),
58     conv_(NULL)
59 {
60 }
61
62 void *PacketListRecord::operator new(size_t size)
63 {
64     return wmem_alloc(wmem_file_scope(), size);
65 }
66
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)
70 {
71     // packet_list_store.c:packet_list_get_value
72     g_assert(fdata_);
73
74     if (!cap_file || column < 0 || column > cap_file->cinfo.num_cols) {
75         return QByteArray();
76     }
77
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);
81     }
82
83     return col_text_->value(column, QByteArray());
84 }
85
86 void PacketListRecord::resetColumns(column_info *cinfo)
87 {
88     col_data_ver_++;
89
90     if (!cinfo) {
91         return;
92     }
93
94     cinfo_column_.clear();
95     int i, j;
96     for (i = 0, j = 0; i < cinfo->num_cols; i++) {
97         if (!col_based_on_frame_data(cinfo, i)) {
98             cinfo_column_[i] = j;
99             j++;
100         }
101     }
102 }
103
104 void PacketListRecord::resetColorized()
105 {
106     colorized_ = false;
107 }
108
109 void PacketListRecord::dissect(capture_file *cap_file, bool dissect_color)
110 {
111     // packet_list_store.c:packet_list_dissect_and_cache_record
112     epan_dissect_t edt;
113     column_info *cinfo = NULL;
114     gboolean create_proto_tree;
115     struct wtap_pkthdr phdr; /* Packet header */
116     Buffer buf; /* Packet data */
117
118     if (!col_text_) col_text_ = new ColumnTextList;
119     gboolean dissect_columns = col_text_->isEmpty() || data_ver_ != col_data_ver_;
120
121     if (!cap_file) {
122         return;
123     }
124
125     memset(&phdr, 0, sizeof(struct wtap_pkthdr));
126
127     if (dissect_columns) {
128         cinfo = &cap_file->cinfo;
129     }
130
131     ws_buffer_init(&buf, 1500);
132     if (!cf_read_record_r(cap_file, fdata_, &phdr, &buf)) {
133         /*
134          * Error reading the record.
135          *
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
141          * error message.
142          */
143         if (dissect_columns) {
144             col_fill_in_error(cinfo, fdata_, FALSE, FALSE /* fill_fd_columns */);
145
146             cacheColumnStrings(cinfo);
147         }
148         if (dissect_color) {
149             fdata_->color_filter = NULL;
150             colorized_ = true;
151         }
152         ws_buffer_free(&buf);
153         return;    /* error reading the record */
154     }
155
156     create_proto_tree = ((dissect_color && color_filters_used()) ||
157                          (dissect_columns && (have_custom_cols(cinfo) ||
158                                               have_field_extractors())));
159
160     epan_dissect_init(&edt, cap_file->epan,
161                       create_proto_tree,
162                       FALSE /* proto_tree_visible */);
163
164     /* Re-color when the coloring rules are changed via the UI. */
165     if (dissect_color) {
166         color_filters_prime_edt(&edt);
167         fdata_->flags.need_colorize = 1;
168     }
169     if (dissect_columns)
170         col_custom_prime_edt(&edt, cinfo);
171
172     /*
173      * XXX - need to catch an OutOfMemoryError exception and
174      * attempt to recover from it.
175      */
176     epan_dissect_run(&edt, cap_file->cd_t, &phdr, frame_tvbuff_new_buffer(fdata_, &buf), fdata_, cinfo);
177
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);
182     }
183
184     if (dissect_color) {
185         colorized_ = true;
186     }
187     data_ver_ = col_data_ver_;
188
189     packet_info *pi = &edt.pi;
190     conv_ = find_conversation(pi->num, &pi->src, &pi->dst, pi->ptype,
191                               pi->srcport, pi->destport, 0);
192
193     epan_dissect_cleanup(&edt);
194     ws_buffer_free(&buf);
195 }
196
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()
201 {
202     g_string_chunk_clear(string_pool_);
203 }
204
205 //#define MINIMIZE_STRING_COPYING 1
206 void PacketListRecord::cacheColumnStrings(column_info *cinfo)
207 {
208     // packet_list_store.c:packet_list_change_record(PacketList *packet_list, PacketListRecord *record, gint col, column_info *cinfo)
209     if (!cinfo) {
210         return;
211     }
212
213     if (col_text_) {
214         col_text_->clear();
215     } else {
216         col_text_ = new ColumnTextList;
217     }
218     lines_ = 1;
219     line_count_changed_ = false;
220
221     for (int column = 0; column < cinfo->num_cols; ++column) {
222         int col_lines = 1;
223
224 #ifdef MINIMIZE_STRING_COPYING
225         int text_col = cinfo_column_.value(column, -1);
226
227         /* Column based on frame_data or it already contains a value */
228         if (text_col < 0) {
229             col_fill_in_frame_data(fdata_, cinfo, column, FALSE);
230             col_text_->append(cinfo->columns[column].col_data);
231             continue;
232         }
233
234         switch (cinfo->col_fmt[column]) {
235         case COL_PROTOCOL:
236         case COL_INFO:
237         case COL_IF_DIR:
238         case COL_DCE_CALL:
239         case COL_8021Q_VLAN_ID:
240         case COL_EXPERT:
241         case COL_FREQ_CHAN:
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));
248                 break;
249             }
250             /* !! FALL-THROUGH!! */
251
252         case COL_DEF_SRC:
253         case COL_RES_SRC:        /* COL_DEF_SRC is currently just like COL_RES_SRC */
254         case COL_UNRES_SRC:
255         case COL_DEF_DL_SRC:
256         case COL_RES_DL_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:
261         case COL_DEF_DST:
262         case COL_RES_DST:        /* COL_DEF_DST is currently just like COL_RES_DST */
263         case COL_UNRES_DST:
264         case COL_DEF_DL_DST:
265         case COL_RES_DL_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:
270         default:
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]);
275             } else {
276                 col_text_->append(cinfo->columns[column].col_data);
277             }
278             break;
279         }
280 #else // MINIMIZE_STRING_COPYING
281         const char *col_str;
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];
285         } else {
286             int text_col = cinfo_column_.value(column, -1);
287
288             if (text_col < 0) {
289                 col_fill_in_frame_data(fdata_, cinfo, column, FALSE);
290             }
291             col_str = cinfo->columns[column].col_data;
292         }
293         // g_string_chunk_insert_const manages a hash table of pointers to
294         // strings:
295         // https://git.gnome.org/browse/glib/tree/glib/gstringchunk.c
296         // We might be better off adding the equivalent functionality to
297         // wmem_tree.
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++;
301         }
302         if (col_lines > lines_) {
303             lines_ = col_lines;
304             line_count_changed_ = true;
305         }
306 #endif // MINIMIZE_STRING_COPYING
307     }
308 }
309
310 /*
311  * Editor modelines
312  *
313  * Local Variables:
314  * c-basic-offset: 4
315  * tab-width: 8
316  * indent-tabs-mode: nil
317  * End:
318  *
319  * ex: set shiftwidth=4 tabstop=8 expandtab:
320  * :indentSize=4:tabSize=8:noTabs=true:
321  */