+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.util;\r
-\r
-/* \r
- * NetMask - a class to quickly validate whether a given IP is part of a mask, as defined by bytes or standard String format.\r
- * \r
- * Needs the IPV6 Mask Builder. \r
- */\r
-public class NetMask {\r
- private long mask;\r
-\r
- public NetMask(byte[] inBytes) {\r
- mask = derive(inBytes);\r
- }\r
- \r
- public NetMask(String string) throws MaskFormatException {\r
- mask = derive(string,true);\r
- }\r
- \r
- public boolean isInNet(byte[] inBytes) {\r
- long addr = derive(inBytes);\r
- return (mask & addr) == addr;\r
- }\r
- \r
- public boolean isInNet(String str) {\r
- long addr;\r
- try {\r
- addr = derive(str,false);\r
- return (mask & addr) == addr;\r
- } catch (MaskFormatException e) {\r
- // will not hit this code;\r
- return false;\r
- }\r
- }\r
-\r
- public static long derive(byte[] inBytes) {\r
- long addr = 0L;\r
- int offset = inBytes.length*8;\r
- for(int i=0;i<inBytes.length;++i) {\r
- addr&=(inBytes[i]<<offset);\r
- offset-=8;\r
- }\r
- return addr;\r
- }\r
-\r
- public static long derive(String str, boolean check) throws MaskFormatException {\r
- long rv=0L;\r
- int idx=str.indexOf(':');\r
- int slash = str.indexOf('/');\r
-\r
- if(idx<0) { // Not IPV6, so it's IPV4... Is there a mask of 123/254?\r
- idx=str.indexOf('.');\r
- int offset = 24;\r
- int end = slash>=0?slash:str.length();\r
- int bits = slash>=0?Integer.parseInt(str.substring(slash+1)):32;\r
- if(check && bits>32) {\r
- throw new MaskFormatException("Invalid Mask Offset in IPV4 Address");\r
- }\r
- int prev = 0;\r
- long lbyte;\r
- while(prev<end) {\r
- if(idx<0) {\r
- idx = end;\r
- }\r
- lbyte = Long.parseLong(str.substring(prev, idx));\r
- if(check && (lbyte>255 || lbyte<0)) {\r
- throw new MaskFormatException("Invalid Byte in IPV4 Address");\r
- }\r
- rv|=lbyte<<offset;\r
- prev = ++idx;\r
- idx=str.indexOf('.',prev);\r
- offset-=8;\r
- }\r
- rv|=0x00000000FFFFFFFFL>>bits;\r
- }\r
- return rv;\r
- }\r
-\r
-}\r