using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Web; namespace TBF.BenchControl.Network.Adapter { /// /// Class for the storing information about a network adapter. /// public class AdapterInfo { public string Description; /// Description of the network adapter. public IPAddress IPAddress; /// IP address of the network adapter. public IPAddress NetMask; /// Mask of the network adapter. /// Constructor public AdapterInfo(string description) { Description = description; } public IPAddress GetBroadcastAddress() { byte[] ipAdressBytes = IPAddress.GetAddressBytes(); byte[] subnetMaskBytes = NetMask.GetAddressBytes(); if (ipAdressBytes.Length != subnetMaskBytes.Length) throw new ArgumentException("Lengths of IP address and subnet mask do not match."); byte[] broadcastAddress = new byte[ipAdressBytes.Length]; for (int i = 0; i < broadcastAddress.Length; i++) { broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255)); } return new IPAddress(broadcastAddress); } ///------------------------------------------------------ /// Static members and functions ///------------------------------------------------------ /// /// Private list of the available network adapters. /// static List netAdapters; /// /// Retrieves the list of network adapters. /// Never returns 'null', calls RefreshNetAdaptersInfo() the first time it is used. /// If you want to refresh the list of the network adapters later, explicitly call /// the RefreshNetAdaptersInfo() method. /// public static List NetAdapters { get { if (netAdapters == null) RefreshNetAdaptersInfo(); return netAdapters; } } /// /// Refreshes the information about network adapters stored in the netAdapters list. /// public static void RefreshNetAdaptersInfo() { if (netAdapters == null) netAdapters = new List(); else netAdapters.Clear(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { AdapterInfo info = new AdapterInfo(adapter.Description); netAdapters.Add(info); IPInterfaceProperties properties = adapter.GetIPProperties(); if (properties == null) continue; UnicastIPAddressInformationCollection uniCast = properties.UnicastAddresses; if (uniCast == null) continue; foreach (UnicastIPAddressInformation uni in uniCast) { if (info == null) { info = new AdapterInfo(adapter.Description); netAdapters.Add(info); } info.IPAddress = uni.Address; if (uni.IPv4Mask != null) info.NetMask = uni.IPv4Mask; info = null; } } } /// /// Gets the loopback network adapter. /// public static AdapterInfo LoopbackAdapter { get { List adapters = NetAdapters; foreach (AdapterInfo a in adapters) { if (a.IPAddress != null && a.IPAddress.Equals(IPAddress.Loopback)) return a; } return null; } } /// /// Gets the default network adapter - the first one which his not a loopback adapter. /// Network adapters with IP address have preference, adapters without IP address /// (like an unconnected wireless adapter) are used just when there is no adapter with IP. /// /// Default adapter is used when none was specified in the configuration file /// and it is used to initialize Options-General dialog-tab. /// public static AdapterInfo DefaultAdapter { get { List adapters = NetAdapters; /// Make a copy to avoid interference AdapterInfo adapterWithoutIP = null; /// Use this one if there is no other better adapter foreach (AdapterInfo a in adapters) { if (!a.IPAddress.Equals(IPAddress.Loopback)) { if (a.IPAddress != null) return a; else if (adapterWithoutIP == null) adapterWithoutIP = a; } } return adapterWithoutIP == null ? LoopbackAdapter : adapterWithoutIP; } } /// /// Get network adapter info from the description string. /// /// Description /// AdapterInfo object reference public static AdapterInfo GetNetAdapter(string description) { List adapters = NetAdapters; foreach (AdapterInfo a in adapters) { if (a.Description != description || a.IPAddress == null || a.IPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 || a.IPAddress.Equals(IPAddress.Loopback) || a.NetMask == null) { continue; } return a; } return DefaultAdapter; } /// /// Encodes the given string as URL parameter. /// /// String to be encoded. /// String encoded as the URL parameter. public static string UrlPathEncode(string pathToEncode) { StringBuilder sb = new StringBuilder(pathToEncode.Length * 3); sb.Append(pathToEncode); // % must be first!!! sb.Replace("%", "%" + Convert.ToInt32('%').ToString("X2")); // ?, & and / are not convertable by HttpUtility.UrlPathEncode sb.Replace("?", "%" + Convert.ToInt32('?').ToString("X2")); sb.Replace("&", "%" + Convert.ToInt32('&').ToString("X2")); sb.Replace("/", "%" + Convert.ToInt32('/').ToString("X2")); // Additional chars. E.g.: # didn't work in the path passed to the Uri class. "c:\\sour#ce\\my # proj". sb.Replace("#", "%" + Convert.ToInt32('#').ToString("X2")); // Backslash must be removed because it is not accepted by Uri class when passing path into it. E.g.: "C:%5Csource" is not acceptable. //sb.Replace("\\", "%" + Convert.ToInt32('\\').ToString("X2")); //// I removed following chars because I do not know if it is good idea to replace them (see previous line with '\\' char). sb.Replace("\r", "%" + Convert.ToInt32('\r').ToString("X2")); sb.Replace("\n", "%" + Convert.ToInt32('\n').ToString("X2")); sb.Replace("\t", "%" + Convert.ToInt32('\t').ToString("X2")); string s = sb.ToString(); s = HttpUtility.UrlPathEncode(s); //sb.Length = 0; //sb.Append(HttpUtility.UrlPathEncode(s)); //for (int i = 0; i < sb.Length; i += 3) //{ // char c = sb[i]; // if (c == '%') // continue; // sb[i] = '%'; // sb.Insert(i + 1, Convert.ToUInt64(c).ToString("X2")); //} return s; } } }