private DhcpServerScopeReservation(DhcpServerScope scope, DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes)
        {
            Scope              = scope;
            Address            = address;
            HardwareAddress    = hardwareAddress;
            AllowedClientTypes = allowedClientTypes;

            Options = new DhcpServerScopeReservationOptionValueCollection(this);
        }
Example #2
0
        private static void GetVersion(DhcpServerIpAddress address, out int versionMajor, out int versionMinor)
        {
            var result = Api.DhcpGetVersion(ServerIpAddress: address,
                                            MajorVersion: out versionMajor,
                                            MinorVersion: out versionMinor);

            if (result != DhcpErrors.SUCCESS)
            {
                throw new DhcpServerException(nameof(Api.DhcpGetVersion), result);
            }
        }
 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;
 }
        private DhcpServerIpRange(DhcpServerIpAddress startAddress, DhcpServerIpAddress endAddress, DhcpServerIpRangeType type, int bootpClientsAllocated, int maxBootpAllowed)
        {
            if (startAddress > endAddress)
            {
                throw new ArgumentOutOfRangeException(nameof(endAddress), "The ip range start address cannot be greater than the end address");
            }

            this.startAddress          = startAddress;
            this.endAddress            = endAddress;
            this.type                  = type;
            this.bootpClientsAllocated = bootpClientsAllocated;
            this.maxBootpAllowed       = maxBootpAllowed;
        }
Example #5
0
        private DhcpServer(DhcpServerIpAddress address, string name)
        {
            Address = address;
            Name    = name;

            Classes               = new DhcpServerClassCollection(this);
            Options               = new DhcpServerOptionCollection(this);
            Scopes                = new DhcpServerScopeCollection(this);
            Clients               = new DhcpServerClientCollection(this);
            BindingElements       = new DhcpServerBindingElementCollection(this);
            FailoverRelationships = new DhcpServerFailoverRelationshipCollection(this);

            GetVersion(address, out var versionMajor, out var versionMinor);
            VersionMajor = versionMajor;
            VersionMinor = versionMinor;
        }
Example #6
0
        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;
            }
        }
        private static DhcpServerIpRange FromCidr(string cidrSubnet, DhcpServerIpRangeType type, int bootpClientsAllocated, int maxBootpAllowed)
        {
            if (string.IsNullOrEmpty(cidrSubnet))
            {
                throw new ArgumentNullException(nameof(cidrSubnet));
            }

            var slashIndex = cidrSubnet.IndexOf('/');

            if (slashIndex < 7 || !BitHelper.TryParseByteFromSubstring(cidrSubnet, ++slashIndex, cidrSubnet.Length - slashIndex, out var significantBits))
            {
                throw new ArgumentException("Invalid CIDR subnet notation format");
            }

            var address = DhcpServerIpAddress.FromNative(BitHelper.StringToIpAddress(cidrSubnet, 0, --slashIndex));
            var mask    = DhcpServerIpMask.FromSignificantBits(significantBits);

            return(FromMask(address, mask, type, bootpClientsAllocated, maxBootpAllowed));
        }
 internal static DhcpServerDnsSettings GetScopeDnsSettings(DhcpServer server, DhcpServerIpAddress address)
 {
     // Flag is Option 81
     try
     {
         var option = DhcpServerOptionValue.GetScopeDefaultOptionValue(server, address, 81);
         if (option.Values.FirstOrDefault() is DhcpServerOptionElementDWord value)
         {
             return(new DhcpServerDnsSettings((uint)value.RawValue));
         }
         else
         {
             return(GetGlobalDnsSettings(server));
         }
     }
     catch (DhcpServerException e) when(e.ApiErrorId == (uint)DhcpErrors.ERROR_FILE_NOT_FOUND)
     {
         return(GetGlobalDnsSettings(server));
     }
 }
        private static DhcpServerIpRange FromMask(DhcpServerIpAddress address, DhcpServerIpMask mask, DhcpServerIpRangeType type, int bootpClientsAllocated, int maxBootpAllowed)
        {
            var startAddressNative = address.Native & mask.Native;
            var endAddressNative   = (address.Native & mask.Native) | ~mask.Native;

            if (type == DhcpServerIpRangeType.ScopeDhcpOnly ||
                type == DhcpServerIpRangeType.ScopeDhcpAndBootp ||
                type == DhcpServerIpRangeType.ScopeBootpOnly)
            {
                // remove subnet id and broadcast address from range
                startAddressNative++;
                endAddressNative--;
            }

            return(new DhcpServerIpRange(startAddress: DhcpServerIpAddress.FromNative(startAddressNative),
                                         endAddress: DhcpServerIpAddress.FromNative(endAddressNative),
                                         type: type,
                                         bootpClientsAllocated: bootpClientsAllocated,
                                         maxBootpAllowed: maxBootpAllowed));
        }
 /// <summary>
 /// Gets the DHCP scope with the associated scope address
 /// </summary>
 /// <param name="scopeAddress"></param>
 /// <returns></returns>
 public IDhcpServerScope GetScope(DhcpServerIpAddress scopeAddress)
 => DhcpServerScope.GetScope(Server, scopeAddress);
