001 /*
002 *
003 * $Revision$ $Date$
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.access.mcrimpl;
025
026 import java.net.InetAddress;
027 import java.net.UnknownHostException;
028
029 /**
030 * A class for representing an IP Address, or a range of IP addresses
031 *
032 * @author Matthias Kramm
033 */
034 public class MCRIPAddress {
035 byte[] address;
036
037 byte[] mask;
038
039 public MCRIPAddress(String ip) throws UnknownHostException {
040 int i = ip.indexOf('/');
041
042 if (i >= 0) {
043 String ipstr = ip.substring(0, i);
044 String maskstr = ip.substring(i + 1);
045 InetAddress address = InetAddress.getByName(ipstr);
046 InetAddress mask = InetAddress.getByName(maskstr);
047 init(address, mask);
048 } else {
049 InetAddress address = InetAddress.getByName(ip);
050 init(address);
051 }
052 }
053
054 public MCRIPAddress(InetAddress address, InetAddress mask) {
055 init(address, mask);
056 }
057
058 public MCRIPAddress(InetAddress address) {
059 init(address);
060 }
061
062 public void init(InetAddress address, InetAddress mask) {
063 this.address = address.getAddress();
064 this.mask = mask.getAddress();
065 }
066
067 public void init(InetAddress address) {
068 int t;
069 this.address = address.getAddress();
070 this.mask = new byte[this.address.length];
071
072 for (t = 0; t < this.address.length; t++)
073 this.mask[t] = (byte) 255;
074 }
075
076 boolean contains(MCRIPAddress other) {
077 int t;
078
079 if (this.address.length != other.address.length) {
080 throw new IllegalStateException("can't map IPv6 to IPv4 and vice versa");
081 }
082
083 for (t = 0; t < address.length; t++) {
084 if ((this.address[t] & this.mask[t]) != (other.address[t] & this.mask[t])) {
085 return false;
086 }
087 }
088
089 return true;
090 }
091
092 public String toString(){
093 StringBuffer sb = new StringBuffer("");
094 for (int i=0; i<address.length; i++){
095 if(i > 0)
096 sb.append(".");
097 sb.append((address[i]&255));
098 }
099 sb.append("/");
100 for (int i=0; i<mask.length; i++){
101 if(i > 0)
102 sb.append(".");
103 sb.append((mask[i]&255));
104 }
105 return sb.toString();
106
107 }
108 }