Esempio n. 1
0
        protected bool AllIaAddrsOnLink(DhcpV6IaNaOption dhcpIaNaOption, DhcpLink clientLink)
        {
            bool onLink = true;

            //  assume all IPs are on link
            if ((dhcpIaNaOption != null))
            {
                List <DhcpV6IaAddrOption> iaAddrOpts = dhcpIaNaOption.GetIaAddrOptions();
                if ((iaAddrOpts != null))
                {
                    foreach (DhcpV6IaAddrOption iaAddrOpt in iaAddrOpts)
                    {
                        if (this.clientLink.GetSubnet().GetSubnetAddress().IsIPv6LinkLocal)
                        {
                            //  if the Link address is link-local, then check if the
                            //  address is within one of the pools configured for this
                            //  local Link, which automatically makes this server
                            //  "authoritative" (in ISC parlance) for this local net
                            v6AddressPool p = DhcpServerConfiguration.FindNaAddrPool(this.clientLink.GetLink(), iaAddrOpt.GetInetAddress());
                            if ((p == null))
                            {
                                log.Info(("No local address pool found for requested IA_NA: " + (iaAddrOpt.ToString() + " - considered to be off link")));
                                iaAddrOpt.SetPreferredLifetime(0);
                                iaAddrOpt.SetValidLifetime(0);
                                onLink = false;
                            }
                        }
                        else
                        {
                            //  it the Link address is remote, then check
                            //  if the address is valid for that link
                            if (!this.clientLink.GetSubnet().Contains(iaAddrOpt.GetInetAddress()))
                            {
                                log.Info("Setting zero(0) lifetimes for off link address: " + iaAddrOpt.GetIpAddress());
                                iaAddrOpt.SetPreferredLifetime(0);
                                iaAddrOpt.SetValidLifetime(0);
                                onLink = false;
                            }
                        }
                    }
                }
            }

            return(onLink);
        }
        /**
         * Effective policy.
         *
         * @param requestMsg the request msg
         * @param link the link
         * @param prop the prop
         * @return the string
         */
        public static string EffectivePolicy(DhcpV4Message requestMsg, link link, Property prop)
        {
            string policy = null;

            if ((requestMsg != null) && (link != null))
            {
                List <linkFilter> linkFilters = link.linkFilters;
                if (linkFilters != null)
                {
                    foreach (linkFilter linkFilter in linkFilters)
                    {
                        if (DhcpServerConfiguration.MsgMatchesFilter(requestMsg, linkFilter))
                        {
                            policy = GetPolicy(linkFilter.policies, prop.Key());
                        }
                    }
                    // if the client request matches at least one filter on the link,
                    // and that filter has configured a value for the policy, then return
                    // that value from the last filter that the client matches
                    if (policy != null)
                    {
                        return(policy);
                    }
                }
            }
            if (link != null)
            {
                // client does not match a link filter
                // get the value of the policy on the link, if any
                policy = GetPolicy(link.policies, prop.Key());
                if (policy != null)
                {
                    return(policy);
                }
            }
            return(GlobalPolicy(prop));
        }
        protected BindingPool FindBindingPool(link link, IPAddress inetAddr, DhcpMessage requestMsg)
        {
            List <BindingPool> bps = bindingPoolMap.ContainsKey(link.Address) ? bindingPoolMap[link.Address] : null;

            if ((bps != null) && bps.Count > 0)
            {
                foreach (BindingPool bindingPool in bps)
                {
                    //				if (log.isDebugEnabled()) {
                    //					if (bindingPool instanceof AddressBindingPool) {
                    //						AddressBindingPool abp = (AddressBindingPool) bindingPool;
                    //						log.Debug("AddressBindingPool: " + abp.toString());
                    //						log.Debug("FreeList: " + abp.freeListToString());
                    //					}
                    //				}
                    if (bindingPool.Contains(inetAddr))
                    {
                        if ((requestMsg != null) && (bindingPool.GetLinkFilter() != null))
                        {
                            if (DhcpServerConfiguration.MsgMatchesFilter(requestMsg,
                                                                         bindingPool.GetLinkFilter()))
                            {
                                _log.Info("Found filter binding pool: " + bindingPool);
                                return(bindingPool);
                            }
                        }
                        else
                        {
                            _log.Info("Found binding pool: " + bindingPool);
                            return(bindingPool);
                        }
                    }
                }
            }
            return(null);
        }
        /// <summary>
        /// Gets the next free address from the pool(s) configured for the client link.
        /// </summary>
        /// <param name="clientLink">client link</param>
        /// <param name="requestMsg">request message</param>
        /// <returns></returns>
        protected IPAddress GetNextFreeAddress(DhcpLink clientLink, DhcpMessage requestMsg, IPAddress clientV4IPAddress)
        {
            if (clientLink != null)
            {
                List <BindingPool> pools = bindingPoolMap.ContainsKey(clientLink.GetLinkAddress()) ? bindingPoolMap[clientLink.GetLinkAddress()] : null;
                if ((pools != null) && pools.Count > 0)
                {
                    foreach (BindingPool bp in pools)
                    {
                        linkFilter filter = bp.GetLinkFilter();
                        if ((requestMsg != null) && (filter != null))
                        {
                            if (!DhcpServerConfiguration.MsgMatchesFilter(requestMsg, filter))
                            {
                                _log.Info("Client request does not match filter, skipping pool: " + bp.ToString());
                                continue;
                            }
                        }
                        _log.Debug("Getting next available address from pool: " + bp.ToString());

                        if (clientV4IPAddress != null)
                        {
                            //if (bp.GetV6AssignRule() != AssignDhcpV6Rule.Noen && bp.GetV6AssignRule() != AssignDhcpV6Rule.AssignV6IPAndDNSByV6Pool)
                            //{
                            //    string[] clientV4IPArray = clientV4IPAddress.ToString().Split('.');
                            //    IPAddress clientV6Addr = null;
                            //    if (bp.GetV6AssignRule() == AssignDhcpV6Rule.AssignV6IPAndDNSByTransferV4IPLast2)
                            //    {
                            //        clientV6Addr = bp.GetNextAvailableAddress(clientV4IPArray[2], clientV4IPArray[3]);
                            //    }
                            //    else
                            //    {
                            //        clientV6Addr = bp.GetNextAvailableAddress("", clientV4IPArray[3]);
                            //    }
                            //    if (clientV6Addr != null)
                            //    {
                            //        return clientV6Addr;
                            //    }
                            //    // Callback sreach V4 IP Using MAC
                            //    // 依照回傳的 IPv4 IP 決定回傳的 IPv6 IP
                            //    // Return new IPAddress(clientLink.GetLinkAddress() + "IPv4 尾 1 碼或 2 碼")
                            //    // byte[] v4TransferV6 = new byte[] { 32, 1, 176, 48, 17, 40, 1, 96, 0, 0, 0, 0, 0, 97, 1, 151 };
                            //    // return new IPAddress(v4TransferV6);
                            //    // 若無 v4 IP 則依照既有的邏輯進行派發,派發的 IP 從::255:255 以後開始派發
                            //}
                        }
                        IPAddress free = bp.GetNextAvailableAddress();
                        if (free != null)
                        {
                            _log.Debug("Found next available address: " + free.ToString());
                            return(free);
                        }
                        else
                        {
                            // warning here, b/c there may be more pools
                            _log.Warn("No free addresses available in pool: " + bp.ToString());
                        }
                    }
                    // if we get this far, then we did not find any free(virgin) addresses
                    // so we must start searching for any that can be used in the pools
                    foreach (BindingPool bp in pools)
                    {
                        linkFilter filter = bp.GetLinkFilter();
                        if ((requestMsg != null) && (filter != null))
                        {
                            if (!DhcpServerConfiguration.MsgMatchesFilter(requestMsg, filter))
                            {
                                _log.Info("Client request does not match filter, skipping pool: " + bp.ToString());
                                continue;
                            }
                        }
                        IPAddress reused = ReuseAvailableAddress(bp);
                        if (reused != null)
                        {
                            return(reused);
                        }
                    }
                }
                else
                {
                    _log.Error("No Pools defined in server configuration for Link: " + clientLink.GetLinkAddress());
                }
            }
            else
            {
                throw new Exception("ClientLink is null");
            }
            return(null);
        }