diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e53a7388..adbfc836 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -30,7 +30,9 @@
+
+
diff --git a/client/android/usb.cpp b/client/android/usb.cpp
new file mode 100644
index 00000000..c7b26fee
--- /dev/null
+++ b/client/android/usb.cpp
@@ -0,0 +1,60 @@
+/*
+ * WiVRn VR streaming
+ * Copyright (C) 2026 Patrick Nicolas
+ *
+ * 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 .
+ */
+
+#include "application.h"
+#include "jni.h"
+
+#include
+#include
+
+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);
+}
diff --git a/client/android/usb.h b/client/android/usb.h
new file mode 100644
index 00000000..60626ff9
--- /dev/null
+++ b/client/android/usb.h
@@ -0,0 +1,19 @@
+/*
+ * WiVRn VR streaming
+ * Copyright (C) 2026 Patrick Nicolas
+ *
+ * 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 .
+ */
+
+bool usb_link_properties_changed();
diff --git a/client/application.cpp b/client/application.cpp
index 4f4f4791..2f110eb2 100644
--- a/client/application.cpp
+++ b/client/application.cpp
@@ -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 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>("getApplication");
+ auto ctx = app.call>("getApplicationContext");
+ auto system_service = ctx.call>("getSystemService", jni::string("connectivity"));
+
+ auto cb = act.field>("netcb");
+ try
+ {
+ if (enabled)
+ {
+ auto req = jni::new_object<"android/net/NetworkRequest$Builder">()
+ .call>("removeCapability", jni::Int(12) /*NET_CAPABILITY_INTERNET*/)
+ .call>("removeCapability", jni::Int(14) /*NET_CAPABILITY_TRUSTED*/)
+ .call>("addTransportType", jni::Int(8) /*TRANSPORT_USB*/)
+ .call>("build");
+
+ // system_service.call("requestNetwork", req, jni::new_object<"org/meumeu/wivrn/NetworkInfoCallback">());
+ system_service.call("requestNetwork", req, cb);
+ }
+ else
+ {
+ system_service.call("unregisterNetworkCallback", cb);
+ }
+ }
+ catch (...)
+ {}
+}
+#endif
+
void application::poll_actions()
{
std::array action_sets{
diff --git a/client/application.h b/client/application.h
index aad85da7..2d2d115c 100644
--- a/client/application.h
+++ b/client/application.h
@@ -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
diff --git a/client/configuration.cpp b/client/configuration.cpp
index 96bb2757..3d31fa47 100644
--- a/client/configuration.cpp
+++ b/client/configuration.cpp
@@ -346,6 +346,7 @@ const std::vector & 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;
}
diff --git a/client/configuration.h b/client/configuration.h
index 502e798c..210cd824 100644
--- a/client/configuration.h
+++ b/client/configuration.h
@@ -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;
diff --git a/client/hmd_traits.cpp b/client/hmd_traits.cpp
index d5ac1c0e..90b6fdbf 100644
--- a/client/hmd_traits.cpp
+++ b/client/hmd_traits.cpp
@@ -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")
diff --git a/client/hmd_traits.h b/client/hmd_traits.h
index 685cdd43..7f79d534 100644
--- a/client/hmd_traits.h
+++ b/client/hmd_traits.h
@@ -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 override_shader;
std::unordered_set blacklisted_extensions;
#ifndef NDEBUG
diff --git a/client/java/MainActivity.java b/client/java/MainActivity.java
index b1b690dc..e02230e4 100644
--- a/client/java/MainActivity.java
+++ b/client/java/MainActivity.java
@@ -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));
}
diff --git a/client/java/NetworkInfoCallback.java b/client/java/NetworkInfoCallback.java
new file mode 100644
index 00000000..baac3ae7
--- /dev/null
+++ b/client/java/NetworkInfoCallback.java
@@ -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();
+}
+
diff --git a/client/scenes/gui_settings.cpp b/client/scenes/gui_settings.cpp
index 99ed048f..d2b08dd8 100644
--- a/client/scenes/gui_settings.cpp
+++ b/client/scenes/gui_settings.cpp
@@ -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"),
diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp
index 5298fe13..70a8c562 100644
--- a/client/scenes/lobby.cpp
+++ b/client/scenes/lobby.cpp
@@ -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 scenes::lobby::connect_to_session(wivrn_discover::service service, bool manual_connection)
{
if (!manual_connection)
@@ -239,12 +253,18 @@ std::unique_ptr 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 scenes::lobby::connect_to_session(wivrn_discover:
}
std::string error;
- for (const std::variant & 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(address, service.port, service.tcp_only, keypair, [&](int fd) {
+ return std::make_unique(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 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 discovered_services = discover->get_services();
// TODO: only if discovered_services changed
diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp
index 9967b0f0..90f63f3b 100644
--- a/client/scenes/lobby_gui.cpp
+++ b/client/scenes/lobby_gui.cpp
@@ -56,6 +56,7 @@
#include
#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
diff --git a/client/wivrn_client.cpp b/client/wivrn_client.cpp
index 0298c9c2..0d99cdda 100644
--- a/client/wivrn_client.cpp
+++ b/client/wivrn_client.cpp
@@ -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
@@ -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 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 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 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 pin_enter) :
+ control(address), stream(-1), address(address.sin_addr)
{
try
{
diff --git a/client/wivrn_client.h b/client/wivrn_client.h
index 3cd0410e..f9249df7 100644
--- a/client/wivrn_client.h
+++ b/client/wivrn_client.h
@@ -55,8 +55,8 @@ private:
public:
std::variant address;
- wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function pin_enter);
- wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function pin_enter);
+ wivrn_session(sockaddr_in6 address, bool tcp_only, crypto::key & headset_keypair, std::function pin_enter);
+ wivrn_session(sockaddr_in address, bool tcp_only, crypto::key & headset_keypair, std::function pin_enter);
wivrn_session(const wivrn_session &) = delete;
wivrn_session & operator=(const wivrn_session &) = delete;
diff --git a/client/wivrn_discover.cpp b/client/wivrn_discover.cpp
index dd2bb8ae..747e954e 100644
--- a/client/wivrn_discover.cpp
+++ b/client/wivrn_discover.cpp
@@ -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 record;
+ std::string if_name;
bool operator==(const cache_entry & other) const noexcept
{
@@ -95,6 +106,8 @@ private:
std::vector cache;
std::vector> last_queries;
+ std::map if_names;
+
// pollfds[0] is used for inter-thread communication, the others are for mDNS
std::vector pollfds;
int itc_fd;
@@ -102,7 +115,6 @@ private:
void open_client_sockets(int port = 0)
{
close_client_sockets();
- std::vector 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 interfaces_ipv4;
- // std::vector 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(entry.record);
- record = ip_address_to_string(addr);
+ record = ip_address_to_string(addr.sin_addr);
}
else if (std::holds_alternative(entry.record))
{
type = "AAAA";
auto addr = std::get(entry.record);
- record = ip_address_to_string(addr);
+ record = ip_address_to_string(addr.sin6_addr);
}
else if (std::holds_alternative(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 record, int ttl)
+ void update(mdns_string_t entry, std::variant 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
- std::vector> read(const std::string & name)
+ std::vector> read(const std::string & name)
{
- std::vector> entries;
+ std::vector> 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(i.record))
- entries.emplace_back(std::get(i.record), i.timeout);
+ entries.emplace_back(std::get(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(next_query - now));
- for (auto [ptr, ttl]: cache->read(service_name))
+ for (auto [ptr, _, ttl]: cache->read(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(ptr))
+ std::multimap addr_by_if_name;
+
+ for (auto [srv, _, ttl]: cache->read(ptr))
{
srv_found = true;
srv_min_ttl = std::min(duration_cast(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(srv.hostname))
+ for (auto [a, if_name, ttl]: cache->read(srv.hostname))
{
address_found = true;
address_min_ttl = std::min(duration_cast(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(srv.hostname))
+ for (auto [aaaa, if_name, ttl]: cache->read(srv.hostname))
{
address_found = true;
address_min_ttl = std::min(duration_cast(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(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(ptr))
{
s.txt[txt_record.key] = txt_record.value;
}
diff --git a/client/wivrn_discover.h b/client/wivrn_discover.h
index d7a59946..4c4d996f 100644
--- a/client/wivrn_discover.h
+++ b/client/wivrn_discover.h
@@ -19,12 +19,17 @@ class wivrn_discover
public:
struct service
{
+ struct address
+ {
+ std::string interface;
+ std::variant address;
+ };
std::string name;
std::string hostname;
int port;
bool tcp_only = false;
- std::vector> addresses;
+ std::vector addresses;
std::map txt;
std::chrono::steady_clock::time_point ttl;
std::string pin;
diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp
index c05ee6e6..bc1341d6 100644
--- a/common/wivrn_sockets.cpp
+++ b/common/wivrn_sockets.cpp
@@ -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);
diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h
index 156aa42d..17cc802b 100644
--- a/common/wivrn_sockets.h
+++ b/common/wivrn_sockets.h
@@ -118,8 +118,8 @@ public:
size_t send_raw(serialization_packet && packet);
size_t send_many_raw(std::span 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();
diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp
index 64c7ff5b..ecabc94c 100644
--- a/server/driver/wivrn_connection.cpp
+++ b/server/driver/wivrn_connection.cpp
@@ -260,7 +260,8 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function= 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