From 5f511fbd69ebc5a981634b2d92230b8c58a9e553 Mon Sep 17 00:00:00 2001 From: Florian Reimold <11774314+FlorianReimold@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:15:33 +0200 Subject: [PATCH] Added npcap_helpers passthrough to API (#8) --- ecaludp/CMakeLists.txt | 4 +- .../ecaludp/npcap_helpers.h | 59 +++++++++++++++++++ ecaludp/src/npcap_helpers.cpp | 34 +++++++++++ samples/integration_test_npcap/src/main.cpp | 4 ++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 ecaludp/include_with_udpcap/ecaludp/npcap_helpers.h create mode 100644 ecaludp/src/npcap_helpers.cpp diff --git a/ecaludp/CMakeLists.txt b/ecaludp/CMakeLists.txt index 1123f58..6d07ffc 100644 --- a/ecaludp/CMakeLists.txt +++ b/ecaludp/CMakeLists.txt @@ -64,13 +64,15 @@ set(sources ############################################### if(ECALUDP_ENABLE_NPCAP) list(APPEND includes + include_with_udpcap/ecaludp/npcap_helpers.h include_with_udpcap/ecaludp/socket_npcap.h ) list(APPEND sources - src/socket_npcap.cpp src/async_udpcap_socket.cpp src/async_udpcap_socket.h + src/npcap_helpers.cpp + src/socket_npcap.cpp ) endif() diff --git a/ecaludp/include_with_udpcap/ecaludp/npcap_helpers.h b/ecaludp/include_with_udpcap/ecaludp/npcap_helpers.h new file mode 100644 index 0000000..fcfce27 --- /dev/null +++ b/ecaludp/include_with_udpcap/ecaludp/npcap_helpers.h @@ -0,0 +1,59 @@ +/******************************************************************************** + * Copyright (c) 2024 Continental Corporation + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#pragma once + +#include + +// IWYU pragma: begin_exports +#include +// IWYU pragma: end_exports + +namespace ecaludp +{ + namespace npcap + { + /** + * @brief Initializes Npcap, if not done already. Must be called before calling any native npcap methods. + * + * This method initialized Npcap and must be called at least once before + * calling any ncap functions. + * As it always returns true when npcap has been intialized successfully, it + * can also be used to check whether npcap is available and working properly. + * + * If this function returns true, npcap should work. + * + * @return True if npcap is working + */ + ECALUDP_EXPORT bool initialize(); + + /** + * @brief Checks whether npcap has been initialized successfully + * @return true if npcap has been initialized successfully + */ + ECALUDP_EXPORT bool is_initialized(); + + /** + * @brief Returns a human readible status message. + * + * This message is intended to be displayed in a graphical user interface. + * For terminal based applications it is not needed, as the messages are also + * printed to stderr. + * + * @return The Udpcap status as human-readible text (may be multi-line) + */ + ECALUDP_EXPORT std::string get_human_readable_error_text(); + } +} \ No newline at end of file diff --git a/ecaludp/src/npcap_helpers.cpp b/ecaludp/src/npcap_helpers.cpp new file mode 100644 index 0000000..9534189 --- /dev/null +++ b/ecaludp/src/npcap_helpers.cpp @@ -0,0 +1,34 @@ +/******************************************************************************** + * Copyright (c) 2024 Continental Corporation + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#pragma once + +#include + +#include + +#include + +namespace ecaludp +{ + namespace npcap + { + bool initialize() { return Udpcap::Initialize(); } + + bool is_initialized() { return Udpcap::IsInitialized(); } + + std::string get_human_readable_error_text() { return Udpcap::GetHumanReadibleErrorText(); } + } +} \ No newline at end of file diff --git a/samples/integration_test_npcap/src/main.cpp b/samples/integration_test_npcap/src/main.cpp index 8084e13..038a33c 100644 --- a/samples/integration_test_npcap/src/main.cpp +++ b/samples/integration_test_npcap/src/main.cpp @@ -18,11 +18,15 @@ #include #include +#include #include int main() { + // Initialize npcap explicitely + ecaludp::npcap::initialize(); + // Create a socket ecaludp::SocketNpcap socket({'E', 'C', 'A', 'L'});