Native USB networking for Quest

Requires Horizon OS >= 2.6
This commit is contained in:
Patrick Nicolas
2026-08-29 12:33:59 +02:00
parent 8333322cd2
commit 8738131fb1
21 changed files with 317 additions and 93 deletions
+2
View File
@@ -30,7 +30,9 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- Required for WiFi locking, see https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
+60
View File
@@ -0,0 +1,60 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "application.h"
#include "jni.h"
#include <atomic>
#include <spdlog/spdlog.h>
std::atomic_bool link_properties_changed = false;
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onAvailable(JNIEnv * env, jobject instance, jobject network)
{
spdlog::info("NetworkInfoCallback_onAvailable");
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onBlockedStatusChanged(JNIEnv * env, jobject instance, jobject network, jboolean blocked)
{
spdlog::info("NetworkInfoCallback_onBlockedStatusChanged");
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onCapabilitiesChanged(JNIEnv * env, jobject instance, jobject network, jobject caps)
{
spdlog::info("NetworkInfoCallback_onCapabitilitiesChanged");
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onLinkPropertiesChanged(JNIEnv * env, jobject instance, jobject network, jobject jprops)
{
link_properties_changed = true;
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onLosing(JNIEnv * env, jobject instance, jobject network, jint ms)
{
spdlog::info("NetworkInfoCallback_onLosing");
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onLost(JNIEnv * env, jobject instance, jobject network)
{
spdlog::info("NetworkInfoCallback_onLost");
}
extern "C" void Java_org_meumeu_wivrn_NetworkInfoCallback_onUnavailable(JNIEnv * env, jobject instance)
{
spdlog::info("NetworkInfoCallback_onUnavailable");
application::get_config().usb_network = false;
}
bool usb_link_properties_changed()
{
return link_properties_changed.exchange(false);
}
+19
View File
@@ -0,0 +1,19 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
bool usb_link_properties_changed();
+36
View File
@@ -1407,6 +1407,10 @@ void application::initialize()
config.emplace(xr_system_id, xr_session, application::get_config_path() / "client.json");
default_config.emplace(xr_system_id, xr_session);
#ifdef __ANDROID__
set_usb_networking(config->usb_network);
#endif
// HTC face tracker fails if created later
// we can destroy it right away, it actually stores static handles
if (xr_system_id.face_tracker_supported() == xr::face_tracker_type::htc)
@@ -1827,6 +1831,38 @@ void application::push_scene(std::shared_ptr<scene> s)
instance().scene_stack.push_back(std::move(s));
}
#ifdef __ANDROID__
void application::set_usb_networking(bool enabled)
{
jni::object<""> act(app_info.native_app->activity->clazz);
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
auto system_service = ctx.call<jni::object<"java/lang/Object">>("getSystemService", jni::string("connectivity"));
auto cb = act.field<jni::object<"android/net/ConnectivityManager$NetworkCallback">>("netcb");
try
{
if (enabled)
{
auto req = jni::new_object<"android/net/NetworkRequest$Builder">()
.call<jni::object<"android/net/NetworkRequest$Builder">>("removeCapability", jni::Int(12) /*NET_CAPABILITY_INTERNET*/)
.call<jni::object<"android/net/NetworkRequest$Builder">>("removeCapability", jni::Int(14) /*NET_CAPABILITY_TRUSTED*/)
.call<jni::object<"android/net/NetworkRequest$Builder">>("addTransportType", jni::Int(8) /*TRANSPORT_USB*/)
.call<jni::object<"android/net/NetworkRequest">>("build");
// system_service.call<void>("requestNetwork", req, jni::new_object<"org/meumeu/wivrn/NetworkInfoCallback">());
system_service.call<void>("requestNetwork", req, cb);
}
else
{
system_service.call<void>("unregisterNetworkCallback", cb);
}
}
catch (...)
{}
}
#endif
void application::poll_actions()
{
std::array<XrActionSet, 2> action_sets{
+4
View File
@@ -312,6 +312,10 @@ public:
return *instance().wifi;
}
#ifdef __ANDROID__
void set_usb_networking(bool enabled);
#endif
static void ignore_debug_reports_for(void * object)
{
#ifndef NDEBUG
+1
View File
@@ -346,6 +346,7 @@ const std::vector<config_field> & configuration::config_fields()
scalar("stream_scale", &configuration::stream_scale),
scalar("codec", &configuration::codec),
scalar("bit_depth", &configuration::bit_depth),
scalar("usb_network", &configuration::usb_network),
};
return fields;
}
+2
View File
@@ -114,6 +114,8 @@ public:
bool high_power_mode = true;
uint32_t fps_divider = 1;
bool usb_network = false;
// Allow unsafe config values
bool extended_config = false;
+16
View File
@@ -190,12 +190,28 @@ void hmd_traits::init()
{
panel_width_override = 2064; // Quest 3
controller_profile = "meta-quest-touch-plus";
usb_net = true;
}
else if (device == "panther") // Quest 3S
{
panel_width_override = 1832;
controller_profile = "meta-quest-touch-plus";
}
if (auto v = get_property("ro.hzos.build.display_name"))
{
auto digits = utils::split(*v, ".");
try
{
auto major = std::stoi(digits.at(0));
auto minor = std::stoi(digits.at(1));
usb_net = major > 2 or (major == 2 and minor >= 6);
}
catch (...)
{
spdlog::warn("Failed to parse Horizon OS version {}", *v);
}
}
}
else if (model == "Lynx-R1")
+1
View File
@@ -55,6 +55,7 @@ public:
bool hand_interaction_grip_surface = true;
bool pico_face_tracker = false;
bool discard_frame = true; // can do xrBeginFrame twice to discard the first one
bool usb_net = false;
std::unordered_map<std::string, std::string> override_shader;
std::unordered_set<std::string> blacklisted_extensions;
#ifndef NDEBUG
+2
View File
@@ -17,6 +17,7 @@ public class MainActivity extends android.app.NativeActivity
public native void onActivityResult(int requestCode, int resultCode, android.content.Intent data);
private android.content.BroadcastReceiver BatteryInfoReceiver = null;
public android.net.ConnectivityManager.NetworkCallback netcb = null;
@Override
protected void onCreate(android.os.Bundle savedInstanceState)
@@ -24,6 +25,7 @@ public class MainActivity extends android.app.NativeActivity
super.onCreate(savedInstanceState);
this.BatteryInfoReceiver = new BroadcastReceiver();
this.netcb = new org.meumeu.wivrn.NetworkInfoCallback();
this.registerReceiver(this.BatteryInfoReceiver, new android.content.IntentFilter(android.content.Intent.ACTION_BATTERY_CHANGED));
}
+26
View File
@@ -0,0 +1,26 @@
package org.meumeu.wivrn;
public class NetworkInfoCallback extends android.net.ConnectivityManager.NetworkCallback
{
@Override
public native void onAvailable(android.net.Network network);
@Override
public native void onBlockedStatusChanged(android.net.Network network, boolean blocked);
@Override
public native void onCapabilitiesChanged(android.net.Network network, android.net.NetworkCapabilities caps);
@Override
public native void onLinkPropertiesChanged(android.net.Network network, android.net.LinkProperties props);
@Override
public native void onLosing(android.net.Network network, int maxMsToLive);
@Override
public native void onLost(android.net.Network network);
@Override
public native void onUnavailable();
}
+13
View File
@@ -719,6 +719,19 @@ void settings_system(const settings_context & ctx)
.default_int = language_index(default_config),
});
#ifdef __ANDROID__
if (application::get_hmd_traits().usb_net)
list.push_back({
.id = "##usbnet",
.label = _C("setting name", "USB networking"),
.description = _("Enables connection by USB without ADB or developer mode.\nMake sure the server allows IPv6 link-local only connections."),
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.usb_network; },
.set_bool = [&config](bool v) { config.usb_network = v;config.save(); application::instance().set_usb_networking(v); },
.default_bool = default_config.usb_network,
});
#endif
list.push_back({
.id = "##extended",
.label = _C("setting name", "Extended configuration"),
+37 -12
View File
@@ -64,6 +64,10 @@
#include "vk/renderdoc.h"
#endif
#ifdef __ANDROID__
#include "android/usb.h"
#endif
using namespace std::chrono_literals;
static bool force_autoconnect = false;
@@ -198,6 +202,16 @@ static std::string ip_address_to_string(const in6_addr & addr)
return buf;
}
static std::string ip_address_to_string(const sockaddr_in & addr)
{
return ip_address_to_string(addr.sin_addr);
}
static std::string ip_address_to_string(const sockaddr_in6 & addr)
{
return ip_address_to_string(addr.sin6_addr);
}
std::unique_ptr<wivrn_session> scenes::lobby::connect_to_session(wivrn_discover::service service, bool manual_connection)
{
if (!manual_connection)
@@ -239,12 +253,18 @@ std::unique_ptr<wivrn_session> scenes::lobby::connect_to_session(wivrn_discover:
{
switch (i->ai_family)
{
case AF_INET:
service.addresses.push_back(((sockaddr_in *)i->ai_addr)->sin_addr);
break;
case AF_INET6:
service.addresses.push_back(((sockaddr_in6 *)i->ai_addr)->sin6_addr);
break;
case AF_INET: {
auto addr = (sockaddr_in *)i->ai_addr;
addr->sin_port = htons(service.port);
service.addresses.push_back({.address = *addr});
}
break;
case AF_INET6: {
auto addr = (sockaddr_in6 *)i->ai_addr;
addr->sin6_port = htons(service.port);
service.addresses.push_back({.address = *addr});
}
break;
}
}
@@ -252,22 +272,22 @@ std::unique_ptr<wivrn_session> scenes::lobby::connect_to_session(wivrn_discover:
}
std::string error;
for (const std::variant<in_addr, in6_addr> & address: service.addresses)
for (const auto & entry: service.addresses)
{
std::string address_string = std::visit([](auto & address) {
std::string address_string = std::visit([](const auto & address) {
return ip_address_to_string(address);
},
address);
entry.address);
struct connection_cancelled
{};
try
{
spdlog::debug("Connection to {}", address_string);
spdlog::info("Connection to {}", address_string);
return std::visit([this, &service](auto & address) {
return std::make_unique<wivrn_session>(address, service.port, service.tcp_only, keypair, [&](int fd) {
return std::make_unique<wivrn_session>(address, service.tcp_only, keypair, [&](int fd) {
auto request = pin_request.lock();
request->pin_requested = true;
request->pin_cancelled = false;
@@ -309,7 +329,7 @@ std::unique_ptr<wivrn_session> scenes::lobby::connect_to_session(wivrn_discover:
return request->pin;
});
},
address);
entry.address);
}
catch (connection_cancelled)
{
@@ -340,6 +360,11 @@ void scenes::lobby::update_server_list()
if (not discover)
return;
#ifdef __ANDROID__
if (usb_link_properties_changed())
discover.emplace();
#endif
std::vector<wivrn_discover::service> discovered_services = discover->get_services();
// TODO: only if discovered_services changed
+14 -1
View File
@@ -56,6 +56,7 @@
#include <vulkan/vulkan_to_string.hpp>
#include "IconsFontAwesome7.h"
#include "IconsFontAwesome7Brands.h"
using namespace std::chrono_literals;
@@ -427,7 +428,19 @@ void scenes::lobby::gui_server_list()
const ImVec2 chip_sz = ui::chip_size(chip_label);
float trailing = bh + gap + cw + (data.autoconnect ? gap + chip_sz.x : 0) + ui::metrics::list_row_pad;
const auto row = ui::begin_list_row("##row", ICON_FA_SERVER, 0, name, sub, false, trailing);
const auto server_icon = [](const wivrn_discover::service & service) {
if (not service.addresses.empty())
{
const auto & interface = service.addresses.front().interface;
if (interface.starts_with("usb"))
return ICON_FA_USB;
if (interface.starts_with("w"))
return ICON_FA_TOWER_BROADCAST;
}
return ICON_FA_SERVER;
}(data.service);
const auto row = ui::begin_list_row("##row", server_icon, 0, name, sub, false, trailing);
float x = row.max.x;
// overflow menu
+18 -6
View File
@@ -59,6 +59,15 @@ void init_stream(T & stream)
{
stream.set_receive_buffer_size(1024 * 1024 * 5);
}
auto & port(sockaddr_in & sa)
{
return sa.sin_port;
}
auto & port(sockaddr_in6 & sa)
{
return sa.sin6_port;
}
} // namespace
template <typename T>
@@ -120,7 +129,9 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
{
stream = decltype(stream)();
stream.connect(address, h.stream_port);
port(address) = htons(h.stream_port);
stream.connect(address);
init_stream(stream);
}
break;
@@ -171,7 +182,8 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
stream = decltype(stream)();
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_to_headset, s.stream_iv_header_from_headset);
stream.connect(address, h.stream_port);
port(address) = htons(h.stream_port);
stream.connect(address);
init_stream(stream);
}
break;
@@ -209,8 +221,8 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
}
}
wivrn_session::wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
control(address, port), stream(-1), address(address)
wivrn_session::wivrn_session(sockaddr_in6 address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
control(address), stream(-1), address(address.sin6_addr)
{
try
{
@@ -222,8 +234,8 @@ wivrn_session::wivrn_session(in6_addr address, int port, bool tcp_only, crypto::
}
}
wivrn_session::wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
control(address, port), stream(-1), address(address)
wivrn_session::wivrn_session(sockaddr_in address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter) :
control(address), stream(-1), address(address.sin_addr)
{
try
{
+2 -2
View File
@@ -55,8 +55,8 @@ private:
public:
std::variant<in_addr, in6_addr> address;
wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(sockaddr_in6 address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(sockaddr_in address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(const wivrn_session &) = delete;
wivrn_session & operator=(const wivrn_session &) = delete;
+48 -42
View File
@@ -60,6 +60,16 @@ static bool operator==(const in6_addr & a, const in6_addr & b)
return memcmp(&a, &b, sizeof(a)) == 0;
}
static bool operator==(const sockaddr_in & a, const sockaddr_in & b)
{
return a.sin_addr == b.sin_addr and a.sin_port == b.sin_port;
}
static bool operator==(const sockaddr_in6 & a, const sockaddr_in6 & b)
{
return a.sin6_addr == b.sin6_addr and a.sin6_port == b.sin6_port;
}
class dnssd_cache
{
public:
@@ -70,8 +80,8 @@ public:
int port;
bool operator==(const srv &) const noexcept = default;
};
using a = in_addr;
using aaaa = in6_addr;
using a = sockaddr_in;
using aaaa = sockaddr_in6;
struct txt
{
std::string key;
@@ -85,6 +95,7 @@ private:
steady_clock::time_point timeout;
std::string name;
std::variant<ptr, srv, a, aaaa, txt> record;
std::string if_name;
bool operator==(const cache_entry & other) const noexcept
{
@@ -95,6 +106,8 @@ private:
std::vector<cache_entry> cache;
std::vector<std::tuple<steady_clock::time_point, mdns_record_type, std::string>> last_queries;
std::map<int, std::string> if_names;
// pollfds[0] is used for inter-thread communication, the others are for mDNS
std::vector<pollfd> pollfds;
int itc_fd;
@@ -102,7 +115,6 @@ private:
void open_client_sockets(int port = 0)
{
close_client_sockets();
std::vector<int> sockets;
ifaddrs * addresses;
if (getifaddrs(&addresses) < 0)
@@ -114,9 +126,6 @@ private:
const int required_flags = IFF_UP | IFF_MULTICAST;
const int forbidden_flags = IFF_LOOPBACK;
// std::vector<std::string> interfaces_ipv4;
// std::vector<std::string> interfaces_ipv6;
for (ifaddrs * i = addresses; i; i = i->ifa_next)
{
spdlog::debug("address {} flags 0x{:x} required 0x{:x} forbidden 0x{:x}", uintptr_t(i->ifa_addr), i->ifa_flags, required_flags, forbidden_flags);
@@ -131,16 +140,7 @@ private:
if (i->ifa_addr->sa_family == AF_INET6)
{
// if (std::any_of(interfaces_ipv6.begin(), interfaces_ipv6.end(), [&](const auto & j) { return j == i->ifa_name; }))
// continue;
struct sockaddr_in6 * saddr = (struct sockaddr_in6 *)i->ifa_addr;
// Ignore link-local addresses
if (saddr->sin6_scope_id)
{
spdlog::debug("Ignoring link-local address {}", ip_address_to_string(i->ifa_addr));
continue;
}
static const unsigned char localhost[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
static const unsigned char localhost_mapped[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1};
if (memcmp(saddr->sin6_addr.s6_addr, localhost, 16) &&
@@ -150,10 +150,9 @@ private:
int sock = mdns_socket_open_ipv6(saddr);
if (sock >= 0)
{
sockets.push_back(sock);
if_names.emplace(sock, i->ifa_name);
spdlog::info("Local IPv6 address: {}", ip_address_to_string(i->ifa_addr));
// interfaces_ipv6.push_back(i->ifa_name);
}
else
{
@@ -165,9 +164,6 @@ private:
}
else if (i->ifa_addr->sa_family == AF_INET)
{
// if (std::any_of(interfaces_ipv4.begin(), interfaces_ipv4.end(), [&](const auto & j) { return j == i->ifa_name; }))
// continue;
struct sockaddr_in * saddr = (struct sockaddr_in *)i->ifa_addr;
if (saddr->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
{
@@ -175,10 +171,9 @@ private:
int sock = mdns_socket_open_ipv4(saddr);
if (sock >= 0)
{
sockets.push_back(sock);
if_names.emplace(sock, i->ifa_name);
spdlog::info("Local IPv4 address: {}", ip_address_to_string(i->ifa_addr));
// interfaces_ipv4.push_back(i->ifa_name);
}
else
{
@@ -196,7 +191,7 @@ private:
freeifaddrs(addresses);
pollfds.reserve(sockets.size() + 1);
pollfds.reserve(if_names.size() + 1);
int fds[2];
if (pipe(fds) < 0)
@@ -208,7 +203,7 @@ private:
itc_fd = fds[1];
pollfds.push_back({.fd = fds[0], .events = POLLIN});
for (int socket: sockets)
for (auto [socket, _]: if_names)
pollfds.push_back(pollfd{.fd = socket, .events = POLLIN});
}
@@ -218,6 +213,7 @@ private:
mdns_socket_close(socket.fd);
pollfds.clear();
if_names.clear();
itc_fd = -1;
}
@@ -240,13 +236,13 @@ private:
{
type = "A";
auto addr = std::get<a>(entry.record);
record = ip_address_to_string(addr);
record = ip_address_to_string(addr.sin_addr);
}
else if (std::holds_alternative<aaaa>(entry.record))
{
type = "AAAA";
auto addr = std::get<aaaa>(entry.record);
record = ip_address_to_string(addr);
record = ip_address_to_string(addr.sin6_addr);
}
else if (std::holds_alternative<txt>(entry.record))
{
@@ -285,7 +281,7 @@ private:
spdlog::info("Sending query for {}, type {}", service_name, record_type_name(record_type));
}
void update(mdns_string_t entry, std::variant<ptr, srv, a, aaaa, txt> record, int ttl)
void update(mdns_string_t entry, std::variant<ptr, srv, a, aaaa, txt> record, int ttl, std::string if_name)
{
auto now = steady_clock::now();
@@ -294,6 +290,7 @@ private:
.timeout = now + seconds(ttl),
.name = std::string(entry.str, entry.length),
.record = std::move(record),
.if_name = std::move(if_name),
};
// log_entry(e);
@@ -342,26 +339,28 @@ private:
case MDNS_RECORDTYPE_PTR: {
char namebuffer[256];
auto ptr_record = mdns_record_parse_ptr(data, size, record_offset, record_length, namebuffer, sizeof(namebuffer));
self->update(entrystr, std::string{ptr_record.str, ptr_record.length}, ttl);
self->update(entrystr, std::string{ptr_record.str, ptr_record.length}, ttl, self->if_names[sock]);
break;
}
case MDNS_RECORDTYPE_SRV: {
char namebuffer[256];
auto srv_record = mdns_record_parse_srv(data, size, record_offset, record_length, namebuffer, sizeof(namebuffer));
self->update(entrystr, srv{{srv_record.name.str, srv_record.name.length}, srv_record.port}, ttl);
self->update(entrystr, srv{{srv_record.name.str, srv_record.name.length}, srv_record.port}, ttl, self->if_names[sock]);
break;
}
case MDNS_RECORDTYPE_A: {
sockaddr_in ipv4_buffer;
self->update(entrystr, mdns_record_parse_a(data, size, record_offset, record_length, &ipv4_buffer)->sin_addr, ttl);
self->update(entrystr, *mdns_record_parse_a(data, size, record_offset, record_length, &ipv4_buffer), ttl, self->if_names[sock]);
break;
}
case MDNS_RECORDTYPE_AAAA: {
sockaddr_in6 ipv6_buffer;
self->update(entrystr, mdns_record_parse_aaaa(data, size, record_offset, record_length, &ipv6_buffer)->sin6_addr, ttl);
mdns_record_parse_aaaa(data, size, record_offset, record_length, &ipv6_buffer);
ipv6_buffer.sin6_scope_id = ((sockaddr_in6 *)from)->sin6_scope_id;
self->update(entrystr, ipv6_buffer, ttl, self->if_names[sock]);
break;
}
@@ -373,7 +372,7 @@ private:
{
std::string key{txt_buffer[i].key.str, txt_buffer[i].key.length};
std::string value{txt_buffer[i].value.str, txt_buffer[i].value.length};
self->update(entrystr, txt{key, value}, ttl);
self->update(entrystr, txt{key, value}, ttl, self->if_names[sock]);
}
break;
}
@@ -448,9 +447,9 @@ public:
}
template <typename T>
std::vector<std::pair<T, steady_clock::time_point>> read(const std::string & name)
std::vector<std::tuple<T, std::string, steady_clock::time_point>> read(const std::string & name)
{
std::vector<std::pair<T, steady_clock::time_point>> entries;
std::vector<std::tuple<T, std::string, steady_clock::time_point>> entries;
auto now = steady_clock::now();
std::erase_if(cache, [now](const cache_entry & i) {
@@ -460,7 +459,7 @@ public:
for (const auto & i: cache)
{
if (i.name == name && std::holds_alternative<T>(i.record))
entries.emplace_back(std::get<T>(i.record), i.timeout);
entries.emplace_back(std::get<T>(i.record), i.if_name, i.timeout);
}
return entries;
@@ -503,7 +502,7 @@ void wivrn_discover::discover(std::string service_name)
}
poll_timeout = std::min(poll_timeout, duration_cast<milliseconds>(next_query - now));
for (auto [ptr, ttl]: cache->read<dnssd_cache::ptr>(service_name))
for (auto [ptr, _, ttl]: cache->read<dnssd_cache::ptr>(service_name))
{
bool srv_found = false;
auto srv_min_ttl = milliseconds::max();
@@ -515,7 +514,9 @@ void wivrn_discover::discover(std::string service_name)
else
s.name = ptr;
for (auto [srv, ttl]: cache->read<dnssd_cache::srv>(ptr))
std::multimap<std::string, decltype(service::address::address)> addr_by_if_name;
for (auto [srv, _, ttl]: cache->read<dnssd_cache::srv>(ptr))
{
srv_found = true;
srv_min_ttl = std::min(duration_cast<milliseconds>(ttl - now), srv_min_ttl);
@@ -533,21 +534,26 @@ void wivrn_discover::discover(std::string service_name)
bool address_found = false;
auto address_min_ttl = milliseconds::max();
for (auto [a, ttl]: cache->read<dnssd_cache::a>(srv.hostname))
for (auto [a, if_name, ttl]: cache->read<dnssd_cache::a>(srv.hostname))
{
address_found = true;
address_min_ttl = std::min(duration_cast<milliseconds>(ttl - now), address_min_ttl);
s.addresses.push_back(a);
a.sin_port = htons(srv.port);
addr_by_if_name.emplace(if_name, a);
}
for (auto [aaaa, ttl]: cache->read<dnssd_cache::aaaa>(srv.hostname))
for (auto [aaaa, if_name, ttl]: cache->read<dnssd_cache::aaaa>(srv.hostname))
{
address_found = true;
address_min_ttl = std::min(duration_cast<milliseconds>(ttl - now), address_min_ttl);
s.addresses.push_back(aaaa);
aaaa.sin6_port = htons(srv.port);
addr_by_if_name.emplace(if_name, aaaa);
}
for (auto [txt_record, ttl]: cache->read<dnssd_cache::txt>(ptr))
for (auto && [if_name, addr]: addr_by_if_name)
s.addresses.push_back({.interface = std::move(if_name), .address = addr});
for (auto [txt_record, _, ttl]: cache->read<dnssd_cache::txt>(ptr))
{
s.txt[txt_record.key] = txt_record.value;
}
+6 -1
View File
@@ -19,12 +19,17 @@ class wivrn_discover
public:
struct service
{
struct address
{
std::string interface;
std::variant<sockaddr_in, sockaddr_in6> address;
};
std::string name;
std::string hostname;
int port;
bool tcp_only = false;
std::vector<std::variant<in_addr, in6_addr>> addresses;
std::vector<address> addresses;
std::map<std::string, std::string> txt;
std::chrono::steady_clock::time_point ttl;
std::string pin;
+4 -24
View File
@@ -85,24 +85,14 @@ void wivrn::UDP::bind(sockaddr_in6 address)
throw std::system_error{errno, std::generic_category()};
}
void wivrn::UDP::connect(in6_addr address, int port)
void wivrn::UDP::connect(sockaddr_in6 sa)
{
sockaddr_in6 sa;
sa.sin6_family = AF_INET6;
sa.sin6_addr = address;
sa.sin6_port = htons(port);
if (::connect(fd, (sockaddr *)&sa, sizeof(sa)) < 0)
throw std::system_error{errno, std::generic_category()};
}
void wivrn::UDP::connect(in_addr address, int port)
void wivrn::UDP::connect(sockaddr_in sa)
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr = address;
sa.sin_port = htons(port);
if (::connect(fd, (sockaddr *)&sa, sizeof(sa)) < 0)
throw std::system_error{errno, std::generic_category()};
}
@@ -173,18 +163,13 @@ wivrn::TCP::TCP(int fd)
init();
}
wivrn::TCP::TCP(in6_addr address, int port)
wivrn::TCP::TCP(sockaddr_in6 sa)
{
fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fd < 0)
throw std::system_error{errno, std::generic_category()};
fcntl(fd, F_SETFD, FD_CLOEXEC);
sockaddr_in6 sa;
sa.sin6_family = AF_INET6;
sa.sin6_addr = address;
sa.sin6_port = htons(port);
if (connect(fd, (sockaddr *)&sa, sizeof(sa)) < 0)
{
::close(fd);
@@ -194,18 +179,13 @@ wivrn::TCP::TCP(in6_addr address, int port)
init();
}
wivrn::TCP::TCP(in_addr address, int port)
wivrn::TCP::TCP(sockaddr_in sa)
{
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
throw std::system_error{errno, std::generic_category()};
fcntl(fd, F_SETFD, FD_CLOEXEC);
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr = address;
sa.sin_port = htons(port);
if (connect(fd, (sockaddr *)&sa, sizeof(sa)) < 0)
{
::close(fd);
+4 -4
View File
@@ -118,8 +118,8 @@ public:
size_t send_raw(serialization_packet && packet);
size_t send_many_raw(std::span<serialization_packet> packets);
void connect(in6_addr address, int port);
void connect(in_addr address, int port);
void connect(sockaddr_in6 address);
void connect(sockaddr_in address);
void bind(sockaddr_in6 address);
void subscribe_multicast(in6_addr address);
void unsubscribe_multicast(in6_addr address);
@@ -144,8 +144,8 @@ class TCP : public fd_base
public:
TCP() = default;
TCP(in6_addr address, int port);
TCP(in_addr address, int port);
TCP(sockaddr_in6 address);
TCP(sockaddr_in address);
explicit TCP(int fd);
deserialization_packet receive_raw();
+2 -1
View File
@@ -260,7 +260,8 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
if (client_port >= 0)
{
stream.connect(client_address.sin6_addr, client_port);
client_address.sin6_port = htons(client_port);
stream.connect(client_address);
stream.set_send_buffer_size(1024 * 1024 * 5);
}
else