private DhcpServerFailoverRelationship( DhcpServer server, string name, DhcpServerFailoverMode mode, byte modePercentage, DhcpServerFailoverState state, DhcpServerFailoverState previousState, DhcpServerIpAddress primaryServerAddress, string primaryServerName, DhcpServerIpAddress secondaryServerAddress, string secondaryServerName, DhcpServerFailoverServerType serverType, string sharedSecret, TimeSpan maximumClientLeadTime, TimeSpan?stateSwitchInterval, List <DhcpServerIpAddress> scopeAddresses ) { Server = server; Name = name; Mode = mode; State = state; PreviousState = previousState; PrimaryServerAddress = primaryServerAddress; PrimaryServerName = primaryServerName; SecondaryServerAddress = secondaryServerAddress; SecondaryServerName = secondaryServerName; ServerType = serverType; SharedSecret = sharedSecret; MaximumClientLeadTime = maximumClientLeadTime; StateSwitchoverInterval = stateSwitchInterval; this.scopeAddresses = scopeAddresses; switch (mode) { case DhcpServerFailoverMode.LoadBalance: LoadBalancePercentage = modePercentage; break; case DhcpServerFailoverMode.HotStandby: HotStandbyAddressesReservedPercentage = modePercentage; break; } }
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);
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)); } }
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);