@@ -69,6 +69,13 @@ private SubnetInfo() {}
69
69
private int low () { return network () + 1 ; }
70
70
private int high () { return broadcast () - 1 ; }
71
71
72
+ /**
73
+ * Returns true if the parameter <code>address</code> is in the
74
+ * range of usable endpoint addresses for this subnet. This excludes the
75
+ * network and broadcast adresses.
76
+ * @param address A dot-delimited IPv4 address, e.g. "192.168.0.1"
77
+ * @return True if in range, false otherwise
78
+ */
72
79
public boolean isInRange (String address ) { return isInRange (toInteger (address )); }
73
80
74
81
private boolean isInRange (int address ) {
@@ -131,10 +138,12 @@ private void calculate(String mask) {
131
138
address = matchAddress (matcher );
132
139
133
140
/* Create a binary netmask from the number of bits specification /x */
134
- int cidrPart = rangeCheck (Integer .parseInt (matcher .group (5 )), 0 , NBITS -1 );
141
+ int cidrPart = rangeCheck (Integer .parseInt (matcher .group (5 )), - 1 , NBITS -1 );
135
142
for (int j = 0 ; j < cidrPart ; ++j ) {
136
143
netmask |= (1 << 31 -j );
137
144
}
145
+
146
+ rangeCheck (pop (netmask ),0 , NBITS );
138
147
139
148
/* Calculate base network address */
140
149
network = (address & netmask );
@@ -165,7 +174,7 @@ private int toInteger(String address) {
165
174
private int matchAddress (Matcher matcher ) {
166
175
int addr = 0 ;
167
176
for (int i = 1 ; i <= 4 ; ++i ) {
168
- int n = (rangeCheck (Integer .parseInt (matcher .group (i )), 0 , 255 ));
177
+ int n = (rangeCheck (Integer .parseInt (matcher .group (i )), - 1 , 255 ));
169
178
addr |= ((n & 0xff ) << 8 *(4 -i ));
170
179
}
171
180
return addr ;
@@ -196,10 +205,12 @@ private String format(int[] octets) {
196
205
}
197
206
198
207
/*
199
- * Convenience function to check integer boundaries
208
+ * Convenience function to check integer boundaries.
209
+ * Checks if a value x is in the range (begin,end].
210
+ * Returns x if it is in range, throws an exception otherwise.
200
211
*/
201
212
private int rangeCheck (int value , int begin , int end ) {
202
- if (value > begin && value <= end ) // (0,nbits ]
213
+ if (value > begin && value <= end ) // (begin,end ]
203
214
return value ;
204
215
205
216
throw new IllegalArgumentException ("Value out of range: [" + value + "]" );
0 commit comments