예제 #1
0
        private static DhcpServerOptionValue GetOptionValueV0(DhcpServer Server, IntPtr ScopeInfo, int OptionId)
        {
            try
            {
                IntPtr valuePtr;

                var result = Api.DhcpGetOptionValue(Server.ipAddress.ToString(), OptionId, ScopeInfo, out valuePtr);

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

                try
                {
                    var value = (DHCP_OPTION_VALUE)Marshal.PtrToStructure(valuePtr, typeof(DHCP_OPTION_VALUE));

                    return(FromNative(Server, value, null, null));
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(valuePtr);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(ScopeInfo);
            }
        }
예제 #2
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));
            }
        }
예제 #3
0
        internal static DhcpServerClass GetClass(DhcpServer server, string name)
        {
            var query = new DHCP_CLASS_INFO_Managed(className: name,
                                                    classDataLength: 0,
                                                    classData: IntPtr.Zero);

            var result = Api.DhcpGetClassInfo(ServerIpAddress: server.Address,
                                              ReservedMustBeZero: 0,
                                              PartialClassInfo: query,
                                              FilledClassInfo: out var classIntoPtr);

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpGetClassInfo), result);
            }

            try
            {
                using (var classInfo = classIntoPtr.MarshalToStructure <DHCP_CLASS_INFO>())
                {
                    return(FromNative(server, in classInfo));
                }
            }
            finally
            {
                Api.FreePointer(classIntoPtr);
            }
        }
예제 #4
0
        private static DhcpServerClient GetClientVQ(DhcpServer Server, IntPtr SearchInfo)
        {
            try
            {
                IntPtr clientPtr;

                DhcpErrors result = Api.DhcpGetClientInfoVQ(Server.ipAddress.ToString(), SearchInfo, out clientPtr);

                if (result == DhcpErrors.JET_ERROR)
                {
                    return(null);
                }

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

                try
                {
                    var client = (DHCP_CLIENT_INFO_VQ)Marshal.PtrToStructure(clientPtr, typeof(DHCP_CLIENT_INFO_VQ));

                    return(DhcpServerClient.FromNative(Server, client));
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(clientPtr);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(SearchInfo);
            }
        }
        internal static IEnumerable <DhcpServerBindingElement> GetBindingInfo(DhcpServer server)
        {
            var result = Api.DhcpGetServerBindingInfo(ServerIpAddress: server.Address,
                                                      Flags: 0,
                                                      BindElementsInfo: out var elementsPtr);

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpGetServerBindingInfo), result);
            }

            try
            {
                using (var elements = elementsPtr.MarshalToStructure <DHCP_BIND_ELEMENT_ARRAY>())
                {
                    foreach (var element in elements.Elements)
                    {
                        yield return(FromNative(server, in element));
                    }
                }
            }
            finally
            {
                Api.FreePointer(elementsPtr);
            }
        }
        internal static DhcpServerScopeFailoverStatistics GetScopeFailoverStatistics(DhcpServer server, DhcpServerScope scope)
        {
            var result = Api.DhcpV4FailoverGetScopeStatistics(ServerIpAddress: server.Address,
                                                              ScopeId: scope.Address.ToNativeAsNetwork(),
                                                              Stats: out var statisticsPtr);

            if (result == DhcpErrors.FO_SCOPE_NOT_IN_RELATIONSHIP)
            {
                return(null);
            }

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpV4FailoverGetScopeStatistics), result);
            }

            try
            {
                var statistics = statisticsPtr.MarshalToStructure <DHCP_FAILOVER_STATISTICS>();

                return(FromNative(server, scope, in statistics));
            }
            finally
            {
                Api.FreePointer(statisticsPtr);
            }
        }
예제 #7
0
        private static DhcpServerOptionValue GetOptionValueV5(DhcpServer Server, IntPtr ScopeInfo, int OptionId, string ClassName, string VendorName)
        {
            try
            {
                IntPtr valuePtr;
                uint   flags = VendorName == null ? 0 : Constants.DHCP_FLAGS_OPTION_IS_VENDOR;

                var result = Api.DhcpGetOptionValueV5(Server.ipAddress.ToString(), flags, OptionId, ClassName, VendorName, ScopeInfo, out valuePtr);

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

                try
                {
                    var value = (DHCP_OPTION_VALUE)Marshal.PtrToStructure(valuePtr, typeof(DHCP_OPTION_VALUE));

                    return(FromNative(Server, value, ClassName, VendorName));
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(valuePtr);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(ScopeInfo);
            }
        }
예제 #8
0
        private static DhcpServerScope CreateOrReplicatePartnerScope(DhcpServer partnerServer, DhcpServerScope sourceScope)
        {
            // create scope on partner server (if it doesn't exist)
            DhcpServerScope partnerScope;

            try
            {
                // retrieve scope from partner
                partnerScope = (DhcpServerScope)partnerServer.Scopes.GetScope(sourceScope.Address);

                var existingRelationship = partnerScope.GetFailoverRelationship();
                if (existingRelationship != null)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverCreateRelationship), DhcpErrors.FO_SCOPE_ALREADY_IN_RELATIONSHIP);
                }

                // deactivate scope
                partnerScope.Deactivate();
            }
            catch (DhcpServerException ex) when(ex.ApiErrorNative == DhcpErrors.SUBNET_NOT_PRESENT)
            {
                // create scope (including excluded ranges)
                partnerScope = (DhcpServerScope)partnerServer.Scopes.AddScope(sourceScope.Name, sourceScope.Comment, sourceScope.IpRange, sourceScope.Mask, sourceScope.TimeDelayOffer, sourceScope.LeaseDuration);
            }

            // replicate scope
            sourceScope.ReplicateTo(partnerScope);

            return(partnerScope);
        }
