1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.mina.filter.firewall;
22
23 import java.net.Inet4Address;
24 import java.net.Inet6Address;
25 import java.net.InetAddress;
26
27
28
29
30
31
32
33 public class Subnet {
34
35 private static final int IP_MASK_V4 = 0x80000000;
36
37 private static final long IP_MASK_V6 = 0x8000000000000000L;
38
39 private static final int BYTE_MASK = 0xFF;
40
41 private InetAddress subnet;
42
43
44 private int subnetInt;
45
46
47 private long subnetLong;
48
49 private long subnetMask;
50
51 private int suffix;
52
53
54
55
56
57
58
59
60 public Subnet(InetAddress subnet, int mask) {
61 if (subnet == null) {
62 throw new IllegalArgumentException("Subnet address can not be null");
63 }
64
65 if (!(subnet instanceof Inet4Address) && !(subnet instanceof Inet6Address)) {
66 throw new IllegalArgumentException("Only IPv4 and IPV6 supported");
67 }
68
69 if (subnet instanceof Inet4Address) {
70
71 if ((mask < 0) || (mask > 32)) {
72 throw new IllegalArgumentException("Mask has to be an integer between 0 and 32 for an IPV4 address");
73 } else {
74 this.subnet = subnet;
75 subnetInt = toInt(subnet);
76 this.suffix = mask;
77
78
79 this.subnetMask = IP_MASK_V4 >> (mask - 1);
80 }
81 } else {
82
83 if ((mask < 0) || (mask > 128)) {
84 throw new IllegalArgumentException("Mask has to be an integer between 0 and 128 for an IPV6 address");
85 } else {
86 this.subnet = subnet;
87 subnetLong = toLong(subnet);
88 this.suffix = mask;
89
90
91 this.subnetMask = IP_MASK_V6 >> (mask - 1);
92 }
93 }
94 }
95
96
97
98
99 private int toInt(InetAddress inetAddress) {
100 byte[] address = inetAddress.getAddress();
101 int result = 0;
102
103 for (int i = 0; i < address.length; i++) {
104 result <<= 8;
105 result |= address[i] & BYTE_MASK;
106 }
107
108 return result;
109 }
110
111
112
113
114 private long toLong(InetAddress inetAddress) {
115 byte[] address = inetAddress.getAddress();
116 long result = 0;
117
118 for (int i = 0; i < address.length; i++) {
119 result <<= 8;
120 result |= address[i] & BYTE_MASK;
121 }
122
123 return result;
124 }
125
126
127
128
129
130
131
132
133 private long toSubnet(InetAddress address) {
134 if (address instanceof Inet4Address) {
135 return toInt(address) & (int) subnetMask;
136 } else {
137 return toLong(address) & subnetMask;
138 }
139 }
140
141
142
143
144
145
146 public boolean inSubnet(InetAddress address) {
147 if (address.isAnyLocalAddress()) {
148 return true;
149 }
150
151 if (address instanceof Inet4Address) {
152 return (int) toSubnet(address) == subnetInt;
153 } else {
154 return toSubnet(address) == subnetLong;
155 }
156 }
157
158
159
160
161 @Override
162 public String toString() {
163 return subnet.getHostAddress() + "/" + suffix;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (!(obj instanceof Subnet)) {
169 return false;
170 }
171
172 Subnet"../../../../../org/apache/mina/filter/firewall/Subnet.html#Subnet">Subnet other = (Subnet) obj;
173
174 return other.subnetInt == subnetInt && other.suffix == suffix;
175 }
176
177 }