From 627393d1b9ca8aba66869a766091c5b420c28122 Mon Sep 17 00:00:00 2001 From: Karl Hiramoto Date: Fri, 7 Feb 2014 17:18:10 +0100 Subject: [PATCH 1/2] Allow NetworkInterfaceType.GigabitEthernet interfaces Tunnel adapter isatap interfaces case an exception in GetProperties() https://github.com/utapyngo/icsmanager/issues/3 It seems safe to ignore these. --- IcsManagerLibrary/IcsManager.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/IcsManagerLibrary/IcsManager.cs b/IcsManagerLibrary/IcsManager.cs index b5a4ec4..8b50104 100644 --- a/IcsManagerLibrary/IcsManager.cs +++ b/IcsManagerLibrary/IcsManager.cs @@ -17,6 +17,7 @@ from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.Supports(NetworkInterfaceComponent.IPv4) where (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) || (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) + || (nic.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet) select nic; } @@ -81,9 +82,21 @@ public static INetConnection FindConnectionByIdOrName(string shared) public static INetConnection GetConnectionById(string guid) { - return (from INetConnection c in GetAllConnections() - where GetProperties(c).Guid == guid - select c).DefaultIfEmpty(null).First(); + INetSharingEveryConnectionCollection connections = GetAllConnections(); + foreach (INetConnection c in connections) + { + try + { + INetConnectionProps props = GetProperties(c); + if (props.Guid == guid) + return c; + } + catch + { + // Ignore these It'ts known that Tunnel adapter isatap causes getProperties to fail. + } + } + return null; } public static INetConnection GetConnectionByName(string name) From e21f16b72ffcf5cb92bce4aaeb8dc336f4e24d84 Mon Sep 17 00:00:00 2001 From: Karl Hiramoto Date: Fri, 7 Feb 2014 18:16:22 +0100 Subject: [PATCH 2/2] Handle exceptions. GetProperties() and GetConfiguration() can throw exceptions on some tunnel interfaces. Ignore the exceptions, as sharing them doesn't make sense anyways. --- IcsManagerLibrary/IcsManager.cs | 56 +++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/IcsManagerLibrary/IcsManager.cs b/IcsManagerLibrary/IcsManager.cs index 8b50104..154d593 100644 --- a/IcsManagerLibrary/IcsManager.cs +++ b/IcsManagerLibrary/IcsManager.cs @@ -23,18 +23,28 @@ where nic.Supports(NetworkInterfaceComponent.IPv4) public static NetShare GetCurrentlySharedConnections() { - INetConnection sharedConnection = ( - from INetConnection c in SharingManager.EnumEveryConnection - where GetConfiguration(c).SharingEnabled - where GetConfiguration(c).SharingConnectionType == - tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC - select c).DefaultIfEmpty(null).First(); - INetConnection homeConnection = ( - from INetConnection c in SharingManager.EnumEveryConnection - where GetConfiguration(c).SharingEnabled - where GetConfiguration(c).SharingConnectionType == - tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE - select c).DefaultIfEmpty(null).First(); + INetConnection sharedConnection = null; + INetConnection homeConnection = null; + INetSharingEveryConnectionCollection connections = SharingManager.EnumEveryConnection; + foreach (INetConnection c in connections) + { + try + { + + INetSharingConfiguration config = GetConfiguration(c); + if (config.SharingEnabled) + { + if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) + sharedConnection = c; + else if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE) + homeConnection = c; + } + } + catch (System.Runtime.InteropServices.ExternalException) + { + } + } + return new NetShare(sharedConnection, homeConnection); } @@ -91,8 +101,8 @@ public static INetConnection GetConnectionById(string guid) if (props.Guid == guid) return c; } - catch - { + catch (System.Runtime.InteropServices.ExternalException) + { // Ignore these It'ts known that Tunnel adapter isatap causes getProperties to fail. } } @@ -101,9 +111,21 @@ public static INetConnection GetConnectionById(string guid) public static INetConnection GetConnectionByName(string name) { - return (from INetConnection c in GetAllConnections() - where GetProperties(c).Name == name - select c).DefaultIfEmpty(null).First(); + INetSharingEveryConnectionCollection connections = GetAllConnections(); + foreach (INetConnection c in connections) + { + try + { + INetConnectionProps props = GetProperties(c); + if (props.Name == name) + return c; + } + catch (System.Runtime.InteropServices.ExternalException) + { + // Ignore these It'ts known that Tunnel adapter isatap causes getProperties to fail. + } + } + return null; } }