Пример #1
0
        internal DHCPv6Packet HandleSolicit(DHCPv6Packet packet, IDHCPv6ServerPropertiesResolver properyResolver)
        {
            DHCPv6Packet innerPacket = packet.GetInnerPacket();

            var     addressProperties = GetAddressProperties();
            Boolean rapitCommit       = addressProperties.IsRapitCommitEnabled() && innerPacket.HasRapitCommitOption();

            DUID   clientIdentifier            = innerPacket.GetClientIdentifer();
            UInt32 identityAssociationId       = innerPacket.GetNonTemporaryIdentityAssocationId().Value;
            UInt32?prefixIdentityAsscocationId = innerPacket.GetPrefixDelegationIdentityAssocationId();

            DHCPv6Lease currentLease = Leases.GetLeaseAssociationIdAndDuid(identityAssociationId, clientIdentifier);

            if (currentLease == null && Resolver.HasUniqueIdentifier)
            {
                currentLease = Leases.GetLeaseByUniqueIdentifier(Resolver.GetUniqueIdentifier(packet));
            }

            IPv6Address excludeFromLease  = IPv6Address.Empty;
            IPv6Address excludeFromPrefix = IPv6Address.Empty;

            IPv6Address            leaseAddress = IPv6Address.Empty;
            DHCPv6PrefixDelegation leasedPrefix = DHCPv6PrefixDelegation.None;

            Boolean skipLeaseCreation = false;

            if (currentLease != DHCPv6Lease.NotFound)
            {
                _logger.LogDebug("active lease found for client {duid} and {iaid}", clientIdentifier, identityAssociationId);

                if (IsFreshLease(currentLease) == true)
                {
                    _logger.LogInformation("a fresh lease found.  Skipping creating of a new lease");
                    skipLeaseCreation = true;
                }
                else
                {
                    if (addressProperties.ReuseAddressIfPossible == true && currentLease.CanBeExtended() == true)
                    {
                        leaseAddress = currentLease.Address;
                        leasedPrefix = currentLease.PrefixDelegation;
                    }
                    else
                    {
                        if (currentLease.IsActive() == true || currentLease.IsPending() == true)
                        {
                            excludeFromLease  = IPv6Address.FromByteArray(currentLease.Address.GetBytes());
                            excludeFromPrefix = currentLease.PrefixDelegation.NetworkAddress;
                        }
                    }
                }
            }

            if (skipLeaseCreation == false)
            {
                if (leaseAddress == IPv6Address.Empty)
                {
                    leaseAddress = GetLeasedAddress(addressProperties, excludeFromLease);

                    if (leaseAddress == IPv6Address.Empty)
                    {
                        _logger.LogError("{scope} has no ip addresses left", Name);
                        base.Apply(new DHCPv6ScopeAddressesAreExhaustedEvent(Id));
                        base.Apply(new DHCPv6SolicitHandledEvent(this.Id, packet, DHCPv6SolicitHandledEvent.SolicitErros.NoAddressesLeft));
                        return(DHCPv6Packet.Empty);
                    }
                }

                if (prefixIdentityAsscocationId.HasValue == true && leasedPrefix == DHCPv6PrefixDelegation.None)
                {
                    leasedPrefix = addressProperties.GetValidPrefix(Leases.GetUsedPrefixes(), prefixIdentityAsscocationId.Value, excludeFromPrefix);
                }

                currentLease = AddLease(packet, addressProperties, clientIdentifier, identityAssociationId, prefixIdentityAsscocationId, currentLease, leaseAddress, leasedPrefix);
            }

            DHCPv6Packet response;

            if (rapitCommit == true)
            {
                response = HandleRequestInternal(packet, properyResolver, true, out DHCPv6RequestHandledEvent.RequestErrors _, skipLeaseCreation);
            }
            else
            {
                response = DHCPv6Packet.AsAdvertise(
                    packet,
                    currentLease,
                    prefixIdentityAsscocationId.HasValue == false ? DHCPv6PrefixDelegation.None : leasedPrefix,
                    addressProperties,
                    GetScopeProperties(),
                    properyResolver.GetServerDuid());
            }

            base.Apply(new DHCPv6SolicitHandledEvent(this.Id, packet, response, rapitCommit));
            return(response);
        }
Пример #2
0
        internal DHCPv4Packet HandleDiscover(DHCPv4Packet packet)
        {
            DHCPv4ScopeAddressProperties addressProperties = GetAddressProperties();

            IPv4Address leaseAddress   = IPv4Address.Empty;
            Boolean     newLeaseNeeded = true;

            DHCPv4ClientIdentifier clientIdentifier = packet.GetClientIdentifier();
            DHCPv4Lease            currentLease     = Leases.GetLeaseByClientIdentifier(clientIdentifier);

            IPv4Address excludeFromLease = IPv4Address.Empty;

            if (currentLease != DHCPv4Lease.Empty)
            {
                if (addressProperties.ReuseAddressIfPossible == true)
                {
                    LeaseTimeValues timers = GetLeaseTimers(addressProperties);
                    currentLease.Renew(timers.Lifespan, timers.RenewTime, timers.ReboundTime, true);
                    newLeaseNeeded = false;

                    leaseAddress = IPv4Address.FromAddress(currentLease.Address);
                }
                else
                {
                    excludeFromLease = IPv4Address.FromByteArray(currentLease.Address.GetBytes());
                    Leases.Revoke(currentLease);
                }
            }
            else
            {
                if (Resolver.HasUniqueIdentifier == true)
                {
                    currentLease = Leases.GetLeaseByUniqueIdentifier(Resolver.GetUniqueIdentifier(packet));
                    if (currentLease != DHCPv4Lease.Empty)
                    {
                        Leases.Revoke(currentLease);
                        excludeFromLease = IPv4Address.FromAddress(currentLease.Address);

                        if (addressProperties.ReuseAddressIfPossible == true)
                        {
                            leaseAddress = IPv4Address.FromAddress(currentLease.Address);
                        }
                    }
                }
            }

            if (leaseAddress == IPv4Address.Empty)
            {
                leaseAddress = GetLeaseAddress(addressProperties, excludeFromLease);

                if (leaseAddress == IPv4Address.Empty)
                {
                    base.Apply(new DHCPv4ScopeAddressesAreExhaustedEvent(Id));
                    base.Apply(new DHCPv4DiscoverHandledEvent(this.Id, packet, DHCPv4DiscoverHandledEvent.DisoverErros.NoAddressesLeft));
                    return(DHCPv4Packet.Empty);
                }
            }

            if (newLeaseNeeded == true)
            {
                currentLease = AddLease(packet, addressProperties, leaseAddress, clientIdentifier);
            }

            DHCPv4ScopeProperties scopeProperties = GetScopeProperties();

            DHCPv4Packet response = DHCPv4Packet.AsDiscoverResponse(
                packet,
                currentLease,
                addressProperties,
                scopeProperties.Properties
                );

            base.Apply(new DHCPv4DiscoverHandledEvent(this.Id, packet, response));
            return(response);
        }