Skip to content

Commit

Permalink
Make methods non-global
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Feb 7, 2024
1 parent f03a790 commit b61b146
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 14 additions & 14 deletions cpp/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ using namespace spdlog;
using namespace s2p_util;
using namespace network_util;

static string br_setif(int br_socket_fd, const string &bridgename, const string &ifname, bool add)
string CTapDriver::BrSetif(int fd, const string &bridge, const string &interface, bool add)
{
#ifdef __linux__
ifreq ifr;
ifr.ifr_ifindex = if_nametoindex(ifname.c_str());
ifr.ifr_ifindex = if_nametoindex(interface.c_str());
if (!ifr.ifr_ifindex) {
return "Can't if_nametoindex " + ifname;
return "Can't if_nametoindex " + interface;
}
strncpy(ifr.ifr_name, bridgename.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe
if (ioctl(br_socket_fd, add ? SIOCBRADDIF : SIOCBRDELIF, &ifr) == -1) {
strncpy(ifr.ifr_name, bridge.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe
if (ioctl(fd, add ? SIOCBRADDIF : SIOCBRDELIF, &ifr) == -1) {
return "Can't ioctl " + string(add ? "SIOCBRADDIF" : "SIOCBRDELIF");
}
#endif

return "";
}

string ip_link(int fd, const char *ifname, bool up)
string CTapDriver::IpLink(int fd, const string &interface, bool up)
{
ifreq ifr;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); // NOSONAR Using strncpy is safe
strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ - 1); // NOSONAR Using strncpy is safe
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
return "Can't ioctl SIOCGIFFLAGS";
}
Expand Down Expand Up @@ -122,13 +122,13 @@ bool CTapDriver::Init(const param_map &const_params)
}

trace(">ip link set " + BRIDGE_INTERFACE_NAME + " up");
if (const string error = ip_link(ip_fd, BRIDGE_INTERFACE_NAME.c_str(), true); !error.empty()) {
if (const string error = IpLink(ip_fd, BRIDGE_INTERFACE_NAME, true); !error.empty()) {
return cleanUp(error);
}

if (requires_bridge) {
trace(">brctl addif " + BRIDGE_NAME + " " + BRIDGE_INTERFACE_NAME);
if (const string error = br_setif(bridge_fd, BRIDGE_NAME, BRIDGE_INTERFACE_NAME, true); !error.empty()) {
if (const string error = BrSetif(bridge_fd, BRIDGE_NAME, BRIDGE_INTERFACE_NAME, true); !error.empty()) {
return cleanUp(error);
}
}
Expand Down Expand Up @@ -162,14 +162,14 @@ void CTapDriver::CleanUp() const
} else {
if (requires_bridge) {
trace(">brctl delif " + BRIDGE_NAME + " " + BRIDGE_INTERFACE_NAME);
if (const string error = br_setif(fd, BRIDGE_NAME, BRIDGE_INTERFACE_NAME, false); !error.empty()) {
if (const string error = BrSetif(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");
}
}

trace(">ip link set dev " + BRIDGE_NAME + " down");
if (const string error = ip_link(fd, BRIDGE_NAME.c_str(), false); !error.empty()) {
if (const string error = IpLink(fd, BRIDGE_NAME, false); !error.empty()) {
warn(error);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ bool CTapDriver::CreateBridge(int br_socket_fd, int ip_fd, Cleanup cleanUp)

if (!bridge_interface.starts_with("wlan")) {
trace(">ip link set dev " + BRIDGE_NAME + " up");
if (const string error = ip_link(ip_fd, BRIDGE_NAME.c_str(), true); !error.empty()) {
if (const string error = IpLink(ip_fd, BRIDGE_NAME, true); !error.empty()) {
return cleanUp(error);
}

Expand Down Expand Up @@ -301,7 +301,7 @@ string CTapDriver::SetUpEth0(int fd, const string &bridge_interface)

trace(">brctl addif " + BRIDGE_NAME + " " + bridge_interface);

if (const string error = br_setif(fd, BRIDGE_NAME, bridge_interface, true); !error.empty()) {
if (const string error = BrSetif(fd, BRIDGE_NAME, bridge_interface, true); !error.empty()) {
return error;
}
#endif
Expand Down Expand Up @@ -356,7 +356,7 @@ string CTapDriver::IpLink(bool enable)
{
const int fd = socket(PF_INET, SOCK_DGRAM, 0);
trace(string(">ip link set " + BRIDGE_INTERFACE_NAME + " ") + (enable ? "up" : "down"));
const string result = ip_link(fd, BRIDGE_INTERFACE_NAME.c_str(), enable);
const string result = IpLink(fd, BRIDGE_INTERFACE_NAME, enable);
close(fd);

return result;
Expand Down
2 changes: 2 additions & 0 deletions cpp/devices/ctapdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class CTapDriver

private:

static string IpLink(int, const string&, bool);
static string BrSetif(int fd, const string&, const string&, bool);
static string SetUpEth0(int, const string&);
string SetUpNonEth0(int, int, const string&) const;
template<class Cleanup>
Expand Down

0 comments on commit b61b146

Please sign in to comment.