KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > IPAcl > NetMaskImpl


1 /*
2  * @(#)file NetMaskImpl.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.6
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12 package com.sun.jmx.snmp.IPAcl;
13
14
15
16 import java.util.Vector JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.io.Serializable JavaDoc;
19 import java.net.UnknownHostException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21
22 import java.security.Principal JavaDoc;
23 import java.security.acl.Group JavaDoc;
24
25 import com.sun.jmx.trace.Trace;
26
27 /**
28  * This class is used to represent a subnet mask (a group of hosts matching the same
29  * IP mask).
30  *
31  * @see java.security.acl.Group
32  * @version 4.5 02/03/00
33  * @author Sun Microsystems, Inc
34  */

35
36 class NetMaskImpl extends PrincipalImpl implements Group JavaDoc, Serializable JavaDoc {
37     protected byte[] subnet = null;
38     protected int prefix = -1;
39     /**
40      * Constructs an empty group.
41      * @exception UnknownHostException Not implemented
42      */

43     public NetMaskImpl () throws UnknownHostException JavaDoc {
44     }
45   
46     private byte[] extractSubNet(byte[] b) {
47     int addrLength = b.length;
48     byte[] subnet = null;
49     if(isDebugOn()) {
50         debug("extractSubNet","BINARY ARRAY :");
51         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
52         for(int i =0; i < addrLength; i++) {
53         buff.append((int)(b[i] &0xFF) +":");
54         }
55         debug("extractSubNet", buff.toString());
56     }
57     
58     // 8 is a byte size. Common to any InetAddress (V4 or V6).
59
int fullyCoveredByte = prefix / 8;
60     if(fullyCoveredByte == addrLength) {
61         if(isDebugOn()) {
62         debug("extractSubNet"," The mask is the complete address, strange..." + addrLength);
63         }
64         subnet = b;
65         return subnet;
66     }
67     if(fullyCoveredByte > addrLength) {
68         if(isDebugOn()) {
69         debug("extractSubNet"," The number of covered byte is longer than the address. BUG");
70         }
71         throw new IllegalArgumentException JavaDoc("The number of covered byte is longer than the address.");
72     }
73     int partialyCoveredIndex = fullyCoveredByte;
74     if(isDebugOn()) {
75         debug("extractSubNet"," Partialy covered index : " + partialyCoveredIndex);
76     }
77     byte toDeal = b[partialyCoveredIndex];
78     if(isDebugOn()) {
79         debug("extractSubNet"," Partialy covered byte : " + toDeal);
80     }
81     
82     // 8 is a byte size. Common to any InetAddress (V4 or V6).
83
int nbbits = prefix % 8;
84     int subnetSize = 0;
85     
86     if(nbbits == 0)
87     subnetSize = partialyCoveredIndex;
88     else
89     subnetSize = partialyCoveredIndex + 1;
90     
91     if(isDebugOn()) {
92         debug("extractSubNet"," Remains : " + nbbits);
93     }
94     
95     byte mask = 0;
96     for(int i = 0; i < nbbits; i++) {
97         mask |= (1 << (7 - i));
98     }
99     if(isDebugOn()) {
100         debug("extractSubNet","Mask value" + (int) (mask & 0xFF));
101     }
102     
103     byte maskedValue = (byte) ((int)toDeal & (int)mask);
104     
105     if(isDebugOn()) {
106         debug("extractSubNet","Masked byte :" + (int) (maskedValue &0xFF));
107     }
108     subnet = new byte[subnetSize];
109     if(isDebugOn()) {
110         debug("extractSubNet","Resulting subnet : ");
111     }
112     for(int i = 0; i < partialyCoveredIndex; i++) {
113         subnet[i] = b[i];
114         
115         if(isDebugOn()) {
116         debug("extractSubNet",(int) (subnet[i] & 0xFF) +":");
117         }
118     }
119     
120     if(nbbits != 0) {
121         subnet[partialyCoveredIndex] = maskedValue;
122         if(isDebugOn()) {
123         debug("extractSubNet"," Last subnet byte : " + (int) (subnet[partialyCoveredIndex] &0xFF));
124         }
125     }
126     return subnet;
127     }
128
129   /**
130    * Constructs a group using the specified subnet mask.
131    * THIS ALGORITHM IS V4 and V6 compatible.
132    *
133    * @exception UnknownHostException if the subnet mask cann't be built.
134    */

135   public NetMaskImpl (String JavaDoc a, int prefix) throws UnknownHostException JavaDoc {
136     super(a);
137     this.prefix = prefix;
138     subnet = extractSubNet(getAddress().getAddress());
139   }
140   
141   /**
142    * Adds the specified member to the group.
143    *
144    * @param p the principal to add to this group.
145    * @return true if the member was successfully added, false if the
146    * principal was already a member.
147    */

148   public boolean addMember(Principal JavaDoc p) {
149     // we don't need to add members because the ip address is a subnet mask
150
return true;
151   }
152
153   public int hashCode() {
154     return super.hashCode();
155   }
156   
157   /**
158    * Compares this group to the specified object. Returns true if the object
159    * passed in matches the group represented.
160    *
161    * @param p the object to compare with.
162    * @return true if the object passed in matches the subnet mask,
163    * false otherwise.
164    */

165     public boolean equals (Object JavaDoc p) {
166     if (p instanceof PrincipalImpl || p instanceof NetMaskImpl){
167         PrincipalImpl received = (PrincipalImpl) p;
168         InetAddress JavaDoc addr = received.getAddress();
169         if(isDebugOn()) {
170         debug("equals","Received Address : " + addr);
171         }
172         byte[] recAddr = addr.getAddress();
173         for(int i = 0; i < subnet.length; i++) {
174         if(isDebugOn()) {
175             debug("equals","(recAddr[i]) :" + (recAddr[i] & 0xFF));
176             debug("equals","(recAddr[i] & subnet[i]) :" +
177               ( (int) (recAddr[i] & (int)subnet[i]) &0xFF) +
178               "subnet[i] :" + (int) (subnet[i] &0xFF));
179         }
180         if((recAddr[i] & subnet[i]) != subnet[i]) {
181             if(isDebugOn()) {
182             debug("equals","FALSE");
183             }
184             return false;
185         }
186         }
187         if(isDebugOn()) {
188         debug("equals","TRUE");
189         }
190         return true;
191     } else
192         return false;
193     }
194   /**
195    * Returns true if the passed principal is a member of the group.
196    *
197    * @param p the principal whose membership is to be checked.
198    * @return true if the principal is a member of this group, false otherwise.
199    */

200   public boolean isMember(Principal JavaDoc p) {
201     if ((p.hashCode() & super.hashCode()) == p.hashCode()) return true;
202     else return false;
203   }
204   
205   /**
206    * Returns an enumeration which contains the subnet mask.
207    *
208    * @return an enumeration which contains the subnet mask.
209    */

210   public Enumeration JavaDoc members(){
211     Vector JavaDoc v = new Vector JavaDoc(1);
212     v.addElement(this);
213     return v.elements();
214   }
215   
216   /**
217    * Removes the specified member from the group. (Not implemented)
218    *
219    * @param p the principal to remove from this group.
220    * @return allways return true.
221    */

222   public boolean removeMember(Principal JavaDoc p) {
223     return true;
224   }
225   
226   /**
227    * Prints a string representation of this group.
228    *
229    * @return a string representation of this group.
230    */

231   public String JavaDoc toString() {
232     return ("NetMaskImpl :"+ super.getAddress().toString() + "/" + prefix);
233   }
234
235     // TRACES & DEBUG
236
//---------------
237

238     boolean isTraceOn() {
239         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
240     }
241
242     void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
243         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
244     }
245
246     void trace(String JavaDoc func, String JavaDoc info) {
247         trace(dbgTag, func, info);
248     }
249     
250     boolean isDebugOn() {
251         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
252     }
253
254     void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
255         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
256     }
257
258     void debug(String JavaDoc clz, String JavaDoc func, Throwable JavaDoc exception) {
259         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception);
260     }
261
262     void debug(String JavaDoc func, String JavaDoc info) {
263         debug(dbgTag, func, info);
264     }
265     
266     void debug(String JavaDoc func, Throwable JavaDoc exception) {
267         debug(dbgTag, func, exception);
268     }
269     
270     String JavaDoc dbgTag = "NetMaskImpl";
271 }
272
273
274
Popular Tags