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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.net.InetAddress;
31 import java.net.UnknownHostException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
36 * Determine if an IP address is from a machine.
40 private static final EELFLogger logger = EELFManager.getInstance().getLogger(IsFrom.class);
41 private long nextcheck;
46 * Create an IsFrom for the specified fully qualified domain name.
48 public IsFrom(String fqdn) {
53 * Configure the JVM DNS cache to have a 10 second TTL. This needs to be called very very early or it won't have
56 public static void setDNSCache() {
57 java.security.Security.setProperty("networkaddress.cache.ttl", "10");
61 * Check if an IP address matches. If it has been more than 10 seconds since DNS was last checked for changes to
62 * the IP address(es) of this FQDN, check again. Then check if the specified IP address belongs to the FQDN.
64 public synchronized boolean isFrom(String ip) {
65 long now = System.currentTimeMillis();
66 if (now > nextcheck) {
67 nextcheck = now + 10000;
68 ArrayList<String> hostAddrArray = new ArrayList<>();
70 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
71 for (InetAddress addr : addrs) {
72 hostAddrArray.add(addr.getHostAddress());
74 } catch (UnknownHostException e) {
75 logger.error("IsFrom: UnknownHostEx: " + e.toString(), e);
77 ips = hostAddrArray.toArray(new String[0]);
78 logger.debug("IsFrom: DNS ENTRIES FOR FQDN " + fqdn + " : " + Arrays.toString(ips));
80 for (String ipAddr : ips) {
81 if (ipAddr.equals(ip)) {
88 synchronized boolean isReachable(String ip) {
90 if (InetAddress.getByName(ip).isReachable(1000)) {
93 } catch (UnknownHostException e) {
94 logger.error("IsFrom: UnknownHostEx: " + e, e);
95 } catch (IOException e) {
96 logger.error("IsFrom: Failed to parse IP : " + ip + " : " + e, e);
102 * Return the fully qualified domain name.
104 public String toString() {