/// <summary> /// Gets the data plan status for either a machine-wide internet connection , or the first-hop of routing to a specific destination /// on a connection. If an IPv4/IPv6 address is not specified, this method returns the data plan status of the connection used for /// machine-wide Internet connectivity. /// </summary> /// <param name="destIPAddr"> /// The destination IPv4/IPv6 address. If <see langword="null"/>, this method will instead return the data plan status of the /// connection used for machine-wide Internet connectivity. /// </param> /// <returns> /// An NLM_DATAPLAN_STATUS structure that describes the data plan status associated with a connection used to route to a destination. /// If destIPAddr specifies a tunnel address, the first available data plan status in the interface stack is returned. /// </returns> public static NLM_DATAPLAN_STATUS GetConnectionDataPlanStatus(IPAddress destIPAddr = null) { var addr = NLM_SOCKADDR.FromIPAddress(destIPAddr); var cost = new NLM_DATAPLAN_STATUS(); (costmgr ?? (costmgr = (INetworkCostManager)Manager))?.GetDataPlanStatus(out cost, addr); return(cost); }
/// <summary> /// Gets the current cost of either a machine-wide internet connection, or the first-hop of routing to a specific destination on a /// connection. If destIPaddr is NULL, this method instead returns the cost of the network used for machine-wide Internet connectivity. /// </summary> /// <param name="destIPAddr"> /// The destination IPv4/IPv6 address. If <see langword="null"/>, this method will instead return the cost associated with the /// preferred connection used for machine Internet connectivity. /// </param> /// <returns>The cost of the connection.</returns> public static NLM_CONNECTION_COST GetConnectionCost(IPAddress destIPAddr = null) { var addr = NLM_SOCKADDR.FromIPAddress(destIPAddr); var cost = NLM_CONNECTION_COST.NLM_CONNECTION_COST_UNKNOWN; (costmgr ?? (costmgr = (INetworkCostManager)Manager))?.GetCost(out cost, addr); return(cost); }
/// <summary>Creates a <see cref="NLM_SOCKADDR"/> from an <see cref="IPAddress"/> instance.</summary> /// <param name="address">The IP address to encapsulate.</param> /// <returns>A <see cref="NLM_SOCKADDR"/> instance with its data field set to either the IPv4 or IPv6 address supplied by <paramref name="address"/>.</returns> public static NLM_SOCKADDR FromIPAddress(IPAddress address) { const ushort AF_INET = 2; const ushort AF_INET6 = 23; if (address == null) { throw new ArgumentNullException(nameof(address)); } var sockAddr = new NLM_SOCKADDR { data = new byte[dataSize] }; // Seems to be compatible with SOCKADDR_STORAGE, which in turn is compatible with SOCKADDR_IN and SOCKADDR_IN6 using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data))) { if (address.AddressFamily == AddressFamily.InterNetwork) { // AF_INT writer.Write(AF_INET); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); } else { // AF_INT6 writer.Write(AF_INET6); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); // Scope ID writer.Write((ulong)address.ScopeId); } } return(sockAddr); }
private static NLM_SOCKADDR CreateIpv4SockAddr(IPAddress address) { var sockAddr = new NLM_SOCKADDR() { data = new byte[sockaddrDataSize] }; using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data))) { // AF_INT writer.Write(AF_INET); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); } return(sockAddr); }
private static NLM_SOCKADDR CreateIPv6SockAddr(IPAddress address) { var sockAddr = new NLM_SOCKADDR() { data = new byte[sockaddrDataSize] }; // Seems to be compatible with SOCKADDR_STORAGE, which in turn is compatible with SOCKADDR_IN6 using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data))) { // AF_INT6 writer.Write(AF_INET6); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); // Scope ID writer.Write((ulong)address.ScopeId); } return(sockAddr); }
private static NLM_SOCKADDR CreateIpv4SockAddr(IPAddress address) { var sockAddr = new NLM_SOCKADDR() { data = new byte[sockaddrDataSize] }; using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data))) { // AF_INT writer.Write(AF_INET); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); } return sockAddr; }
private static NLM_SOCKADDR CreateIPv6SockAddr(IPAddress address) { var sockAddr = new NLM_SOCKADDR() { data = new byte[sockaddrDataSize] }; // Seems to be compatible with SOCKADDR_STORAGE, which in turn is compatible with SOCKADDR_IN6 using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data))) { // AF_INT6 writer.Write(AF_INET6); // Port writer.Write((ushort)0); // Flow Info writer.Write((uint)0); // Address writer.Write(address.GetAddressBytes()); // Scope ID writer.Write((ulong)address.ScopeId); } return sockAddr; }