summaryrefslogtreecommitdiff
path: root/ui/qt/tcp_stream_dialog.cpp
blob: 45d8a2462d6d5f61322f484b1899bd7d93c43aba (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
/* tcp_stream_dialog.cpp
 *
 * $Id$
 *
 * 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.
 */

#include "tcp_stream_dialog.h"
#include "ui_tcp_stream_dialog.h"

#include "tango_colors.h"

#include <QDebug>

TCPStreamDialog::TCPStreamDialog(QWidget *parent, capture_file *cf, tcp_graph_type graph_type) :
    QDialog(parent),
    ui(new Ui::TCPStreamDialog),
    cap_file_(cf)
{
    struct segment current;

    ui->setupUi(this);

    if (!select_tcpip_session(cap_file_, &current)) {
        done(QDialog::Rejected);
    }

    memset (&graph_, 0, sizeof(graph_));
    graph_.type = graph_type;
    graph_segment_list_get(cap_file_, &graph_, FALSE);
    ui->streamPlot->setInteractions(
                QCP::iRangeDrag |
                QCP::iRangeZoom |
                QCP::iSelectPlottables
                );

    QVector<double> rel_time, seq;
    double rel_time_min = QCPRange::maxRange, rel_time_max = QCPRange::minRange;
    double seq_min = QCPRange::maxRange, seq_max = QCPRange::minRange;
    for (struct segment *cur = graph_.segments; cur != NULL; cur = cur->next) {
        if (!compare_headers(&graph_.src_address, &graph_.dst_address,
                             graph_.src_port, graph_.dst_port,
                             &cur->ip_src, &cur->ip_dst,
                             cur->th_sport, cur->th_dport,
                             COMPARE_CURR_DIR)) {
            continue;
        }

        double rt_val = cur->rel_secs + cur->rel_usecs / 1000000.0;

        rel_time.append(rt_val);
        if (rel_time_min > rt_val) rel_time_min = rt_val;
        if (rel_time_max < rt_val) rel_time_max = rt_val;

        seq.append(cur->th_seq);
        if (seq_min > cur->th_seq) seq_min = cur->th_seq;
        if (seq_max < cur->th_seq) seq_max = cur->th_seq;
    }

    ui->streamPlot->addGraph();
    ui->streamPlot->graph(0)->setData(rel_time, seq);
    // True Stevens-style graphs don't have lines but I like them - gcc
    ui->streamPlot->graph(0)->setPen(QPen(QBrush(tango_sky_blue_5), 0.25));
    ui->streamPlot->graph(0)->setLineStyle(QCPGraph::lsStepLeft);
    ui->streamPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));

    ui->streamPlot->xAxis->setLabel(tr("Time (s)"));
    double range_pad = (rel_time_max - rel_time_min) * 0.05;
    data_range_.setLeft(rel_time_min - range_pad);
    data_range_.setRight(rel_time_max + range_pad);
    ui->streamPlot->xAxis->setRange(data_range_.left(), data_range_.right());
    ui->streamPlot->yAxis->setLabel(tr("Sequence number (B)"));
    range_pad = (seq_max - seq_min) * 0.05;
    data_range_.setBottom(seq_min - range_pad);
    data_range_.setTop(seq_max + range_pad);
    ui->streamPlot->yAxis->setRange(data_range_.bottom(), data_range_.top());
    // XXX - QCustomPlot doesn't seem to draw any sort of focus indicator.
    ui->streamPlot->setFocus();
}

TCPStreamDialog::~TCPStreamDialog()
{
    delete ui;
}

void TCPStreamDialog::keyPressEvent(QKeyEvent *event)
{
    double h_factor = ui->streamPlot->axisRect()->rangeZoomFactor(Qt::Horizontal);
    double v_factor = ui->streamPlot->axisRect()->rangeZoomFactor(Qt::Vertical);
    bool scale_range = false;

    double h_pan = 0.0;
    double v_pan = 0.0;

    // XXX - This differs from the main window but matches other applications (e.g. Mozilla and Safari)
    switch(event->key()) {
    case Qt::Key_Minus:
    case Qt::Key_Underscore:    // Shifted minus on U.S. keyboards
    case Qt::Key_O:             // GTK+
        h_factor = pow(h_factor, -1);
        v_factor = pow(v_factor, -1);
        scale_range = true;
        break;
    case Qt::Key_Plus:
    case Qt::Key_Equal:         // Unshifted plus on U.S. keyboards
    case Qt::Key_I:             // GTK+
        scale_range = true;
        break;

    case Qt::Key_Right:
    case Qt::Key_L:
        h_pan = ui->streamPlot->xAxis->range().size() * 0.1;
        break;
    case Qt::Key_Left:
    case Qt::Key_H:
        h_pan = ui->streamPlot->xAxis->range().size() * -0.1;
        break;
    case Qt::Key_Up:
    case Qt::Key_K:
        v_pan = ui->streamPlot->yAxis->range().size() * 0.1;
        break;
    case Qt::Key_Down:
    case Qt::Key_J:
        v_pan = ui->streamPlot->yAxis->range().size() * -0.1;
        break;

    // Reset
    case Qt::Key_0:
    case Qt::Key_ParenRight:    // Shifted 0 on U.S. keyboards
    case Qt::Key_R:
    case Qt::Key_Home:
        ui->streamPlot->xAxis->setRange(data_range_.left(), data_range_.right());
        ui->streamPlot->yAxis->setRange(data_range_.bottom(), data_range_.top());
        ui->streamPlot->replot();
        break;
    // Alas, there is no Blade Runner-style Qt::Key_Ehance
    }

    if (scale_range) {
        ui->streamPlot->xAxis->scaleRange(h_factor, ui->streamPlot->xAxis->range().center());
        ui->streamPlot->yAxis->scaleRange(v_factor, ui->streamPlot->yAxis->range().center());
        ui->streamPlot->replot();
    }

    double pan_mul = event->modifiers() & Qt::ShiftModifier ? 0.1 : 1.0;

    // The GTK+ version won't pan unless we're zoomed. Should we do the same here?
    if (h_pan) {
        ui->streamPlot->xAxis->moveRange(h_pan * pan_mul);
        ui->streamPlot->replot();
    }
    if (v_pan) {
        ui->streamPlot->yAxis->moveRange(v_pan * pan_mul);
        ui->streamPlot->replot();
    }
    QDialog::keyPressEvent(event);
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */