9707047c582c37cbf2b19884812e2220caed3c51
[aaf/cadi.git] / core / src / main / java / com / att / cadi / util / NetMask.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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 package com.att.cadi.util;\r
24 \r
25 /* \r
26  * NetMask - a class to quickly validate whether a given IP is part of a mask, as defined by bytes or standard String format.\r
27  * \r
28  * Needs the IPV6 Mask Builder. \r
29  */\r
30 public class NetMask {\r
31         private long mask;\r
32 \r
33         public NetMask(byte[] inBytes) {\r
34                 mask = derive(inBytes);\r
35         }\r
36         \r
37         public NetMask(String string) throws MaskFormatException {\r
38                 mask = derive(string,true);\r
39         }\r
40         \r
41         public boolean isInNet(byte[] inBytes) {\r
42                 long addr = derive(inBytes);\r
43                 return (mask & addr) == addr;\r
44         }\r
45         \r
46         public boolean isInNet(String str) {\r
47                 long addr;\r
48                 try {\r
49                         addr = derive(str,false);\r
50                         return (mask & addr) == addr;\r
51                 } catch (MaskFormatException e) {\r
52                         // will not hit this code;\r
53                         return false;\r
54                 }\r
55         }\r
56 \r
57         public static long derive(byte[] inBytes) {\r
58                 long addr = 0L;\r
59                 int offset = inBytes.length*8;\r
60                 for(int i=0;i<inBytes.length;++i) {\r
61                         addr&=(inBytes[i]<<offset);\r
62                         offset-=8;\r
63                 }\r
64                 return addr;\r
65         }\r
66 \r
67         public static long derive(String str, boolean check) throws MaskFormatException {\r
68                 long rv=0L;\r
69                 int idx=str.indexOf(':');\r
70                 int slash = str.indexOf('/');\r
71 \r
72                 if(idx<0) { // Not IPV6, so it's IPV4... Is there a mask of 123/254?\r
73                         idx=str.indexOf('.');\r
74                         int offset = 24;\r
75                         int end = slash>=0?slash:str.length();\r
76                         int bits = slash>=0?Integer.parseInt(str.substring(slash+1)):32;\r
77                         if(check && bits>32) {\r
78                                 throw new MaskFormatException("Invalid Mask Offset in IPV4 Address");\r
79                         }\r
80                         int prev = 0;\r
81                         long lbyte;\r
82                         while(prev<end) {\r
83                                 if(idx<0) {\r
84                                         idx = end;\r
85                                 }\r
86                                 lbyte = Long.parseLong(str.substring(prev, idx));\r
87                                 if(check && (lbyte>255 || lbyte<0)) {\r
88                                         throw new MaskFormatException("Invalid Byte in IPV4 Address");\r
89                                 }\r
90                                 rv|=lbyte<<offset;\r
91                                 prev = ++idx;\r
92                                 idx=str.indexOf('.',prev);\r
93                                 offset-=8;\r
94                         }\r
95                         rv|=0x00000000FFFFFFFFL>>bits;\r
96                 }\r
97                 return rv;\r
98         }\r
99 \r
100 }\r