예제 #1
0
        internal static void DeconfigureScopeFailover(DhcpServerScope scope)
        {
            var relationship = scope.GetFailoverRelationship();
            var partner      = relationship.ConnectToPartner();
            var partnerScope = partner.Scopes.GetScope(scope.Address);

            // deactivate scope on the failover relationship
            partnerScope.Deactivate();

            using (var relationshipNative = new DHCP_FAILOVER_RELATIONSHIP_Managed(relationshipName: relationship.Name,
                                                                                   scopes: new DHCP_IP_ARRAY_Managed(scope.Address.ToNativeAsNetwork())))
            {
                // remove scope from failover relationship on partner server
                var result = Api.DhcpV4FailoverDeleteScopeFromRelationship(partner.Address, in relationshipNative);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverDeleteScopeFromRelationship), result, "Failed to delete scope from relationship on partner server");
                }

                // remove scope from failover relationship on server
                result = Api.DhcpV4FailoverDeleteScopeFromRelationship(scope.Server.Address, in relationshipNative);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverDeleteScopeFromRelationship), result, "Failed to delete scope from relationship");
                }
            }

            // delete scope on partner server
            partnerScope.Delete();
        }
        private DhcpServerScopeReservation(DhcpServerScope scope, DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes)
        {
            Scope              = scope;
            Address            = address;
            HardwareAddress    = hardwareAddress;
            AllowedClientTypes = allowedClientTypes;

            Options = new DhcpServerScopeReservationOptionValueCollection(this);
        }
        internal static DhcpServerScopeReservation CreateReservation(DhcpServerScope scope, DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes)
        {
            if (!scope.IpRange.Contains(address))
            {
                throw new ArgumentOutOfRangeException(nameof(address), "The DHCP scope does not include the provided address");
            }

            DhcpServerScope.AddSubnetReservationElement(scope.Server, scope.Address, address, hardwareAddress, allowedClientTypes);

            return(new DhcpServerScopeReservation(scope, address, hardwareAddress, allowedClientTypes));
        }
 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;
 }
        private static DhcpServerScopeReservation FromNative(DhcpServerScope Scope, DHCP_IP_RESERVATION_V4 Native)
        {
            var reservedForClient = Native.ReservedForClient;

            return(new DhcpServerScopeReservation(Scope)
            {
                ipAddress = Native.ReservedIpAddress,
                ipAddressMask = reservedForClient.ClientIpAddressMask,
                macAddress = reservedForClient.ClientMacAddress,
                AllowedClientTypes = (DhcpServerClientTypes)Native.bAllowedClientTypes
            });
        }
예제 #6
0
        private static IEnumerable <DhcpServerOptionValue> EnumScopeOptionValues(DhcpServerScope Scope, string ClassName, string VendorName)
        {
            var scopeInfo = new DHCP_OPTION_SCOPE_INFO_SUBNET()
            {
                ScopeType       = DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions,
                SubnetScopeInfo = Scope.address
            };

            var scopeInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(scopeInfo));

            Marshal.StructureToPtr(scopeInfo, scopeInfoPtr, true);

            return(EnumOptionValues(Scope.Server, scopeInfoPtr, ClassName, VendorName));
        }
예제 #7
0
        internal static DhcpServerOptionValue GetScopeOptionValue(DhcpServerScope Scope, int OptionId, string ClassName, string VendorName)
        {
            var scopeInfo = new DHCP_OPTION_SCOPE_INFO_SUBNET()
            {
                ScopeType       = DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions,
                SubnetScopeInfo = Scope.address
            };

            var scopeInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(scopeInfo));

            Marshal.StructureToPtr(scopeInfo, scopeInfoPtr, true);

            return(GetOptionValue(Scope.Server, scopeInfoPtr, OptionId, ClassName, VendorName));
        }
예제 #8
0
        internal static IEnumerable <DhcpServerOptionValue> GetAllScopeOptionValues(DhcpServerScope Scope)
        {
            var scopeInfo = new DHCP_OPTION_SCOPE_INFO_SUBNET()
            {
                ScopeType       = DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions,
                SubnetScopeInfo = Scope.address
            };

            var scopeInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(scopeInfo));

            Marshal.StructureToPtr(scopeInfo, scopeInfoPtr, true);

            return(GetAllOptionValues(Scope.Server, scopeInfoPtr));
        }
