This repository was archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPTree.cs
More file actions
141 lines (122 loc) · 3.87 KB
/
IPTree.cs
File metadata and controls
141 lines (122 loc) · 3.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace Tireless.Net.Blocking
{
public class IPTree
{
private class TreeNode
{
public TreeNode NextNodeBitNotSet
{
get;
set;
}
public TreeNode NextNodeBitSet
{
get;
set;
}
private Boolean isBlocked;
public Boolean IsBlocked
{
get
{
return this.isBlocked || this.NextNodeBitNotSet == null && this.NextNodeBitSet == null;
}
set
{
isBlocked = value;
}
}
public String Identifier
{
get;
set;
}
}
private Dictionary<AddressFamily, TreeNode> roots;
public Int32 NodeCount
{
get;
private set;
}
public Int32 NetworkCount
{
get;
private set;
}
public IPTree()
{
this.roots = new Dictionary<AddressFamily, TreeNode>();
}
public void AddNetwork(IPAddress network, Int32 mask, String identifier = null)
{
var addressBytes = network.GetAddressBytes();
if (this.roots.ContainsKey(network.AddressFamily) == false)
this.roots.Add(network.AddressFamily, new TreeNode());
var currentNode = this.roots[network.AddressFamily];
for (int i = 0; i < mask; i++)
{
var bitset = (addressBytes[i / 8] & (0x80 >> (i % 8))) > 0;
if (bitset)
{
if (currentNode.NextNodeBitSet == null)
currentNode.NextNodeBitSet = this.CreateTreeNode();
currentNode = currentNode.NextNodeBitSet;
}
else
{
if (currentNode.NextNodeBitNotSet == null)
currentNode.NextNodeBitNotSet = this.CreateTreeNode();
currentNode = currentNode.NextNodeBitNotSet;
}
if (i == mask - 1)
{
currentNode.IsBlocked = true;
currentNode.Identifier = identifier;
this.NetworkCount++;
}
}
}
public void AddIPAddress(IPAddress ipAddress, String identifier = null)
{
this.AddNetwork(ipAddress, ipAddress.GetAddressBytes().Length * 8, identifier);
}
private TreeNode CreateTreeNode()
{
this.NodeCount++;
return new TreeNode();
}
public String IsBlocked(IPAddress client)
{
var addressBytes = client.GetAddressBytes();
if (this.roots.ContainsKey(client.AddressFamily) == false)
return null;
var currentNode = this.roots[client.AddressFamily];
int i = 0;
for (; i < addressBytes.Length * 8; i++)
{
var bitset = (addressBytes[i / 8] & (0x80 >> (i % 8))) > 0;
if (bitset)
{
if (currentNode.NextNodeBitSet == null)
break;
currentNode = currentNode.NextNodeBitSet;
}
else
{
if (currentNode.NextNodeBitNotSet == null)
break;
currentNode = currentNode.NextNodeBitNotSet;
}
if (currentNode.IsBlocked == true)
{
return currentNode.Identifier ?? "NULL";
}
}
return null;
}
}
}