Skip to content

Commit

Permalink
Add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 2, 2023
1 parent e2b2b07 commit 481cf35
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cpp/hal/log.h
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__)
75 changes: 75 additions & 0 deletions cpp/shared/network_util.cpp
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;
}
24 changes: 24 additions & 0 deletions cpp/shared/network_util.h
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 *);
}
33 changes: 33 additions & 0 deletions cpp/test/network_util_test.cpp
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));
}

0 comments on commit 481cf35

Please sign in to comment.