예제 #9
0
 internal static IEnumerable <DhcpServerClient> GetClients(DhcpServerScope Scope)
 {
     if (Scope.Server.IsCompatible(DhcpServerVersions.Windows2008R2))
     {
         return(GetClientsVQ(Scope.Server, Scope.address));
     }
     else if (Scope.Server.IsCompatible(DhcpServerVersions.Windows2000))
     {
         return(GetClientsV0(Scope.Server, Scope.address));
     }
     else
     {
         throw new PlatformNotSupportedException(string.Format("DHCP Server v{0}.{1} does not support this feature", Scope.Server.VersionMajor, Scope.Server.VersionMinor));
     }
 }
        internal static IEnumerable <DhcpServerScopeReservation> GetReservations(DhcpServerScope scope)
        {
            var resumeHandle = IntPtr.Zero;
            var result       = Api.DhcpEnumSubnetElementsV5(ServerIpAddress: scope.Server.Address,
                                                            SubnetAddress: scope.Address.ToNativeAsNetwork(),
                                                            EnumElementType: DHCP_SUBNET_ELEMENT_TYPE.DhcpReservedIps,
                                                            ResumeHandle: ref resumeHandle,
                                                            PreferredMaximum: 0xFFFFFFFF,
                                                            EnumElementInfo: out var reservationsPtr,
                                                            ElementsRead: out var elementsRead,
                                                            ElementsTotal: out _);

            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(nameof(Api.DhcpEnumSubnetElementsV5), result);
            }

            try
            {
                if (elementsRead == 0)
                {
                    yield break;
                }

                using (var reservations = reservationsPtr.MarshalToStructure <DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5>())
                {
                    foreach (var element in reservations.Elements)
                    {
                        var elementIp = element.ReadReservedIp();
                        yield return(FromNative(scope, in elementIp));
                    }
                }
            }
            finally
            {
                Api.FreePointer(reservationsPtr);
            }
        }
예제 #11
0
 internal static DhcpServerDnsSettings GetScopeDnsSettings(DhcpServerScope Scope)
 {
     // Flag is Option 81
     try
     {
         var option = DhcpServerOptionValue.GetScopeDefaultOptionValue(Scope, 81);
         if (option.Values.Count != 1)
         {
             return(GetGlobalDnsSettings(Scope.Server));
         }
         else
         {
             return(null);
         }
     }
     catch (DhcpServerException e) when(e.ApiErrorId == (uint)DhcpErrors.ERROR_FILE_NOT_FOUND)
     {
         return(null);
     }
 }
        internal static IEnumerable <DhcpServerScopeReservation> GetReservations(DhcpServerScope Scope)
        {
            IntPtr reservationsPtr;
            int    elementsRead, elementsTotal;
            IntPtr resumeHandle = IntPtr.Zero;

            var result = Api.DhcpEnumSubnetElementsV5(Scope.Server.ipAddress.ToString(), Scope.address, DHCP_SUBNET_ELEMENT_TYPE_V5.DhcpReservedIps, ref resumeHandle, 0xFFFFFFFF, out reservationsPtr, 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("DhcpEnumSubnetElementsV5", result);
            }

            if (elementsRead == 0)
            {
                yield break;
            }

            try
            {
                var reservations = (DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5)Marshal.PtrToStructure(reservationsPtr, typeof(DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5));

                foreach (var element in reservations.Elements)
                {
                    yield return(FromNative(Scope, element.ReadReservedIp()));
                }
            }
            finally
            {
                Api.DhcpRpcFreeMemory(reservationsPtr);
            }
        }
예제 #13
0
 internal static DhcpServerOptionValue GetScopeDefaultOptionValue(DhcpServerScope Scope, int OptionId)
 {
     return(GetScopeOptionValue(Scope, OptionId, null, null));
 }
 /// <summary>
 /// Creates a new DHCP Scope
 /// </summary>
 /// <param name="name">Name of the DHCP Scope</param>
 /// <param name="comment">Comment for the DHCP Scope</param>
 /// <param name="ipRange">IP Range to be associated with the DHCP Scope</param>
 /// <param name="mask">Subnet mask to be associated with the DHCP Scope</param>
 /// <param name="timeDelayOffer">Milliseconds to wait before sending a lease offer (maximum 1 second)</param>
 /// <param name="leaseDuration">Number of seconds the lease is held for</param>
 /// <returns></returns>
 public IDhcpServerScope AddScope(string name, string comment, DhcpServerIpRange ipRange, DhcpServerIpMask mask, TimeSpan timeDelayOffer, TimeSpan?leaseDuration)
 => DhcpServerScope.CreateScope(Server, name, comment, ipRange, mask, timeDelayOffer, leaseDuration);
