Exemplo n.º 1
0
        public static void GetDHCPleases(List <string> DHCPServerIPs, IDictionary <string, string> MACIPmapping)
        {
            uint   parsedMask     = 0; //Gets all scopes
            uint   resumeHandle   = 0;
            uint   numClientsRead = 0;
            uint   totalClients   = 0;
            IntPtr info_array_ptr = IntPtr.Zero;

            foreach (string DHCPServer in DHCPServerIPs)
            {
                uint response = DhcpEnumSubnetClients(DHCPServer, parsedMask, ref resumeHandle, 65536, out info_array_ptr, ref numClientsRead, ref totalClients);

                //If no results, next DHCP server
                if ((int)info_array_ptr == 0)
                {
                    continue;
                }

                //Set up client array casted to a DHCP_CLIENT_INFO_ARRAY using the pointer from the response object above
                DHCP_CLIENT_INFO_ARRAY rawClients = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(info_array_ptr, typeof(DHCP_CLIENT_INFO_ARRAY));

                // Loop through the clients structure inside rawClients adding to the dchpClient collection
                IntPtr current = rawClients.Clients;

                for (int i = 0; i < (int)rawClients.NumElements; i++)
                {
                    string machineIP, machineMAC;

                    //Create machine object using the struct
                    DHCP_CLIENT_INFO rawMachine = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(current), typeof(DHCP_CLIENT_INFO));

                    var a = BitConverter.GetBytes(rawMachine.ip).Reverse().ToArray();
                    var b = BitConverter.ToUInt32(a, 0);

                    //Transform machine information
                    machineIP = new IPAddress(b).ToString();

                    machineMAC = string.Format("{0:x2} {1:x2} {2:x2} {3:x2} {4:x2} {5:x2}",
                                               Marshal.ReadByte(rawMachine.mac.Data),
                                               Marshal.ReadByte(rawMachine.mac.Data, 1),
                                               Marshal.ReadByte(rawMachine.mac.Data, 2),
                                               Marshal.ReadByte(rawMachine.mac.Data, 3),
                                               Marshal.ReadByte(rawMachine.mac.Data, 4),
                                               Marshal.ReadByte(rawMachine.mac.Data, 5)).ToUpper();

                    //Add to mapping object
                    MACIPmapping.Add(machineMAC, machineIP);

                    //Move pointer to next machine
                    current = (IntPtr)((int)current + (int)Marshal.SizeOf(typeof(IntPtr)));
                }
            }
        }
Exemplo n.º 2
0
 static ArrayList findDhcpClients(string server, string subnet)
 {
     // set up container for processed clients
     ArrayList foundClients = new ArrayList();
     // make call to unmanaged code
     uint parsedMask     = StringIPAddressToUInt32(subnet);
     uint resumeHandle   = 0;
     uint numClientsRead = 0;
     uint totalClients   = 0;
     IntPtr info_array_ptr;
     uint response = DhcpEnumSubnetClients(
         server,
         parsedMask,
         ref resumeHandle,
         65536,
         out info_array_ptr,
         ref numClientsRead,
         ref totalClients
         );
     // set up client array casted to a DHCP_CLIENT_INFO_ARRAY
     // using the pointer from the response object above
     DHCP_CLIENT_INFO_ARRAY rawClients =
         (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(info_array_ptr, typeof(DHCP_CLIENT_INFO_ARRAY));
     // loop through the clients structure inside rawClients 
     // adding to the dchpClient collection
     IntPtr current = rawClients.Clients;
     for (int i = 0; i < (int)rawClients.NumElements; i++)
     {
         // 1. Create machine object using the struct
         DHCP_CLIENT_INFO rawMachine =
             (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(current), typeof(DHCP_CLIENT_INFO));
         // 2. create new C# dhcpClient object and add to the 
         // collection (for hassle-free use elsewhere!!)
         dhcpClient thisClient = new dhcpClient();
         thisClient.ip = UInt32IPAddressToString(rawMachine.ip);
         thisClient.hostname = rawMachine.ClientName;
         thisClient.mac = String.Format("{0:x2}{1:x2}.{2:x2}{3:x2}.{4:x2}{5:x2}",
             Marshal.ReadByte(rawMachine.mac.Data),
             Marshal.ReadByte(rawMachine.mac.Data, 1),
             Marshal.ReadByte(rawMachine.mac.Data, 2),
             Marshal.ReadByte(rawMachine.mac.Data, 3),
             Marshal.ReadByte(rawMachine.mac.Data, 4),
             Marshal.ReadByte(rawMachine.mac.Data, 5));
         foundClients.Add(thisClient);
         // 3. move pointer to next machine
         current = (IntPtr)((int)current + (int)Marshal.SizeOf(typeof(IntPtr)));
     }
     return foundClients;
 }
Exemplo n.º 3
0
 private static DhcpServerClient FromNative(DhcpServer Server, DHCP_CLIENT_INFO Native)
 {
     return(new DhcpServerClient(Server)
     {
         ipAddress = Native.ClientIpAddress,
         subnetMask = Native.SubnetMask,
         hardwareAddress = Native.ClientHardwareAddress.Data,
         Name = Native.ClientName,
         Comment = Native.ClientComment,
         LeaseExpires = Native.ClientLeaseExpires.ToDateTime(),
         Type = DhcpServerClientTypes.Unspecified,
         AddressState = DhcpServerClientAddressStates.Unknown,
         QuarantineStatus = DhcpServerClientQuarantineStatuses.NoQuarantineInformation,
         ProbationEnds = DateTime.MaxValue,
         QuarantineCapable = false
     });
 }
Exemplo n.º 4
0
        /// <summary>
        /// Возвращает список клиентов подсети на сервере DHCP.
        /// </summary>
        /// <param name="server">Сервер</param>
        /// <param name="subnet">Подсеть</param>
        /// <returns>Список клиентов</returns>
        public static List <DhcpClient> FindDhcpClients(string server, string subnet)
        {
            List <DhcpClient> foundClients = new List <DhcpClient>();
            uint parsedMask     = StringIPAddressToUInt32(subnet);
            uint resumeHandle   = 0;
            uint numClientsRead = 0;
            uint totalClients   = 0;

            _ = DhcpEnumSubnetClients(server, parsedMask, ref resumeHandle, 65536, out IntPtr info_array_ptr, ref numClientsRead, ref totalClients);
            var    rawClients = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(info_array_ptr, typeof(DHCP_CLIENT_INFO_ARRAY));
            IntPtr current    = rawClients.Clients;

            for (int i = 0; i < (int)rawClients.NumElements; i++)
            {
                DHCP_CLIENT_INFO rawMachine = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(current), typeof(DHCP_CLIENT_INFO));
                string           ip         = UInt32IPAddressToString(rawMachine.ip);
                DhcpClient       thisClient = new DhcpClient(ip);
                foundClients.Add(thisClient);
                current = (IntPtr)((int)current + Marshal.SizeOf(typeof(IntPtr)));
            }
            return(foundClients);
        }
