Ejemplo n.º 1
0
 protected void AddStaticBindings(DhcpLink dhcpLink, v4AddressBinding binding)
 {
     if (staticBindingMap.ContainsKey(dhcpLink.GetLinkAddress()))
     {
         V4StaticAddressBinding sb = new V4StaticAddressBinding(binding);
         staticBindingMap[dhcpLink.GetLinkAddress()].Add(sb);
     }
 }
Ejemplo n.º 2
0
        /// <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);
        }