From 42e21846f5a23b4ac47fb487d616c1fcbbb2a674 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Fri, 26 Jan 2024 12:41:23 +0100 Subject: [PATCH] Extract method --- cpp/devices/ctapdriver.cpp | 57 +++++++++++++++++++++++--------------- cpp/devices/ctapdriver.h | 2 +- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/cpp/devices/ctapdriver.cpp b/cpp/devices/ctapdriver.cpp index 82558cf7..c81b7ded 100644 --- a/cpp/devices/ctapdriver.cpp +++ b/cpp/devices/ctapdriver.cpp @@ -171,35 +171,32 @@ bool CTapDriver::Init(const param_map &const_params) void CTapDriver::CleanUp() const { - if (tap_fd != -1) { - if (const int fd = socket(AF_LOCAL, SOCK_STREAM, 0); fd == -1) { - LogErrno("Can't open bridge socket"); - } else { - trace(">brctl delif " + BRIDGE_NAME + " " + BRIDGE_INTERFACE_NAME); - if (const string error = br_setif(fd, BRIDGE_NAME, BRIDGE_INTERFACE_NAME, false); !error.empty()) { - warn("Removing " + BRIDGE_INTERFACE_NAME + " from the bridge failed: " + error); - warn("You may need to manually remove the tap device"); - } + if (tap_fd == -1) { + return; + } - trace(">ip link set dev " + BRIDGE_NAME + " down"); - if (const string error = ip_link(fd, BRIDGE_NAME.c_str(), false); !error.empty()) { - warn(error); - } + if (const int fd = socket(AF_LOCAL, SOCK_STREAM, 0); fd == -1) { + LogErrno("Can't open bridge socket"); + } else { + trace(">brctl delif " + BRIDGE_NAME + " " + BRIDGE_INTERFACE_NAME); + if (const string error = br_setif(fd, BRIDGE_NAME, BRIDGE_INTERFACE_NAME, false); !error.empty()) { + warn("Removing " + BRIDGE_INTERFACE_NAME + " from the bridge failed: " + error); + warn("You may need to manually remove the tap device"); + } -#ifdef __linux__ - if (bridge_created) { - trace(">brctl delbr " + BRIDGE_NAME); - if (ioctl(fd, SIOCBRDELBR, BRIDGE_NAME.c_str()) == -1) { - warn("Removing " + BRIDGE_NAME + " failed: " + strerror(errno)); - } - } -#endif + trace(">ip link set dev " + BRIDGE_NAME + " down"); + if (const string error = ip_link(fd, BRIDGE_NAME.c_str(), false); !error.empty()) { + warn(error); + } - close(fd); + if (const string error = DeleteBridge(fd); !error.empty()) { + warn(error); } - close(tap_fd); + close(fd); } + + close(tap_fd); } param_map CTapDriver::GetDefaultParams() const @@ -303,6 +300,20 @@ string CTapDriver::AddBridge(int fd) return ""; } +string CTapDriver::DeleteBridge(int fd) const +{ +#ifdef __linux__ + if (bridge_created) { + trace(">brctl delbr " + BRIDGE_NAME); + if (ioctl(fd, SIOCBRDELBR, BRIDGE_NAME.c_str()) == -1) { + return "Removing " + BRIDGE_NAME + " failed: " + strerror(errno); + } + } +#endif + + return ""; +} + string CTapDriver::IpLink(bool enable) { const int fd = socket(PF_INET, SOCK_DGRAM, 0); diff --git a/cpp/devices/ctapdriver.h b/cpp/devices/ctapdriver.h index b9d674f0..65d30181 100644 --- a/cpp/devices/ctapdriver.h +++ b/cpp/devices/ctapdriver.h @@ -58,8 +58,8 @@ class CTapDriver return BRIDGE_NAME; } - // Add the piscsi_bridge bridge static string AddBridge(int); + string DeleteBridge(int) const; // Enable/Disable the piscsi0 interface static string IpLink(bool);