public static List <DhcpClient> DhcpEnumSubnetClients(string IpAddress, string subnetMask) { DHCP_CLIENT_INFO_ARRAY nativeClientArray; DHCP_CLIENT_INFO nativeClient; uint PreferredMaximum = 65536, ResumeHandle = 0, ClientsRead = 0, ClientsTotal = 0; IntPtr ClientInfo, client; List <DhcpClient> dhcpclientslist = new List <DhcpClient>(); uint SubnetMask = StringToUint(subnetMask); int Error = dhcpsapimethods.DhcpEnumSubnetClients(IpAddress, SubnetMask, ref ResumeHandle, PreferredMaximum, out ClientInfo, out ClientsRead, out ClientsTotal); if (Error != 0) { return(dhcpclientslist); } nativeClientArray = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(ClientInfo, typeof(DHCP_CLIENT_INFO_ARRAY)); client = nativeClientArray.Clients; for (int i = 0; i < (int)nativeClientArray.NumElements; i++) { nativeClient = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(client), typeof(DHCP_CLIENT_INFO)); DhcpClient dhcpclient = new DhcpClient(); dhcpclient.ClientComment = nativeClient.ClientComment; dhcpclient.ClientHardwareAddress = ByteToString(nativeClient.ClientHardwareAddress); dhcpclient.ClientIpAddress = UintToString(nativeClient.ClientIpAddress); dhcpclient.ClientLeaseExpires = nativeClient.ClientLeaseExpires.ConvertToNonNative(); dhcpclient.ClientName = nativeClient.ClientName; dhcpclientslist.Add(dhcpclient); client = (IntPtr)((int)client + (int)Marshal.SizeOf(typeof(IntPtr))); } return(dhcpclientslist); }
public static DATE_TIME ConvertDateTimeToNative(DhcpClient client) { DATE_TIME dTime = new DATE_TIME(); dTime.dwHighDateTime = (uint)(client.ClientLeaseExpires.ToFileTime() >> 32); dTime.dwLowDateTime = (uint)(client.ClientLeaseExpires.ToFileTime() + 0xFFFFFFFF); return(dTime); }
public static void DhcpCreateClientInfo(string ServerIpAddress, DhcpClient dhcpClient) { DHCP_CLIENT_INFO clientInfo = ConvertClientInfoToNative(dhcpClient); int error = dhcpsapimethods.DhcpCreateClientInfo(ServerIpAddress, ref clientInfo); if (error != 0) { Console.WriteLine(error); Console.ReadLine(); } }
public static DHCP_CLIENT_INFO ConvertClientInfoToNative(DhcpClient client) { DHCP_CLIENT_INFO ClientInfo = new DHCP_CLIENT_INFO(); ClientInfo.ClientIpAddress = StringToUint(client.ClientIpAddress); ClientInfo.SubnetMask = StringToUint(client.SubnetMask); ClientInfo.ClientHardwareAddress = ConverToNativeMac(client.ClientHardwareAddress); ClientInfo.ClientName = client.ClientName; ClientInfo.ClientComment = client.ClientComment; ClientInfo.ClientLeaseExpires = ConvertDateTimeToNative(client); DHCP_HOST_INFO host = new DHCP_HOST_INFO(); host.HostName = client.OwnerHost.HostName; host.IpAddress = StringToUint(client.OwnerHost.IpAddress); host.NetBiosName = client.OwnerHost.NetBiosName; ClientInfo.OwnerHost = host; return(ClientInfo); }
static void Main(string[] args) { DhcpOption option = new DhcpOption(); option.OptionID = 82; option.OptionComment = "test"; option.OptionName = "TestOption"; option.IntData = new uint[] { DhcpClient.StringToUint("172.22.249.213"), DhcpClient.StringToUint("172.22.249.214") }; option.OptionType = DHCP_OPTION_TYPE.DhcpArrayTypeOption; option.OptionDataType = DHCP_OPTION_DATA_TYPE.DhcpIpAddressOption; DhcpOption.DhcpCreateOption("192.168.2.246", option); //List<DhcpClient> clients = DhcpClient.DhcpEnumSubnetClients("192.168.2.246", "192.168.2.0"); //foreach (DhcpClient client in clients) //{ // Console.WriteLine(client.ClientName); // Console.WriteLine(client.ClientLeaseExpires); // Console.WriteLine(client.ClientIpAddress); // Console.WriteLine(client.ClientComment); // Console.WriteLine(client.ClientHardwareAddress); // Console.WriteLine(client.OwnerHost.HostName); // Console.WriteLine(client.OwnerHost.IpAddress); // Console.WriteLine(client.OwnerHost.NetBiosName); // Console.WriteLine(client.SubnetMask); // Console.ReadLine(); //} DhcpClient dhcpClient = new DhcpClient(); dhcpClient.ClientIpAddress = "192.168.2.48"; dhcpClient.SubnetMask = "192.168.2.0"; dhcpClient.ClientComment = "this is a test"; dhcpClient.ClientName = "test1"; dhcpClient.ClientHardwareAddress = "0800690201FC"; dhcpClient.ClientLeaseExpires = DateTime.Now.AddDays(30); dhcpClient.OwnerHost.HostName = "MEAN-DEV"; dhcpClient.OwnerHost.IpAddress = "192.168.2.246"; DhcpClient.DhcpCreateClientInfo("192.168.2.246", dhcpClient); }
public static DHCP_CLIENT_INFO ConvertClientInfoToNative(DhcpClient client) { DHCP_CLIENT_INFO ClientInfo = new DHCP_CLIENT_INFO(); ClientInfo.ClientIpAddress = StringToUint(client.ClientIpAddress); ClientInfo.SubnetMask = StringToUint(client.SubnetMask); ClientInfo.ClientHardwareAddress = ConverToNativeMac(client.ClientHardwareAddress); ClientInfo.ClientName = client.ClientName; ClientInfo.ClientComment = client.ClientComment; ClientInfo.ClientLeaseExpires = ConvertDateTimeToNative(client); DHCP_HOST_INFO host = new DHCP_HOST_INFO(); host.HostName = client.OwnerHost.HostName; host.IpAddress = StringToUint(client.OwnerHost.IpAddress); host.NetBiosName = client.OwnerHost.NetBiosName; ClientInfo.OwnerHost = host; return ClientInfo; }
public static List<DhcpClient> DhcpEnumSubnetClients(string IpAddress, string subnetMask) { DHCP_CLIENT_INFO_ARRAY nativeClientArray; DHCP_CLIENT_INFO nativeClient; uint PreferredMaximum = 65536, ResumeHandle = 0, ClientsRead = 0, ClientsTotal = 0; IntPtr ClientInfo, client; List<DhcpClient> dhcpclientslist = new List<DhcpClient>(); uint SubnetMask = StringToUint(subnetMask); int Error = dhcpsapimethods.DhcpEnumSubnetClients(IpAddress, SubnetMask, ref ResumeHandle, PreferredMaximum, out ClientInfo, out ClientsRead, out ClientsTotal); if (Error != 0) return dhcpclientslist; nativeClientArray = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(ClientInfo, typeof(DHCP_CLIENT_INFO_ARRAY)); client = nativeClientArray.Clients; for (int i = 0; i < (int)nativeClientArray.NumElements; i++) { nativeClient = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(client), typeof(DHCP_CLIENT_INFO)); DhcpClient dhcpclient = new DhcpClient(); dhcpclient.ClientComment = nativeClient.ClientComment; dhcpclient.ClientHardwareAddress = ByteToString(nativeClient.ClientHardwareAddress); dhcpclient.ClientIpAddress = UintToString(nativeClient.ClientIpAddress); dhcpclient.ClientLeaseExpires = nativeClient.ClientLeaseExpires.ConvertToNonNative(); dhcpclient.ClientName = nativeClient.ClientName; dhcpclientslist.Add(dhcpclient); client = (IntPtr)((int)client + (int)Marshal.SizeOf(typeof(IntPtr))); } return dhcpclientslist; }
public static DATE_TIME ConvertDateTimeToNative(DhcpClient client) { DATE_TIME dTime = new DATE_TIME(); dTime.dwHighDateTime = (uint)(client.ClientLeaseExpires.ToFileTime() >> 32); dTime.dwLowDateTime = (uint)(client.ClientLeaseExpires.ToFileTime() + 0xFFFFFFFF); return dTime; }