Skip to content

Commit

Permalink
Merge pull request #94 from CESNET/bsd-support
Browse files Browse the repository at this point in the history
BSD support
  • Loading branch information
Lukas955 authored Aug 2, 2024
2 parents dd0b57b + 8bf2adc commit 10833f2
Show file tree
Hide file tree
Showing 30 changed files with 225 additions and 28 deletions.
40 changes: 40 additions & 0 deletions CMakeModules/FindLibEpollShim.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# LIBEPOLLSHIM_FOUND - System has libepoll-shim
# LIBEPOLLSHIM_INCLUDE_DIRS - The libepoll-shim include directories
# LIBEPOLLSHIM_LIBRARIES - The libraries needed to use libepoll-shim
# LIBEPOLLSHIM_DEFINITIONS - Compiler switches required for using libepoll-shim

# use pkg-config to get the directories and then use these values
# in the find_path() and find_library() calls
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_EPOLLSHIM QUIET epoll-shim)
set(LIBEPOLLSHIM_DEFINITIONS ${PC_EPOLLSHIM_CFLAGS_OTHER})
endif()

find_path(
EPOLLSHIM_INCLUDE_DIR libepoll-shim/sys/epoll.h
HINTS ${PC_EPOLLSHIM_INCLUDEDIR} ${PC_EPOLLSHIM_INCLUDE_DIRS}
PATH_SUFFIXES include
)
find_library(
EPOLLSHIM_LIBRARY NAMES epoll-shim libepoll-shim
HINTS ${PC_EPOLLSHIM_LIBDIR} ${PC_EPOLLSHIM_LIBRARY_DIRS}
PATH_SUFFIXES lib lib64
)

if (PC_EPOLLSHIM_VERSION)
# Version extracted from pkg-config
set(EPOLLSHIM_VERSION_STRING ${PC_EPOLLSHIM_VERSION})
endif()

# handle the QUIETLY and REQUIRED arguments and set LIBEPOLLSHIM_FOUND to TRUE
# if all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibEpollShim
REQUIRED_VARS EPOLLSHIM_LIBRARY EPOLLSHIM_INCLUDE_DIR
VERSION_VAR EPOLLSHIM_VERSION_STRING
)

set(LIBEPOLLSHIM_LIBRARIES ${EPOLLSHIM_LIBRARY})
set(LIBEPOLLSHIM_INCLUDE_DIRS ${EPOLLSHIM_INCLUDE_DIR}/libepoll-shim)
mark_as_advanced(EPOLLSHIM_INCLUDE_DIR EPOLLSHIM_LIBRARY)
4 changes: 4 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ target_link_libraries(ipfixcol2base

# Build IPFIXCOL2 exacutable with all symbols from the base library
set(BASE_LIB -Wl,--whole-archive ipfixcol2base -Wl,--no-whole-archive)
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "OpenBSD")
set(BASE_LIB ${BASE_LIB} -rdynamic)
endif()

add_executable(ipfixcol2 main.cpp)
target_link_libraries(ipfixcol2 ${BASE_LIB})
set_target_properties(ipfixcol2 PROPERTIES # by default, hide all symbols
Expand Down
1 change: 1 addition & 0 deletions src/core/configurator/cpipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/


#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
Expand Down
12 changes: 12 additions & 0 deletions src/core/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <pthread_np.h>
#else
#include <sys/prctl.h>
#endif

#include "context.h"
#include "extension.h"
Expand Down Expand Up @@ -640,13 +644,17 @@ thread_set_name(const char *ident)
strncpy(name, ident, size - 1);
name[size - 1] = '\0';

#if defined(__OpenBSD__) || defined(__FreeBSD__)
pthread_set_name_np(pthread_self(), name);
#else
int rc = prctl(PR_SET_NAME, name, 0, 0, 0);
if (rc == -1) {
const char *err_str;
ipx_strerror(errno, err_str);
IPX_WARNING(comp_str, "Failed to set the name of a thread. prctl() failed: %s",
err_str);
}
#endif
}

/**
Expand All @@ -656,6 +664,9 @@ thread_set_name(const char *ident)
static inline void
thread_get_name(char ident[16])
{
#if defined(__OpenBSD__) || defined(__FreeBSD__)
pthread_get_name_np(pthread_self(), ident, 16);
#else
int rc = prctl(PR_GET_NAME, ident, 0, 0, 0);
if (rc == -1) {
const char *err_str;
Expand All @@ -664,6 +675,7 @@ thread_get_name(char ident[16])
err_str);
ident[0] = '\0';
}
#endif
}

int
Expand Down
3 changes: 2 additions & 1 deletion src/core/message_ipfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
*/

#include <assert.h>
#include <ipfixcol2.h>
#include <libfds.h>
#include "message_base.h"
Expand Down Expand Up @@ -190,4 +191,4 @@ ipx_msg_ipfix_add_drec_ref(struct ipx_msg_ipfix **msg_ref)
const size_t offset = msg->rec_info.cnt_valid * msg->rec_info.rec_size;
msg->rec_info.cnt_valid++;
return ((struct ipx_ipfix_record *) (((uint8_t *) msg->recs) + offset));
}
}
3 changes: 2 additions & 1 deletion src/core/message_terminate.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
*/

