From 7c3df1b67713b4cd6eeaf914ba34b0219d49aa32 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 7 Feb 2024 18:25:10 +0100 Subject: [PATCH] Set bridge addr --- cpp/devices/ctapdriver.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cpp/devices/ctapdriver.cpp b/cpp/devices/ctapdriver.cpp index 482c6a47..b730ee80 100644 --- a/cpp/devices/ctapdriver.cpp +++ b/cpp/devices/ctapdriver.cpp @@ -134,8 +134,33 @@ 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"); + } + 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"); + } + // TODO }