1 /* stratoshark_main.cpp
3 * Stratoshark - System call and event log analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
11 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
20 #include <wsutil/console_win32.h>
23 #include <ws_exit_codes.h>
24 #include <wsutil/application_flavor.h>
25 #include <wsutil/clopts_common.h>
26 #include <wsutil/cmdarg_err.h>
28 #include <wsutil/time_util.h>
29 #include <wsutil/filesystem.h>
30 #include <wsutil/privileges.h>
31 #include <wsutil/socket.h>
32 #include <wsutil/wslog.h>
34 #include <wsutil/plugins.h>
36 #include <wsutil/please_report_bug.h>
37 #include <wsutil/unicode-utils.h>
38 #include <wsutil/version_info.h>
40 #include <epan/addr_resolv.h>
41 #include <epan/ex-opt.h>
43 #include <epan/stat_tap_ui.h>
44 #include <epan/column.h>
45 #include <epan/disabled_protos.h>
46 #include <epan/prefs.h>
49 #include <epan/packet.h>
50 #include <epan/asn1.h>
51 #include <epan/dissectors/packet-kerberos.h>
54 #include <wsutil/codecs.h>
58 /* general (not Qt specific) */
60 #include "epan/color_filters.h"
62 #include "epan/rtd_table.h"
63 #include "epan/srt_table.h"
65 #include "ui/alert_box.h"
66 #include "ui/iface_lists.h"
67 #include "ui/language.h"
68 #include "ui/persfilepath_opt.h"
69 #include "ui/recent.h"
70 #include "ui/simple_dialog.h"
72 #include "ui/dissect_opts.h"
73 #include "ui/commandline.h"
74 #include "ui/capture_ui_utils.h"
75 #include "ui/preference_utils.h"
76 #include "ui/software_update.h"
79 #include "ui/qt/conversation_dialog.h"
80 #include "ui/qt/utils/color_utils.h"
81 #include "ui/qt/coloring_rules_dialog.h"
82 #include "ui/qt/endpoint_dialog.h"
83 #include "ui/qt/glib_mainloop_on_qeventloop.h"
84 #include "ui/stratoshark/stratoshark_main_window.h"
85 #include "ui/qt/simple_dialog.h"
86 #include "ui/qt/simple_statistics_dialog.h"
87 #include <ui/qt/widgets/splash_overlay.h>
88 #include "ui/stratoshark/stratoshark_application.h"
90 #include "capture/capture-pcap-util.h"
92 #include <QMessageBox>
96 # include "capture/capture-wpcap.h"
97 # include <wsutil/file_util.h>
101 # include <capture/airpcap.h>
102 # include <capture/airpcap_loader.h>
103 //# include "airpcap_dlg.h"
104 //# include "airpcap_gui_utils.h"
107 #include "epan/crypt/dot11decrypt_ws.h"
109 /* Handle the addition of View menu items without request */
110 #if defined(Q_OS_MAC)
111 #include <ui/macosx/cocoa_bridge.h>
114 #include <ui/qt/utils/qt_ui_utils.h>
116 //#define DEBUG_STARTUP_TIME 1
118 /* update the main window */
119 void main_window_update(void)
121 StratosharkApplication::processEvents();
124 void exit_application(int status
) {
132 * Report an error in command-line arguments.
134 * On Windows, Stratoshark is built for the Windows subsystem, and runs
135 * without a console, so we create a console on Windows to receive the
138 * See create_console(), in ui/win32/console_win32.c, for an example
139 * of code to check whether we need to create a console.
143 * If Stratoshark is run from the command line, its output either goes
144 * to the terminal or to wherever the standard error was redirected.
146 * If Stratoshark is run by executing it as a remote command, e.g. with
147 * ssh, its output either goes to whatever socket was set up for the
148 * remote command's standard error or to wherever the standard error
151 * If Stratoshark was run from the GUI, e.g. by double-clicking on its
152 * icon or on a file that it opens, there are no guarantees as to
153 * where the standard error went. It could be going to /dev/null
154 * (current macOS), or to a socket to systemd for the journal, or
155 * to a log file in the user's home directory, or to the "console
156 * device" ("workstation console"), or....
158 * Part of determining that, at least for locally-run Stratoshark,
159 * is to try to open /dev/tty to determine whether the process
160 * has a controlling terminal. (It fails, at a minimum, for
161 * Stratoshark launched from the GUI under macOS, Ubuntu with GNOME,
162 * and Ubuntu with KDE; in all cases, an attempt to open /dev/tty
163 * fails with ENXIO.) If it does have a controlling terminal,
164 * write to the standard error, otherwise assume that the standard
165 * error might not go anywhere that the user will be able to see.
166 * That doesn't handle the "run by ssh" case, however; that will
167 * not have a controlling terminal. (This means running it by
168 * remote execution, not by remote login.) Perhaps there's an
169 * environment variable to check there.
171 // xxx copied from ../gtk/main.c
173 stratoshark_cmdarg_err(const char *fmt
, va_list ap
)
178 fprintf(stderr
, "stratoshark: ");
179 vfprintf(stderr
, fmt
, ap
);
180 fprintf(stderr
, "\n");
184 * Report additional information for an error in command-line arguments.
185 * Creates a console on Windows.
187 // xxx copied from ../gtk/main.c
189 stratoshark_cmdarg_err_cont(const char *fmt
, va_list ap
)
194 vfprintf(stderr
, fmt
, ap
);
195 fprintf(stderr
, "\n");
199 gather_wireshark_qt_compiled_info(feature_list l
)
202 with_feature(l
, "Qt %s", QT_VERSION_STR
);
204 with_feature(l
, "Qt (version unknown)");
206 gather_caplibs_compile_info(l
);
207 epan_gather_compile_info(l
);
208 #ifdef QT_MULTIMEDIA_LIB
209 with_feature(l
, "QtMultimedia");
211 without_feature(l
, "QtMultimedia");
214 const char *update_info
= software_update_info();
216 with_feature(l
, "automatic updates using %s", update_info
);
218 without_feature(l
, "automatic updates");
222 gather_airpcap_compile_info(l
);
224 without_feature(l
, "AirPcap");
229 with_feature(l
, "Minizip");
231 without_feature(l
, "Minizip");
235 with_feature(l
, "falcosecurity-libs sinsp/scap %d.%d.%d", SINSP_VERSION_MAJOR
, SINSP_VERSION_MINOR
, SINSP_VERSION_MICRO
);
237 without_feature(l
, "falcosecurity-libs sinsp/scap");
242 gather_wireshark_runtime_info(feature_list l
)
244 with_feature(l
, "Qt %s", qVersion());
246 gather_caplibs_runtime_info(l
);
248 epan_gather_runtime_info(l
);
251 gather_airpcap_runtime_info(l
);
255 // Display information
256 const char *display_mode
= ColorUtils::themeIsDark() ? "dark" : "light";
257 with_feature(l
, "%s display mode", display_mode
);
260 foreach (QScreen
*screen
, mainApp
->screens()) {
261 if (screen
->devicePixelRatio() > 1.0) {
265 if (hidpi_count
== mainApp
->screens().count()) {
266 with_feature(l
, "HiDPI");
267 } else if (hidpi_count
) {
268 with_feature(l
, "mixed DPI");
270 without_feature(l
, "HiDPI");
276 qt_log_message_handler(QtMsgType type
, const QMessageLogContext
&, const QString
&msg
)
278 enum ws_log_level log_level
= LOG_LEVEL_DEBUG
;
282 log_level
= LOG_LEVEL_INFO
;
284 // We want qDebug() messages to show up at our default log level.
287 log_level
= LOG_LEVEL_WARNING
;
290 log_level
= LOG_LEVEL_CRITICAL
;
293 log_level
= LOG_LEVEL_ERROR
;
298 ws_log(LOG_DOMAIN_QTUI
, log_level
, "%s", qUtf8Printable(msg
));
302 /* Check if there's something important to tell the user during startup.
303 * We want to do this *after* showing the main window so that any windows
304 * we pop up will be above the main window.
307 check_and_warn_user_startup()
309 char *cur_user
, *cur_group
;
311 /* Tell the user not to run as root. */
312 if (running_with_special_privs() && recent
.privs_warn_if_elevated
) {
313 cur_user
= get_cur_username();
314 cur_group
= get_cur_groupname();
315 simple_message_box(ESD_TYPE_WARN
, &recent
.privs_warn_if_elevated
,
316 "Running as user \"%s\" and group \"%s\".\n"
317 "This could be dangerous.\n\n"
318 "If you're running Stratoshark this way in order to perform live capture, "
319 "you may want to be aware that there is a better way documented at\n"
320 WS_WIKI_URL("CaptureSetup/CapturePrivileges"), cur_user
, cur_group
);
328 // Try to avoid library search path collisions. QCoreApplication will
329 // search QT_INSTALL_PREFIX/plugins for platform DLLs before searching
330 // the application directory. If
332 // - You have Qt version 5.x.y installed in the default location
333 // (C:\Qt\5.x) on your machine.
337 // - You install Stratoshark that was built on a machine with Qt version
338 // 5.x.z installed in the default location.
340 // Qt5Core.dll will load qwindows.dll from your local C:\Qt\5.x\...\plugins
341 // directory. This may not be compatible with qwindows.dll from that
342 // same path on the build machine. At any rate, loading DLLs from paths
343 // you don't control is ill-advised. We work around this by removing every
344 // path except our application directory.
347 win32_reset_library_path(void)
349 QString app_path
= QDir(get_progfile_dir()).path();
350 foreach (QString path
, QCoreApplication::libraryPaths()) {
351 QCoreApplication::removeLibraryPath(path
);
353 QCoreApplication::addLibraryPath(app_path
);
358 // Try to work around
360 // https://gitlab.com/wireshark/wireshark/-/issues/17075
364 // https://bugreports.qt.io/browse/QTBUG-87014
368 // https://codereview.qt-project.org/c/qt/qtbase/+/322228/3/src/plugins/platforms/cocoa/qnsview_drawing.mm
370 // enables layer backing if we're running on Big Sur OR we're running on
371 // Catalina AND we were built with the Catalina SDK. Enable layer backing
372 // here by setting QT_MAC_WANTS_LAYER=1, but only if we're running on Big
373 // Sur and our version of Qt doesn't have a fix for QTBUG-87014.
374 #include <QOperatingSystemVersion>
376 macos_enable_layer_backing(void)
378 // At the time of this writing, the QTBUG-87014 for layerEnabledByMacOS is...
380 // ...in https://github.com/qt/qtbase/blob/5.15/src/plugins/platforms/cocoa/qnsview_drawing.mm
381 // ...not in https://github.com/qt/qtbase/blob/5.15.2/src/plugins/platforms/cocoa/qnsview_drawing.mm
382 // ...not in https://github.com/qt/qtbase/blob/6.0/src/plugins/platforms/cocoa/qnsview_drawing.mm
383 // ...not in https://github.com/qt/qtbase/blob/6.0.0/src/plugins/platforms/cocoa/qnsview_drawing.mm
385 // We'll assume that it will be fixed in 5.15.3, 6.0.1, and >= 6.1
386 // Note that we only ship LTS versions of Qt with our macOS packages.
387 // Feel free to add other versions if needed.
389 ((QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 3)) \
390 || (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 1)) \
392 QOperatingSystemVersion os_ver
= QOperatingSystemVersion::current();
393 int major_ver
= os_ver
.majorVersion();
394 int minor_ver
= os_ver
.minorVersion();
395 if ( (major_ver
== 10 && minor_ver
>= 16) || major_ver
>= 11 ) {
396 if (qgetenv("QT_MAC_WANTS_LAYER").isEmpty()) {
397 qputenv("QT_MAC_WANTS_LAYER", "1");
406 capture_opts_get_interface_list(int *err _U_
, char **err_str _U_
)
408 // Stratoshark only wants the IF_EXTCAP interfaces, so there's no point
409 // in spawning dumpcap to retrieve the other types of interfaces.
412 GList
*if_list
= mainApp
->getInterfaceList();
413 if (if_list
== NULL
) {
414 if_list
= capture_interface_list(err
, err_str
, main_window_update
);
415 mainApp
->setInterfaceList(if_list
);
419 return capture_interface_list(err
, err_str
, main_window_update
);
421 return append_extcap_interface_list(NULL
);
425 /* And now our feature presentation... [ fade to music ] */
426 int main(int argc
, char *qt_argv
[])
428 StratosharkMainWindow
*main_w
;
434 int ret_val
= EXIT_SUCCESS
;
435 char **argv
= qt_argv
;
440 char *err_str
, *err_str_secondary
;
448 char *err_msg
= NULL
;
449 df_error_t
*df_err
= NULL
;
451 QString dfilter
, read_filter
;
453 int caps_queries
= 0;
455 /* Start time in microseconds */
456 uint64_t start_time
= g_get_monotonic_time();
458 /* Set the program name. */
459 g_set_prgname("stratoshark");
461 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
467 * https://doc.qt.io/qt-5/qvector.html#maximum-size-and-out-of-memory-conditions
469 * https://forum.qt.io/topic/114950/qvector-realloc-throwing-sigsegv-when-very-large-surface3d-is-rendered
471 * for why we're doing this; the widget we use for the packet list
472 * uses QVector, so those limitations apply to it.
474 * Apparently, this will be fixed in Qt 6:
476 * https://github.com/qt/qtbase/commit/215ca735341b9487826023a7983382851ce8bf26
478 * https://github.com/qt/qtbase/commit/2a6cdec718934ca2cc7f6f9c616ebe62f6912123#diff-724f419b0bb0487c2629bb16cf534c4b268ddcee89b5177189b607f940cfd83dR192
480 * Hopefully QList won't cause any performance hits relative to
483 * We pick 53 million records as a value that should avoid the problem;
484 * see the Wireshark issue for why that value was chosen.
486 cf_set_max_records(53000000);
490 macos_enable_layer_backing();
493 cmdarg_err_init(stratoshark_cmdarg_err
, stratoshark_cmdarg_err_cont
);
495 /* Initialize log handler early so we can have proper logging during startup. */
496 ws_log_init(vcmdarg_err
);
497 /* For backward compatibility with GLib logging and Wireshark 3.4. */
498 ws_log_console_writer_set_use_stdout(true);
500 qInstallMessageHandler(qt_log_message_handler
);
506 #ifdef DEBUG_STARTUP_TIME
507 ws_log_console_open
= LOG_CONSOLE_OPEN_ALWAYS
;
508 #endif /* DEBUG_STARTUP_TIME */
510 #if defined(Q_OS_MAC)
511 /* Disable automatic addition of tab menu entries in view menu */
512 CocoaBridge::cleanOSGeneratedMenuItems();
516 * Set the C-language locale to the native environment and set the
517 * code page to UTF-8 on Windows.
520 setlocale(LC_ALL
, ".UTF-8");
522 setlocale(LC_ALL
, "");
529 // On Windows, QCoreApplication has its own WinMain(), which gets the
530 // command line using GetCommandLineW(), breaks it into individual
531 // arguments using CommandLineToArgvW(), and then "helpfully"
532 // converts those UTF-16LE arguments into strings in the local code
535 // We don't want that, because not all file names can be represented
536 // in the local code page, so we do the same, but we convert the
537 // strings into UTF-8.
539 wc_argv
= CommandLineToArgvW(GetCommandLineW(), &wc_argc
);
542 argv
= arg_list_utf_16to8(wc_argc
, wc_argv
);
544 } /* XXX else bail because something is horribly, horribly wrong? */
546 create_app_running_mutex();
549 /* Early logging command-line initialization. */
550 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, WS_EXIT_INVALID_OPTION
);
551 ws_noisy("Finished log init and parsing command line log arguments");
554 * Get credential information for later use, and drop privileges
555 * before doing anything else.
556 * Let the user know if anything happened.
558 init_process_policies();
559 relinquish_special_privs_perm();
562 * Attempt to get the pathname of the directory containing the
565 /* configuration_init_error = */ configuration_init(argv
[0]);
566 set_application_flavor(APPLICATION_FLAVOR_STRATOSHARK
);
567 /* ws_log(NULL, LOG_LEVEL_DEBUG, "progfile_dir: %s", get_progfile_dir()); */
570 ws_init_dll_search_path();
571 /* Load wpcap if possible. Do this before collecting the run-time version information */
575 /* Load the airpcap.dll. This must also be done before collecting
576 * run-time version information. */
579 airpcap_dll_ret_val
= load_airpcap();
581 switch (airpcap_dll_ret_val
) {
583 /* load the airpcap interfaces */
584 g_airpcap_if_list
= get_airpcap_interface_list(&err
, &err_str
);
586 if (g_airpcap_if_list
== NULL
|| g_list_length(g_airpcap_if_list
) == 0) {
587 if (err
== CANT_GET_AIRPCAP_INTERFACE_LIST
&& err_str
!= NULL
) {
588 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "%s", "Failed to open Airpcap Adapters.");
591 airpcap_if_active
= NULL
;
595 /* select the first as default (THIS SHOULD BE CHANGED) */
596 airpcap_if_active
= airpcap_get_default_if(airpcap_if_list
);
600 * XXX - Maybe we need to warn the user if one of the following happens???
602 case AIRPCAP_DLL_OLD
:
603 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "%s","AIRPCAP_DLL_OLD\n");
606 case AIRPCAP_DLL_ERROR
:
607 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "%s","AIRPCAP_DLL_ERROR\n");
610 case AIRPCAP_DLL_NOT_FOUND
:
611 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "%s","AIRPCAP_DDL_NOT_FOUND\n");
615 #endif /* HAVE_AIRPCAP */
618 /* Get the compile-time version information string */
619 ws_init_version_info("Stratoshark", gather_wireshark_qt_compiled_info
,
620 gather_wireshark_runtime_info
);
622 init_report_alert_box("Stratoshark");
624 /* Create the user profiles directory */
625 if (create_profiles_dir(&rf_path
) == -1) {
626 simple_dialog(ESD_TYPE_WARN
, ESD_BTN_OK
,
627 "Could not create profiles directory\n\"%s\": %s.",
628 rf_path
, g_strerror(errno
));
632 profile_store_persconffiles(true);
635 /* Read the profile independent recent file. We have to do this here so we can */
636 /* set the profile before it can be set from the command line parameter */
637 if (!recent_read_static(&rf_path
, &rf_open_errno
)) {
638 simple_dialog(ESD_TYPE_WARN
, ESD_BTN_OK
,
639 "Could not open common recent file\n\"%s\": %s.",
640 rf_path
, g_strerror(rf_open_errno
));
644 commandline_early_options(argc
, argv
);
647 win32_reset_library_path();
650 // Handle DPI scaling on Windows. This causes problems in at least
651 // one case on X11 and we don't yet support Android.
652 // We do the equivalent on macOS by setting NSHighResolutionCapable
654 // Note that this enables Windows 8.1-style Per-monitor DPI
655 // awareness but not Windows 10-style Per-monitor v2 awareness.
656 // https://doc.qt.io/qt-5/scalability.html
657 // https://doc.qt.io/qt-5/highdpi.html
658 // https://bugreports.qt.io/browse/QTBUG-53022 - The device pixel ratio is pretty much bogus on Windows.
659 // https://bugreports.qt.io/browse/QTBUG-55510 - Windows have wrong size
660 #if defined(Q_OS_WIN)
661 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling
);
664 // This function must be called before creating the application object.
665 // Qt::HighDpiScaleFactorRoundingPolicy::PassThrough is the default in Qt6,
666 // so this doesn't have any effect (Round is the default in 5.14 & 5.15)
667 #if defined(Q_OS_WIN)
668 QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough
);
671 /* Create The Stratoshark app */
672 StratosharkApplication
ss_app(argc
, qt_argv
);
674 /* initialize the funnel mini-api */
676 //initialize_funnel_ops();
678 Dot11DecryptInitContext(&dot11decrypt_ctx
);
681 unsigned int in_file_type
= WTAP_TYPE_AUTO
;
683 err_msg
= ws_init_sockets();
686 cmdarg_err("%s", err_msg
);
688 cmdarg_err_cont("%s", please_report_bug());
689 ret_val
= WS_EXIT_INIT_FAILED
;
693 /* Read the profile dependent (static part) of the recent file. */
694 /* Only the static part of it will be read, as we don't have the gui now to fill the */
695 /* recent lists which is done in the dynamic part. */
696 /* We have to do this already here, so command line parameters can overwrite these values. */
697 if (!recent_read_profile_static(&rf_path
, &rf_open_errno
)) {
698 simple_dialog(ESD_TYPE_WARN
, ESD_BTN_OK
,
699 "Could not open recent file\n\"%s\": %s.",
700 rf_path
, g_strerror(rf_open_errno
));
703 ssApp
->applyCustomColorsFromRecent();
705 // Initialize our language
706 read_language_prefs();
707 ssApp
->loadLanguage(language
);
709 /* ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_DEBUG, "Translator %s", language); */
711 // Init the main window (and splash)
712 main_w
= new(StratosharkMainWindow
);
714 // Setup GLib mainloop on Qt event loop to enable GLib and GIO watches
715 GLibMainloopOnQEventLoop::setup(main_w
);
716 // We may not need a queued connection here but it would seem to make sense
717 // to force the issue.
718 main_w
->connect(&ss_app
, SIGNAL(openCaptureFile(QString
,QString
,unsigned int)),
719 main_w
, SLOT(openCaptureFile(QString
,QString
,unsigned int)));
720 main_w
->connect(&ss_app
, &StratosharkApplication::openCaptureOptions
,
721 main_w
, &StratosharkMainWindow::showCaptureOptionsDialog
);
724 * If we have a saved "last directory in which a file was opened"
725 * in the recent file, set it as the one for the app.
727 * (do this after the path settings are processed)
729 if (recent
.gui_fileopen_remembered_dir
&&
730 test_for_directory(recent
.gui_fileopen_remembered_dir
) == EISDIR
) {
731 set_last_open_dir(recent
.gui_fileopen_remembered_dir
);
734 #ifdef DEBUG_STARTUP_TIME
735 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "set_console_log_handler, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
739 /* Set the initial values in the capture options. This might be overwritten
740 by preference settings and then again by the command line parameters. */
741 capture_opts_init(&global_capture_opts
, capture_opts_get_interface_list
);
745 * Libwiretap must be initialized before libwireshark is, so that
746 * dissection-time handlers for file-type-dependent blocks can
747 * register using the file type/subtype value for the file type.
751 splash_update(RA_DISSECTORS
, NULL
, NULL
);
752 #ifdef DEBUG_STARTUP_TIME
753 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Calling epan init, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
755 /* Register all dissectors; we must do this before checking for the
756 "-G" flag, as the "-G" flag dumps information registered by the
757 dissectors, and we must do it before we read the preferences, in
758 case any dissectors register preferences. */
759 if (!epan_init(splash_update
, NULL
, true)) {
760 SimpleDialog::displayQueuedMessages(main_w
);
761 ret_val
= WS_EXIT_INIT_FAILED
;
764 #ifdef DEBUG_STARTUP_TIME
765 /* epan_init resets the preferences */
766 ws_log_console_open
= LOG_CONSOLE_OPEN_ALWAYS
;
767 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "epan done, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
770 /* Register all audio codecs. */
773 // Read the dynamic part of the recent file. This determines whether or
774 // not the recent list appears in the main window so the earlier we can
775 // call this the better.
776 if (!recent_read_dynamic(&rf_path
, &rf_open_errno
)) {
777 simple_dialog(ESD_TYPE_WARN
, ESD_BTN_OK
,
778 "Could not open recent file\n\"%s\": %s.",
779 rf_path
, g_strerror(rf_open_errno
));
782 ssApp
->refreshRecentCaptures();
784 splash_update(RA_LISTENERS
, NULL
, NULL
);
785 #ifdef DEBUG_STARTUP_TIME
786 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Register all tap listeners, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
788 /* Register all tap listeners; we do this before we parse the arguments,
789 as the "-z" argument can specify a registered tap. */
791 register_all_tap_listeners(tap_reg_listener
);
793 conversation_table_set_gui_info(init_conversation_table
);
794 endpoint_table_set_gui_info(init_endpoint_table
);
795 // srt_table_iterate_tables(register_service_response_tables, NULL);
796 // rtd_table_iterate_tables(register_response_time_delay_tables, NULL);
797 stat_tap_iterate_tables(register_simple_stat_tables
, NULL
);
799 if (ex_opt_count("read_format") > 0) {
800 in_file_type
= open_info_name_to_type(ex_opt_get_next("read_format"));
803 splash_update(RA_PREFERENCES
, NULL
, NULL
);
804 #ifdef DEBUG_STARTUP_TIME
805 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Calling module preferences, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
808 /* Read the preferences, but don't apply them yet. */
809 global_commandline_info
.prefs_p
= ss_app
.readConfigurationFiles(false);
811 /* Now let's see if any of preferences were overridden at the command
812 * line, and store them. We have to do this before applying the
813 * preferences to the capture options.
815 commandline_override_prefs(argc
, argv
, true);
817 /* Register the extcap preferences. We do this after seeing if the
818 * capture_no_extcap preference is set in the configuration file
819 * or command line. This will re-read the extcap specific preferences.
821 #ifdef DEBUG_STARTUP_TIME
822 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Calling extcap_register_preferences, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
824 splash_update(RA_EXTCAP
, NULL
, NULL
);
825 extcap_register_preferences();
827 /* Apply the extcap command line options now that the extcap preferences
830 commandline_options_apply_extcap();
832 /* Some of the preferences affect the capture options. Apply those
833 * before getting the other command line arguments, which can also
834 * affect the capture options. The command line arguments should be
835 * applied last to take precedence (at least until the user saves
836 * preferences, or switches profiles.)
838 prefs_to_capture_opts();
840 /* Now get our remaining args */
841 commandline_other_options(argc
, argv
, true);
843 /* Convert some command-line parameters to QStrings */
844 if (global_commandline_info
.cf_name
!= NULL
)
845 cf_name
= QString(global_commandline_info
.cf_name
);
846 if (global_commandline_info
.rfilter
!= NULL
)
847 read_filter
= QString(global_commandline_info
.rfilter
);
848 if (global_commandline_info
.dfilter
!= NULL
)
849 dfilter
= QString(global_commandline_info
.dfilter
);
851 timestamp_set_type(recent
.gui_time_format
);
852 timestamp_set_precision(recent
.gui_time_precision
);
853 timestamp_set_seconds_type (recent
.gui_seconds_format
);
856 if (global_commandline_info
.list_link_layer_types
)
857 caps_queries
|= CAPS_QUERY_LINK_TYPES
;
858 if (global_commandline_info
.list_timestamp_types
)
859 caps_queries
|= CAPS_QUERY_TIMESTAMP_TYPES
;
861 if (global_commandline_info
.start_capture
|| caps_queries
) {
862 /* We're supposed to do a live capture or get a list of link-layer/timestamp
863 types for a live capture device; if the user didn't specify an
864 interface to use, pick a default. */
865 ret_val
= capture_opts_default_iface_if_necessary(&global_capture_opts
,
866 ((global_commandline_info
.prefs_p
->capture_device
) && (*global_commandline_info
.prefs_p
->capture_device
!= '\0')) ? get_if_name(global_commandline_info
.prefs_p
->capture_device
) : NULL
);
873 * If requested, list the link layer types and/or time stamp types
882 /* Get the list of link-layer types for the capture devices. */
883 ret_val
= EXIT_SUCCESS
;
884 GList
*if_cap_queries
= NULL
;
885 if_cap_query_t
*if_cap_query
;
886 GHashTable
*capability_hash
;
887 for (i
= 0; i
< global_capture_opts
.ifaces
->len
; i
++) {
888 interface_options
*interface_opts
;
890 interface_opts
= &g_array_index(global_capture_opts
.ifaces
, interface_options
, i
);
891 if_cap_query
= g_new(if_cap_query_t
, 1);
892 if_cap_query
->name
= interface_opts
->name
;
893 if_cap_query
->monitor_mode
= interface_opts
->monitor_mode
;
894 if_cap_query
->auth_username
= NULL
;
895 if_cap_query
->auth_password
= NULL
;
896 if_cap_queries
= g_list_prepend(if_cap_queries
, if_cap_query
);
898 if_cap_queries
= g_list_reverse(if_cap_queries
);
899 capability_hash
= capture_get_if_list_capabilities(if_cap_queries
, &err_str
, &err_str_secondary
, NULL
);
900 g_list_free_full(if_cap_queries
, g_free
);
901 for (i
= 0; i
< global_capture_opts
.ifaces
->len
; i
++) {
902 interface_options
*interface_opts
;
903 if_capabilities_t
*caps
;
904 interface_opts
= &g_array_index(global_capture_opts
.ifaces
, interface_options
, i
);
905 caps
= static_cast<if_capabilities_t
*>(g_hash_table_lookup(capability_hash
, interface_opts
->name
));
907 cmdarg_err("%s%s%s", err_str
, err_str_secondary
? "\n" : "", err_str_secondary
? err_str_secondary
: "");
909 g_free(err_str_secondary
);
910 ret_val
= WS_EXIT_INVALID_CAPABILITY
;
913 ret_val
= capture_opts_print_if_capabilities(caps
, interface_opts
,
915 if (ret_val
!= EXIT_SUCCESS
) {
922 g_hash_table_destroy(capability_hash
);
926 #ifdef DEBUG_STARTUP_TIME
927 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Calling fill_in_local_interfaces, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
929 splash_update(RA_INTERFACES
, NULL
, NULL
);
931 if (!global_commandline_info
.cf_name
&& !prefs
.capture_no_interface_load
) {
932 ssApp
->scanLocalInterfaces(nullptr);
935 capture_opts_trim_snaplen(&global_capture_opts
, MIN_PACKET_SIZE
);
936 capture_opts_trim_ring_num_files(&global_capture_opts
);
937 #endif /* HAVE_LIBPCAP */
939 /* Notify all registered modules that have had any of their preferences
940 changed either from one of the preferences file or from the command
941 line that their preferences have changed. */
942 #ifdef DEBUG_STARTUP_TIME
943 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Calling prefs_apply_all, elapsed time %" PRIu64
" us \n", g_get_monotonic_time() - start_time
);
946 ColorUtils::setScheme(prefs
.gui_color_scheme
);
947 ssApp
->emitAppSignal(StratosharkApplication::ColorsChanged
);
948 ssApp
->emitAppSignal(StratosharkApplication::PreferencesChanged
);
951 if ((global_capture_opts
.num_selected
== 0) &&
952 (prefs
.capture_device
!= NULL
)) {
955 for (i
= 0; i
< global_capture_opts
.all_ifaces
->len
; i
++) {
956 device
= &g_array_index(global_capture_opts
.all_ifaces
, interface_t
, i
);
957 if (!device
->hidden
&& strcmp(device
->display_name
, prefs
.capture_device
) == 0) {
958 device
->selected
= true;
959 global_capture_opts
.num_selected
++;
967 * Enabled and disabled protocols and heuristic dissectors as per
968 * command-line options.
970 if (!setup_enabled_and_disabled_protocols()) {
971 ret_val
= WS_EXIT_INVALID_OPTION
;
975 build_column_format_array(&CaptureFile::globalCapFile()->cinfo
, global_commandline_info
.prefs_p
->num_cols
, true);
976 ssApp
->emitAppSignal(StratosharkApplication::ColumnsChanged
); // We read "recent" widths above.
977 ssApp
->emitAppSignal(StratosharkApplication::RecentPreferencesRead
); // Must be emitted after PreferencesChanged.
979 ssApp
->setMonospaceFont(prefs
.gui_font_name
);
981 /* For update of WindowTitle (When use gui.window_title preference) */
982 main_w
->setWSWindowTitle();
984 if (!color_filters_init(&err_msg
, color_filter_add_cb
)) {
985 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "%s", err_msg
);
989 ssApp
->allSystemsGo();
990 ws_log(LOG_DOMAIN_MAIN
, LOG_LEVEL_INFO
, "Stratoshark is up and ready to go, elapsed time %.3fs", (float) (g_get_monotonic_time() - start_time
) / 1000000);
991 SimpleDialog::displayQueuedMessages(main_w
);
993 /* User could specify filename, or display filter, or both */
994 if (!dfilter
.isEmpty())
995 main_w
->filterPackets(dfilter
, false);
996 if (!cf_name
.isEmpty()) {
997 if (main_w
->openCaptureFile(cf_name
, read_filter
, in_file_type
)) {
999 /* Open stat windows; we do so after creating the main window,
1000 to avoid Qt warnings, and after successfully opening the
1001 capture file, so we know we have something to compute stats
1002 on, and after registering all dissectors, so that MATE will
1003 have registered its field array and we can have a tap filter
1004 with one of MATE's late-registered fields as part of the
1006 start_requested_stats();
1008 if (global_commandline_info
.go_to_packet
!= 0) {
1009 /* Jump to the specified frame number, kept for backward
1011 cf_goto_frame(CaptureFile::globalCapFile(), global_commandline_info
.go_to_packet
, false);
1012 } else if (global_commandline_info
.jfilter
!= NULL
) {
1013 dfilter_t
*jump_to_filter
= NULL
;
1014 /* try to compile given filter */
1015 if (!dfilter_compile(global_commandline_info
.jfilter
, &jump_to_filter
, &df_err
)) {
1016 // Similar code in MainWindow::mergeCaptureFile().
1017 QMessageBox::warning(main_w
, QObject::tr("Invalid Display Filter"),
1018 QObject::tr("The filter expression \"%1\" isn't a valid display filter.\n(%2).")
1019 .arg(global_commandline_info
.jfilter
, df_err
->msg
),
1021 df_error_free(&df_err
);
1023 /* Filter ok, jump to the first packet matching the filter
1024 conditions. Default search direction is forward, but if
1025 option j was given, search backwards */
1026 cf_find_packet_dfilter(CaptureFile::globalCapFile(), jump_to_filter
, global_commandline_info
.jump_backwards
, false);
1033 if (global_commandline_info
.start_capture
) {
1034 if (global_capture_opts
.save_file
!= NULL
) {
1035 /* Save the directory name for future file dialogs. */
1036 /* (get_dirname overwrites filename) */
1037 char *s
= g_strdup(global_capture_opts
.save_file
);
1038 set_last_open_dir(get_dirname(s
));
1041 /* "-k" was specified; start a capture. */
1042 check_and_warn_user_startup();
1044 /* If no user interfaces were specified on the command line,
1045 copy the list of selected interfaces to the set of interfaces
1046 to use for this capture. */
1047 if (global_capture_opts
.ifaces
->len
== 0)
1048 collect_ifaces(&global_capture_opts
);
1049 CaptureFile::globalCapFile()->window
= main_w
;
1050 if (capture_start(&global_capture_opts
, global_commandline_info
.capture_comments
,
1051 main_w
->captureSession(), main_w
->captureInfoData(),
1052 main_window_update
)) {
1053 /* The capture started. Open stat windows; we do so after creating
1054 the main window, to avoid GTK warnings, and after successfully
1055 opening the capture file, so we know we have something to compute
1056 stats on, and after registering all dissectors, so that MATE will
1057 have registered its field array and we can have a tap filter with
1058 one of MATE's late-registered fields as part of the filter. */
1059 start_requested_stats();
1062 /* if the user didn't supply a capture filter, use the one to filter out remote connections like SSH */
1063 if (!global_commandline_info
.start_capture
&& !global_capture_opts
.default_options
.cfilter
) {
1064 global_capture_opts
.default_options
.cfilter
= g_strdup(get_conn_cfilter());
1067 #endif /* HAVE_LIBPCAP */
1069 // UAT and UI settings files used in configuration profiles which are used
1070 // in Qt dialogs are not registered during startup because they only get
1071 // loaded when the dialog is shown. Register them here.
1072 profile_register_persconffile("io_graphs");
1073 profile_register_persconffile("import_hexdump.json");
1074 profile_register_persconffile("remote_hosts.json");
1076 profile_store_persconffiles(false);
1078 // If the ssApp->exec() event loop exits cleanly, we call
1079 // StratosharkApplication::cleanup().
1080 ret_val
= ssApp
->exec();
1083 // Many widgets assume that they always have valid epan data, so this
1084 // must be called before epan_cleanup().
1085 // XXX We need to clean up the Lua GUI here. We currently paper over
1086 // this in FunnelStatistics::~FunnelStatistics, which leaks memory.
1094 Dot11DecryptDestroyContext(&dot11decrypt_ctx
);
1096 ws_cleanup_sockets();
1099 /* For some unknown reason, the "atexit()" call in "create_console()"
1100 doesn't arrange that "destroy_console()" be called when we exit,
1101 so we call it here if a console was created. */
1107 capture_opts_cleanup(&global_capture_opts
);
1109 col_cleanup(&CaptureFile::globalCapFile()->cinfo
);
1113 commandline_options_free();
1114 exit_application(ret_val
);