예제 #9
0
        internal static IEnumerable <DhcpServerOption> GetAllOptions(DhcpServer Server)
        {
            IntPtr optionsPtr;

            var result = Api.DhcpGetAllOptions(Server.ipAddress.ToString(), 0, out optionsPtr);

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

            try
            {
                var bytes = new byte[64];
                Marshal.Copy(optionsPtr, bytes, 0, 64);

                var options = (DHCP_ALL_OPTIONS)Marshal.PtrToStructure(optionsPtr, typeof(DHCP_ALL_OPTIONS));

                foreach (var option in options.NonVendorOptions.Options)
                {
                    yield return(DhcpServerOption.FromNative(Server, option, null, null));
                }

                foreach (var option in options.VendorOptions)
                {
                    yield return(DhcpServerOption.FromNative(Server, option));
                }
            }
            finally
            {
                Api.DhcpRpcFreeMemory(optionsPtr);
            }
        }
예제 #10
0
        internal static IEnumerable <DhcpServerOption> GetAllOptions(DhcpServer server)
        {
            var result = Api.DhcpGetAllOptions(ServerIpAddress: server.Address,
                                               Flags: 0,
                                               OptionStruct: out var optionsPtr);

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpGetAllOptions), result);
            }

            try
            {
                using (var options = optionsPtr.MarshalToStructure <DHCP_ALL_OPTIONS>())
                {
                    foreach (var option in options.NonVendorOptions.Options)
                    {
                        yield return(FromNative(server, in option, null, null));
                    }

                    foreach (var option in options.VendorOptions)
                    {
                        yield return(FromNative(server, in option));
                    }
                }
            }
            finally
            {
                Api.FreePointer(optionsPtr);
            }
        }
예제 #11
0
        internal static DhcpServerFailoverRelationship GetFailoverRelationship(DhcpServer server, DhcpServerIpAddress subnetAddress)
        {
            var result = Api.DhcpV4FailoverGetScopeRelationship(ServerIpAddress: server.Address,
                                                                ScopeId: subnetAddress.ToNativeAsNetwork(),
                                                                Relationship: out var relationshipPtr);

            if (result == DhcpErrors.FO_SCOPE_NOT_IN_RELATIONSHIP)
            {
                return(null);
            }

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpV4FailoverGetScopeRelationship), result);
            }

            try
            {
                using (var relationship = relationshipPtr.MarshalToStructure <DHCP_FAILOVER_RELATIONSHIP>())
                {
                    return(FromNative(server, in relationship));
                }
            }
            finally
            {
                Api.FreePointer(relationshipPtr);
            }
        }
예제 #12
0
        internal static IEnumerable <DhcpServerClass> GetClasses(DhcpServer Server)
        {
            IntPtr enumInfoPtr;
            int    elementsRead, elementsTotal;
            IntPtr resumeHandle = IntPtr.Zero;

            var result = Api.DhcpEnumClasses(Server.ipAddress.ToString(), 0, ref resumeHandle, 0xFFFFFFFF, out enumInfoPtr, out elementsRead, out elementsTotal);

            if (result == DhcpErrors.ERROR_NO_MORE_ITEMS || result == DhcpErrors.EPT_S_NOT_REGISTERED)
            {
                yield break;
            }

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

            if (elementsRead > 0)
            {
                var enumInfo = (DHCP_CLASS_INFO_ARRAY)Marshal.PtrToStructure(enumInfoPtr, typeof(DHCP_CLASS_INFO_ARRAY));
                try
                {
                    foreach (var element in enumInfo.Classes)
                    {
                        yield return(FromNative(Server, element));
                    }
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(enumInfoPtr);
                }
            }
        }
