Ejemplo n.º 1
0
        internal static DhcpServerClient GetClient(DhcpServer Server, DHCP_IP_ADDRESS IpAddress)
        {
            var searchInfo = new DHCP_SEARCH_INFO_IPADDRESS
            {
                SearchType      = DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress,
                ClientIpAddress = IpAddress
            };

            var searchInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(searchInfo));

            Marshal.StructureToPtr(searchInfo, searchInfoPtr, true);

            if (Server.IsCompatible(DhcpServerVersions.Windows2008R2))
            {
                return(GetClientVQ(Server, searchInfoPtr));
            }
            else if (Server.IsCompatible(DhcpServerVersions.Windows2000))
            {
                return(GetClientV0(Server, searchInfoPtr));
            }
            else
            {
                Marshal.FreeHGlobal(searchInfoPtr);

                throw new PlatformNotSupportedException(string.Format("DHCP Server v{0}.{1} does not support this feature", Server.VersionMajor, Server.VersionMinor));
            }
        }
Ejemplo n.º 2
0
        internal DhcpServer(DHCP_IP_ADDRESS ipAddress, string name)
        {
            this.ipAddress = ipAddress;
            this.name      = name;

            this.version         = new Lazy <Tuple <int, int> >(GetVersion);
            this.config          = new Lazy <DhcpServerConfiguration>(() => DhcpServerConfiguration.GetConfiguration(this));
            this.specificStrings = new Lazy <Tuple <string, string> >(GetSpecificStrings);
            this.auditLog        = new Lazy <DhcpServerAuditLog>(() => DhcpServerAuditLog.GetParams(this));
            this.dnsSettings     = new Lazy <DhcpServerDnsSettings>(() => DhcpServerDnsSettings.GetGlobalDnsSettings(this));
        }
