1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.node;
\r
30 * Compare IP addresses as byte arrays to a subnet specified as a CIDR
\r
32 public class SubnetMatcher {
\r
37 * Construct a subnet matcher given a CIDR
\r
38 * @param subnet The CIDR to match
\r
40 public SubnetMatcher(String subnet) {
\r
41 int i = subnet.lastIndexOf('/');
\r
43 sn = NodeUtils.getInetAddress(subnet);
\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
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
57 public boolean matches(byte[] addr) {
\r
58 if (addr.length != sn.length) {
\r
61 for (int i = 0; i < len; i++) {
\r
62 if (addr[i] != sn[i]) {
\r
66 if (mask != 0 && ((addr[len] ^ sn[len]) & mask) != 0) {
\r