Exemplo n.º 5
0
        /// <summary>DhcpEnumSubnetClientstub</summary>
        public static void DhcpEnumSubnetClientstub
        (
            ref UtilityDynamicHostConfigurationProtocolDHCPArgument utilityDynamicHostConfigurationProtocolDHCPArgument,
            ref string exceptionMessage,
            ref IPAddress[]                                         ipAddress,
            ref IntPtr[]                                            enumInfoElements,
            ref DHCP_CLIENT_INFO[][]                                dhcpClientInfo
        )
        {
            int    dhcpEnumSubnetClients;
            int    clientsRead       = -1;
            int    clientsTotal      = -1;
            int    resumeHandle      = 0;
            IntPtr clientInfo        = IntPtr.Zero;
            IntPtr clientInfoClient  = IntPtr.Zero;
            IntPtr clientInfoClients = IntPtr.Zero;
            DHCP_CLIENT_INFO_ARRAY dhcpClientInfoArray;

            dhcpClientInfo = null;
            if (ipAddress == null)
            {
                return;
            }
            dhcpClientInfo = new DHCP_CLIENT_INFO[ipAddress.Length][];
            for (int ipAddressIndex = 0; ipAddressIndex < ipAddress.Length; ++ipAddressIndex)
            {
                dhcpEnumSubnetClients = DhcpEnumSubnetClients
                                        (
                    ipAddress[ipAddressIndex].ToString(),
                    Marshal.ReadInt32(enumInfoElements[ipAddressIndex]),
                    ref resumeHandle,
                    PreferredMaximum,
                    ref clientInfo,
                    ref clientsRead,
                    ref clientsTotal
                                        );
                System.Console.WriteLine
                (
                    "dhcpEnumSubnetClients: {0}",
                    dhcpEnumSubnetClients
                );
                if (dhcpEnumSubnetClients == 0 && clientInfo != IntPtr.Zero)
                {
                    dhcpClientInfoArray = ( DHCP_CLIENT_INFO_ARRAY )Marshal.PtrToStructure
                                          (
                        clientInfo,
                        typeof(DHCP_CLIENT_INFO_ARRAY)
                                          );
                    dhcpClientInfo[ipAddressIndex] = new DHCP_CLIENT_INFO[dhcpClientInfoArray.NumElements];
                    clientInfoClients = dhcpClientInfoArray.Clients;
                    for (int clientIndex = 0; clientIndex < clientsTotal; ++clientIndex)
                    {
                        clientInfoClient = Marshal.ReadIntPtr(clientInfoClients);
                        dhcpClientInfo[ipAddressIndex][clientIndex] = ( DHCP_CLIENT_INFO )Marshal.PtrToStructure
                                                                      (
                            clientInfoClient,
                            typeof(DHCP_CLIENT_INFO)
                                                                      );
      #if ( DEBUG )
                        System.Console.WriteLine
                        (
                            "IPAddress[{0}][{1}]: {2}",
                            ipAddressIndex,
                            clientIndex,
                            dhcpClientInfo[ipAddressIndex][clientIndex]
                        );
      #endif
                        clientInfoClient = new IntPtr
                                           (
                            clientInfoClients.ToInt32() + IntPtr.Size
                                           );
                    }
                }
            }
        }