summaryrefslogtreecommitdiffstats
path: root/ui/qt/capture_file.cpp
blob: 0fd088b9207c03d98ac4e5ec7bed35df9cec31a2 (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
/* capture_file.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "capture_file.h"

/*
 * @file Capture file class
 *
 * Wraps the capture_file struct, cfile global, and callbacks.
 */

#include "globals.h"
capture_file cfile;

#include "file.h"

#include "epan/epan_dissect.h"

#include "ui/capture.h"

#include <QFileInfo>
#include <QTimer>
#include <QDebug>

CaptureEvent::CaptureEvent(Context ctx, EventType evt) :
    _ctx(ctx),
    _evt(evt),
    _session(Q_NULLPTR)
{
}

CaptureEvent::CaptureEvent(Context ctx, EventType evt, QString file) :
    _ctx(ctx),
    _evt(evt),
    _filePath(file),
    _session(Q_NULLPTR)
{
}

CaptureEvent::CaptureEvent(Context ctx, EventType evt, capture_session * session) :
    _ctx(ctx),
    _evt(evt),
    _session(session)
{
}

CaptureEvent::CaptureEvent(const CaptureEvent &ce) :
    _ctx(ce._ctx),
    _evt(ce._evt),
    _filePath(ce._filePath),
    _session(ce._session)
{
}

CaptureEvent::Context CaptureEvent::captureContext() const
{ return _ctx; }

CaptureEvent::EventType CaptureEvent::eventType() const
{ return _evt; }

QString CaptureEvent::filePath() const
{ return _filePath; }

capture_session * CaptureEvent::capSession() const
{ return _session; }

// To do:
// - Add getters and (if needed) setters:
//   - Full filename
//   - Capture state (stopped, prepared, running).
// - Call common_create_progress_dlg. This would let us manage the stop
//   flag here as well as emit progress signals.

QString CaptureFile::no_capture_file_ = QObject::tr("[no capture file]");

CaptureFile::CaptureFile(QObject *parent, capture_file *cap_file) :
    QObject(parent),
    cap_file_(cap_file),
    file_state_(QString())
{
#ifdef HAVE_LIBPCAP
    capture_callback_add(captureCallback, (gpointer) this);
#endif
    cf_callback_add(captureFileCallback, (gpointer) this);
}

CaptureFile::~CaptureFile()
{
    cf_callback_remove(captureFileCallback, this);
}

bool CaptureFile::isValid() const
{
    if (cap_file_ && cap_file_->state != FILE_CLOSED && cap_file_->state != FILE_READ_PENDING) { // XXX FILE_READ_IN_PROGRESS as well?
        return true;
    }
    return false;
}

const QString CaptureFile::filePath()
{
    QString path;

    if (isValid()) {
        //
        // Sadly, some UN*Xes don't necessarily use UTF-8
        // for their file names, so we have to map the
        // file path to UTF-8.  If that fails, we're somewhat
        // stuck.
        //
        char *utf8_filename = g_filename_to_utf8(cap_file_->filename,
                                                 -1,
                                                 NULL,
                                                 NULL,
                                                 NULL);
        if (utf8_filename) {
            path = QString::fromUtf8(utf8_filename);
            g_free(utf8_filename);
        } else {
            // So what the heck else can we do here?
            path = QString();
        }
    } else {
        path = QString();
    }
    return path;
}

const QString CaptureFile::fileName()
{
    QString path, name;

    path = filePath();
    if (!path.isEmpty()) {
        QFileInfo cfi(path);
        name = cfi.fileName();
    } else {
        name = QString();
    }

    return name;
}

const QString CaptureFile::fileBaseName()
{
    QString baseName;

    if (isValid()) {
        char *basename = cf_get_basename(cap_file_);
        baseName = basename;
        g_free(basename);
    } else {
        baseName = QString();
    }
    return baseName;
}

const QString CaptureFile::fileDisplayName()
{
    QString displayName;

    if (isValid()) {
        char *display_name = cf_get_display_name(cap_file_);
        displayName = display_name;
        g_free(display_name);
    } else {
        displayName = QString();
    }
    return displayName;
}

const QString CaptureFile::fileTitle()
{
    QString title;

    if (isValid()) {
        title = fileDisplayName() + file_state_;
    } else {
        title = no_capture_file_;
    }
    return title;
}

struct _packet_info *CaptureFile::packetInfo()
{
    if (capFile() && capFile()->edt) {
        return &(capFile()->edt->pi);
    }
    return NULL;
}

int CaptureFile::timestampPrecision()
{
    if (capFile() && capFile()->provider.wth) {
        return wtap_file_tsprec(capFile()->provider.wth);
    }
    return WTAP_TSPREC_UNKNOWN;
}

void CaptureFile::retapPackets()
{
    if (cap_file_) {
        cf_retap_packets(cap_file_);
    }
}

void CaptureFile::delayedRetapPackets()
{
    QTimer::singleShot(0, this, SLOT(retapPackets()));
}

void CaptureFile::reload()
{
    if (cap_file_ && cap_file_->state == FILE_READ_DONE) {
        cf_reload(cap_file_);
    }
}

void CaptureFile::stopLoading()
{
    setCaptureStopFlag(true);
}

QString CaptureFile::displayFilter() const
{
    if (isValid())
        return QString(cap_file_->dfilter);
    return QString();
}

capture_file *CaptureFile::globalCapFile()
{
    return &cfile;
}

gpointer CaptureFile::window()
{
    if (cap_file_) return cap_file_->window;
    return NULL;
}

void CaptureFile::setCaptureStopFlag(bool stop_flag)
{
    if (cap_file_) cap_file_->stop_flag = stop_flag;
}

void CaptureFile::captureFileCallback(gint event, gpointer data, gpointer user_data)
{
    CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
    if (!capture_file) return;

    capture_file->captureFileEvent(event, data);
}

#ifdef HAVE_LIBPCAP
void CaptureFile::captureCallback(gint event, capture_session *cap_session, gpointer user_data)
{
    CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
    if (!capture_file) return;

    capture_file->captureSessionEvent(event, cap_session);
}
#endif

void CaptureFile::captureFileEvent(int event, gpointer data)
{
    switch(event) {
    case(cf_cb_file_opened):
        cap_file_ = (capture_file *) data;
        emit captureEvent(CaptureEvent(CaptureEvent::File, CaptureEvent::Opened));
        break;
    case(cf_cb_file_closing):
        file_state_ = tr(" [closing]");
        emit captureEvent(CaptureEvent(CaptureEvent::File, CaptureEvent::Closing));
        break;
    case(cf_cb_file_closed):
        file_state_ = tr(" [closed]");
        emit captureEvent(CaptureEvent(CaptureEvent::File, CaptureEvent::Closed));
        cap_file_ = NULL;
        file_state_ = QString();
        break;
    case(cf_cb_file_read_started):
        emit captureEvent(CaptureEvent(CaptureEvent::File, CaptureEvent::Started));
        break;
    case(cf_cb_file_read_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::File, CaptureEvent::Finished));
        break;
    case(cf_cb_file_reload_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Reload, CaptureEvent::Started));
        break;
    case(cf_cb_file_reload_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Reload, CaptureEvent::Finished));
        break;
    case(cf_cb_file_rescan_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Rescan, CaptureEvent::Started));
        break;
    case(cf_cb_file_rescan_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Rescan, CaptureEvent::Finished));
        break;
    case(cf_cb_file_retap_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Retap, CaptureEvent::Started));
        break;
    case(cf_cb_file_retap_finished):
        /* Flush any pending tapped packet before emitting captureFileRetapFinished() */
        emit captureEvent(CaptureEvent(CaptureEvent::Retap, CaptureEvent::Finished));
        emit captureEvent(CaptureEvent(CaptureEvent::Retap, CaptureEvent::Flushed));
        break;
    case(cf_cb_file_merge_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Merge, CaptureEvent::Started));
        break;
    case(cf_cb_file_merge_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Merge, CaptureEvent::Finished));
        break;

    case(cf_cb_file_fast_save_finished):
        // gtk/main.c calls main_cf_cb_file_rescan_finished. Should we do
        // the equivalent?
        break;

    case(cf_cb_file_save_started):
    {
        emit captureEvent(CaptureEvent(CaptureEvent::Save, CaptureEvent::Started, QString((const char *)data)));
        break;
    }
    case(cf_cb_file_save_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Save, CaptureEvent::Finished));
        break;
    case(cf_cb_file_save_failed):
        emit captureEvent(CaptureEvent(CaptureEvent::Save, CaptureEvent::Failed));
        break;
    case(cf_cb_file_save_stopped):
        emit captureEvent(CaptureEvent(CaptureEvent::Save, CaptureEvent::Stopped));
        break;

    default:
        qWarning() << "CaptureFile::captureFileCallback: event " << event << " unknown";
        Q_ASSERT(false);
        break;
    }
}

