Skip to content

Commit

Permalink
Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 26, 2024
1 parent d3673c9 commit 42e2184
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
57 changes: 34 additions & 23 deletions cpp/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/ctapdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 42e2184

Please sign in to comment.