aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/lbm_stream_dialog.cpp
blob: d5b2a6ff7f73d9154a7a5a2734c108ca5107a1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* lbm_stream_dialog.cpp
 *
 * Copyright (c) 2005-2014 Informatica Corporation. All Rights Reserved.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

// Adapted from stats_tree_packet.cpp

#include "lbm_stream_dialog.h"
#include <ui_lbm_stream_dialog.h>

#include "file.h"

#include <ui/qt/utils/qt_ui_utils.h>
#include "wireshark_application.h"

#include <QClipboard>
#include <QMessageBox>
#include <QTreeWidget>
#include <QTreeWidgetItemIterator>
#include <epan/packet_info.h>
#include <epan/to_str.h>
#include <epan/tap.h>
#include <epan/dissectors/packet-lbm.h>

#include <QDebug>

namespace
{
    static const int Stream_Column = 0;
    static const int EndpointA_Column = 1;
    static const int EndpointB_Column = 2;
    static const int Messages_Column = 3;
    static const int Bytes_Column = 4;
    static const int FirstFrame_Column = 5;
    static const int LastFrame_Column = 6;
}

class LBMSubstreamEntry
{
    public:
        LBMSubstreamEntry(guint64 channel, guint32 substream_id, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port);
        ~LBMSubstreamEntry(void);
        void processPacket(guint32 frame, guint32 bytes);
        void setItem(QTreeWidgetItem * item);
        QTreeWidgetItem * getItem(void)
        {
            return (m_item);
        }

    private:
        void fillItem(gboolean update_only = TRUE);
        guint64 m_channel;
        guint32 m_substream_id;
        QString m_endpoint_a;
        QString m_endpoint_b;
        guint32 m_first_frame;
        guint32 m_flast_frame;
        guint32 m_messages;
        guint32 m_bytes;
        QTreeWidgetItem * m_item;
};

LBMSubstreamEntry::LBMSubstreamEntry(guint64 channel, guint32 substream_id, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port) :
    m_channel(channel),
    m_substream_id(substream_id),
    m_first_frame((guint32)(~0)),
    m_flast_frame(0),
    m_messages(0),
    m_bytes(0),
    m_item(NULL)
{
    m_endpoint_a = QString("%1:%2")
        .arg(address_to_qstring(source_address))
        .arg(source_port);
    m_endpoint_b = QString("%1:%2")
        .arg(address_to_qstring(destination_address))
        .arg(destination_port);
}

LBMSubstreamEntry::~LBMSubstreamEntry(void)
{
}

void LBMSubstreamEntry::processPacket(guint32 frame, guint32 bytes)
{
    if (m_first_frame > frame)
    {
        m_first_frame = frame;
    }
    if (m_flast_frame < frame)
    {
        m_flast_frame = frame;
    }
    m_bytes += bytes;
    m_messages++;
    fillItem();
}

void LBMSubstreamEntry::setItem(QTreeWidgetItem * item)
{
    m_item = item;
    fillItem(FALSE);
}

void LBMSubstreamEntry::fillItem(gboolean update_only)
{
    if (update_only == FALSE)
    {
        m_item->setText(Stream_Column, QString("%1.%2").arg(m_channel).arg(m_substream_id));
        m_item->setText(EndpointA_Column, m_endpoint_a);
        m_item->setText(EndpointB_Column, m_endpoint_b);
    }
    m_item->setText(Messages_Column, QString("%1").arg(m_messages));
    m_item->setText(Bytes_Column, QString("%1").arg(m_bytes));
    m_item->setText(FirstFrame_Column, QString("%1").arg(m_first_frame));
    m_item->setText(LastFrame_Column, QString("%1").arg(m_flast_frame));
}

typedef QMap<guint32, LBMSubstreamEntry *> LBMSubstreamMap;
typedef QMap<guint32, LBMSubstreamEntry *>::iterator LBMSubstreamMapIterator;

class LBMStreamEntry
{
    public:
        LBMStreamEntry(const packet_info * pinfo, guint64 channel, const lbm_uim_stream_endpoint_t * endpoint_a, const lbm_uim_stream_endpoint_t * endpoint_b);
        ~LBMStreamEntry(void);
        void processPacket(const packet_info * pinfo, const lbm_uim_stream_tap_info_t * stream_info);
        void setItem(QTreeWidgetItem * item);
        QTreeWidgetItem * getItem(void)
        {
            return (m_item);
        }