#ifdef HAVE_LIBPCAP
void CaptureFile::captureSessionEvent(int event, capture_session *cap_session)
{
    switch(event) {
    case(capture_cb_capture_prepared):
        emit captureEvent(CaptureEvent(CaptureEvent::Capture, CaptureEvent::Prepared, cap_session));
        cap_file_ = cap_session->cf;
        break;
    case(capture_cb_capture_update_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Update, CaptureEvent::Started, cap_session));
        break;
    case(capture_cb_capture_update_continue):
        emit captureEvent(CaptureEvent(CaptureEvent::Update, CaptureEvent::Continued, cap_session));
        break;
    case(capture_cb_capture_update_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Update, CaptureEvent::Finished, cap_session));
        break;
    case(capture_cb_capture_fixed_started):
        emit captureEvent(CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Started, cap_session));
        break;
    case(capture_cb_capture_fixed_continue):
        emit captureEvent(CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Continued, cap_session));
        break;
    case(capture_cb_capture_fixed_finished):
        emit captureEvent(CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Finished, cap_session));
        break;
    case(capture_cb_capture_stopping):
        /* Beware: this state won't be called, if the capture child
             * closes the capturing on it's own! */
        emit captureEvent(CaptureEvent(CaptureEvent::Capture, CaptureEvent::Stopping, cap_session));
        break;
    case(capture_cb_capture_failed):
        emit captureEvent(CaptureEvent(CaptureEvent::Capture, CaptureEvent::Failed, cap_session));
        break;
    default:
        qWarning() << "main_capture_callback: event " << event << " unknown";
    }
}
#endif // HAVE_LIBPCAP