예제 #13
0
        internal static DhcpServerClass GetClass(DhcpServer Server, string Name)
        {
            IntPtr classIntoPtr;

            var query = new DHCP_CLASS_INFO()
            {
                ClassName       = Name,
                ClassDataLength = 0,
                ClassData       = IntPtr.Zero
            };

            var result = Api.DhcpGetClassInfo(Server.ipAddress.ToString(), 0, query, out classIntoPtr);

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

            var classInfo = (DHCP_CLASS_INFO)Marshal.PtrToStructure(classIntoPtr, typeof(DHCP_CLASS_INFO));

            var dhcpClass = FromNative(Server, classInfo);

            Api.DhcpRpcFreeMemory(classIntoPtr);

            return(dhcpClass);
        }
        internal static IEnumerable <DhcpServerBindingElement> GetBindingInfo(DhcpServer Server)
        {
            IntPtr elementsPtr;

            var result = Api.DhcpGetServerBindingInfo(Server.ipAddress.ToString(), 0, out elementsPtr);

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

            try
            {
                var elements = (DHCP_BIND_ELEMENT_ARRAY)Marshal.PtrToStructure(elementsPtr, typeof(DHCP_BIND_ELEMENT_ARRAY));

                foreach (var element in elements.Elements)
                {
                    yield return(FromNative(Server, element));
                }
            }
            finally
            {
                Api.DhcpRpcFreeMemory(elementsPtr);
            }
        }
예제 #15
0
        private static DhcpServerOption GetOptionV5(DhcpServer server, int optionId, string className, string vendorName)
        {
            var result = Api.DhcpGetOptionInfoV5(ServerIpAddress: server.Address,
                                                 Flags: (vendorName == null) ? 0 : Constants.DHCP_FLAGS_OPTION_IS_VENDOR,
                                                 OptionID: optionId,
                                                 ClassName: className,
                                                 VendorName: vendorName,
                                                 OptionInfo: out var optionPtr);

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpGetOptionInfoV5), result);
            }

            try
            {
                using (var option = optionPtr.MarshalToStructure <DHCP_OPTION>())
                {
                    return(FromNative(server, in option, className, vendorName));
                }
            }
            finally
            {
                Api.FreePointer(optionPtr);
            }
        }
예제 #16
0
        internal static IEnumerable <DhcpServerScope> GetScopes(DhcpServer Server)
        {
            IntPtr enumInfoPtr;
            int    elementsRead, elementsTotal;
            IntPtr resumeHandle = IntPtr.Zero;

            var result = Api.DhcpEnumSubnets(Server.ipAddress.ToString(), ref resumeHandle, 0xFFFFFFFF, out enumInfoPtr, out elementsRead, out elementsTotal);

            if (result == DhcpErrors.ERROR_NO_MORE_ITEMS || result == DhcpErrors.EPT_S_NOT_REGISTERED)
            {
                yield break;
            }

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

            if (elementsRead > 0)
            {
                var enumInfo = (DHCP_IP_ARRAY)Marshal.PtrToStructure(enumInfoPtr, typeof(DHCP_IP_ARRAY));

                try
                {
                    foreach (var scopeAddress in enumInfo.Elements)
                    {
                        yield return(new DhcpServerScope(Server, (DHCP_IP_ADDRESS)scopeAddress));
                    }
                }
                finally
                {
                    Api.DhcpRpcFreeMemory(enumInfoPtr);
                }
            }
        }
예제 #17
0
 private DhcpServerAuditLog(DhcpServer server, string auditLogDir, int diskCheckInterval, int maxLogFilesSize, int minSpaceOnDisk)
 {
     Server            = server;
     AuditLogDirectory = auditLogDir;
     DiskCheckInterval = diskCheckInterval;
     MaxLogFilesSize   = maxLogFilesSize;
     MinSpaceOnDisk    = minSpaceOnDisk;
 }
예제 #18
0
 private DhcpServerClass(DhcpServer server, string name, string comment, bool isVendorClass, byte[] data)
 {
     Server        = server;
     Name          = name;
     Comment       = comment;
     IsVendorClass = isVendorClass;
     Data          = data;
 }
예제 #19
0
        internal static IEnumerable <DhcpServerOption> EnumVendorOptions(DhcpServer Server, string VendorName)
        {
            if (!Server.IsCompatible(DhcpServerVersions.Windows2008R2))
            {
                throw new PlatformNotSupportedException(string.Format("DHCP Server v{0}.{1} does not support this feature", Server.VersionMajor, Server.VersionMinor));
            }

            return(EnumOptionsV5(Server, null, VendorName));
        }
예제 #20
0
        internal static IEnumerable <DhcpServerOption> EnumVendorOptions(DhcpServer server, string vendorName)
        {
            if (!server.IsCompatible(DhcpServerVersions.Windows2008R2))
            {
                throw new PlatformNotSupportedException($"DHCP Server v{server.VersionMajor}.{server.VersionMinor} does not support this feature");
            }

            return(EnumOptionsV5(server, null, vendorName));
        }