    private:
        void fillItem(gboolean update_only = TRUE);
        QString formatEndpoint(const packet_info * pinfo, const lbm_uim_stream_endpoint_t * endpoint);
        guint64 m_channel;
        QString m_endpoint_a;
        QString m_endpoint_b;
        guint32 m_first_frame;
        guint32 m_flast_frame;
        guint32 m_messages;
        guint32 m_bytes;
        QTreeWidgetItem * m_item;
        LBMSubstreamMap m_substreams;
};

LBMStreamEntry::LBMStreamEntry(const packet_info * pinfo, guint64 channel, const lbm_uim_stream_endpoint_t * endpoint_a, const lbm_uim_stream_endpoint_t * endpoint_b) :
    m_channel(channel),
    m_first_frame((guint32)(~0)),
    m_flast_frame(0),
    m_messages(0),
    m_bytes(0),
    m_item(NULL),
    m_substreams()
{
    m_endpoint_a = formatEndpoint(pinfo, endpoint_a);
    m_endpoint_b = formatEndpoint(pinfo, endpoint_b);
}

LBMStreamEntry::~LBMStreamEntry(void)
{
    LBMSubstreamMapIterator it;

    for (it = m_substreams.begin(); it != m_substreams.end(); ++it)
    {
        delete *it;
    }
    m_substreams.clear();
}

QString LBMStreamEntry::formatEndpoint(const packet_info * pinfo, const lbm_uim_stream_endpoint_t * endpoint)
{
    if (endpoint->type == lbm_uim_instance_stream)
    {
        return QString(bytes_to_str(pinfo->pool, endpoint->stream_info.ctxinst.ctxinst, sizeof(endpoint->stream_info.ctxinst.ctxinst)));
    }
    else
    {
        return QString("%1:%2:%3")
               .arg(endpoint->stream_info.dest.domain)
               .arg(address_to_str(pinfo->pool, &(endpoint->stream_info.dest.addr)))
               .arg(endpoint->stream_info.dest.port);
    }
}

void LBMStreamEntry::processPacket(const packet_info * pinfo, const lbm_uim_stream_tap_info_t * stream_info)
{
    LBMSubstreamEntry * substream = NULL;
    LBMSubstreamMapIterator it;

    if (m_first_frame > pinfo->num)
    {
        m_first_frame = pinfo->num;
    }
    if (m_flast_frame < pinfo->num)
    {
        m_flast_frame = pinfo->num;
    }
    m_bytes += stream_info->bytes;
    m_messages++;
    it = m_substreams.find(stream_info->substream_id);
    if (m_substreams.end() == it)
    {
        QTreeWidgetItem * item = NULL;

        substream = new LBMSubstreamEntry(m_channel, stream_info->substream_id, &(pinfo->src), pinfo->srcport, &(pinfo->dst), pinfo->destport);
        m_substreams.insert(stream_info->substream_id, substream);
        item = new QTreeWidgetItem();
        substream->setItem(item);
        m_item->addChild(item);
        m_item->sortChildren(Stream_Column, Qt::AscendingOrder);
    }
    else
    {
        substream = it.value();
    }
    fillItem();
    substream->processPacket(pinfo->num, stream_info->bytes);
}

void LBMStreamEntry::setItem(QTreeWidgetItem * item)
{
    m_item = item;
    fillItem(FALSE);
}

void LBMStreamEntry::fillItem(gboolean update_only)
{
    if (update_only == FALSE)
    {
        m_item->setData(Stream_Column, Qt::DisplayRole, QVariant((qulonglong)m_channel));
        m_item->setText(EndpointA_Column, m_endpoint_a);
        m_item->setText(EndpointB_Column, m_endpoint_b);
    }
    m_item->setText(Messages_Column, QString("%1").arg(m_messages));
    m_item->setText(Bytes_Column, QString("%1").arg(m_bytes));
    m_item->setText(FirstFrame_Column, QString("%1").arg(m_first_frame));
    m_item->setText(LastFrame_Column, QString("%1").arg(m_flast_frame));
}

typedef QMap<guint64, LBMStreamEntry *> LBMStreamMap;
typedef QMap<guint64, LBMStreamEntry *>::iterator LBMStreamMapIterator;

class LBMStreamDialogInfo
{
    public:
        LBMStreamDialogInfo(void);
        ~LBMStreamDialogInfo(void);
        void setDialog(LBMStreamDialog * dialog);
        LBMStreamDialog * getDialog(void);
        void processPacket(const packet_info * pinfo, const lbm_uim_stream_tap_info_t * stream_info);
        void resetStreams(void);

    private:
        LBMStreamDialog * m_dialog;
        LBMStreamMap m_streams;
};

LBMStreamDialogInfo::LBMStreamDialogInfo(void) :
    m_dialog(NULL),
    m_streams()
{
}

