Skip to content

Commit 488387d

Browse files
authored
fix(metering): Catch exceptions when retrieving network interface (fixes #41) (#43)
For example if properties are unsupported due to disabled IPv6
1 parent 0d7b3d4 commit 488387d

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/SyncTrayzor/Services/Metering/NetworkCostManager.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,24 @@ await Task.Run(async () =>
4848

4949
private async Task<bool> IsConnectionMeteredUnsafe(IPAddress address)
5050
{
51-
var profile = await NetworkUtils.GetNetworkProfileForIpAddress(address);
52-
if (profile == null)
51+
try
5352
{
53+
var profile = await NetworkUtils.GetNetworkProfileForIpAddress(address);
54+
if (profile == null)
55+
{
56+
return false;
57+
}
58+
59+
var cost = profile.GetConnectionCost();
60+
// A network is "metered" if it is not unrestricted or unknown:
61+
return cost.NetworkCostType != NetworkCostType.Unrestricted &&
62+
cost.NetworkCostType != NetworkCostType.Unknown;
63+
}
64+
catch (Exception exception)
65+
{
66+
Logger.Warn(exception, $"Unable to retrieve network profile for IP {address}");
5467
return false;
5568
}
56-
57-
var cost = profile.GetConnectionCost();
58-
// A network is "metered" if it is not unrestricted or unknown:
59-
return cost.NetworkCostType != NetworkCostType.Unrestricted &&
60-
cost.NetworkCostType != NetworkCostType.Unknown;
6169
}
6270

6371
private void NetworkStatusChanged(object sender)

src/SyncTrayzor/Services/Metering/NetworkUtils.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,31 @@ public static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
106106
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
107107
{
108108
var ipProperties = networkInterface.GetIPProperties();
109-
switch (remoteAddress.AddressFamily)
109+
try
110110
{
111-
case AddressFamily.InterNetwork:
112-
if (ipProperties.GetIPv4Properties().Index == interfaceIndex)
113-
{
114-
return networkInterface;
115-
}
116-
117-
break;
118-
case AddressFamily.InterNetworkV6:
119-
if (ipProperties.GetIPv6Properties().Index == interfaceIndex)
120-
{
121-
return networkInterface;
122-
}
123-
124-
break;
125-
default:
126-
throw new ArgumentOutOfRangeException(nameof(remoteAddress));
111+
switch (remoteAddress.AddressFamily)
112+
{
113+
case AddressFamily.InterNetwork:
114+
if (ipProperties.GetIPv4Properties().Index == interfaceIndex)
115+
{
116+
return networkInterface;
117+
}
118+
119+
break;
120+
case AddressFamily.InterNetworkV6:
121+
if (ipProperties.GetIPv6Properties().Index == interfaceIndex)
122+
{
123+
return networkInterface;
124+
}
125+
126+
break;
127+
default:
128+
throw new ArgumentOutOfRangeException(nameof(remoteAddress));
129+
}
130+
}
131+
catch (NetworkInformationException)
132+
{
133+
// Interface does not support the IP address, ignore
127134
}
128135
}
129136

@@ -133,6 +140,7 @@ public static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
133140
public static async Task<ConnectionProfile?> GetNetworkProfileForIpAddress(IPAddress remoteAddress)
134141
{
135142
var networkInterface = GetBestNetworkInterface(remoteAddress);
143+
136144
var networkInterfaceGuid = Guid.Parse(networkInterface.Id);
137145
var adapters = NetworkInformation.GetConnectionProfiles()
138146
.Select(existingProfile => existingProfile.NetworkAdapter).Distinct();

0 commit comments

Comments
 (0)