[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-node / src / main / java / com / att / research / datarouter / node / SubnetMatcher.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 com.att.research.datarouter.node;\r
26 \r
27 import java.net.*;\r
28 \r
29 /**\r
30  *      Compare IP addresses as byte arrays to a subnet specified as a CIDR\r
31  */\r
32 public class SubnetMatcher      {\r
33         private byte[]  sn;\r
34         private int     len;\r
35         private int     mask;\r
36         /**\r
37          *      Construct a subnet matcher given a CIDR\r
38          *      @param subnet   The CIDR to match\r
39          */\r
40         public SubnetMatcher(String subnet) {\r
41                 int i = subnet.lastIndexOf('/');\r
42                 if (i == -1) {\r
43                         sn = NodeUtils.getInetAddress(subnet);\r
44                         len = sn.length;\r
45                 } else {\r
46                         len = Integer.parseInt(subnet.substring(i + 1));\r
47                         sn = NodeUtils.getInetAddress(subnet.substring(0, i));\r
48                         mask = ((0xff00) >> (len % 8)) & 0xff;\r
49                         len /= 8;\r
50                 }\r
51         }\r
52         /**\r
53          *      Is the IP address in the CIDR?\r
54          *      @param addr the IP address as bytes in network byte order\r
55          *      @return true if the IP address matches.\r
56          */\r
57         public boolean matches(byte[] addr) {\r
58                 if (addr.length != sn.length) {\r
59                         return(false);\r
60                 }\r
61                 for (int i = 0; i < len; i++) {\r
62                         if (addr[i] != sn[i]) {\r
63                                 return(false);\r
64                         }\r
65                 }\r
66                 if (mask != 0 && ((addr[len] ^ sn[len]) & mask) != 0) {\r
67                         return(false);\r
68                 }\r
69                 return(true);\r
70         }\r
71 }\r