private V4BindingAddress BuildV4StaticBindingFromIaAddr(IaAddress iaAddr,
                                                                StaticBinding staticBinding)
        {
            V4BindingAddress bindingAddr = new V4BindingAddress(iaAddr, staticBinding);

            return(bindingAddr);
        }
        protected override BindingObject BuildBindingObject(IPAddress inetAddr,
                                                            DhcpLink clientLink, DhcpMessage requestMsg)
        {
            V4AddressBindingPool bp =
                (V4AddressBindingPool)FindBindingPool(clientLink.GetLink(), inetAddr, requestMsg);

            if (bp != null)
            {
                bp.SetUsed(inetAddr);   // TODO check if this is necessary
                IaAddress iaAddr = new IaAddress();
                iaAddr.SetIpAddress(inetAddr);
                V4BindingAddress bindingAddr = new V4BindingAddress(iaAddr, bp);
                SetBindingObjectTimes(bindingAddr,
                                      bp.GetPreferredLifetimeMs(), bp.GetPreferredLifetimeMs());
                // TODO store the configured options in the persisted binding?
                // bindingAddr.setDhcpOptions(bp.getDhcpOptions());
                return(bindingAddr);
            }
            else
            {
                log.Error("Failed to create V4BindingAddress: No V4BindingPool found for IP=" +
                          inetAddr.ToString());
            }
            // MUST have a BindingPool, otherwise something's broke
            return(null);
        }
        /**
         * Create a Binding given an IdentityAssoc loaded from the database.
         *
         * @param ia the ia
         * @param clientLink the client link
         * @param requestMsg the request msg
         *
         * @return the binding
         */
        protected override Binding BuildBindingFromIa(IdentityAssoc ia, DhcpLink clientLink,
                                                      DhcpMessage requestMsg)
        {
            Binding          binding = new Binding(ia, clientLink);
            List <IaAddress> iaAddrs = ia.GetIaAddresses();

            if ((iaAddrs != null) && iaAddrs.Count > 0)
            {
                List <IaAddress> bindingAddrs = new List <IaAddress>();
                foreach (IaAddress iaAddr in iaAddrs)
                {
                    if (!clientLink.GetSubnet().Contains(iaAddr.GetIpAddress()))
                    {
                        log.Info("Ignoring off-link binding address: " +
                                 iaAddr.GetIpAddress().ToString());
                        continue;
                    }
                    V4BindingAddress bindingAddr   = null;
                    StaticBinding    staticBinding =
                        FindStaticBinding(clientLink.GetLink(), ia.GetDuid(),
                                          ia.GetIatype(), ia.GetIaid(), requestMsg);
                    if (staticBinding != null)
                    {
                        bindingAddr =
                            BuildV4StaticBindingFromIaAddr(iaAddr, staticBinding);
                    }
                    else
                    {
                        bindingAddr =
                            BuildV4BindingAddressFromIaAddr(iaAddr, clientLink.GetLink(), requestMsg);
                    }
                    if (bindingAddr != null)
                    {
                        bindingAddrs.Add(bindingAddr);
                    }
                }
                // replace the collection of IaAddresses with BindingAddresses
                binding.SetIaAddresses(bindingAddrs);
            }
            else
            {
                log.Warn("IA has no addresses, binding is empty.");
            }
            return(binding);
        }
        /// <summary>
        /// Perform the DDNS delete processing when a lease is released or expired.
        /// </summary>
        /// <param name="ia">iaAddr the released or expired IaAddress </param>
        /// <param name="iaAddr">iaAddr the released or expired IaAddress </param>
        protected override void DdnsDelete(IdentityAssoc ia, IaAddress iaAddr)
        {
            DhcpV4ClientFqdnOption clientFqdnOption = null;

            try
            {
                if ((ia != null) && (iaAddr != null))
                {
                    List <DhcpOption> opts = iaAddr.GetDhcpOptions();
                    if (opts != null)
                    {
                        foreach (DhcpOption opt in opts)
                        {
                            if (opt.GetCode() == DhcpConstants.V4OPTION_CLIENT_FQDN)
                            {
                                clientFqdnOption = new DhcpV4ClientFqdnOption();
                                //clientFqdnOption.Decode(ByteBuffer.wrap(opt.GetValue()));
                                break;
                            }
                        }
                    }
                    if (clientFqdnOption != null)
                    {
                        string fqdn = clientFqdnOption.GetDomainName();
                        if ((fqdn != null) && fqdn.Count() > 0)
                        {
                            DhcpLink link = serverConfig.FindLinkForAddress(iaAddr.GetIpAddress());
                            if (link != null)
                            {
                                V4BindingAddress bindingAddr   = null;
                                StaticBinding    staticBinding =
                                    FindStaticBinding(link.GetLink(), ia.GetDuid(),
                                                      ia.GetIatype(), ia.GetIaid(), null);
                                if (staticBinding != null)
                                {
                                    bindingAddr =
                                        BuildV4StaticBindingFromIaAddr(iaAddr, staticBinding);
                                }
                                else
                                {
                                    bindingAddr =
                                        BuildV4BindingAddressFromIaAddr(iaAddr, link.GetLink(), null);  // safe to send null requestMsg
                                }
                                if (bindingAddr != null)
                                {
                                    //DdnsCallback ddnsComplete =
                                    //    new DhcpV4DdnsComplete(bindingAddr, clientFqdnOption);

                                    //DhcpConfigObject configObj = bindingAddr.getConfigObj();

                                    //DdnsUpdater ddns =
                                    //    new DdnsUpdater(link.getLink(), configObj,
                                    //            bindingAddr.getIpAddress(), fqdn, ia.getDuid(),
                                    //            configObj.getValidLifetime(),
                                    //            clientFqdnOption.getUpdateABit(), true,
                                    //            ddnsComplete);

                                    //ddns.processUpdates();
                                }
                                else
                                {
                                    log.Error("Failed to find binding for address: " +
                                              iaAddr.GetIpAddress().ToString());
                                }
                            }
                            else
                            {
                                log.Error("Failed to find link for binding address: " +
                                          iaAddr.GetIpAddress().ToString());
                            }
                        }
                        else
                        {
                            log.Error("FQDN is null or empty.  No DDNS deletes performed.");
                        }
                    }
                    else
                    {
                        log.Warn("No Client FQDN option in current binding.  No DDNS deletes performed.");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Failed to perform DDNS delete");
            }
        }