public static IPv4Address GetRoutableInterface(IPv4Address routeTo) { IPv4Address ret = new IPv4Address(); UInt32 routeToBytes = routeTo.AddressAsUInt32(); foreach (NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces()) { if (netif.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProperties = netif.GetIPProperties(); foreach (UnicastIPAddressInformation addressInfo in ipProperties.UnicastAddresses) { if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork) { IPv4Address ifAddress = new IPv4Address(addressInfo.Address); try { if (addressInfo.IPv4Mask != null) { byte[] maskBytes = addressInfo.IPv4Mask.GetAddressBytes(); if (BitConverter.IsLittleEndian) { Array.Reverse(maskBytes); } UInt32 ifaddressMask = BitConverter.ToUInt32(maskBytes, 0); if ((routeToBytes & ifaddressMask) == (ifAddress.AddressAsUInt32() & ifaddressMask)) { ret = ifAddress; break; } } } catch (Exception) { /** Mono will except on the IPv4Mask property... just assign it this address */ ret = ifAddress; Log.SysLogText(LogLevel.INFO, "GetRoutableInterface - assigning {0}", ifAddress); } } } } } return(ret); }