Support multi-homed hosts and plumb MAC addresses for Linux

This commit is contained in:
Cameron Gutman
2020-01-20 17:34:22 -08:00
parent 6f3927b6ae
commit 0631d9dfb2
5 changed files with 20 additions and 45 deletions

View File

@@ -77,7 +77,6 @@ struct pair_session_t {
std::unordered_map<std::string, pair_session_t> map_id_sess; std::unordered_map<std::string, pair_session_t> map_id_sess;
std::unordered_map<std::string, client_t> map_id_client; std::unordered_map<std::string, client_t> map_id_client;
std::string unique_id; std::string unique_id;
std::string local_ip;
net::net_e origin_pin_allowed; net::net_e origin_pin_allowed;
using args_t = SimpleWeb::CaseInsensitiveMultimap; using args_t = SimpleWeb::CaseInsensitiveMultimap;
@@ -442,9 +441,9 @@ void serverinfo(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> res
tree.put("root.appversion", VERSION); tree.put("root.appversion", VERSION);
tree.put("root.GfeVersion", GFE_VERSION); tree.put("root.GfeVersion", GFE_VERSION);
tree.put("root.uniqueid", unique_id); tree.put("root.uniqueid", unique_id);
tree.put("root.mac", "00:00:00:00:00:00"); tree.put("root.mac", platf::get_mac_address(request->local_endpoint_address()));
tree.put("root.MaxLumaPixelsHEVC", config::video.hevc_mode > 0 ? "1869449984" : "0"); tree.put("root.MaxLumaPixelsHEVC", config::video.hevc_mode > 0 ? "1869449984" : "0");
tree.put("root.LocalIP", local_ip); tree.put("root.LocalIP", request->local_endpoint_address());
if(config::video.hevc_mode == 2) { if(config::video.hevc_mode == 2) {
tree.put("root.ServerCodecModeSupport", "3843"); tree.put("root.ServerCodecModeSupport", "3843");
@@ -654,15 +653,7 @@ void appasset(resp_https_t response, req_https_t request) {
} }
void start(std::shared_ptr<safe::event_t<bool>> shutdown_event) { void start(std::shared_ptr<safe::event_t<bool>> shutdown_event) {
local_ip = platf::get_local_ip();
origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed); origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed);
if(local_ip.empty()) {
BOOST_LOG(fatal) << "Could not determine the local ip-address"sv;
log_flush();
std::abort();
}
load_state(); load_state();
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str()); conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());

View File

@@ -75,7 +75,7 @@ void freeInput(void*);
using input_t = util::safe_ptr<void, freeInput>; using input_t = util::safe_ptr<void, freeInput>;
std::string get_local_ip(); std::string get_mac_address(const std::string_view &address);
std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate); std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate);
std::shared_ptr<display_t> display(); std::shared_ptr<display_t> display();

View File

@@ -5,6 +5,8 @@
#include "common.h" #include "common.h"
#include "../main.h" #include "../main.h"
#include <fstream>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <net/if.h> #include <net/if.h>
@@ -385,42 +387,22 @@ std::string from_sockaddr(const sockaddr *const ip_addr) {
return std::string { data }; return std::string { data };
} }
std::string get_local_ip(int family) { std::string get_mac_address(const std::string_view &address) {
std::bitset<2> family_f {}; auto ifaddrs = get_ifaddrs();
for(auto pos = ifaddrs.get(); pos != nullptr; pos = pos->ifa_next) {
if(family == 0) { if(pos->ifa_addr && address == from_sockaddr(pos->ifa_addr)) {
family_f[0] = true; std::ifstream mac_file("/sys/class/net/"s + pos->ifa_name + "/address");
family_f[1] = true; if(mac_file.good()) {
} std::string mac_address;
std::getline(mac_file, mac_address);
if(family == AF_INET) { return mac_address;
family_f[0] = true;
}
if(family == AF_INET6) {
family_f[1] = true;
}
std::string ip_addr;
auto ifaddr = get_ifaddrs();
for(auto pos = ifaddr.get(); pos != nullptr; pos = pos->ifa_next) {
if(pos->ifa_addr && pos->ifa_flags & IFF_UP && !(pos->ifa_flags & IFF_LOOPBACK)) {
if(
(family_f[0] && pos->ifa_addr->sa_family == AF_INET) ||
(family_f[1] && pos->ifa_addr->sa_family == AF_INET6)
){
ip_addr = from_sockaddr(pos->ifa_addr);
break;
} }
} }
} }
BOOST_LOG(warning) << "Unable to find MAC address for "sv << address;
return ip_addr; return "00:00:00:00:00:00"s;
} }
std::string get_local_ip() { return get_local_ip(AF_INET); }
void freeImage(XImage *p) { void freeImage(XImage *p) {
XDestroyImage(p); XDestroyImage(p);
} }

View File

@@ -57,7 +57,9 @@ public:
client_t client; client_t client;
}; };
std::string get_local_ip() { return "192.168.0.119"s; } std::string get_mac_address(const std::string_view &address) {
return "00:00:00:00:00:00"s;
}
input_t input() { input_t input() {
input_t result { new vigem_t {} }; input_t result { new vigem_t {} };