-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel_engine.py
More file actions
81 lines (47 loc) · 2.22 KB
/
sentinel_engine.py
File metadata and controls
81 lines (47 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
sentinel_engine - High performing IP Indexing Module.
Core Algorithm :
1.compress IPv4_addresses to a 32-bit integer (Bit wise left shift).
2.maps the integer to a bucket index using Prime modulo hashing.
3.registers request Frequency to detect DDoS patterns .
Measured Latency ~ 1078 ns (Benchmark)
"""
"""
the original core engine .
def ip_address_to_hashed_index(register,ip_address,prime) :
ip_index=((ip_address[0]<<24)+(ip_address[1]<<16)+(ip_address[2]<<8)+(ip_address[3]))% prime
register[ip_index]+=1
"""
import random
import time
ip_mapper=[0]*1000003 #the map of the ip_addresses
prime_number=1000003 #the prime number used to modulus the index to distribute even patterns
# a test loop only runs on main .
if __name__== "__main__":
Total_fake_ips=100000
mapper = ip_mapper
total_time,count,ip_address_for_passing=0,0,[[0,0,0,0]*Total_fake_ips]
print("Initialising... ")
for i in range(Total_fake_ips):
ip=[random.randint(0,255),random.randint(0,255),random.randint(0,255),random.randint(0,255)]
ip_address_for_passing.append(ip)
print("Fake ips generated ....")
print("Starting engine..3..2..1;")
t_1 = time.perf_counter()
"""
--------------------------------------------------------------------------------------------------------
A High speed optimisation : Inlining & Local Caching
--------------------------------------------------------------------------------------------------------
# I initially Changed Function calls --> Inline function || Global Variable --> Local variable , to bypass
python's function overhead.
# This improves speed from ~600k RPS to ~970k RPS.
---------------------------------------------------------------------------------------------------------
"""
for ip_address in ip_address_for_passing:
ip_index = ((ip_address[0] << 24) + (ip_address[1] << 16) + (ip_address[2] << 8) + (ip_address[3]))% 1000003
mapper[ip_index] += 1
t_2 = time.perf_counter()
total_time=t_2 - t_1
duration=Total_fake_ips/total_time
print("average latency:",duration,"packets per second")
print("latency per packet :",total_time/Total_fake_ips)