Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / IsFrom.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.node;\r
26 \r
27 import java.util.*;\r
28 import java.net.*;\r
29 \r
30 /**\r
31  *      Determine if an IP address is from a machine\r
32  */\r
33 public class IsFrom     {\r
34         private long    nextcheck;\r
35         private String[] ips;\r
36         private String  fqdn;\r
37         /**\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
39          */\r
40         public static void setDNSCache() {\r
41                 java.security.Security.setProperty("networkaddress.cache.ttl", "10");\r
42         }\r
43         /**\r
44          *      Create an IsFrom for the specified fully qualified domain name.\r
45          */\r
46         public IsFrom(String fqdn) {\r
47                 this.fqdn = fqdn;\r
48         }\r
49         /**\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
54          */\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
60                         try {\r
61                                 InetAddress[] addrs = InetAddress.getAllByName(fqdn);\r
62                                 for (InetAddress a: addrs) {\r
63                                         v.add(a.getHostAddress());\r
64                                 }\r
65                         } catch (Exception e) {\r
66                         }\r
67                         ips = v.toArray(new String[v.size()]);\r
68                 }\r
69                 for (String s: ips) {\r
70                         if (s.equals(ip)) {\r
71                                 return(true);\r
72                         }\r
73                 }\r
74                 return(false);\r
75         }\r
76         /**\r
77          *      Return the fully qualified domain name\r
78          */\r
79         public String toString() {\r
80                 return(fqdn);\r
81         }\r
82 }\r