KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringTokenizer JavaDoc;
23
24 /**
25  */

26 public abstract class IPUtils {
27
28     private IPUtils() {
29     }
30     
31     /**
32      * @param ip
33      * @param mask
34      * @return int[]
35      */

36     public static int[] calcNetworkNumber(int ip[], int mask[]) {
37         int ret[] = new int[4];
38         for (int i = 0; i < 4; i++)
39             ret[i] = ip[i] & mask[i];
40         return ret;
41     }
42
43     /**
44      * @param in
45      * @param mask
46      * @return int[]
47      */

48     public static int[] calcLastAddress(int in[], int mask) {
49         int ret[] = new int[4];
50         ret = calcBroadcastAddress(in, mask);
51         ret[3] = ret[3] - 1;
52         return ret;
53     }
54
55     /**
56      * @param bit
57      * @return int[]
58      */

59     public static int[] createMaskArray(int bit) {
60         int[] mask = new int[4];
61         int rem = (bit + 1) / 8;
62         int mod = (bit + 1) % 8;
63         Integer JavaDoc Int = new Integer JavaDoc(2);
64         Integer JavaDoc modInt = new Integer JavaDoc(8 - mod);
65         double d = Math.pow(Int.doubleValue(), modInt.doubleValue());
66         Double JavaDoc dd = new Double JavaDoc(d);
67         int i;
68         for (i = 0; i < rem; i++)
69             mask[i] = 255;
70
71         mask[i] = 256 - dd.intValue();
72         for (i++; i < 4; i++)
73             mask[i] = 0;
74
75         return mask;
76     }
77
78     /**
79      * @param ip
80      * @param mask
81      * @return int[]
82      */

83     public static int[] calcFirstAddress(int ip[], int mask[]) {
84         int ret[] = new int[4];
85         ret = calcNetworkNumber(ip, mask);
86         ret[3] = ret[3] + 1;
87         return ret;
88     }
89
90     /**
91      * @param in
92      * @param m
93      * @return int[]
94      */

95     public static int[] calcBroadcastAddress(int in[], int m) {
96         int ret[] = new int[4];
97         Integer JavaDoc totalBits = new Integer JavaDoc(32);
98         Integer JavaDoc bits = new Integer JavaDoc(totalBits.intValue() - m - 1);
99         int mask[] = createMaskArray(m);
100         double two = 2D;
101         Double JavaDoc hosts = new Double JavaDoc(Math.pow(two, bits.doubleValue()));
102         hosts.intValue();
103         int ffOctets = bits.intValue() / 8;
104         Integer JavaDoc modBits = new Integer JavaDoc(bits.intValue() % 8);
105         for (int i = 0; i < 4; i++) {
106             ret[i] = in[i];
107             if (i > 4 - ffOctets - 1)
108                 ret[i] = 255;
109         }
110
111         hosts = new Double JavaDoc(Math.pow(two, modBits.doubleValue()));
112         if (ffOctets > 0)
113             ret[4 - ffOctets - 1] = (in[4 - ffOctets - 1] + hosts.intValue()) - 1;
114         else
115             ret[3] = (mask[3] + hosts.intValue()) - 1;
116         return ret;
117     }
118
119     /**
120      * @param ip
121      * @param m
122      * @return int
123      */

124     public static int getNumberOfHosts(int ip[], int m) {
125         Integer JavaDoc totalBits = new Integer JavaDoc(32);
126         Integer JavaDoc bits = new Integer JavaDoc(totalBits.intValue() - m - 1);
127         double two = 2D;
128         Double JavaDoc hosts = new Double JavaDoc(Math.pow(two, bits.doubleValue()));
129         return hosts.intValue() - 2;
130     }
131
132     /**
133      * @param addr
134      * @return String
135      */

136     public static String JavaDoc createAddressString(int[] addr) {
137
138         return addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3];
139     }
140
141     /**
142      * @param ip
143      * @return int[]
144      */

145     public static int[] nextAddress(int[] ip) {
146         if (ip[3] == 255) {
147             ip[3] = 0;
148             if (ip[2] == 255) {
149                 ip[2] = 0;
150                 if (ip[1] == 255) {
151                     ip[1] = 0;
152                     if (ip[0] == 255)
153                         return null;
154                     else
155                         ip[0]++;
156                 } else
157                     ip[1]++;
158             } else
159                 ip[2]++;
160         } else
161             ip[3]++;
162         return ip;
163     }
164
165     /**
166      * @param ipAddress
167      * @return int[]
168      */

169     public static int[] getByteAddress(String JavaDoc ipAddress) {
170         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(ipAddress, ".");
171         int[] ip = new int[4];
172         for (int i = 0; i < ip.length; i++) {
173             if (!tokens.hasMoreTokens())
174                 throw new IllegalArgumentException JavaDoc("IP address must consist of xxx.xxx.xxx.xxx");
175             try {
176                 ip[i] = Integer.parseInt(tokens.nextToken());
177             } catch (NumberFormatException JavaDoc ex) {
178                 throw new IllegalArgumentException JavaDoc("Invalid IP address " + ipAddress);
179             }
180         }
181         return ip;
182     }
183 }
Popular Tags