From 0631d9dfb2d7978065d07c7f06e20a5e6fd01583 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 20 Jan 2020 17:34:22 -0800 Subject: [PATCH] Support multi-homed hosts and plumb MAC addresses for Linux --- Simple-Web-Server | 2 +- sunshine/nvhttp.cpp | 13 ++--------- sunshine/platform/common.h | 2 +- sunshine/platform/linux.cpp | 44 +++++++++++------------------------ sunshine/platform/windows.cpp | 4 +++- 5 files changed, 20 insertions(+), 45 deletions(-) diff --git a/Simple-Web-Server b/Simple-Web-Server index f4b7101a..f37a41d4 160000 --- a/Simple-Web-Server +++ b/Simple-Web-Server @@ -1 +1 @@ -Subproject commit f4b7101ad369f56423291d6ac39ebf61a6518298 +Subproject commit f37a41d48b895c81241f562f8b3d5c19e07bdfea diff --git a/sunshine/nvhttp.cpp b/sunshine/nvhttp.cpp index 2f274ecf..f978519b 100644 --- a/sunshine/nvhttp.cpp +++ b/sunshine/nvhttp.cpp @@ -77,7 +77,6 @@ struct pair_session_t { std::unordered_map map_id_sess; std::unordered_map map_id_client; std::string unique_id; -std::string local_ip; net::net_e origin_pin_allowed; using args_t = SimpleWeb::CaseInsensitiveMultimap; @@ -442,9 +441,9 @@ void serverinfo(std::shared_ptr::Response> res tree.put("root.appversion", VERSION); tree.put("root.GfeVersion", GFE_VERSION); 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.LocalIP", local_ip); + tree.put("root.LocalIP", request->local_endpoint_address()); if(config::video.hevc_mode == 2) { tree.put("root.ServerCodecModeSupport", "3843"); @@ -654,15 +653,7 @@ void appasset(resp_https_t response, req_https_t request) { } void start(std::shared_ptr> shutdown_event) { - local_ip = platf::get_local_ip(); 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(); conf_intern.pkey = read_file(config::nvhttp.pkey.c_str()); diff --git a/sunshine/platform/common.h b/sunshine/platform/common.h index c7216fc4..5e1fed5a 100644 --- a/sunshine/platform/common.h +++ b/sunshine/platform/common.h @@ -75,7 +75,7 @@ void freeInput(void*); using input_t = util::safe_ptr; -std::string get_local_ip(); +std::string get_mac_address(const std::string_view &address); std::unique_ptr microphone(std::uint32_t sample_rate); std::shared_ptr display(); diff --git a/sunshine/platform/linux.cpp b/sunshine/platform/linux.cpp index 64ce9685..acfe68a7 100644 --- a/sunshine/platform/linux.cpp +++ b/sunshine/platform/linux.cpp @@ -5,6 +5,8 @@ #include "common.h" #include "../main.h" +#include + #include #include #include @@ -385,42 +387,22 @@ std::string from_sockaddr(const sockaddr *const ip_addr) { return std::string { data }; } -std::string get_local_ip(int family) { - std::bitset<2> family_f {}; - - if(family == 0) { - family_f[0] = true; - family_f[1] = true; - } - - if(family == AF_INET) { - 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; +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; } } } - - return ip_addr; + BOOST_LOG(warning) << "Unable to find MAC address for "sv << address; + return "00:00:00:00:00:00"s; } -std::string get_local_ip() { return get_local_ip(AF_INET); } - void freeImage(XImage *p) { XDestroyImage(p); } diff --git a/sunshine/platform/windows.cpp b/sunshine/platform/windows.cpp index 24fce910..0c45cdce 100755 --- a/sunshine/platform/windows.cpp +++ b/sunshine/platform/windows.cpp @@ -57,7 +57,9 @@ public: 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 result { new vigem_t {} };