1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.api.dnsservice.util;
21
22 import java.net.InetAddress;
23 import java.util.Collection;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26
27 import org.apache.james.api.dnsservice.DNSService;
28
29 /**
30 * Class which can be used to check if an ipAddress match a network
31 */
32 public class NetMatcher
33 {
34 private DNSService dnsServer;
35
36 private ArrayList networks;
37
38
39 /**
40 * Init the class with the given networks
41 *
42 * @param nets a Collection which holds all networks
43 */
44 public void initInetNetworks(final Collection nets)
45 {
46 networks = new ArrayList();
47
48 InetNetwork in = new InetNetwork(dnsServer);
49
50 for (Iterator iter = nets.iterator(); iter.hasNext(); ) try
51 {
52 InetNetwork net = in.getFromString((String) iter.next());
53 if (!networks.contains(net)) networks.add(net);
54 }
55 catch (java.net.UnknownHostException uhe)
56 {
57 log("Cannot resolve address: " + uhe.getMessage());
58 }
59 networks.trimToSize();
60 }
61
62 /**
63 * Init the class with the given networks
64 *
65 * @param nets a String[] which holds all networks
66 */
67 public void initInetNetworks(final String[] nets)
68 {
69
70 networks = new ArrayList();
71
72 InetNetwork in = new InetNetwork(dnsServer);
73
74 for (int i = 0; i < nets.length; i++) try
75 {
76 InetNetwork net = in.getFromString(nets[i]);
77 if (!networks.contains(net)) networks.add(net);
78 }
79 catch (java.net.UnknownHostException uhe)
80 {
81 log("Cannot resolve address: " + uhe.getMessage());
82 }
83 networks.trimToSize();
84 }
85
86 /**
87 * Return true if passed host match a network which was used to init the Netmatcher
88 *
89 * @param hostIP the ipAddress or hostname to check
90 * @return true if match the network
91 */
92 public boolean matchInetNetwork(final String hostIP)
93 {
94 InetAddress ip = null;
95
96 try
97 {
98 ip = dnsServer.getByName(hostIP);
99 }
100 catch (java.net.UnknownHostException uhe)
101 {
102 log("Cannot resolve address for " + hostIP + ": " + uhe.getMessage());
103 }
104
105 boolean sameNet = false;
106
107 if (ip != null) for (Iterator iter = networks.iterator(); (!sameNet) && iter.hasNext(); )
108 {
109 InetNetwork network = (InetNetwork) iter.next();
110 sameNet = network.contains(ip);
111 }
112 return sameNet;
113 }
114
115 /**
116 * @see #matchInetNetwork(String)
117 */
118 public boolean matchInetNetwork(final InetAddress ip)
119 {
120 boolean sameNet = false;
121
122 for (Iterator iter = networks.iterator(); (!sameNet) && iter.hasNext(); )
123 {
124 InetNetwork network = (InetNetwork) iter.next();
125 sameNet = network.contains(ip);
126 }
127 return sameNet;
128 }
129
130 /**
131 * Create a new instance of Netmatcher
132 *
133 * @param nets a String[] which holds all networks
134 * @param dnsServer the DNSService which will be used in this class
135 */
136 public NetMatcher(final String[] nets,DNSService dnsServer)
137 {
138 this.dnsServer = dnsServer;
139 initInetNetworks(nets);
140 }
141
142 /**
143 * Create a new instance of Netmatcher
144 *
145 * @param nets a Collection which holds all networks
146 * @param dnsServer the DNSService which will be used in this class
147 */
148 public NetMatcher(final Collection nets,DNSService dnsServer)
149 {
150 this.dnsServer = dnsServer;
151 initInetNetworks(nets);
152 }
153
154 /**
155 * @see InetNetwork#toString()
156 */
157 public String toString() {
158 return networks.toString();
159 }
160
161 /**
162 * Can be overwritten for loggin
163 *
164 * @param s the String to log
165 */
166 protected void log(String s) { }
167 }