mirror of
https://github.com/akuker/RASCSI.git
synced 2024-12-21 23:29:39 +00:00
41bdcd4aed
* Update logging * Remove duplicate code * Update unit tests * Clean up includes * Merge ProtobufSerializer into protobuf_util namespace * Precompile regex * Add const * Add Split() convenience method, update log level/ID parsing * Move log.h to legacy folder * Elimininate gotos * Fixes for gcc 13 * Update compiler flags * Update default folder handling * Use references instead of pointers * Move code for better encapsulation * Move code * Remove unused method argument * Move device logger * Remove redundant to_string * Rename for consistency * Update handling of protobuf pointers * Simplify protobuf usage * Memory handling update * Add hasher
76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
//---------------------------------------------------------------------------
|
|
//
|
|
// 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;
|
|
}
|