Source code: hsr/ipm/storing/Host.java
1 package hsr.ipm.storing;
2
3 import com.ezjavabeans.ezwhois.EzWhois;
4 import hsr.ipm.alerting.ErrorManager;
5
6 import java.net.InetAddress;
7
8 /*
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /**
25 * Represents a Host object. Class is able to do dns lookups, reverse dns lookups and
26 * whois lookups for AS number of connections.
27 *
28 * @author Christian Niklaus
29 * @author Daniel Kamm
30 * @version 1.0
31 */
32 public class Host {
33
34 private long id;
35 private ErrorManager errorhandler = ErrorManager.getInstance();
36 private String asNumber;
37 private String hostNameTs;
38 private String hostName;
39 private String ipAddress;
40
41 public Host() {
42 }
43
44 /**
45 * Sets only hostname
46 * @param aHostName
47 */
48 public void setHostName(String aHostName) {
49 hostName = aHostName;
50 }
51
52 /**
53 * Compares two Host if the IPAddress is the same
54 * @param inHost
55 */
56 public boolean equals(Host inHost) {
57 if (ipAddress.equals(inHost.getIpAddress())) {
58 return true;
59 }
60 return false;
61 }
62
63
64 /**
65 * Gets the hostname
66 */
67 public String getHostName() {
68 return hostName;
69 }
70
71 /**
72 * Sets the the HostnameTsattribute
73 */
74 public void setHostNameTs(String aHostName) {
75 hostNameTs = aHostName;
76 }
77
78 /**
79 * Gets the the HostnameTsattribute
80 */
81 public String getHostNameTs() {
82 return hostNameTs;
83 }
84
85 /**
86 * Sets the ip address
87 * @param aIpAddress
88 */
89 public void setIpAddress(String aIpAddress) {
90 ipAddress = aIpAddress;
91 }
92
93 /**
94 * Gets the ip address
95 */
96 public String getIpAddress() {
97 return ipAddress;
98 }
99
100 /**
101 * Sets the as number
102 * @param aASNumber
103 */
104 public void setAsNumber(String aASNumber) {
105 asNumber = aASNumber;
106 }
107
108 /**
109 * Gets the as number
110 */
111 public String getAsNumber() {
112 return asNumber;
113 }
114
115 /**
116 * Sets the id
117 */
118 public long getId() {
119 return id;
120 }
121
122 /**
123 * Gets the id
124 * @param id
125 */
126 public void setId(long id) {
127 this.id = id;
128 }
129
130
131 // private methods
132
133 /**
134 * Completes ip address field of host by given host name field.
135 */
136 private void findIpAddressByName() {
137 int unsignedByte;
138 byte[] address = null;
139 StringBuffer tempIpAddress = new StringBuffer();
140
141 try {
142 InetAddress iAddress = InetAddress.getByName(hostName);
143 address = iAddress.getAddress();
144 for (int i = 0; i < address.length; i++) {
145 if (address[i] < 0) {
146 unsignedByte = address[i] + 256;
147 if (i < 3) {
148 tempIpAddress.append(unsignedByte).append(".");
149 } else {
150 tempIpAddress.append(unsignedByte);
151 }
152 } else {
153 if (i < 3) {
154 tempIpAddress.append(address[i]).append(".");
155 } else {
156 tempIpAddress.append(address[i]);
157 }
158 }
159 }
160 } catch (Exception e) {
161 errorhandler.error("Could not find Host");
162 }
163 ipAddress = tempIpAddress.toString();
164 }
165
166
167 /**
168 * Complete as number field in host by given ip address field.
169 */
170 private void findAsNumberByIp() {
171 StringBuffer whoisResponse = new StringBuffer();
172 String keyword = "origin";
173 String tempAsNumber = null;
174
175 try {
176 EzWhois whois = new EzWhois();
177 whois.setWhoisServer("whois.radb.net");
178 whois.setTimeout(300000);
179 whois.setWhoisPort(43);
180 String info = whois.query(ipAddress);
181 if (!info.startsWith("%")) {
182 whoisResponse.append(info);
183 tempAsNumber = whoisResponse.substring(whoisResponse.indexOf(keyword));
184 tempAsNumber = tempAsNumber.substring(tempAsNumber.indexOf("AS"), tempAsNumber.indexOf("\n") - 1);
185 asNumber = tempAsNumber;
186 } else {
187 asNumber = "0";
188 errorhandler.info("No Entries found");
189 }
190
191 } catch (Exception ex) {
192 System.out.println(ex.getMessage());
193 errorhandler.error("Timeout of RADB-Datenbank => no ASResolution possible");
194 asNumber = "0";
195 }
196 }
197
198
199 /**
200 * Finds hostname in Host by given ip address
201 */
202 private void findHostNameByIp() {
203 StringBuffer tempHostName = new StringBuffer();
204
205 try {
206 InetAddress inetAddress = InetAddress.getByName(ipAddress);
207 tempHostName.append(inetAddress.getCanonicalHostName());
208 } catch (Exception e) {
209 errorhandler.error("Could not find Host");
210 }
211 hostName = tempHostName.toString();
212 }
213
214
215 /**
216 * Returns the content of this Host as a string.
217 */
218 public String toString() {
219 StringBuffer host = new StringBuffer();
220
221 if (ipAddress != null)
222 host.append(ipAddress);
223 else
224 host.append("ip address not resolved");
225 host.append(" (");
226 if (hostName != null)
227 host.append(hostName);
228 else
229 host.append("host name not resolved");
230 host.append(") [");
231 if (asNumber != null)
232 host.append(asNumber);
233 else
234 host.append("as number not resolved");
235 host.append("] ");
236
237 return host.toString();
238 }
239
240 /**
241 * Sets host name of desired host and completes ip address and as number automatically
242 */
243 public void completeHostByName(String hostName) {
244 this.hostName = hostName;
245 findIpAddressByName();
246 findAsNumberByIp();
247 }
248
249 /**
250 * Sets ip address of desired host and completes host name and as number automatically
251 */
252 public void completeHostByIp(String ipAddress) {
253 this.ipAddress = ipAddress;
254 findHostNameByIp();
255 findAsNumberByIp();
256 }
257
258 // main for testing purpose
259 public static void main(String[] args) {
260 Host host = new Host();
261 host.completeHostByIp("130.59.10.40");
262 System.out.println(host.toString());
263
264 Host host1 = new Host();
265 host1.completeHostByName("www.hsr.ch");
266 System.out.println(host1.toString());
267
268 Host host2 = new Host();
269 host2.completeHostByIp("195.186.1.110");
270 System.out.println(host2.toString());
271
272
273 }
274 }