Пример #1
0
        private void SetIaNaT1(DhcpLink clientLink, DhcpV6IaNaOption iaNaOption, long minPreferredLifetime)
        {
            float t1 = DhcpServerPolicies.EffectivePolicyAsFloat(this.clientLink.GetLink(), Property.IA_NA_T1);

            if ((t1 > 1))
            {
                log.Debug(("Setting IA_NA T1 to configured number of seconds: " + t1));
                //  if T1 is greater than one, then treat it as an
                //  absolute value which specifies the number of seconds
                iaNaOption.SetT1(((long)(t1)));
            }
            else
            {
                //  if T1 is less than one and greater than or equal to zero,
                //  then treat is as a percentage of the minimum preferred lifetime
                //  unless the minimum preferred lifetime is infinity (0xffffffff)
                if ((minPreferredLifetime == 4294967295))
                {
                    log.Debug(("Setting IA_NA T1 to minPreferredLifetime of infinity: " + minPreferredLifetime));
                    iaNaOption.SetT1(minPreferredLifetime);
                }
                else if ((t1 >= 0))
                {
                    //  zero means let the client decide
                    log.Debug(("Setting IA_NA T1 to configured ratio="
                               + (t1 + (" of minPreferredLifetime=" + minPreferredLifetime))));
                    iaNaOption.SetT1(((long)((t1 * minPreferredLifetime))));
                }
                else
                {
                    log.Debug(("Setting IA_NA T1 to standard ratio=0.5" + (" of minPreferredLifetime=" + minPreferredLifetime)));
                    iaNaOption.SetT1(((long)((0.5 * minPreferredLifetime))));
                }
            }
        }
Пример #2
0
 public void AddIaNaOption(DhcpV6IaNaOption iaNaOption)
 {
     if (iaNaOptions == null)
     {
         iaNaOptions = new List <DhcpV6IaNaOption>();
     }
     iaNaOptions.Add(iaNaOption);
 }
Пример #3
0
        protected void AddIaNaOptionStatusToReply(DhcpV6IaNaOption iaNaOption, int statusCode)
        {
            DhcpV6IaNaOption replyIaNaOption = new DhcpV6IaNaOption();

            replyIaNaOption.SetIaId(iaNaOption.GetIaId());
            DhcpV6StatusCodeOption status = new DhcpV6StatusCodeOption();

            status.SetStatusCode(statusCode);
            replyIaNaOption.PutDhcpOption(status);
            this.replyMsg.AddIaNaOption(replyIaNaOption);
        }
Пример #4
0
        protected void PopulateIaNaOptions(DhcpV6IaNaOption iaNaOption, DhcpLink dhcpLink)
        {
            Dictionary <int, DhcpOption> optionMap = dhcpServerConfig.EffectiveIaNaOptions(this.requestMsg, dhcpLink);

            if (DhcpServerPolicies.EffectivePolicyAsBoolean(dhcpLink.GetLink(), Property.SEND_REQUESTED_OPTIONS_ONLY))
            {
                optionMap = this.RequestedOptions(optionMap, this.requestMsg);
            }

            iaNaOption.PutAllDhcpOptions(optionMap);
        }
Пример #5
0
        /// <summary>
        /// 租期結束時間
        /// </summary>
        public DateTime GetValidEndTime()
        {
            long             validLifetime = 0;
            DhcpV6IaNaOption iaOption      = (DhcpV6IaNaOption)GetIaNaOptions().FirstOrDefault();

            if (iaOption != null && iaOption.GetIaAddrOptions() != null)
            {
                var iaAddress = iaOption.GetIaAddrOptions().FirstOrDefault();
                validLifetime = iaAddress.GetValidLifetime();
            }

            return(DateTime.Now.AddSeconds(validLifetime));
        }
