Debian Codegraph @

src/DynamicIPList.cpp

raw
1 // DynamicIPList - maintains a sorted list of IP addresses, for checking &
2 // limiting the number of concurrent proxy users.
3
4 // For all support, instructions and copyright go to:
5 // http://e2guardian.org/
6 // Released under the GPL v2, with the OpenSSL exception described in the README file.
7
8 // INCLUDES
9
10 #ifdef HAVE_CONFIG_H
11 #include "e2config.h"
12 #endif
13 #include "DynamicIPList.hpp"
14 #include "Logger.hpp"
15
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <time.h>
19 #include <stdio.h>
20 #include <string>
21 #include <cstdint>
22
23 // GLOBALS
24 extern thread_local std::string thread_id;
25
26 // IMPLEMENTATION
27
28 // constructor - store our options & allocate our lists
29 DynamicIPList::DynamicIPList(int maxitems, int maxitemage)
30 : data(new unsigned long int[maxitems]), datatime(new unsigned long int[maxitems]), size(maxitems), items(0), maxage(maxitemage)
31 {
32 }
33
34 // delete the memory block when the class is destryed
35 DynamicIPList::~DynamicIPList()
36 {
37 delete[] data;
38 delete[] datatime;
39 }
40
41 // store the timestamp for the given entry, used to determine age during purge
42 void DynamicIPList::stamp(unsigned int pos)
43 {
44 datatime[pos] = time(NULL);
45 }
46
47 // binary search for the given item
48 int DynamicIPList::search(int a, int s, unsigned long int ip)
49 {
50 if (a > s)
51 return (-1 - a);
52 int m = (a + s) / 2;
53 unsigned long int i = data[m];
54 if (ip == i)
55 return m;
56 if (ip < i)
57 return search(m + 1, s, ip);
58 if (a == s)
59 return (-1 - a);
60 return search(a, m - 1, ip);
61 }
62
63 // remove entries older then maxage
64 void DynamicIPList::purgeOldEntries()
65 {
66 if (items < 1)
67 return;
68 unsigned long int timenow = time(NULL);
69 for (int i = 0; i < items; i++) {
70 if ((timenow - datatime[i]) > (unsigned)maxage) {
71 data[i] = 0;
72 }
73 }
74 empties();
75 }
76
77 // search for given item in list
78 // -ve if not found 0-(pos + 1) is where it would go
79 // 0 to size if found
80 int DynamicIPList::posInList(unsigned long int ip)
81 {
82 #ifdef DEBUG_LOW
83 DEBUG_debug("****** ip cache table ******");
84 DEBUG_debug("items: ", items);
85 int d;
86 for (d = 0; d < items; d++) {
87 DEBUG_debug(data[d]);
88 }
89 DEBUG_debug("****** ip cache table ******");
90 #endif
91 if (items == 0) {
92 return -1;
93 }
94 return search(0, items - 1, ip);
95 }
96
97 // return whether or not given IP is in/could be added to list
98 // (i.e. returns false if list already full & this IP's not in it)
99 bool DynamicIPList::inList(unsigned long int ip)
100 {
101 // is item already in list?
102 int pos = posInList(ip);
103 if (pos > -1) {
104 stamp(pos);
105 return true;
106 }
107
108 // is list full?
109 if (items >= size) {
110 return false;
111 }
112
113 // list isn't full, and IP not already there, so add it
114 pos = 0 - pos - 1;
115 int i;
116 for (i = items; i > pos; i--) {
117 data[i] = data[i - 1];
118 datatime[i] = datatime[i - 1];
119 }
120 data[pos] = ip;
121 stamp(pos);
122 items++;
123 return true;
124 }
125
126 // shuffle the list to remove gaps left by a purge
127 void DynamicIPList::empties()
128 {
129 int decrement = 0;
130 unsigned long int t;
131 int i;
132 if (data[0] == 0) {
133 decrement = 1;
134 }
135 for (i = 1; i < items; i++) {
136 t = data[i];
137 if (t == 0) {
138 decrement++;
139 } else {
140 if (decrement > 0) {
141 data[i - decrement] = t;
142 datatime[i - decrement] = datatime[i];
143 }
144 }
145 }
146 items -= decrement;
147 }