From 298012359b52a9bf1ca22e0d1bedf23ec3e7680f Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Mon, 22 Feb 2016 16:28:15 +0100 Subject: [PATCH] extcap: move windows functions into extcap-base Change-Id: Iec7fed027a24992afd673b09c32470af51739ae5 Reviewed-on: https://code.wireshark.org/review/14075 Reviewed-by: Roland Knall --- CMakeLists.txt | 4 ++ extcap/Makefile.common | 11 ++++-- extcap/androiddump.c | 46 ----------------------- extcap/extcap-base.c | 85 ++++++++++++++++++++++++++++++++++++++++++ extcap/extcap-base.h | 7 ++++ extcap/randpktdump.c | 43 --------------------- extcap/sshdump.c | 44 ---------------------- 7 files changed, 103 insertions(+), 137 deletions(-) create mode 100644 extcap/extcap-base.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d39c3086ec..dca9ee746f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -209,6 +209,7 @@ include_directories( ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/epan ${CMAKE_SOURCE_DIR}/tools/lemon + ${CMAKE_SOURCE_DIR}/extcap ) include( CMakeOptions.txt ) @@ -2367,6 +2368,7 @@ if(BUILD_androiddump) endif() set(androiddump_FILES extcap/androiddump.c + extcap/extcap-base.c ) add_executable(androiddump WIN32 ${androiddump_FILES}) @@ -2387,6 +2389,7 @@ if(BUILD_sshdump AND LIBSSH_FOUND) endif() set(sshdump_FILES extcap/sshdump.c + extcap/extcap-base.c ) add_executable(sshdump WIN32 ${sshdump_FILES}) @@ -2407,6 +2410,7 @@ if(BUILD_randpktdump) ) set(randpktdump_FILES extcap/randpktdump.c + extcap/extcap-base.c ) add_executable(randpktdump WIN32 ${randpktdump_FILES}) diff --git a/extcap/Makefile.common b/extcap/Makefile.common index a2f26a69b1..1f2ac07537 100644 --- a/extcap/Makefile.common +++ b/extcap/Makefile.common @@ -23,15 +23,18 @@ # androiddump specifics androiddump_SOURCES = \ - androiddump.c + androiddump.c \ + extcap-base.c # randpktdump specifics randpktdump_SOURCES = \ - randpktdump.c + randpktdump.c \ + extcap-base.c # sshdump specifics sshdump_SOURCES = \ - sshdump.c + sshdump.c \ + extcap-base.c noinst_HEADERS = \ - extcap-base.h + extcap-base.c diff --git a/extcap/androiddump.c b/extcap/androiddump.c index e5b68fbfb7..a862c9ec76 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -104,7 +104,6 @@ #define PACKET_LENGTH 65535 #define verbose_print(...) { if (verbose) printf(__VA_ARGS__); } -#define errmsg_print(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } enum { EXTCAP_BASE_OPTIONS_ENUM, @@ -1990,52 +1989,7 @@ static int capture_android_logcat(char *interface, char *fifo, /*============================================================================*/ -#ifdef _WIN32 -BOOLEAN IsHandleRedirected(DWORD handle) -{ - HANDLE h = GetStdHandle(handle); - if (h) { - BY_HANDLE_FILE_INFORMATION fi; - if (GetFileInformationByHandle(h, &fi)) { - return TRUE; - } - } - return FALSE; -} -static void attach_parent_console() -{ - BOOL outRedirected, errRedirected; - - outRedirected = IsHandleRedirected(STD_OUTPUT_HANDLE); - errRedirected = IsHandleRedirected(STD_ERROR_HANDLE); - - if (outRedirected && errRedirected) { - /* Both standard output and error handles are redirected. - * There is no point in attaching to parent process console. - */ - return; - } - - if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) { - /* Console attach failed. */ - return; - } - - /* Console attach succeeded */ - if (outRedirected == FALSE) { - if (!freopen("CONOUT$", "w", stdout)) { - errmsg_print("WARNING: Cannot redirect to stdout."); - } - } - - if (errRedirected == FALSE) { - if (!freopen("CONOUT$", "w", stderr)) { - errmsg_print("WARNING: Cannot redirect to strerr."); - } - } -} -#endif /*----------------------------------------------------------------------------*/ /* Android Wifi Tcpdump */ diff --git a/extcap/extcap-base.c b/extcap/extcap-base.c new file mode 100644 index 0000000000..51d2061469 --- /dev/null +++ b/extcap/extcap-base.c @@ -0,0 +1,85 @@ +/* extcap_base.c + * Base function for extcaps + * + * Copyright 2015, Dario Lombardo + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * 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 "extcap-base.h" + +#ifdef _WIN32 +BOOLEAN IsHandleRedirected(DWORD handle) +{ + HANDLE h = GetStdHandle(handle); + if (h) { + BY_HANDLE_FILE_INFORMATION fi; + if (GetFileInformationByHandle(h, &fi)) { + return TRUE; + } + } + return FALSE; +} + +void attach_parent_console() +{ + BOOL outRedirected, errRedirected; + + outRedirected = IsHandleRedirected(STD_OUTPUT_HANDLE); + errRedirected = IsHandleRedirected(STD_ERROR_HANDLE); + + if (outRedirected && errRedirected) { + /* Both standard output and error handles are redirected. + * There is no point in attaching to parent process console. + */ + return; + } + + if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) { + /* Console attach failed. */ + return; + } + + /* Console attach succeeded */ + if (outRedirected == FALSE) { + if (!freopen("CONOUT$", "w", stdout)) { + errmsg_print("WARNING: Cannot redirect to stdout."); + } + } + + if (errRedirected == FALSE) { + if (!freopen("CONOUT$", "w", stderr)) { + errmsg_print("WARNING: Cannot redirect to strerr."); + } + } +} +#endif + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: t + * End: + * + * vi: set shiftwidth=4 tabstop=4 noexpandtab: + * :indentSize=4:tabSize=4:noTabs=false: + */ diff --git a/extcap/extcap-base.h b/extcap/extcap-base.h index 83500e449e..07617b15df 100644 --- a/extcap/extcap-base.h +++ b/extcap/extcap-base.h @@ -83,6 +83,13 @@ { "extcap-capture-filter", required_argument, NULL, OPT_CAPTURE_FILTER}, \ { "fifo", required_argument, NULL, OPT_FIFO} \ +#ifdef _WIN32 +BOOLEAN IsHandleRedirected(DWORD handle); +void attach_parent_console(); +#endif + +#define errmsg_print(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } + #endif /* diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index c808fbaf74..7651b6635c 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -34,7 +34,6 @@ #define RANDPKTDUMP_VERSION_RELEASE 0 #define verbose_print(...) { if (verbose) printf(__VA_ARGS__); } -#define errmsg_print(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } static gboolean verbose = TRUE; @@ -63,48 +62,6 @@ static struct option longopts[] = { { 0, 0, 0, 0 } }; -#ifdef _WIN32 -BOOLEAN IsHandleRedirected(DWORD handle) -{ - HANDLE h = GetStdHandle(handle); - if (h) { - BY_HANDLE_FILE_INFORMATION fi; - if (GetFileInformationByHandle(h, &fi)) { - return TRUE; - } - } - return FALSE; -} - -static void attach_parent_console() -{ - BOOL outRedirected, errRedirected; - - outRedirected = IsHandleRedirected(STD_OUTPUT_HANDLE); - errRedirected = IsHandleRedirected(STD_ERROR_HANDLE); - - if (outRedirected && errRedirected) { - /* Both standard output and error handles are redirected. - * There is no point in attaching to parent process console. - */ - return; - } - - if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) { - /* Console attach failed. */ - return; - } - - /* Console attach succeeded */ - if (outRedirected == FALSE) { - freopen("CONOUT$", "w", stdout); - } - - if (errRedirected == FALSE) { - freopen("CONOUT$", "w", stderr); - } -} -#endif static void help(const char* binname) { diff --git a/extcap/sshdump.c b/extcap/sshdump.c index c57de0bdd2..da7df908dd 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -68,7 +68,6 @@ #define DEFAULT_CAPTURE_BIN "dumpcap" #define verbose_print(...) { if (verbose) printf(__VA_ARGS__); } -#define errmsg_print(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } static gboolean verbose = FALSE; @@ -544,49 +543,6 @@ static char* concat_filters(const char* extcap_filter, const char* remote_filter return g_strdup_printf("(%s) and (%s)", extcap_filter, remote_filter); } -#ifdef _WIN32 -BOOLEAN IsHandleRedirected(DWORD handle) -{ - HANDLE h = GetStdHandle(handle); - if (h) { - BY_HANDLE_FILE_INFORMATION fi; - if (GetFileInformationByHandle(h, &fi)) { - return TRUE; - } - } - return FALSE; -} - -static void attach_parent_console() -{ - BOOL outRedirected, errRedirected; - - outRedirected = IsHandleRedirected(STD_OUTPUT_HANDLE); - errRedirected = IsHandleRedirected(STD_ERROR_HANDLE); - - if (outRedirected && errRedirected) { - /* Both standard output and error handles are redirected. - * There is no point in attaching to parent process console. - */ - return; - } - - if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) { - /* Console attach failed. */ - return; - } - - /* Console attach succeeded */ - if (outRedirected == FALSE) { - freopen("CONOUT$", "w", stdout); - } - - if (errRedirected == FALSE) { - freopen("CONOUT$", "w", stderr); - } -} -#endif - int main(int argc, char **argv) { int result;