Пример #6
0
        /**
         * Extract the list of IP addresses from within the given IA_NA option.
         *
         * @param iaNaOption the IA_NA option
         *
         * @return the list of InetAddresses for the IPs in the IA_NA option
         */
        private List <IPAddress> GetInetAddrs(DhcpV6IaNaOption iaNaOption)
        {
            List <IPAddress>          inetAddrs = null;
            List <DhcpV6IaAddrOption> iaAddrs   = iaNaOption.GetIaAddrOptions();

            if ((iaAddrs != null) && iaAddrs.Count > 0)
            {
                inetAddrs = new List <IPAddress>();
                foreach (DhcpV6IaAddrOption iaAddr in iaAddrs)
                {
                    IPAddress inetAddr = iaAddr.GetInetAddress();
                    inetAddrs.Add(inetAddr);
                }
            }
            return(inetAddrs);
        }
Пример #7
0
        /// <summary>
        /// 取得派出的 IP
        /// </summary>
        /// <returns></returns>
        public IPAddress GetIaAddress()
        {
            if (GetIaNaOptions() == null || GetIaNaOptions().Count == 0)
            {
                return(IPAddress.IPv6Any);
            }
            DhcpV6IaNaOption iaOption = (DhcpV6IaNaOption)GetIaNaOptions().FirstOrDefault();

            if (iaOption.GetIaAddrOptions() == null || iaOption.GetIaAddrOptions().Count == 0)
            {
                return(IPAddress.IPv6Any);
            }
            var iaAddress = iaOption.GetIaAddrOptions().FirstOrDefault();

            return(iaAddress.GetInetAddress());
        }
Пример #8
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);
        }
Пример #9
0
        private void SetIaNaT2(DhcpLink clientLink, DhcpV6IaNaOption iaNaOption, long minPreferredLifetime)
        {
            float t2 = DhcpServerPolicies.EffectivePolicyAsFloat(this.clientLink.GetLink(), Property.IA_NA_T2);

            if ((t2 > 1))
            {
                log.Debug(("Setting IA_NA T2 to configured number of seconds: " + t2));
                iaNaOption.SetT2(((long)(t2)));
            }
            else
            {
                //  if T2 is less than one and greater than or equal to zero,
                //  then treat is as a percentage of the minimum preferred lifetime
                //  unless the minimum preferred lifetime is infinity (0xffffffff)
                if ((minPreferredLifetime == 4294967295))
                {
                    log.Debug(("Setting IA_NA T2 to minPreferredLifetime of infinity: " + minPreferredLifetime));
                    iaNaOption.SetT2(minPreferredLifetime);
                }
                else if ((t2 >= 0))
                {
                    //  zero means let the client decide
                    log.Debug(("Setting IA_NA T2 to configured ratio="
                               + (t2 + (" of minPreferredLifetime=" + minPreferredLifetime))));
                    iaNaOption.SetT2(((long)((t2 * minPreferredLifetime))));
                }
                else
                {
                    log.Debug(("Setting IA_NA T2 to standard ratio=0.8" + (" of minPreferredLifetime=" + minPreferredLifetime)));
                    iaNaOption.SetT2(((long)((0.8 * minPreferredLifetime))));
                }
            }

            //  ensure that T2 >= T1
            if ((iaNaOption.GetT2() < iaNaOption.GetT1()))
            {
                log.Warn(("IA_NA T2("
                          + (iaNaOption.GetT2() + (")" + (" < IA_NA T1("
                                                          + (iaNaOption.GetT1() + "): setting T2=T1"))))));
                iaNaOption.SetT2(iaNaOption.GetT1());
            }
        }
