Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B3DF2200AE4 for ; Wed, 11 May 2016 05:39:44 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B2A1A160A11; Wed, 11 May 2016 03:39:44 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D6F1516098A for ; Wed, 11 May 2016 05:39:43 +0200 (CEST) Received: (qmail 14702 invoked by uid 500); 11 May 2016 03:39:43 -0000 Mailing-List: contact dev-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list dev@cloudstack.apache.org Received: (qmail 14691 invoked by uid 99); 11 May 2016 03:39:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 May 2016 03:39:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 89A33DFC71; Wed, 11 May 2016 03:39:42 +0000 (UTC) From: jburwell To: dev@cloudstack.apache.org Reply-To: dev@cloudstack.apache.org References: In-Reply-To: Subject: [GitHub] cloudstack pull request: OSPF: adding dynamically routing capabili... Content-Type: text/plain Message-Id: <20160511033942.89A33DFC71@git1-us-west.apache.org> Date: Wed, 11 May 2016 03:39:42 +0000 (UTC) archived-at: Wed, 11 May 2016 03:39:44 -0000 Github user jburwell commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1371#discussion_r62788152 --- Diff: utils/src/main/java/com/cloud/utils/net/cidr/CIDR4.java --- @@ -0,0 +1,172 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.utils.net.cidr; + +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.apache.commons.lang.NotImplementedException; +import org.apache.log4j.Logger; + +import com.cloud.utils.net.NetUtils; + +/** + */ +public class CIDR4 implements CIDR { + protected final static Logger s_logger = Logger.getLogger(CIDR4.class); + + protected InetAddress baseAddress; + protected int cidrMask; + private int addressInt; + private final int addressEndInt; + + protected CIDR4(Inet4Address newaddr, int mask) { + cidrMask = mask; + addressInt = ipv4AddressToInt(newaddr); + int newmask = ipv4PrefixLengthToMask(mask); + addressInt &= newmask; + try { + baseAddress = intToIPv4Address(addressInt); + } catch (UnknownHostException e) { + // this should never happen + } + addressEndInt = addressInt + ipv4PrefixLengthToLength(cidrMask) - 1; + } + + public int compareTo(CIDR arg) { + if (arg instanceof CIDR6) { + throw new NotImplementedException("Not implemented for CIDR6"); + } + CIDR4 o = (CIDR4)arg; + if (o.addressInt == addressInt && o.cidrMask == cidrMask) { + return 0; + } + if (o.addressInt < addressInt) { + return 1; + } + if (o.addressInt > addressInt) { + return -1; + } + if (o.cidrMask < cidrMask) { + // greater Mask means less IpAddresses so -1 + return -1; + } + return 1; + } + + @Override + public boolean isNull() { + return false; + } + + @Override + public boolean contains(InetAddress inetAddress) { + if (inetAddress == null) { + throw new NullPointerException("inetAddress"); + } + + if (cidrMask == 0) { + return true; + } + + int search = ipv4AddressToInt(inetAddress); + return search >= addressInt && search <= addressEndInt; + } + + private static int ipv4PrefixLengthToLength(int prefixLength) { + return 1 << 32 - prefixLength; + } + + private static int ipv4PrefixLengthToMask(int prefixLength) { + return ~((1 << 32 - prefixLength) - 1); + } + + private static InetAddress intToIPv4Address(int addr) throws UnknownHostException { + byte[] a = new byte[4]; + a[0] = (byte)(addr >> 24 & 0xFF); + a[1] = (byte)(addr >> 16 & 0xFF); + a[2] = (byte)(addr >> 8 & 0xFF); + a[3] = (byte)(addr & 0xFF); + return InetAddress.getByAddress(a); + } + + private static int ipv4AddressToInt(InetAddress addr) { + byte[] address; + if (addr instanceof Inet6Address) { + throw new NotImplementedException("Not implemented for ipv6 address"); + } else { + address = addr.getAddress(); + } + return ipv4AddressToInt(address); + } + + private static int ipv4AddressToInt(byte[] address) { + int net = 0; + for (byte addres : address) { + net <<= 8; + net |= addres & 0xFF; + } + return net; + } + + @Override + public InetAddress getBaseAddress() { + return baseAddress; + } + + @Override + public int getMask() { + return cidrMask; + } + + @Override + public boolean contains(CIDR cidr) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean overlaps(CIDR cidr) { + Long[] cidrALong = NetUtils.cidrToLong(this); + Long[] cidrBLong = NetUtils.cidrToLong(cidr); + final long shift = MAX_CIDR - (cidrALong[1] > cidrBLong[1] ? cidrBLong[1] : cidrALong[1]); + return cidrALong[0] >> shift == cidrBLong[0] >> shift; + } + + @Override + public String toString() { + return baseAddress.getHostAddress() + '/' + cidrMask; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof CIDR)) { + return false; + } + return compareTo((CIDR)o) == 0; --- End diff -- ``equals`` should perform a content based comparison for equality. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---