Esempio n. 1
0
        // find or add a connect token entry
        // intentional constant time worst case search
        private bool findOrAddConnectToken(EndPoint address, byte[] mac, double time)
        {
            int    matchingTokenIndex = -1;
            int    oldestTokenIndex   = -1;
            double oldestTokenTime    = 0.0;

            for (int i = 0; i < connectTokenHistory.Length; i++)
            {
                var token = connectTokenHistory[i];
                if (MiscUtils.CompareHMACConstantTime(token.mac, mac))
                {
                    matchingTokenIndex = i;
                }

                if (oldestTokenIndex == -1 || token.time < oldestTokenTime)
                {
                    oldestTokenTime  = token.time;
                    oldestTokenIndex = i;
                }
            }

            // if no entry is found with the mac, this is a new connect token. replace the oldest token entry.
            if (matchingTokenIndex == -1)
            {
                connectTokenHistory[oldestTokenIndex].time     = time;
                connectTokenHistory[oldestTokenIndex].endpoint = address;
                Buffer.BlockCopy(mac, 0, connectTokenHistory[oldestTokenIndex].mac, 0, mac.Length);
                return(true);
            }

            // allow connect tokens we have already seen from the same address
            if (connectTokenHistory[matchingTokenIndex].endpoint.Equals(address))
            {
                return(true);
            }

            return(false);
        }