1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package org.onap.dmaap.datarouter.node;
\r
31 * Determine if an IP address is from a machine
\r
33 public class IsFrom {
\r
34 private long nextcheck;
\r
35 private String[] ips;
\r
36 private String fqdn;
\r
38 * 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.
\r
40 public static void setDNSCache() {
\r
41 java.security.Security.setProperty("networkaddress.cache.ttl", "10");
\r
44 * Create an IsFrom for the specified fully qualified domain name.
\r
46 public IsFrom(String fqdn) {
\r
50 * Check if an IP address matches. If it has been more than
\r
51 * 10 seconds since DNS was last checked for changes to the
\r
52 * IP address(es) of this FQDN, check again. Then check
\r
53 * if the specified IP address belongs to the FQDN.
\r
55 public synchronized boolean isFrom(String ip) {
\r
56 long now = System.currentTimeMillis();
\r
57 if (now > nextcheck) {
\r
58 nextcheck = now + 10000;
\r
59 Vector<String> v = new Vector<String>();
\r
61 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
\r
62 for (InetAddress a: addrs) {
\r
63 v.add(a.getHostAddress());
\r
65 } catch (Exception e) {
\r
67 ips = v.toArray(new String[v.size()]);
\r
69 for (String s: ips) {
\r
77 * Return the fully qualified domain name
\r
79 public String toString() {
\r