예제 #21
0
 private static DhcpServerOptionValue FromNative(DhcpServer Server, DHCP_OPTION_VALUE Native, string ClassName, string VendorName)
 {
     return(new DhcpServerOptionValue(Server)
     {
         OptionId = Native.OptionID,
         Values = DhcpServerOptionElement.ReadNativeElements(Native.Value).ToList(),
         ClassName = ClassName,
         VendorName = VendorName
     });
 }
예제 #22
0
 internal static IEnumerable <DhcpServerOption> EnumDefaultOptions(DhcpServer server)
 {
     if (server.IsCompatible(DhcpServerVersions.Windows2008R2))
     {
         return(EnumOptionsV5(server, null, null));
     }
     else
     {
         return(EnumOptionsV0(server));
     }
 }
예제 #23
0
 public IDhcpServer ConnectToPartner()
 {
     if (ServerType == DhcpServerFailoverServerType.PrimaryServer)
     {
         return(DhcpServer.Connect(SecondaryServerAddress.ToString()));
     }
     else
     {
         return(DhcpServer.Connect(PrimaryServerAddress.ToString()));
     }
 }
예제 #24
0
        internal DhcpServerOptionValue(DhcpServer server, int optionId, string className, string vendorName, List <DhcpServerOptionElement> values)
        {
            Server = server;

            OptionId = optionId;

            ClassName  = className;
            VendorName = vendorName;

            Values = values;
        }
 private DhcpServerBindingElement(DhcpServer server, bool cantModify, bool isBound, DhcpServerIpAddress adapterPrimaryIpAddress, DhcpServerIpMask adapterSubnetAddress, string interfaceDescription, Guid interfaceGuidId, byte[] interfaceId)
 {
     Server     = server;
     CantModify = cantModify;
     IsBound    = isBound;
     AdapterPrimaryIpAddress = adapterPrimaryIpAddress;
     AdapterSubnetAddress    = adapterSubnetAddress;
     InterfaceDescription    = interfaceDescription;
     this.interfaceId        = interfaceId;
     InterfaceGuidId         = interfaceGuidId;
 }
예제 #26
0
 private DhcpServerConfiguration(DhcpServer server, DhcpServerApiProtocol apiProtocolSupport, string databaseName, string databasePath, string backupPath, TimeSpan backupInterval, bool databaseLoggingEnabled, TimeSpan databaseCleanupInterval)
 {
     Server                  = server;
     ApiProtocolSupport      = apiProtocolSupport;
     DatabaseName            = databaseName;
     DatabasePath            = databasePath;
     BackupPath              = backupPath;
     BackupInterval          = backupInterval;
     DatabaseLoggingEnabled  = databaseLoggingEnabled;
     DatabaseCleanupInterval = databaseCleanupInterval;
 }
예제 #27
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));
        }
 private DhcpServerScopeFailoverStatistics(DhcpServer server, DhcpServerScope scope, int addressesTotal, int addressesFree, int addressesInUse, int partnerAddressesFree, int localAddressesFree, int partnerAddressInUse, int localAddressesInUse)
 {
     Server                = server;
     Scope                 = scope;
     AddressesTotal        = addressesTotal;
     AddressesFree         = addressesFree;
     AddressesInUse        = addressesInUse;
     PartnerAddressesFree  = partnerAddressesFree;
     LocalAddressesFree    = localAddressesFree;
     PartnerAddressesInUse = partnerAddressInUse;
     LocalAddressesInUse   = localAddressesInUse;
 }
예제 #29
0
 private DhcpServerOption(DhcpServer server, int optionId, string name, string comment, IEnumerable <DhcpServerOptionElement> defaultValue, bool isUnaryOption, string vendorName, string className)
 {
     Server        = server;
     OptionId      = optionId;
     Name          = name;
     Comment       = comment;
     DefaultValue  = defaultValue;
     ValueType     = defaultValue.First().Type;
     IsUnaryOption = isUnaryOption;
     VendorName    = vendorName;
     ClassName     = className;
 }
예제 #30
0
 private static DhcpServerOption FromNative(DhcpServer Server, DHCP_VENDOR_OPTION Native)
 {
     return(new DhcpServerOption(Server)
     {
         OptionId = Native.OptionID,
         Name = Native.OptionName,
         Comment = Native.OptionComment,
         DefaultValue = DhcpServerOptionElement.ReadNativeElements(Native.DefaultValue).ToList(),
         IsUnaryOption = Native.OptionType == DHCP_OPTION_TYPE.DhcpUnaryElementTypeOption,
         VendorName = Native.VendorName,
         ClassName = Native.ClassName
     });
 }