KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > common > acl > ExtendedInet4Address


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
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
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.core.common.acl;
22
23 import java.net.Inet4Address JavaDoc;
24 import java.util.BitSet JavaDoc;
25
26 /**
27  * Holds an ip address + a network mask<br>
28  * + Utilities to check if an adress is equals to this address or if contained in this mask
29  * @author Jean-Francois POUX
30  */

31 public class ExtendedInet4Address {
32
33     private Inet4Address JavaDoc ipAd = null;
34     private Inet4Address JavaDoc netmask = null;
35
36     public ExtendedInet4Address(Inet4Address JavaDoc ipAd, Inet4Address JavaDoc netmask) {
37         this.ipAd = ipAd;
38         this.netmask = netmask;
39     }
40
41     public ExtendedInet4Address(Inet4Address JavaDoc ipAd) {
42         this.ipAd = ipAd;
43     }
44
45     public boolean isEqualorInMask(Inet4Address JavaDoc ipIn) {
46
47         if (ipIn == null)
48             return false;
49
50         if (ipIn.equals(ipAd))
51             return true;
52
53         if (netmask != null) {
54
55             BitSet JavaDoc mask = makeBitSet(netmask.getAddress());
56             BitSet JavaDoc adrs = makeBitSet(ipAd.getAddress());
57             BitSet JavaDoc against = makeBitSet(ipIn.getAddress());
58
59             for (int i = 0; i < 32; i++) {
60                 if (mask.get(i)) {
61                     if ((adrs.get(i) != against.get(i)))
62                         return false;
63                 }
64             }
65             return true;
66
67         }
68
69         return false;
70     }
71
72     public static BitSet JavaDoc makeBitSetST(String JavaDoc host) {
73         String JavaDoc[] res;
74         host = host.replace(".", "Z");
75         res = host.split("Z");
76         System.out.println("host=" + host + ", len=" + res.length);
77         byte[] dt = new byte[res.length];
78
79         for (int i = 0; i < res.length; i++) {
80             short tmp = Short.parseShort(res[i]);
81             dt[i] = (byte) tmp;
82         }
83
84         return makeBitSet(dt);
85     }
86
87     public static BitSet JavaDoc makeBitSet(byte[] in) {
88         BitSet JavaDoc bb = new BitSet JavaDoc();
89         for (int j = 0; j < in.length; j++) {
90             byte bt = in[j];
91             for (int i = 7; i >= 0; i--)
92                 if (((1 << i) & bt) != 0)
93                     bb.set(-i + 7 + j * 8);
94                 else
95                     bb.clear(-i + 7 + j * 8);
96         }
97         return bb;
98     }
99
100     static void debug(BitSet JavaDoc b) {
101
102         String JavaDoc out = new String JavaDoc();
103         int i = 0;
104         for (int k = 0; k < b.size(); k++) {
105             out += (b.get(k) ? "1" : "0");
106             i++;
107             if (i == 8) {
108                 out += " ";
109                 i = 0;
110             }
111         }
112         System.out.println("bit set contains " + out + ", size=" + b.size());
113     }
114
115 }
Popular Tags