[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / util / NetMask.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.util;\r
25 \r
26 /* \r
27  * NetMask - a class to quickly validate whether a given IP is part of a mask, as defined by bytes or standard String format.\r
28  * \r
29  * Needs the IPV6 Mask Builder. \r
30  */\r
31 public class NetMask {\r
32         private long mask;\r
33 \r
34         public NetMask(byte[] inBytes) {\r
35                 mask = derive(inBytes);\r
36         }\r
37         \r
38         public NetMask(String string) throws MaskFormatException {\r
39                 mask = derive(string,true);\r
40         }\r
41         \r
42         public boolean isInNet(byte[] inBytes) {\r
43                 long addr = derive(inBytes);\r
44                 return (mask & addr) == addr;\r
45         }\r
46         \r
47         public boolean isInNet(String str) {\r
48                 long addr;\r
49                 try {\r
50                         addr = derive(str,false);\r
51                         return (mask & addr) == addr;\r
52                 } catch (MaskFormatException e) {\r
53                         // will not hit this code;\r
54                         return false;\r
55                 }\r
56         }\r
57 \r
58         public static long derive(byte[] inBytes) {\r
59                 long addr = 0L;\r
60                 int offset = inBytes.length*8;\r
61                 for(int i=0;i<inBytes.length;++i) {\r
62                         addr&=(inBytes[i]<<offset);\r
63                         offset-=8;\r
64                 }\r
65                 return addr;\r
66         }\r
67 \r
68         public static long derive(String str, boolean check) throws MaskFormatException {\r
69                 long rv=0L;\r
70                 int idx=str.indexOf(':');\r
71                 int slash = str.indexOf('/');\r
72 \r
73                 if(idx<0) { // Not IPV6, so it's IPV4... Is there a mask of 123/254?\r
74                         idx=str.indexOf('.');\r
75                         int offset = 24;\r
76                         int end = slash>=0?slash:str.length();\r
77                         int bits = slash>=0?Integer.parseInt(str.substring(slash+1)):32;\r
78                         if(check && bits>32) {\r
79                                 throw new MaskFormatException("Invalid Mask Offset in IPV4 Address");\r
80                         }\r
81                         int prev = 0;\r
82                         long lbyte;\r
83                         while(prev<end) {\r
84                                 if(idx<0) {\r
85                                         idx = end;\r
86                                 }\r
87                                 lbyte = Long.parseLong(str.substring(prev, idx));\r
88                                 if(check && (lbyte>255 || lbyte<0)) {\r
89                                         throw new MaskFormatException("Invalid Byte in IPV4 Address");\r
90                                 }\r
91                                 rv|=lbyte<<offset;\r
92                                 prev = ++idx;\r
93                                 idx=str.indexOf('.',prev);\r
94                                 offset-=8;\r
95                         }\r
96                         rv|=0x00000000FFFFFFFFL>>bits;\r
97                 }\r
98                 return rv;\r
99         }\r
100 \r
101 }\r