Example #11
0
 public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost, DhcpServerClientTypes clientType, DhcpServerClientAddressStates addressState, DhcpServerClientQuarantineStatuses quarantineStatus, DateTime probationEnds, bool quarantineCapable)
 => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment, leaseExpires, (DhcpServerHost)ownerHost, clientType, addressState, quarantineStatus, probationEnds, quarantineCapable);
Example #12
0
 public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost)
 => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment, leaseExpires, (DhcpServerHost)ownerHost);
Example #13
0
 private DhcpServerHost(DhcpServerIpAddress address, string netBiosName, string serverName)
 {
     Address     = address;
     NetBiosName = netBiosName;
     ServerName  = serverName;
 }
Example #14
0
 public IDhcpServerOptionValue CreateOptionIpAddressValue(DhcpServerIpAddress value)
 {
     ValidateCreateOptionArguments(DhcpServerOptionElementType.IpAddress);
     return(CreateOptionValue(DhcpServerOptionElement.CreateElement(value)));
 }
 internal DhcpServerOptionElementIpAddress(DhcpServerIpAddress value)
 {
     address = value;
 }
Example #16
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);
            }
        }
        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));
        }
 internal static DhcpServerScopeReservation CreateReservation(DhcpServerScope scope, DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress)
 => CreateReservation(scope, address, hardwareAddress, DhcpServerClientTypes.DhcpAndBootp);
Example #19
0
 internal static DhcpServerIpRange FromMask(DhcpServerIpAddress address, DhcpServerIpMask mask, DhcpServerIpRangeType type)
 => FromMask(address, mask, type, DefaultBootpClientsAllocated, DefaultMaxBootpAllowed);
 public DhcpServerIpAddress DataAsIpAddress() => DhcpServerIpAddress.FromNative(DataAsInt32());
 public DhcpServerIpAddress DataAsIpAddress(int index) => DhcpServerIpAddress.FromNative(DataAsInt32(index));
Example #22
0
 public static DhcpServerIpRange AsExcluded(DhcpServerIpAddress startAddress, DhcpServerIpAddress endAddress)
 => new DhcpServerIpRange(startAddress, endAddress, DhcpServerIpRangeType.Excluded);
Example #23
0
 public static DhcpServerIpRange AsDhcpScope(DhcpServerIpAddress address, DhcpServerIpMask mask)
 => FromMask(address, mask, DhcpServerIpRangeType.ScopeDhcpOnly);
Example #24
0
 public static DhcpServerIpRange AsExcluded(DhcpServerIpAddress address, DhcpServerIpMask mask)
 => FromMask(address, mask, DhcpServerIpRangeType.Excluded);
Example #25
0
 public static DhcpServerIpRange AsDhcpScope(DhcpServerIpAddress startAddress, DhcpServerIpAddress endAddress)
 => new DhcpServerIpRange(startAddress, endAddress, DhcpServerIpRangeType.ScopeDhcpOnly);
 /// <summary>
 /// Creates a DHCP scope reservation
 /// </summary>
 /// <param name="address">IP Address to reserve</param>
 /// <param name="hardwareAddress">Hardware address (MAC address) of client associated with this reservation</param>
 /// <param name="allowedClientTypes">Protocols this reservation supports</param>
 /// <returns>The scope reservation</returns>
 public IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes)
 => DhcpServerScopeReservation.CreateReservation(Scope, address, hardwareAddress, allowedClientTypes);
 internal static List <DhcpServerOptionElement> CreateElement(DhcpServerIpAddress value)
 => new List <DhcpServerOptionElement>(1)
 {
     new DhcpServerOptionElementIpAddress(value)
 };
Example #28
0
 public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress)
 => DhcpServerClient.CreateClient(Scope, address, hardwareAddress);
 /// <summary>
 /// Creates a DHCP scope reservation
 /// </summary>
 /// <param name="address">IP Address to reserve</param>
 /// <param name="hardwareAddress">Hardware address (MAC address) of client associated with this reservation</param>
 /// <returns>The scope reservation</returns>
 public IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress)
 => DhcpServerScopeReservation.CreateReservation(Scope, address, hardwareAddress);
Example #30
0
 public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment)
 => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment);