-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI Target Emulator PiSCSI | ||
// for Raspberry Pi | ||
// | ||
// Powered by XM6 TypeG Technology. | ||
// Copyright (C) 2016-2020 GIMONS | ||
// Copyright (C) 2020 akuker | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
// The legacy code in this file is deprecated and can cause a buffer overflow. Use spdlog directly instead. | ||
|
||
#pragma once | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
static const int LOGBUF_SIZE = 512; | ||
|
||
#define SPDLOGWRAPPER(loglevel, ...) \ | ||
{ \ | ||
char logbuf[LOGBUF_SIZE]; \ | ||
snprintf(logbuf, sizeof(logbuf), __VA_ARGS__); \ | ||
spdlog::log(loglevel, logbuf); \ | ||
}; | ||
|
||
#define LOGTRACE(...) SPDLOGWRAPPER(spdlog::level::trace, __VA_ARGS__) | ||
#define LOGDEBUG(...) SPDLOGWRAPPER(spdlog::level::debug, __VA_ARGS__) | ||
#define LOGINFO(...) SPDLOGWRAPPER(spdlog::level::info, __VA_ARGS__) | ||
#define LOGWARN(...) SPDLOGWRAPPER(spdlog::level::warn, __VA_ARGS__) | ||
#define LOGERROR(...) SPDLOGWRAPPER(spdlog::level::err, __VA_ARGS__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI Target Emulator PiSCSI | ||
// for Raspberry Pi | ||
// | ||
// Copyright (C) 2023 Uwe Seimet | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
#include "network_util.h" | ||
#include <cstring> | ||
#include <ifaddrs.h> | ||
#include <sys/ioctl.h> | ||
#include <netinet/in.h> | ||
#include <net/if.h> | ||
#include <unistd.h> | ||
#include <netdb.h> | ||
#include <unistd.h> | ||
|
||
using namespace std; | ||
|
||
bool network_util::IsInterfaceUp(const string& interface) | ||
{ | ||
ifreq ifr = {}; | ||
strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ - 1); //NOSONAR Using strncpy is safe | ||
const int fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP); | ||
|
||
if (!ioctl(fd, SIOCGIFFLAGS, &ifr) && (ifr.ifr_flags & IFF_UP)) { | ||
close(fd); | ||
return true; | ||
} | ||
|
||
close(fd); | ||
return false; | ||
} | ||
|
||
set<string, less<>> network_util::GetNetworkInterfaces() | ||
{ | ||
set<string, less<>> network_interfaces; | ||
|
||
#ifdef __linux__ | ||
ifaddrs *addrs; | ||
getifaddrs(&addrs); | ||
ifaddrs *tmp = addrs; | ||
|
||
while (tmp) { | ||
if (const string name = tmp->ifa_name; tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET && | ||
name != "lo" && name != "piscsi_bridge" && !name.starts_with("dummy") && IsInterfaceUp(name)) { | ||
// Only list interfaces that are up | ||
network_interfaces.insert(name); | ||
} | ||
|
||
tmp = tmp->ifa_next; | ||
} | ||
|
||
freeifaddrs(addrs); | ||
#endif | ||
|
||
return network_interfaces; | ||
} | ||
|
||
bool network_util::ResolveHostName(const string& host, sockaddr_in *addr) | ||
{ | ||
addrinfo hints = {}; | ||
hints.ai_family = AF_INET; | ||
hints.ai_socktype = SOCK_STREAM; | ||
|
||
if (addrinfo *result; !getaddrinfo(host.c_str(), nullptr, &hints, &result)) { | ||
*addr = *reinterpret_cast<sockaddr_in *>(result->ai_addr); //NOSONAR bit_cast is not supported by the bullseye compiler | ||
freeaddrinfo(result); | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI Target Emulator PiSCSI | ||
// for Raspberry Pi | ||
// | ||
// Copyright (C) 2023 Uwe Seimet | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <set> | ||
|
||
using namespace std; | ||
|
||
struct sockaddr_in; | ||
|
||
namespace network_util | ||
{ | ||
bool IsInterfaceUp(const string&); | ||
set<string, less<>> GetNetworkInterfaces(); | ||
bool ResolveHostName(const string&, sockaddr_in *); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI Target Emulator PiSCSI | ||
// for Raspberry Pi | ||
// | ||
// Copyright (C) 2023 Uwe Seimet | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <netdb.h> | ||
#include <netinet/in.h> | ||
#include "shared/network_util.h" | ||
|
||
using namespace network_util; | ||
|
||
TEST(NetworkUtilTest, IsInterfaceUp) | ||
{ | ||
EXPECT_FALSE(IsInterfaceUp("foo_bar")); | ||
} | ||
|
||
TEST(NetworkUtilTest, GetNetworkInterfaces) | ||
{ | ||
EXPECT_FALSE(GetNetworkInterfaces().empty()); | ||
} | ||
|
||
TEST(NetworkUtilTest, ResolveHostName) | ||
{ | ||
sockaddr_in server_addr = {}; | ||
EXPECT_FALSE(ResolveHostName("foo.foobar", &server_addr)); | ||
EXPECT_TRUE(ResolveHostName("127.0.0.1", &server_addr)); | ||
} |