using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Netch.Tools
{
public class Outbound
{
///
/// 索引
///
public uint Index;
///
/// 适配器
///
public NetworkInterface Interface;
///
/// 地址
///
public IPAddress Address;
///
/// 掩码
///
public IPAddress Netmask;
///
/// 网关
///
public IPAddress Gateway;
///
/// 获取数据
///
///
public bool Get()
{
if (Vanara.PInvoke.IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != Vanara.PInvoke.Win32Error.NO_ERROR)
return false;
this.Index = route.dwForwardIfIndex;
this.Interface = NetworkInterface.GetAllNetworkInterfaces().First(nic =>
{
var ipp = nic.GetIPProperties();
if (nic.Supports(NetworkInterfaceComponent.IPv4))
{
return ipp.GetIPv4Properties().Index == this.Index;
}
return false;
});
if (this.Interface == null)
return false;
var addr = this.Interface.GetIPProperties().UnicastAddresses.First(ipf =>
{
return ipf.Address.AddressFamily == AddressFamily.InterNetwork;
});
this.Address = addr.Address;
this.Netmask = addr.IPv4Mask;
this.Gateway = new IPAddress(route.dwForwardNextHop.S_un_b);
return true;
}
}
}