예제 #15
0
 internal static IEnumerable <DhcpServerOptionValue> EnumScopeUserOptionValues(DhcpServerScope Scope, string ClassName)
 {
     return(EnumScopeOptionValues(Scope, ClassName, null));
 }
예제 #16
0
 internal static IEnumerable <DhcpServerOptionValue> EnumScopeVendorOptionValues(DhcpServerScope Scope, string VendorName)
 {
     return(EnumScopeOptionValues(Scope, null, VendorName));
 }
예제 #17
0
 internal static IEnumerable <DhcpServerOptionValue> EnumScopeDefaultOptionValues(DhcpServerScope Scope)
 {
     return(EnumScopeOptionValues(Scope, null, null));
 }
예제 #18
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);
        }
 /// <summary>
 /// Creates a new DHCP Scope
 /// </summary>
 /// <param name="name">Name of the DHCP Scope</param>
 /// <param name="comment">Comment for the DHCP Scope</param>
 /// <param name="ipRange">IP Range to be associated with the DHCP Scope</param>
 /// <param name="mask">Subnet mask to be associated with the DHCP Scope</param>
 /// <returns>Newly created DHCP Scope</returns>
 public IDhcpServerScope AddScope(string name, string comment, DhcpServerIpRange ipRange, DhcpServerIpMask mask)
 => DhcpServerScope.CreateScope(Server, name, comment, ipRange, mask);
        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);
            }
        }
예제 #21
0
 internal static DhcpServerFailoverRelationship CreateFailoverRelationship(DhcpServerScope scope, DhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode)
 => CreateFailoverRelationship(scope, partnerServer, name, sharedSecret, mode, mode == DhcpServerFailoverMode.HotStandby ? (byte)5 : (byte)50);
 private static DhcpServerScopeFailoverStatistics FromNative(DhcpServer server, DhcpServerScope scope, in DHCP_FAILOVER_STATISTICS native)
예제 #23
0
 internal static DhcpServerFailoverRelationship CreateFailoverRelationship(DhcpServerScope scope, DhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage)
 => CreateFailoverRelationship(scope, partnerServer, name, sharedSecret, mode, modePercentage, TimeSpan.FromHours(1), null);
예제 #24
0
        internal static void ReplicateScopeRelationship(DhcpServerFailoverRelationship relationship, DhcpServerScope sourceScope)
        {
            var partner          = relationship.ConnectToPartner();
            var destinationScope = partner.Scopes.GetScope(sourceScope.Address);

            sourceScope.ReplicateTo((DhcpServerScope)destinationScope);
        }
예제 #25
0
 internal static DhcpServerOptionValue GetScopeVendorOptionValue(DhcpServerScope Scope, int OptionId, string VendorName)
 {
     return(GetScopeOptionValue(Scope, OptionId, null, VendorName));
 }
예제 #26
0
        internal static void AddScopeToFailoverRelationship(DhcpServerFailoverRelationship relationship, DhcpServerScope scope)
        {
            var partnerServer = (DhcpServer)relationship.ConnectToPartner();

            // create/replicate
            var partnerScope = CreateOrReplicatePartnerScope(partnerServer, scope);

            // determine relationship primary/secondary servers
            DhcpServer primaryServer;
            DhcpServer secondaryServer;

            if (relationship.ServerType == DhcpServerFailoverServerType.PrimaryServer)
            {
                primaryServer   = relationship.Server;
                secondaryServer = partnerServer;
            }
            else
            {
                primaryServer   = partnerServer;
                secondaryServer = relationship.Server;
            }

            using (var relationshipNative = new DHCP_FAILOVER_RELATIONSHIP_Managed(relationshipName: relationship.Name,
                                                                                   scopes: new DHCP_IP_ARRAY_Managed(scope.Address.ToNativeAsNetwork())))
            {
                // update relationship on primary server
                var result = Api.DhcpV4FailoverAddScopeToRelationship(primaryServer.Address, in relationshipNative);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverAddScopeToRelationship), result, "Failed to add scope to failover relationship on primary server");
                }

                // update relationship on secondary server
                result = Api.DhcpV4FailoverAddScopeToRelationship(secondaryServer.Address, in relationshipNative);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverAddScopeToRelationship), result, "Failed to add scope to failover relationship on secondary server");
                }
            }

            // update local cache
            relationship.scopeAddresses.Add(scope.Address);

            // activate scope on partner (if the scope is active on primary)
            if (scope.State == DhcpServerScopeState.Enabled || scope.State == DhcpServerScopeState.EnabledSwitched)
            {
                partnerScope = (DhcpServerScope)partnerServer.Scopes.GetScope(partnerScope.Address);
                partnerScope.Activate();
            }
        }
