using System; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace ServerCore; //============================================================================================= // Network Interface 관련 정보를 조건에 해당하는 정보만 로딩하여 보관해 주는 클래스 이다. // // author : kangms // //============================================================================================= public class NetInterface { public string m_desc = ""; public string m_ip = ""; public string m_ipv6 = ""; } public class NetInterfaceManager { // ni num, address private List m_nics = new List(); private string m_ip_addresses = string.Empty; public NetInterfaceManager() { init(); } public void init() { m_nics.Clear(); // Ethernet의 ipv6, ipv4 주소만 수집한다. var nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (var ni in nics) { if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) { bool has_info = false; NetInterface nic = new NetInterface(); nic.m_desc = ni.Description; var unicast_addresses = ni.GetIPProperties().UnicastAddresses; foreach (var ip_info in unicast_addresses) { if (ip_info.Address.AddressFamily == AddressFamily.InterNetwork) { nic.m_ip = ip_info.Address.ToString(); has_info = true; m_ip_addresses += nic.m_ip; } else if (ip_info.Address.AddressFamily == AddressFamily.InterNetworkV6) { // 추가하자. nic.m_ipv6 = ip_info.Address.ToString(); has_info = true; } } if (has_info) { add(nic); } } } } public void add(NetInterface nic) { m_nics.Add(nic); } public NetInterface find(string address) { foreach (var nic in m_nics) { if ( nic.m_ip == address || nic.m_ipv6 == address ) { return nic; } } return new NetInterface(); } public string getIPAddresses() => m_ip_addresses; public List getNetInterfaces() => m_nics; }