LBMStreamDialogInfo::~LBMStreamDialogInfo(void)
{
    resetStreams();
}

void LBMStreamDialogInfo::setDialog(LBMStreamDialog * dialog)
{
    m_dialog = dialog;
}

LBMStreamDialog * LBMStreamDialogInfo::getDialog(void)
{
    return (m_dialog);
}

void LBMStreamDialogInfo::processPacket(const packet_info * pinfo, const lbm_uim_stream_tap_info_t * stream_info)
{
    LBMStreamEntry * stream = NULL;
    LBMStreamMapIterator it;

    it = m_streams.find(stream_info->channel);
    if (m_streams.end() == it)
    {
        QTreeWidgetItem * item = NULL;
        QTreeWidgetItem * parent = NULL;
        Ui::LBMStreamDialog * ui = NULL;

        stream = new LBMStreamEntry(pinfo, stream_info->channel, &(stream_info->endpoint_a), &(stream_info->endpoint_b));
        it = m_streams.insert(stream_info->channel, stream);
        item = new QTreeWidgetItem();
        stream->setItem(item);
        ui = m_dialog->getUI();
        ui->lbm_stream_TreeWidget->addTopLevelItem(item);
        parent = ui->lbm_stream_TreeWidget->invisibleRootItem();
        parent->sortChildren(Stream_Column, Qt::AscendingOrder);
    }
    else
    {
        stream = it.value();
    }
    stream->processPacket(pinfo, stream_info);
}

void LBMStreamDialogInfo::resetStreams(void)
{
    LBMStreamMapIterator it = m_streams.begin();

    while (it != m_streams.end())
    {
        delete *it;
        ++it;
    }
    m_streams.clear();
}

LBMStreamDialog::LBMStreamDialog(QWidget * parent, capture_file * cfile) :
    QDialog(parent),
    m_ui(new Ui::LBMStreamDialog),
    m_dialog_info(NULL),
    m_capture_file(cfile)
{
    m_ui->setupUi(this);
    m_dialog_info = new LBMStreamDialogInfo();
    connect(this, SIGNAL(accepted()), this, SLOT(closeDialog()));
    connect(this, SIGNAL(rejected()), this, SLOT(closeDialog()));
    fillTree();
}

LBMStreamDialog::~LBMStreamDialog(void)
{
    delete m_ui;
    if (m_dialog_info != NULL)
    {
        delete m_dialog_info;
    }
}

void LBMStreamDialog::setCaptureFile(capture_file * cfile)
{
    if (cfile == NULL) // We only want to know when the file closes.
    {
        m_capture_file = NULL;
        m_ui->displayFilterLineEdit->setEnabled(false);
        m_ui->applyFilterButton->setEnabled(false);
    }
}

void LBMStreamDialog::fillTree(void)
{
    GString * error_string;

    if (m_capture_file == NULL)
    {
        return;
    }
    m_dialog_info->setDialog(this);

    error_string = register_tap_listener("lbm_stream",
        (void *)m_dialog_info,
        m_ui->displayFilterLineEdit->text().toUtf8().constData(),
        TL_REQUIRES_COLUMNS,
        resetTap,
        tapPacket,
        drawTreeItems);
    if (error_string)
    {
        QMessageBox::critical(this, tr("LBM Stream failed to attach to tap"),
            error_string->str);
        g_string_free(error_string, TRUE);
        reject();
    }

    cf_retap_packets(m_capture_file);
    drawTreeItems(&m_dialog_info);
    remove_tap_listener((void *)m_dialog_info);
}

void LBMStreamDialog::resetTap(void * tap_data)
{
    LBMStreamDialogInfo * info = (LBMStreamDialogInfo *)tap_data;
    LBMStreamDialog * dialog = info->getDialog();
    if (dialog == NULL)
    {
        return;
    }
    info->resetStreams();
    dialog->m_ui->lbm_stream_TreeWidget->clear();
}

gboolean LBMStreamDialog::tapPacket(void * tap_data, packet_info * pinfo, epan_dissect_t *, const void * stream_info)
{
    if (pinfo->fd->flags.passed_dfilter == 1)
    {
        const lbm_uim_stream_tap_info_t * tapinfo = (const lbm_uim_stream_tap_info_t *)stream_info;
        LBMStreamDialogInfo * info = (LBMStreamDialogInfo *)tap_data;

        info->processPacket(pinfo, tapinfo);
    }
    return (TRUE);
}

void LBMStreamDialog::drawTreeItems(void *)
{
}

void LBMStreamDialog::on_applyFilterButton_clicked(void)
{
    fillTree();
}

void LBMStreamDialog::closeDialog(void)
{
    delete this;
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */