Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
fasthash.cpp
Go to the documentation of this file.
1 
2 /*
3  Copyright 2011 John Selbie
4 
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16 */
17 
18 #include "commonincludes.hpp"
19 
20 #define IS_DIVISIBLE_BY(x, y) ((x % y)==0)
21 
22 static unsigned int FindPrime(unsigned int val)
23 {
24  unsigned int result = val;
25  bool fPrime = false;
26 
27  if (val <= 2)
28  {
29  return 2;
30  }
31 
32  if (val == 3)
33  {
34  return 3;
35  }
36 
37  if ((result % 2)==0)
38  {
39  result++;
40  }
41 
42  while (fPrime == false)
43  {
44  unsigned int stop = (unsigned int)(sqrt(result)+1);
45  fPrime = true;
46  // test to see if result is prime, if it is, return it
47  for (unsigned int x = 3; x <= stop; x++)
48  {
49  if (IS_DIVISIBLE_BY(result, x))
50  {
51  fPrime = false;
52  break;
53  }
54  }
55  if (fPrime==false)
56  {
57  result += 2;
58  }
59  }
60 
61  return result;
62 }
63 
64 size_t FastHash_GetHashTableWidth(unsigned int maxConnections)
65 {
66  // find highest prime, greater than or equal to maxConnections
67  return FindPrime(maxConnections);
68 }
69 
#define IS_DIVISIBLE_BY(x, y)
Definition: fasthash.cpp:20
static unsigned int FindPrime(unsigned int val)
Definition: fasthash.cpp:22
size_t FastHash_GetHashTableWidth(unsigned int maxConnections)
Definition: fasthash.cpp:64