예제 #27
0
 internal static DhcpServerOptionValue GetScopeUserOptionValue(DhcpServerScope Scope, int OptionId, string ClassName)
 {
     return(GetScopeOptionValue(Scope, OptionId, ClassName, null));
 }
예제 #28
0
 /// <summary>
 /// The Option Value associated with the Scope Configuration
 /// </summary>
 internal DhcpServerOptionValue GetScopeValue(DhcpServerScope scope)
 => DhcpServerOptionValue.GetScopeOptionValue(scope, OptionId, ClassName, VendorName);
예제 #29
0
 /// <summary>
 /// The Option Value associated with the Scope Configuration
 /// </summary>
 public DhcpServerOptionValue GetScopeValue(DhcpServerScope Scope)
 {
     return(DhcpServerOptionValue.GetScopeOptionValue(Scope, OptionId, ClassName, VendorName));
 }
예제 #30
0
        internal static DhcpServerFailoverRelationship CreateFailoverRelationship(DhcpServerScope scope, DhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage, TimeSpan maximumClientLeadTime, TimeSpan?stateSwitchInterval)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }
            if (partnerServer == null)
            {
                throw new ArgumentNullException(nameof(partnerServer));
            }
            if (string.IsNullOrWhiteSpace(sharedSecret))
            {
                throw new ArgumentNullException(nameof(sharedSecret));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = BuildRelationshipName(scope.Server, partnerServer);
            }

            if (!scope.Server.IsCompatible(DhcpServerVersions.Windows2012) || !partnerServer.IsCompatible(DhcpServerVersions.Windows2012))
            {
                throw new DhcpServerException(nameof(Api.DhcpV4FailoverCreateRelationship), DhcpErrors.ERROR_INVALID_PARAMETER, "Windows Server 2012 is required to establish a failover relationship");
            }

            var primaryServerAddress = scope.Server.Address;

            if (primaryServerAddress == (DhcpServerIpAddress)0x7F_00_00_01) // localhost: 127.0.0.1
            {
                primaryServerAddress = scope.Server.BindingElements.First().AdapterPrimaryIpAddress;
            }

            var existingRelationship = scope.GetFailoverRelationship();

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

            // create/replicate
            var partnerScope = CreateOrReplicatePartnerScope(partnerServer, scope);

            // define relationship
            using (var primaryRelationship = new DHCP_FAILOVER_RELATIONSHIP_Managed(primaryServer: primaryServerAddress.ToNativeAsNetwork(),
                                                                                    secondaryServer: partnerServer.Address.ToNativeAsNetwork(),
                                                                                    mode: (DHCP_FAILOVER_MODE)mode,
                                                                                    serverType: DHCP_FAILOVER_SERVER.PrimaryServer,
                                                                                    state: FSM_STATE.NO_STATE,
                                                                                    prevState: FSM_STATE.NO_STATE,
                                                                                    mclt: (int)maximumClientLeadTime.TotalSeconds,
                                                                                    safePeriod: (int)(stateSwitchInterval?.TotalSeconds ?? -1),
                                                                                    relationshipName: name,
                                                                                    primaryServerName: scope.Server.Name,
                                                                                    secondaryServerName: partnerServer.Name,
                                                                                    scopes: new DHCP_IP_ARRAY_Managed(scope.Address.ToNativeAsNetwork()),
                                                                                    percentage: modePercentage,
                                                                                    sharedSecret: sharedSecret))
            {
                // create relationship on partner server
                var partnerRelationship = primaryRelationship.InvertRelationship();
                var result = Api.DhcpV4FailoverCreateRelationship(partnerServer.Address, in partnerRelationship);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverCreateRelationship), result, "Failed to create relationship on partner server");
                }

                // create relationship on primary server
                result = Api.DhcpV4FailoverCreateRelationship(scope.Server.Address, in primaryRelationship);
                if (result != DhcpErrors.SUCCESS)
                {
                    throw new DhcpServerException(nameof(Api.DhcpV4FailoverCreateRelationship), result, "Failed to create relationship on primary server");
                }

                // activate scope on partner (if the scope is active on primary)
                if (scope.State == DhcpServerScopeState.Enabled || scope.State == DhcpServerScopeState.EnabledSwitched)
                {
                    partnerScope = (DhcpServerScope)partnerServer.Scopes.GetScope(partnerScope.Address);
                    partnerScope.Activate();
                }

                return(FromNative(scope.Server, in primaryRelationship));
            }
        }