Ejemplo n.º 3
0
        internal DhcpServerScope(DhcpServer Server, DHCP_IP_ADDRESS SubnetAddress)
        {
            this.Server  = Server;
            this.address = SubnetAddress;

            this.info             = new Lazy <DHCP_SUBNET_INFO>(GetInfo);
            this.timeDelayOffer   = new Lazy <TimeSpan>(GetTimeDelayOffer);
            this.ipRange          = new Lazy <DhcpServerIpRange>(GetIpRange);
            this.excludedIpRanges = new Lazy <List <DhcpServerIpRange> >(GetExcludedIpRanges);
            this.leaseDuration    = new Lazy <TimeSpan>(GetLeaseDuration);
            this.dnsSettings      = new Lazy <DhcpServerDnsSettings>(() => DhcpServerDnsSettings.GetScopeDnsSettings(this));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Connects to a DHCP server
        /// </summary>
        /// <param name="HostNameOrAddress"></param>
        public static DhcpServer Connect(string HostNameOrAddress)
        {
            if (string.IsNullOrWhiteSpace(HostNameOrAddress))
            {
                throw new ArgumentNullException("HostNameOrAddress");
            }

            var dnsEntry = Dns.GetHostEntry(HostNameOrAddress);

            var dnsAddress = dnsEntry.AddressList.Where(a => a.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();

            if (dnsAddress == null)
            {
                throw new NotSupportedException("Unable to resolve an IPv4 address for the DHCP Server");
            }

            var address = DHCP_IP_ADDRESS.FromIPAddress(dnsAddress);

            return(new DhcpServer(address, dnsEntry.HostName));
        }
Ejemplo n.º 5
0
 internal bool Contains(DHCP_IP_ADDRESS IpAddress)
 {
     return(IpAddress >= startAddress && IpAddress <= endAddress);
 }
Ejemplo n.º 6
0
 public bool Contains(string IpAddress)
 {
     return(Contains(DHCP_IP_ADDRESS.FromString(IpAddress)));
 }
Ejemplo n.º 7
0
 public bool Contains(IPAddress IpAddress)
 {
     return(Contains(DHCP_IP_ADDRESS.FromIPAddress(IpAddress)));
 }
Ejemplo n.º 8
0
 private DhcpServerIpRange(DHCP_IP_ADDRESS StartAddress, DHCP_IP_ADDRESS EndAddress)
 {
     this.startAddress = StartAddress;
     this.endAddress   = EndAddress;
 }
Ejemplo n.º 9
0
        private static IEnumerable <DhcpServerClient> GetClientsVQ(DhcpServer Server, DHCP_IP_ADDRESS SubnetAddress)
        {
            IntPtr clientsPtr;
            IntPtr resultHandle = IntPtr.Zero;
            int    clientsRead, clientsTotal;

            DhcpErrors result = Api.DhcpEnumSubnetClientsVQ(Server.ipAddress.ToString(), SubnetAddress, ref resultHandle, 65536, out clientsPtr, out clientsRead, out clientsTotal);

            if (result != DhcpErrors.SUCCESS && result != DhcpErrors.ERROR_MORE_DATA)
            {
                throw new DhcpServerException("DhcpEnumSubnetClientsVQ", result);
            }

            while (result == DhcpErrors.SUCCESS || result == DhcpErrors.ERROR_MORE_DATA)
            {
                try
                {
                    using (var clients = DHCP_CLIENT_INFO_ARRAY_VQ.Read(clientsPtr))
                    {
                        foreach (var client in clients.Clients)
                        {
                            yield return(DhcpServerClient.FromNative(Server, client.Item2));
                        }
                    }
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(clientsPtr);
                    clientsPtr = IntPtr.Zero;
                }

                if (result == DhcpErrors.SUCCESS)
                {
                    yield break; // Last results
                }
                clientsRead  = 0;
                clientsTotal = 0;

                result = Api.DhcpEnumSubnetClientsVQ(Server.ipAddress.ToString(), SubnetAddress, ref resultHandle, 65536, out clientsPtr, out clientsRead, out clientsTotal);

                if (result != DhcpErrors.SUCCESS && result != DhcpErrors.ERROR_MORE_DATA)
                {
                    throw new DhcpServerException("DhcpEnumSubnetClientsVQ", result);
                }
            }
        }
Ejemplo n.º 10
0
        private static IEnumerable <DhcpServerClient> GetClientsV5(DhcpServer Server, DHCP_IP_ADDRESS SubnetAddress)
        {
            IntPtr clientsPtr;
            IntPtr resultHandle = IntPtr.Zero;
            int    clientsRead, clientsTotal;

            DhcpErrors result = Api.DhcpEnumSubnetClientsV5(Server.ipAddress.ToString(), SubnetAddress, ref resultHandle, 65536, out clientsPtr, out clientsRead, out clientsTotal);

            if (result != DhcpErrors.SUCCESS && result != DhcpErrors.ERROR_MORE_DATA && result != DhcpErrors.ERROR_NO_MORE_ITEMS)
            {
                throw new DhcpServerException("DhcpEnumSubnetClientsV5", result);
            }

            while (result == DhcpErrors.SUCCESS || result == DhcpErrors.ERROR_MORE_DATA)
            {
                try
                {
                    var clients = (DHCP_CLIENT_INFO_ARRAY_V5)Marshal.PtrToStructure(clientsPtr, typeof(DHCP_CLIENT_INFO_ARRAY_V5));

                    foreach (var client in clients.Clients)
                    {
                        yield return(DhcpServerClient.FromNative(Server, client));
                    }
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(clientsPtr);
                }

                if (result == DhcpErrors.SUCCESS)
                {
                    yield break; // Last results
                }
                result = Api.DhcpEnumSubnetClientsV5(Server.ipAddress.ToString(), SubnetAddress, ref resultHandle, 65536, out clientsPtr, out clientsRead, out clientsTotal);

                if (result != DhcpErrors.SUCCESS && result != DhcpErrors.ERROR_MORE_DATA && result != DhcpErrors.ERROR_NO_MORE_ITEMS)
                {
                    throw new DhcpServerException("DhcpEnumSubnetClientsV5", result);
                }
            }
        }
Ejemplo n.º 11
0
 internal DhcpServerOptionElementIpAddress(DHCP_IP_ADDRESS value)
     : this(value.AsNetworkToIpAddress())
 {
 }
Ejemplo n.º 12
0
 private DhcpServerOptionElementIpAddress(DHCP_IP_ADDRESS Value)
 {
     this.ipAddress = Value;
 }
Ejemplo n.º 13
0
 public static extern void DhcpGetOriginalSubnetMask([MarshalAs(UnmanagedType.LPWStr)] string sAdapterName, out DHCP_IP_ADDRESS dwSubnetMask);
Ejemplo n.º 14
0
 private DhcpServerHost(DHCP_IP_ADDRESS IpAddress)
 {
     this.ipAddress = IpAddress;
 }