Skip to content

Commit

Permalink
Merge pull request #164 from audetto/pcap
Browse files Browse the repository at this point in the history
pcap: drop requirement.
  • Loading branch information
audetto committed May 4, 2024
2 parents 81ad66c + da84dfb commit f710c9a
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 41 deletions.
44 changes: 26 additions & 18 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ pkg_search_module(YAML REQUIRED yaml-0.1)
pkg_search_module(MINIZIP REQUIRED minizip)
pkg_search_module(SLIRP slirp)

# if slirp is not found, we will try pcap
# if it is not found either, a dummy pcap will be compiled
# but network will be badly affected

if ("${SLIRP_FOUND}" STREQUAL "")
message(WARNING "'libslirp' not found. Will use 'libpcap' instead")
endif()
message(WARNING "'libslirp' not found: will try 'libpcap' instead")

pkg_search_module(PCAP libpcap)
if ("${PCAP_FOUND}" STREQUAL "")
# old versions of pcap do not work with pkg-config
# this is necessary on Rapsberri Pi OS
execute_process(COMMAND pcap-config --cflags
OUTPUT_VARIABLE PCAP_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE STATUS)
if ("${STATUS}" STREQUAL "0")
message("Found 'libpcap' via pcap-config")
else()
message(FATAL_ERROR "Cannot locate 'libpcap-dev'")
pkg_search_module(PCAP libpcap)
if ("${PCAP_FOUND}" STREQUAL "")
# old versions of pcap do not work with pkg-config
# this is necessary on Rapsberri Pi OS
execute_process(COMMAND pcap-config --cflags
OUTPUT_VARIABLE PCAP_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE STATUS)
if ("${STATUS}" STREQUAL "0")
message("Found 'libpcap' via pcap-config")
execute_process(COMMAND pcap-config --libs
OUTPUT_VARIABLE PCAP_LIBRARIES
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(PCAP_FOUND "1")
endif()
endif()
execute_process(COMMAND pcap-config --libs
OUTPUT_VARIABLE PCAP_LIBRARIES
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()

find_package(Boost REQUIRED)

set(SOURCE_FILES
Tfe/tfearch.cpp
Tfe/tfesupp.cpp
Tfe/NetworkBackend.cpp
Tfe/PCapBackend.cpp
Expand Down Expand Up @@ -227,6 +229,12 @@ add_library(appleii STATIC
${HEADER_FILES}
)

if ("${PCAP_FOUND}" STREQUAL "1")
target_sources(appleii PRIVATE Tfe/tfearch.cpp)
else()
target_sources(appleii PRIVATE linux/duplicates/tfearch.cpp)
endif()

target_include_directories(appleii PRIVATE
${YAML_INCLUDE_DIRS}
${PCAP_INCLUDE_DIRS}
Expand Down
45 changes: 24 additions & 21 deletions source/frontends/sdl/imgui/sdlsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,37 +301,40 @@ namespace sa2

const UINT uthernetSlot = SLOT3;

const std::string current_interface = PCapBackend::GetRegistryInterface(uthernetSlot);

if (ImGui::BeginCombo("pcap", current_interface.c_str()))
if (PCapBackend::tfe_is_npcap_loaded())
{
std::vector<std::string> ifaces;
if (PCapBackend::tfe_enumadapter_open())
{
std::string name;
std::string description;
const std::string current_interface = PCapBackend::GetRegistryInterface(uthernetSlot);

while (PCapBackend::tfe_enumadapter(name, description))
if (ImGui::BeginCombo("pcap", current_interface.c_str()))
{
std::vector<std::string> ifaces;
if (PCapBackend::tfe_enumadapter_open())
{
ifaces.push_back(name);
}
PCapBackend::tfe_enumadapter_close();
std::string name;
std::string description;

for (const auto & iface : ifaces)
{
const bool isSelected = iface == current_interface;
if (ImGui::Selectable(iface.c_str(), isSelected))
while (PCapBackend::tfe_enumadapter(name, description))
{
// the following line interacts with tfe_enumadapter, so we must run it outside the above loop
PCapBackend::SetRegistryInterface(uthernetSlot, iface);
ifaces.push_back(name);
}
if (isSelected)
PCapBackend::tfe_enumadapter_close();

for (const auto & iface : ifaces)
{
ImGui::SetItemDefaultFocus();
const bool isSelected = iface == current_interface;
if (ImGui::Selectable(iface.c_str(), isSelected))
{
// the following line interacts with tfe_enumadapter, so we must run it outside the above loop
PCapBackend::SetRegistryInterface(uthernetSlot, iface);
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
}
ImGui::EndCombo();
}
ImGui::EndCombo();
}

bool virtualDNS = Uthernet2::GetRegistryVirtualDNS(uthernetSlot);
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/sdl/sdlframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ namespace sa2
#ifdef U2_USE_SLIRP
return std::make_shared<SlirpBackend>(myPortFwds);
#else
return std::make_shared<PCapBackend>(interfaceName);
return common2::GNUFrame::CreateNetworkBackend(interfaceName);
#endif
}

Expand Down
1 change: 1 addition & 0 deletions source/linux/config.h.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#cmakedefine SLIRP_FOUND "@SLIRP_FOUND@"
#cmakedefine PCAP_FOUND "@PCAP_FOUND@"
139 changes: 139 additions & 0 deletions source/linux/duplicates/tfearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* tfearch.c - TFE ("The final ethernet") emulation,
* architecture-dependant stuff
*
* Written by
* Spiro Trikaliotis <Spiro.Trikaliotis@gmx.de>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* 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., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/


#include <StdAfx.h> // this is necessary in linux, but in MSVC windows.h MUST come after winsock2.h (from pcap.h above)
#include "Tfe/tfearch.h"
#include "../Log.h"


/** #define TFE_DEBUG_ARCH 1 **/
/* #define TFE_DEBUG_FRAMES - might be defined in TFE.H! */

int tfe_arch_enumadapter_open(void)
{
return 0;
}

int tfe_arch_enumadapter(std::string & name, std::string & description)
{
return 0;
}

int tfe_arch_enumadapter_close(void)
{
return 0;
}

pcap_t * TfePcapOpenAdapter(const std::string & interface_name)
{
return NULL;
}

void TfePcapCloseAdapter(pcap_t * TfePcapFP)
{
}

/* ------------------------------------------------------------------------- */
/* the architecture-dependend functions */

void tfe_arch_set_mac( const BYTE mac[6] )
{
#if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES)
if(g_fh) fprintf( g_fh, "New MAC address set: %02X:%02X:%02X:%02X:%02X:%02X.\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
#endif
}

void tfe_arch_set_hashfilter(const DWORD hash_mask[2])
{
#if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES)
if(g_fh) fprintf( g_fh, "New hash filter set: %08X:%08X.\n",
hash_mask[1], hash_mask[0]);
#endif
}

void tfe_arch_recv_ctl( int bBroadcast, /* broadcast */
int bIA, /* individual address (IA) */
int bMulticast, /* multicast if address passes the hash filter */
int bCorrect, /* accept correct frames */
int bPromiscuous, /* promiscuous mode */
int bIAHash /* accept if IA passes the hash filter */
)
{
#if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES)
if(g_fh) {
fprintf( g_fh, "tfe_arch_recv_ctl() called with the following parameters:" );
fprintf( g_fh, "\tbBroadcast = %s", bBroadcast ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbIA = %s", bIA ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbMulticast = %s", bMulticast ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbCorrect = %s", bCorrect ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbPromiscuous = %s", bPromiscuous ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbIAHash = %s", bIAHash ? "TRUE" : "FALSE" );
fprintf( g_fh, "\n" );
}
#endif
}

void tfe_arch_line_ctl(int bEnableTransmitter, int bEnableReceiver )
{
#if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES)
if(g_fh) {
fprintf( g_fh, "tfe_arch_line_ctl() called with the following parameters:" );
fprintf( g_fh, "\tbEnableTransmitter = %s", bEnableTransmitter ? "TRUE" : "FALSE" );
fprintf( g_fh, "\tbEnableReceiver = %s", bEnableReceiver ? "TRUE" : "FALSE" );
fprintf( g_fh, "\n" );
}
#endif
}

void tfe_arch_transmit(pcap_t * TfePcapFP,
int txlength, /* Frame length */
BYTE *txframe /* Pointer to the frame to be transmitted */
)
{
}

int tfe_arch_receive(pcap_t * TfePcapFP,
const int size , /* Size of buffer */
BYTE *pbuffer /* where to store a frame */
)
{
return -1;
}

const char * tfe_arch_lib_version()
{
return 0;
}

int tfe_arch_is_npcap_loaded()
{
return 0;
}

//#endif /* #ifdef HAVE_TFE */
4 changes: 4 additions & 0 deletions source/linux/linuxframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "Core.h"
#include "SaveState.h"

#ifndef U2_USE_SLIRP
#include "Tfe/PCapBackend.h"
#endif

void LinuxFrame::FrameDrawDiskLEDS()
{
}
Expand Down
1 change: 0 additions & 1 deletion source/linux/raspbian.list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ libevdev-dev
libsdl2-image-dev
libsdl2-dev
libgles-dev
libpcap-dev
libslirp-dev

0 comments on commit f710c9a

Please sign in to comment.