001: /*
002: *
003: * Jsmtpd, Java SMTP daemon
004: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package org.jsmtpd.core.common.acl;
022:
023: import java.net.Inet4Address;
024: import java.util.BitSet;
025:
026: /**
027: * Holds an ip address + a network mask<br>
028: * + Utilities to check if an adress is equals to this address or if contained in this mask
029: * @author Jean-Francois POUX
030: */
031: public class ExtendedInet4Address {
032:
033: private Inet4Address ipAd = null;
034: private Inet4Address netmask = null;
035:
036: public ExtendedInet4Address(Inet4Address ipAd, Inet4Address netmask) {
037: this .ipAd = ipAd;
038: this .netmask = netmask;
039: }
040:
041: public ExtendedInet4Address(Inet4Address ipAd) {
042: this .ipAd = ipAd;
043: }
044:
045: public boolean isEqualorInMask(Inet4Address ipIn) {
046:
047: if (ipIn == null)
048: return false;
049:
050: if (ipIn.equals(ipAd))
051: return true;
052:
053: if (netmask != null) {
054:
055: BitSet mask = makeBitSet(netmask.getAddress());
056: BitSet adrs = makeBitSet(ipAd.getAddress());
057: BitSet against = makeBitSet(ipIn.getAddress());
058:
059: for (int i = 0; i < 32; i++) {
060: if (mask.get(i)) {
061: if ((adrs.get(i) != against.get(i)))
062: return false;
063: }
064: }
065: return true;
066:
067: }
068:
069: return false;
070: }
071:
072: public static BitSet makeBitSetST(String host) {
073: String[] res;
074: host = host.replace(".", "Z");
075: res = host.split("Z");
076: System.out.println("host=" + host + ", len=" + res.length);
077: byte[] dt = new byte[res.length];
078:
079: for (int i = 0; i < res.length; i++) {
080: short tmp = Short.parseShort(res[i]);
081: dt[i] = (byte) tmp;
082: }
083:
084: return makeBitSet(dt);
085: }
086:
087: public static BitSet makeBitSet(byte[] in) {
088: BitSet bb = new BitSet();
089: for (int j = 0; j < in.length; j++) {
090: byte bt = in[j];
091: for (int i = 7; i >= 0; i--)
092: if (((1 << i) & bt) != 0)
093: bb.set(-i + 7 + j * 8);
094: else
095: bb.clear(-i + 7 + j * 8);
096: }
097: return bb;
098: }
099:
100: static void debug(BitSet b) {
101:
102: String out = new String();
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="
113: + b.size());
114: }
115:
116: }
|