Merge pull request #10 from cgutman/local_ip

Support multi-homed hosts and plumb MAC addresses for Linux
This commit is contained in:
loki-47-6F-64 2020-01-21 18:20:03 +01:00 committed by GitHub
commit c7eb67d761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 45 deletions

@ -1 +1 @@
Subproject commit f4b7101ad369f56423291d6ac39ebf61a6518298
Subproject commit f37a41d48b895c81241f562f8b3d5c19e07bdfea

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, client_t> 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<typename SimpleWeb::ServerBase<T>::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<safe::event_t<bool>> 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());

View File

@ -75,7 +75,7 @@ void freeInput(void*);
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::shared_ptr<display_t> display();

View File

@ -5,6 +5,8 @@
#include "common.h"
#include "../main.h"
#include <fstream>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
@ -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);
}

View File

@ -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 {} };