Skip to content

Commit

Permalink
Merge pull request #90 from klemensn/clonable-bpf
Browse files Browse the repository at this point in the history
CMake, BSD: Fix BPF detection, use clonable device
  • Loading branch information
ofalk authored Apr 18, 2023
2 parents a680ec7 + ced4593 commit 8a0163b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ find_package(TCL)

include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckStructHasMember)
include(CheckSymbolExists)
include(CheckTypeSize)
Expand Down Expand Up @@ -39,7 +40,7 @@ if(UNIX)
foreach (header strings.h
unistd.h sys/bufmod.h sys/dlpi.h sys/dlpihdr.h sys/dlpi_ext.h
sys/ioctl.h sys/mib.h sys/ndd_var.h sys/socket.h sys/sockio.h
sys/sysctl.h sys/time.h sys/stat.h net/bpf.h net/if.h net/if_var.h
sys/sysctl.h sys/time.h sys/stat.h net/if.h net/if_var.h
net/if_arp.h net/if_dl.h net/pfilt.h
net/pfvar.h net/radix.h net/raw.h net/route.h netinet/in_var.h
net/if_tun.h linux/if_tun.h netinet/ip_fw.h linux/ip_fw.h
Expand All @@ -51,6 +52,8 @@ if(UNIX)
string(REGEX REPLACE "\\.|/" "_" var ${var})
check_include_file(${header} ${var})
endforeach ()

check_include_files("sys/types.h;net/bpf.h" HAVE_NET_BPF_H)
endif()

set(CMAKE_REQUIRED_LIBRARIES )
Expand All @@ -72,10 +75,6 @@ if (UNIX)
check_type_size("struct sockaddr_in6" HAVE_SOCKADDR_IN6 LANGUAGE C)
set(CMAKE_EXTRA_INCLUDE_FILES )

find_file(HAVE_BSD_BPFH
NAMES /dev/bpf0
DOC "Check for the Berkeley Packet Filter")

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
file(STRINGS /proc/sys/kernel/ostype PROCFS)
message(STATUS "${PROCFS}")
Expand Down Expand Up @@ -191,10 +190,10 @@ if (HAVE_IPHLPAPI_H)
# list(APPEND PLATFORM_SOURCES src/eth-win32.c)
elseif(HAVE_NET_PFILT_H)
list(APPEND PLATFORM_SOURCES src/eth-pfilt.c)
elseif(HAVE_BSD_BPF)
list(APPEND PLATFORM_SOURCES src/eth-bsd.c)
elseif(HAVE_LINUX_PF_PACKET)
list(APPEND PLATFORM_SOURCES src/eth-linux.c)
elseif(HAVE_NET_BPF_H)
list(APPEND PLATFORM_SOURCES src/eth-bsd.c)
elseif(HAVE_NET_RAW_H)
list(APPEND PLATFORM_SOURCES src/eth-snoop.c)
elseif(HAVE_SYS_NDD_VAR_H)
Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -15506,7 +15506,7 @@ printf %s "checking for Berkeley Packet Filter... " >&6; }
then :
printf %s "(cached) " >&6
else $as_nop
if test -c /dev/bpf0 ; then
if test -c /dev/bpf ; then
ac_cv_dnet_bsd_bpf=yes
else
ac_cv_dnet_bsd_bpf=no
Expand Down
2 changes: 1 addition & 1 deletion m4/acinclude.m4
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ dnl
AC_DEFUN([AC_DNET_BSD_BPF],
[AC_MSG_CHECKING(for Berkeley Packet Filter)
AC_CACHE_VAL(ac_cv_dnet_bsd_bpf,
if test -c /dev/bpf0 ; then
if test -c /dev/bpf ; then
ac_cv_dnet_bsd_bpf=yes
else
ac_cv_dnet_bsd_bpf=no
Expand Down
9 changes: 1 addition & 8 deletions src/eth-bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,11 @@ eth_t *
eth_open(const char *device)
{
struct ifreq ifr;
char file[32];
eth_t *e;
int i;

if ((e = calloc(1, sizeof(*e))) != NULL) {
for (i = 0; i < 128; i++) {
snprintf(file, sizeof(file), "/dev/bpf%d", i);
e->fd = open(file, O_WRONLY);
if (e->fd != -1 || errno != EBUSY)
break;
}
if (e->fd < 0)
if ((e->fd = open("/dev/bpf", O_WRONLY)) < 0)
return (eth_close(e));

memset(&ifr, 0, sizeof(ifr));
Expand Down

0 comments on commit 8a0163b

Please sign in to comment.