aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities/NetworkHelpers.cs
diff options
context:
space:
mode:
authorTSRBerry <20988865+TSRBerry@users.noreply.github.com>2023-04-16 17:25:20 +0200
committerGitHub <noreply@github.com>2023-04-16 15:25:20 +0000
commit69b6ef7a4ae36994c293e423e1203096c294744c (patch)
tree3e8de70aee70ae0d28fd2684bea6e6606142d9cf /Ryujinx.Common/Utilities/NetworkHelpers.cs
parent40e87c634ece65da3f740fcfbb6acb43e5cd71b3 (diff)
[GUI] Add network interface dropdown (#4597)1.1.717
* Add network adapter dropdown from LDN build * Ava: Add NetworkInterfaces to SettingsNetworkTab * Add headless network interface option * Add network interface dropdown to Avalonia * Fix handling network interfaces without a gateway address * gtk: Actually save selected network interface to config * Increment config version
Diffstat (limited to 'Ryujinx.Common/Utilities/NetworkHelpers.cs')
-rw-r--r--Ryujinx.Common/Utilities/NetworkHelpers.cs66
1 files changed, 66 insertions, 0 deletions
diff --git a/Ryujinx.Common/Utilities/NetworkHelpers.cs b/Ryujinx.Common/Utilities/NetworkHelpers.cs
new file mode 100644
index 00000000..b2f6f45d
--- /dev/null
+++ b/Ryujinx.Common/Utilities/NetworkHelpers.cs
@@ -0,0 +1,66 @@
+using System.Net.NetworkInformation;
+
+namespace Ryujinx.Common.Utilities
+{
+ public static class NetworkHelpers
+ {
+ private static (IPInterfaceProperties, UnicastIPAddressInformation) GetLocalInterface(NetworkInterface adapter, bool isPreferred)
+ {
+ IPInterfaceProperties properties = adapter.GetIPProperties();
+
+ if (isPreferred || (properties.GatewayAddresses.Count > 0 && properties.DnsAddresses.Count > 0))
+ {
+ foreach (UnicastIPAddressInformation info in properties.UnicastAddresses)
+ {
+ // Only accept an IPv4 address
+ if (info.Address.GetAddressBytes().Length == 4)
+ {
+ return (properties, info);
+ }
+ }
+ }
+
+ return (null, null);
+ }
+
+ public static (IPInterfaceProperties, UnicastIPAddressInformation) GetLocalInterface(string lanInterfaceId = "0")
+ {
+ if (!NetworkInterface.GetIsNetworkAvailable())
+ {
+ return (null, null);
+ }
+
+ IPInterfaceProperties targetProperties = null;
+ UnicastIPAddressInformation targetAddressInfo = null;
+
+ NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
+
+ string guid = lanInterfaceId;
+ bool hasPreference = guid != "0";
+
+ foreach (NetworkInterface adapter in interfaces)
+ {
+ bool isPreferred = adapter.Id == guid;
+
+ // Ignore loopback and non IPv4 capable interface.
+ if (isPreferred || (targetProperties == null && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && adapter.Supports(NetworkInterfaceComponent.IPv4)))
+ {
+ (IPInterfaceProperties properties, UnicastIPAddressInformation info) = GetLocalInterface(adapter, isPreferred);
+
+ if (properties != null)
+ {
+ targetProperties = properties;
+ targetAddressInfo = info;
+
+ if (isPreferred || !hasPreference)
+ {
+ break;
+ }
+ }
+ }
+ }
+
+ return (targetProperties, targetAddressInfo);
+ }
+ }
+} \ No newline at end of file