#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "message_terminate.h"
Expand Down Expand Up @@ -84,4 +85,4 @@ enum ipx_msg_terminate_type
ipx_msg_terminate_get_type(const ipx_msg_terminate_t *msg)
{
return msg->type;
}
}
3 changes: 2 additions & 1 deletion src/core/netflow2ipfix/netflow5.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <endian.h>
Expand Down Expand Up @@ -552,4 +553,4 @@ void
ipx_nf5_conv_verb(ipx_nf5_conv_t *conv, enum ipx_verb_level v_new)
{
conv->conf.vlevel = v_new;
}
}
3 changes: 2 additions & 1 deletion src/core/netflow2ipfix/netflow9.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <assert.h>
#include <stdint.h>
#include <endian.h>
#include <assert.h>
Expand Down Expand Up @@ -1274,4 +1275,4 @@ void
ipx_nf9_conv_verb(ipx_nf9_conv_t *conv, enum ipx_verb_level v_new)
{
conv->vlevel = v_new;
}
}
3 changes: 2 additions & 1 deletion src/core/odid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
*/

#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
Expand Down Expand Up @@ -433,4 +434,4 @@ ipx_orange_print(const ipx_orange_t *range)
break;
}
}
}
}
31 changes: 27 additions & 4 deletions src/core/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,36 @@
*
*/

// Get GNU specific basename() function
#define _GNU_SOURCE
#include <string.h>

#include <stdint.h>
#include <ipfixcol2.h>
#include <stdlib.h>
#include <inttypes.h>

/**
* \brief Return the name part of a file path, i.e. the part after the last /
* \param path The file path
* \return The name part of the path or NULL if path is NULL
*/
static const char *get_basename(const char *path)
{
if (path == NULL) {
return NULL;
}

const char *res = path;
const char *p = path;
while (*p != '\0') {
if (*p == '/') {
res = p + 1;
}
p++;
}

return res;
}

/**
* \brief Create a source description string from a Network Session structure
*
Expand Down Expand Up @@ -166,7 +187,9 @@ ipx_session_new_file(const char *file_path)
return NULL;
}

res->ident = basename(res->file.file_path); // GNU specific basename()
res->ident = res->file.file_path;

res->ident = (char *) get_basename(res->file.file_path);
if (!res->ident) {
free(res);
return NULL;
Expand All @@ -190,4 +213,4 @@ ipx_session_destroy(struct ipx_session *session)
}

free(session);
}
}
18 changes: 17 additions & 1 deletion src/plugins/input/fds/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@
#include "Exception.hpp"
#include "Reader.hpp"

static inline bool in6_is_addr_v4mapped(const uint8_t addr[16])
{
return
addr[0] == 0 &&
addr[1] == 0 &&
addr[2] == 0 &&
addr[3] == 0 &&
addr[4] == 0 &&
addr[5] == 0 &&
addr[6] == 0 &&
addr[7] == 0 &&
addr[8] == 0 &&
addr[9] == 0 &&
addr[10] == 0xFF &&
addr[11] == 0xFF;
}

Reader::Reader(ipx_ctx_t *ctx, const fds_config *cfg, const char *path)
: m_ctx(ctx), m_cfg(cfg)
Expand Down Expand Up @@ -97,7 +113,7 @@ Reader::session_from_sid(fds_file_sid_t sid)
memset(&session_net, 0, sizeof(session_net));
session_net.port_src = desc->port_src;
session_net.port_dst = desc->port_dst;
if (IN6_IS_ADDR_V4MAPPED(desc->ip_src) && IN6_IS_ADDR_V4MAPPED(desc->ip_dst)) {
if (in6_is_addr_v4mapped(desc->ip_src) && in6_is_addr_v4mapped(desc->ip_dst)) {
session_net.l3_proto = AF_INET;
session_net.addr_src.ipv4 = *reinterpret_cast<const struct in_addr *>(&desc->ip_src[12]);
session_net.addr_dst.ipv4 = *reinterpret_cast<const struct in_addr *>(&desc->ip_dst[12]);
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/input/fds/fds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ file_is_dir(const char *filename)
void
file_list_init(Instance *inst, const char *pattern)
{
#ifndef GLOB_TILDE_CHECK
#define GLOB_TILDE_CHECK GLOB_TILDE
#endif
int glob_flags = GLOB_MARK | GLOB_BRACE | GLOB_TILDE_CHECK;
size_t file_cnt;
int ret;
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/input/ipfix/ipfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ static inline int
files_list_get(ipx_ctx_t *ctx, const char *pattern, glob_t *list)
{
size_t file_cnt;
#ifndef GLOB_TILDE_CHECK
#define GLOB_TILDE_CHECK GLOB_TILDE
#endif
int glob_flags = GLOB_MARK | GLOB_BRACE | GLOB_TILDE_CHECK;
int rc = glob(pattern, glob_flags, NULL, list);

Expand Down
10 changes: 10 additions & 0 deletions src/plugins/input/tcp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ add_library(tcp-input MODULE
config.h
)

if (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "OpenBSD")
find_package(LibEpollShim REQUIRED)
include_directories(
${LIBEPOLLSHIM_INCLUDE_DIRS}
)
target_link_libraries(tcp-input
${LIBEPOLLSHIM_LIBRARIES}
)
endif()

install(
TARGETS tcp-input
LIBRARY DESTINATION "${INSTALL_DIR_LIB}/ipfixcol2/"
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/input/udp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ add_library(udp-input MODULE
config.h
)

if (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "OpenBSD")
find_package(LibEpollShim REQUIRED)
include_directories(
${LIBEPOLLSHIM_INCLUDE_DIRS}
)
target_link_libraries(udp-input
${LIBEPOLLSHIM_LIBRARIES}
)
endif()

install(
TARGETS udp-input
LIBRARY DESTINATION "${INSTALL_DIR_LIB}/ipfixcol2/"
Expand Down
Loading

0 comments on commit 10833f2

Please sign in to comment.