Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Feb 7, 2024
1 parent 196d863 commit 4fcdaf4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
93 changes: 43 additions & 50 deletions cpp/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<string, string> CTapDriver::ExtractAddressAndMask(const string &s)
{
string address = s;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cpp/devices/ctapdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<class Cleanup>
bool CreateBridge(int, int, Cleanup);
static pair<string, string> ExtractAddressAndMask(const string&);
string SetAddressAndNetMask(int, const string&) const;

// File handle
int tap_fd = -1;
Expand Down

0 comments on commit 4fcdaf4

Please sign in to comment.