mirror of
https://github.com/LizardByte/Sunshine.git
synced 2024-11-18 02:09:49 +00:00
88 lines
2.1 KiB
C++
88 lines
2.1 KiB
C++
#include "sunshine/platform/common.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
#include <pwd.h>
|
|
#include <unistd.h>
|
|
|
|
#include "sunshine/main.h"
|
|
|
|
using namespace std::literals;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace platf {
|
|
using ifaddr_t = util::safe_ptr<ifaddrs, freeifaddrs>;
|
|
|
|
ifaddr_t get_ifaddrs() {
|
|
ifaddrs *p { nullptr };
|
|
|
|
getifaddrs(&p);
|
|
|
|
return ifaddr_t { p };
|
|
}
|
|
|
|
fs::path appdata() {
|
|
const char *homedir;
|
|
if((homedir = getenv("HOME")) == nullptr) {
|
|
homedir = getpwuid(geteuid())->pw_dir;
|
|
}
|
|
|
|
return fs::path { homedir } / ".config/sunshine"sv;
|
|
}
|
|
|
|
std::string from_sockaddr(const sockaddr *const ip_addr) {
|
|
char data[INET6_ADDRSTRLEN];
|
|
|
|
auto family = ip_addr->sa_family;
|
|
if(family == AF_INET6) {
|
|
inet_ntop(AF_INET6, &((sockaddr_in6 *)ip_addr)->sin6_addr, data,
|
|
INET6_ADDRSTRLEN);
|
|
}
|
|
|
|
if(family == AF_INET) {
|
|
inet_ntop(AF_INET, &((sockaddr_in *)ip_addr)->sin_addr, data,
|
|
INET_ADDRSTRLEN);
|
|
}
|
|
|
|
return std::string { data };
|
|
}
|
|
|
|
std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const ip_addr) {
|
|
char data[INET6_ADDRSTRLEN];
|
|
|
|
auto family = ip_addr->sa_family;
|
|
std::uint16_t port;
|
|
if(family == AF_INET6) {
|
|
inet_ntop(AF_INET6, &((sockaddr_in6 *)ip_addr)->sin6_addr, data,
|
|
INET6_ADDRSTRLEN);
|
|
port = ((sockaddr_in6 *)ip_addr)->sin6_port;
|
|
}
|
|
|
|
if(family == AF_INET) {
|
|
inet_ntop(AF_INET, &((sockaddr_in *)ip_addr)->sin_addr, data,
|
|
INET_ADDRSTRLEN);
|
|
port = ((sockaddr_in *)ip_addr)->sin_port;
|
|
}
|
|
|
|
return { port, std::string { data } };
|
|
}
|
|
|
|
std::string get_mac_address(const std::string_view &address) {
|
|
auto ifaddrs = get_ifaddrs();
|
|
for(auto pos = ifaddrs.get(); pos != nullptr; pos = pos->ifa_next) {
|
|
if(pos->ifa_addr && address == from_sockaddr(pos->ifa_addr)) {
|
|
std::ifstream mac_file("/sys/class/net/"s + pos->ifa_name + "/address");
|
|
if(mac_file.good()) {
|
|
std::string mac_address;
|
|
std::getline(mac_file, mac_address);
|
|
return mac_address;
|
|
}
|
|
}
|
|
}
|
|
|
|
BOOST_LOG(warning) << "Unable to find MAC address for "sv << address;
|
|
return "00:00:00:00:00:00"s;
|
|
}
|
|
} // namespace platf
|