1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
27 import org.apache.log4j.Logger;
29 import java.io.IOException;
34 * Determine if an IP address is from a machine
37 private long nextcheck;
40 private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.IsFrom");
43 * Configure the JVM DNS cache to have a 10 second TTL. This needs to be called very very early or it won't have any effect.
45 public static void setDNSCache() {
46 java.security.Security.setProperty("networkaddress.cache.ttl", "10");
50 * Create an IsFrom for the specified fully qualified domain name.
52 public IsFrom(String fqdn) {
57 * Check if an IP address matches. If it has been more than
58 * 10 seconds since DNS was last checked for changes to the
59 * IP address(es) of this FQDN, check again. Then check
60 * if the specified IP address belongs to the FQDN.
62 public synchronized boolean isFrom(String ip) {
63 long now = System.currentTimeMillis();
64 if (now > nextcheck) {
65 nextcheck = now + 10000;
66 ArrayList<String> hostAddrArray = new ArrayList<>();
68 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
69 for (InetAddress addr : addrs) {
70 hostAddrArray.add(addr.getHostAddress());
72 } catch (UnknownHostException e) {
73 logger.debug("IsFrom: UnknownHostEx: " + e.toString(), e);
75 ips = hostAddrArray.toArray(new String[0]);
76 logger.info("IsFrom: DNS ENTRIES FOR FQDN " + fqdn + " : " + Arrays.toString(ips));
78 for (String ipAddr : ips) {
79 if (ipAddr.equals(ip)) {
86 synchronized boolean isReachable(String ip) {
88 if (InetAddress.getByName(ip).isReachable(1000)) {
91 } catch (UnknownHostException e) {
92 logger.debug("IsFrom: UnknownHostEx: " + e.toString(), e);
93 } catch (IOException e) {
94 logger.debug("IsFrom: Failed to parse IP : " + ip + " : " + e.toString(), e);
100 * Return the fully qualified domain name
102 public String toString() {