Пример #10
0
        public Binding UpdateBinding(Binding binding, DhcpLink clientLink,
                                     DhcpV6ClientIdOption clientIdOption, DhcpV6IaNaOption iaNaOption,
                                     DhcpMessage requestMsg, byte state, IPAddress clientV4IPAddress)
        {
            byte[] duid = clientIdOption.GetDuid();
            long   iaid = iaNaOption.GetIaId();

            StaticBinding staticBinding =
                FindStaticBinding(clientLink.GetLink(), duid, IdentityAssoc.NA_TYPE, iaid, requestMsg);

            if (staticBinding != null)
            {
                return(base.UpdateStaticBinding(binding, clientLink, duid, IdentityAssoc.NA_TYPE,
                                                iaid, staticBinding, requestMsg));
            }
            else
            {
                return(base.UpdateBinding(binding, clientLink, duid, IdentityAssoc.NA_TYPE,
                                          iaid, GetInetAddrs(iaNaOption), requestMsg, state, clientV4IPAddress));
            }
        }
Пример #11
0
        public Binding FindCurrentBinding(DhcpLink clientLink, DhcpV6ClientIdOption clientIdOption, DhcpV6IaNaOption iaNaOption, DhcpMessage requestMsg)
        {
            byte[] duid = clientIdOption.GetDuid();
            long   iaid = iaNaOption.GetIaId();

            return(base.FindCurrentBinding(clientLink, duid, IdentityAssoc.NA_TYPE,
                                           iaid, requestMsg));
        }
Пример #12
0
        protected void AddNaBindingToReply(DhcpLink clientLink, Binding binding)
        {
            DhcpV6IaNaOption dhcpIaNaOption = new DhcpV6IaNaOption();

            dhcpIaNaOption.SetIaId(binding.GetIaid());
            long minPreferredLifetime           = 0;
            HashSet <BindingObject> bindingObjs = binding.GetBindingObjects();

            if (bindingObjs != null && bindingObjs.Count > 0)
            {
                minPreferredLifetime = 4294967295;
                List <DhcpV6IaAddrOption> dhcpIaAddrOptions = new List <DhcpV6IaAddrOption>();
                foreach (BindingObject bindingObj in bindingObjs)
                {
                    DhcpV6IaAddrOption dhcpIaAddrOption = new DhcpV6IaAddrOption();
                    IPAddress          inetAddr         = bindingObj.GetIpAddress();
                    if ((inetAddr != null))
                    {
                        dhcpIaAddrOption.SetIpAddress(inetAddr);
                        //  must be an DhcpOptionConfigObject for IA_NA binding
                        DhcpV6OptionConfigObject configObj = ((DhcpV6OptionConfigObject)(bindingObj.GetConfigObj()));
                        if ((configObj != null))
                        {
                            long preferred = configObj.GetPreferredLifetime();
                            if (minPreferredLifetime == 4294967295 || preferred < minPreferredLifetime)
                            {
                                minPreferredLifetime = preferred;
                            }

                            dhcpIaAddrOption.SetPreferredLifetime(preferred);
                            dhcpIaAddrOption.SetValidLifetime(configObj.GetValidLifetime());
                            this.PopulateNaAddrOptions(dhcpIaAddrOption, this.clientLink, configObj);
                            dhcpIaAddrOptions.Add(dhcpIaAddrOption);
                            // TODO when do actually start the timer?  currently, two get
                            //      created - one during advertise, one during reply
                            //      policy to allow real-time expiration?
                            //                     bp.startExpireTimerTask(bindingAddr, iaAddrOption.getValidLifetime());
                        }
                        else
                        {
                            log.Error(("Null binding pool in binding: " + binding.ToString()));
                        }
                    }
                    else
                    {
                        log.Error(("Null address in binding: " + binding.ToString()));
                    }
                }

                dhcpIaNaOption.SetIaAddrOptions(dhcpIaAddrOptions);
            }
            else
            {
                log.Error("No IA_NA bindings in binding object!");
            }

            this.SetIaNaT1(this.clientLink, dhcpIaNaOption, minPreferredLifetime);
            this.SetIaNaT2(this.clientLink, dhcpIaNaOption, minPreferredLifetime);
            this.PopulateIaNaOptions(dhcpIaNaOption, this.clientLink);
            this.replyMsg.AddIaNaOption(dhcpIaNaOption);
        }