extcap: move windows functions into extcap-base

Change-Id: Iec7fed027a24992afd673b09c32470af51739ae5
Reviewed-on: https://code.wireshark.org/review/14075
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Dario Lombardo 2016-02-22 16:28:15 +01:00 committed by Roland Knall
parent 3b9306e91b
commit 298012359b
7 changed files with 103 additions and 137 deletions

View File

@ -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})

View File

@ -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

View File

@ -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 */

85
extcap/extcap-base.c Normal file
View File

@ -0,0 +1,85 @@
/* extcap_base.c
* Base function for extcaps
*
* Copyright 2015, Dario Lombardo
*
* 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 "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:
*/

View File

@ -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
/*

View File

@ -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)
{

View File

@ -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;