From 4fcdaf4f0a13443c85a204ab1d1e6a2a72dfd157 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 7 Feb 2024 23:49:39 +0100 Subject: [PATCH] Remove duplicate code --- cpp/devices/ctapdriver.cpp | 93 ++++++++++++++++++-------------------- cpp/devices/ctapdriver.h | 3 +- 2 files changed, 45 insertions(+), 51 deletions(-) diff --git a/cpp/devices/ctapdriver.cpp b/cpp/devices/ctapdriver.cpp index de447d2c..e1226413 100644 --- a/cpp/devices/ctapdriver.cpp +++ b/cpp/devices/ctapdriver.cpp @@ -134,32 +134,13 @@ bool CTapDriver::Init(const param_map &const_params) } } else { - const auto [address, netmask] = ExtractAddressAndMask(inet); - if (address.empty() || netmask.empty()) { - return cleanUp("Error extracting inet address and netmask"); - } - - ifreq ifr_a; - ifr_a.ifr_addr.sa_family = AF_INET; - strncpy(ifr_a.ifr_name, BRIDGE_INTERFACE_NAME.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe - if (auto addr = (sockaddr_in*)&ifr_a.ifr_addr; - inet_pton(AF_INET, address.c_str(), &addr->sin_addr) != 1) { - return cleanUp("Can't convert '" + address + "' into a network address"); - } - - ifreq ifr_n; - ifr_n.ifr_addr.sa_family = AF_INET; - strncpy(ifr_n.ifr_name, BRIDGE_INTERFACE_NAME.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe - if (auto mask = (sockaddr_in*)&ifr_n.ifr_addr; - inet_pton(AF_INET, netmask.c_str(), &mask->sin_addr) != 1) { - return cleanUp("Can't convert '" + netmask + "' into a netmask"); - } - +#ifdef __linux__ trace(">ip addr add " + DEFAULT_IP + " brd + dev " + BRIDGE_INTERFACE_NAME); - if (ioctl(ip_fd, SIOCSIFADDR, &ifr_a) == -1 || ioctl(ip_fd, SIOCSIFNETMASK, &ifr_n) == -1) { - return cleanUp("Can't ioctl SIOCSIFADDR or SIOCSIFNETMASK"); + if (const string error = SetAddressAndNetMask(ip_fd, BRIDGE_INTERFACE_NAME); !error.empty()) { + return cleanUp(error); } +#endif } close(ip_fd); @@ -258,6 +239,41 @@ bool CTapDriver::CreateBridge(int br_socket_fd, int ip_fd, Cleanup cleanUp) return true; } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +string CTapDriver::SetAddressAndNetMask(int fd, const string &interface) const +{ +#ifdef __linux__ + const auto [address, netmask] = ExtractAddressAndMask(inet); + if (address.empty() || netmask.empty()) { + return "Error extracting inet address and netmask"; + } + + ifreq ifr_a; + ifr_a.ifr_addr.sa_family = AF_INET; + strncpy(ifr_a.ifr_name, interface.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe + if (auto addr = (sockaddr_in*)&ifr_a.ifr_addr; + inet_pton(AF_INET, address.c_str(), &addr->sin_addr) != 1) { + return "Can't convert '" + address + "' into a network address"; + } + + ifreq ifr_n; + ifr_n.ifr_addr.sa_family = AF_INET; + strncpy(ifr_n.ifr_name, interface.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe + if (auto mask = (sockaddr_in*)&ifr_n.ifr_addr; + inet_pton(AF_INET, netmask.c_str(), &mask->sin_addr) != 1) { + return "Can't convert '" + netmask + "' into a netmask"; + } + + if (ioctl(fd, SIOCSIFADDR, &ifr_a) == -1 || ioctl(fd, SIOCSIFNETMASK, &ifr_n) == -1) { + return "Can't ioctl SIOCSIFADDR or SIOCSIFNETMASK"; + } +#endif + + return ""; +} +#pragma GCC diagnostic pop + pair CTapDriver::ExtractAddressAndMask(const string &s) { string address = s; @@ -299,44 +315,21 @@ string CTapDriver::SetUpEth0(int socket_fd, const string &bridge_interface) #endif } -string CTapDriver::SetUpNonEth0(int socket_fd, int ip_fd, const string &s) +string CTapDriver::SetUpNonEth0(int socket_fd, int ip_fd, const string &s) const { - const auto [address, netmask] = ExtractAddressAndMask(s); - if (address.empty() || netmask.empty()) { - return "Error extracting inet address and netmask"; - } - #ifdef __linux__ if (const string &error = AddBridge(socket_fd); !error.empty()) { return error; } - ifreq ifr_a; - ifr_a.ifr_addr.sa_family = AF_INET; - strncpy(ifr_a.ifr_name, BRIDGE_NAME.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe - if (auto addr = (sockaddr_in*)&ifr_a.ifr_addr; - inet_pton(AF_INET, address.c_str(), &addr->sin_addr) != 1) { - return "Can't convert '" + address + "' into a network address"; - } - - ifreq ifr_n; - ifr_n.ifr_addr.sa_family = AF_INET; - strncpy(ifr_n.ifr_name, BRIDGE_NAME.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe - if (auto mask = (sockaddr_in*)&ifr_n.ifr_addr; - inet_pton(AF_INET, netmask.c_str(), &mask->sin_addr) != 1) { - return "Can't convert '" + netmask + "' into a netmask"; - } - trace(">ip addr add " + s + " dev " + BRIDGE_NAME); - if (ioctl(ip_fd, SIOCSIFADDR, &ifr_a) == -1 || ioctl(ip_fd, SIOCSIFNETMASK, &ifr_n) == -1) { - return "Can't ioctl SIOCSIFADDR or SIOCSIFNETMASK"; + if (const string error = SetAddressAndNetMask(ip_fd, BRIDGE_NAME); !error.empty()) { + return error; } +#endif return ""; -#else - return " SIOCSIFADDR/SIOCSIFNETMASK: Linux is required"; -#endif } string CTapDriver::AddBridge(int fd) diff --git a/cpp/devices/ctapdriver.h b/cpp/devices/ctapdriver.h index 6010f275..9948e77d 100644 --- a/cpp/devices/ctapdriver.h +++ b/cpp/devices/ctapdriver.h @@ -67,10 +67,11 @@ class CTapDriver private: static string SetUpEth0(int, const string&); - static string SetUpNonEth0(int, int, const string&); + string SetUpNonEth0(int, int, const string&) const; template bool CreateBridge(int, int, Cleanup); static pair ExtractAddressAndMask(const string&); + string SetAddressAndNetMask(int, const string&) const; // File handle int tap_fd = -1;