KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > util > CIDRNetwork


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.util;
21
22 import java.io.EOFException JavaDoc;
23
24 /**
25  */

26 public class CIDRNetwork {
27
28     private final String JavaDoc network;
29     private int networkBits;
30     private String JavaDoc networkAddress;
31     private String JavaDoc subnetMask;
32     private String JavaDoc broadcastAddress;
33     private int[] net;
34     private int[] subnet;
35     private String JavaDoc lastIP;
36
37     /**
38      * Default constructor
39      *
40      * @param network
41      * @throws IllegalArgumentException
42      */

43     public CIDRNetwork(String JavaDoc network) throws IllegalArgumentException JavaDoc {
44
45         int index = network.indexOf("/");
46         if (index == -1)
47             index = network.indexOf("\\");
48
49         if (index == -1)
50             throw new IllegalArgumentException JavaDoc("CIDR network should be in the format 192.168.1.0/24");
51
52         try {
53             networkBits = Integer.parseInt(network.substring(index + 1)) - 1;
54         } catch (NumberFormatException JavaDoc ex) {
55             throw new IllegalArgumentException JavaDoc("CIDR network setting invalid! " + network);
56         }
57
58         this.network = network;
59         subnet = IPUtils.createMaskArray(networkBits);
60         net = IPUtils.getByteAddress(network.substring(0, index));
61         net = IPUtils.calcNetworkNumber(net, subnet);
62         networkAddress = IPUtils.createAddressString(net);
63         subnetMask = IPUtils.createAddressString(subnet);
64     }
65
66     /**
67      * @param startAddress
68      * @param endAddress
69      * @return String
70      * @throws EOFException
71      */

72     public String JavaDoc getNextIPAddress(String JavaDoc startAddress, String JavaDoc endAddress) throws EOFException JavaDoc {
73         if (lastIP == null && (startAddress == null || "".equals(startAddress))) {
74             lastIP = IPUtils.createAddressString(IPUtils.calcFirstAddress(net, subnet));
75         } else if (lastIP == null) {
76             lastIP = startAddress;
77         } else {
78             if (endAddress == null || "".equals(endAddress)) {
79                 String JavaDoc addressString = IPUtils.createAddressString(IPUtils.calcLastAddress(net, networkBits));
80                 if (lastIP.equals(addressString))
81                     throw new EOFException JavaDoc("No more IPs available");
82             } else {
83                 if (lastIP.equals(endAddress))
84                     throw new EOFException JavaDoc("No more IPs available");
85             }
86
87             lastIP = IPUtils.createAddressString(IPUtils.nextAddress(IPUtils.getByteAddress(lastIP)));
88         }
89
90         return lastIP;
91     }
92
93     /**
94      * @return network address
95      */

96     public String JavaDoc getNetworkAddress() {
97         return networkAddress;
98     }
99
100     /**
101      * @return network bits
102      */

103     public int getNetworkBits() {
104         return networkBits + 1;
105     }
106
107     /**
108      * @return subnet mask
109      */

110     public String JavaDoc getSubnetMask() {
111         return subnetMask;
112     }
113
114     /**
115      * @return broadcast address
116      */

117     public String JavaDoc getBroadcastAddress() {
118         return broadcastAddress;
119     }
120
121     /**
122      * @return CIDR string
123      */

124     public String JavaDoc getCIDRString() {
125         return network;
126     }
127
128     public String JavaDoc toString() {
129         return getCIDRString();
130     }
131
132     /**
133      * @param startAddress
134      * @param endAddress
135      * @return true if its valid
136      */

137     public boolean isValidDHCPRange(String JavaDoc startAddress, String JavaDoc endAddress) {
138
139         if (!isValidAddressForNetwork(startAddress))
140             return false;
141
142         if (!isValidAddressForNetwork(endAddress))
143             return false;
144
145         int[] addressOneBytes = IPUtils.getByteAddress(startAddress);
146         int[] addressTwoBytes = IPUtils.getByteAddress(endAddress);
147         boolean valid = false;
148         for (int index = 0; index < addressOneBytes.length; index++) {
149
150             if ((addressOneBytes[index] ^ subnet[index]) < (addressTwoBytes[index] ^ subnet[index])) {
151                 valid = true;
152                 break;
153             }
154         }
155         return valid;
156     }
157
158     /**
159      * @param address
160      * @return true if its valid
161      */

162     public boolean isValidAddressForNetwork(String JavaDoc address) {
163         try {
164             
165             if(address.equals(networkAddress))
166                 return false;
167             
168             int[] bytes = IPUtils.getByteAddress(address);
169
170             boolean valid = true;
171             // Check the network address against the subnet mask
172
for (int index = 0; index < bytes.length; index++) {
173                 int subnetValue = subnet[index];
174                 if (subnetValue == 0)
175                     break;
176
177                 if ((bytes[index] & subnetValue) != net[index]) {
178                     valid = false;
179                     break;
180                 }
181             }
182             return valid;
183         } catch (IllegalArgumentException JavaDoc iae) {
184             return false;
185         }
186     }
187 }
Popular Tags