Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import java.math.BigInteger; |
| 4 | import java.util.Arrays; |
| 5 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 6 | import com.google.common.base.Preconditions; |
| 7 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 8 | public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 9 | public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 11 | private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 12 | super(value, mask); |
| 13 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 14 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 15 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 16 | public IPVersion getIpVersion() { |
| 17 | return IPVersion.IPv6; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 20 | public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 21 | Preconditions.checkNotNull(value, "value must not be null"); |
| 22 | Preconditions.checkNotNull(mask, "mask must not be null"); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 23 | return new IPv6AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 24 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 25 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 26 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 27 | public static IPv6AddressWithMask of(final String string) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 28 | Preconditions.checkNotNull(string, "string must not be null"); |
| 29 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 30 | int slashPos; |
| 31 | String ip = string; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 32 | int maskBits = 128; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 33 | IPv6Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 34 | |
| 35 | // Read mask suffix |
| 36 | if ((slashPos = string.indexOf('/')) != -1) { |
| 37 | ip = string.substring(0, slashPos); |
| 38 | try { |
| 39 | String suffix = string.substring(slashPos + 1); |
| 40 | if (suffix.length() == 0) |
| 41 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 42 | if (suffix.indexOf(':') != -1) { |
| 43 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 44 | maskAddress = IPv6Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 45 | } else { |
| 46 | // CIDR Suffix |
| 47 | maskBits = Integer.parseInt(suffix); |
| 48 | } |
| 49 | } catch (NumberFormatException e) { |
| 50 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 51 | } |
| 52 | if (maskBits < 0 || maskBits > 128) { |
| 53 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 54 | } |
| 55 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 56 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 57 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 58 | IPv6Address ipv6 = IPv6Address.of(ip); |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 59 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 60 | if (maskAddress != null) { |
| 61 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 62 | return IPv6AddressWithMask.of(ipv6, maskAddress); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 63 | } else if (maskBits == 128) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 64 | // No mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 65 | return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 66 | } else if (maskBits == 0) { |
| 67 | // Entirely masked out |
| 68 | return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK); |
| 69 | }else { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | // With mask |
| 71 | BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits); |
| 72 | byte[] maskBytesTemp = mask.toByteArray(); |
| 73 | byte[] maskBytes; |
| 74 | if (maskBytesTemp.length < 16) { |
| 75 | maskBytes = new byte[16]; |
| 76 | System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length); |
| 77 | Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF)); |
| 78 | } else if (maskBytesTemp.length > 16) { |
| 79 | maskBytes = new byte[16]; |
| 80 | System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length); |
| 81 | } else { |
| 82 | maskBytes = maskBytesTemp; |
| 83 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 84 | return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public boolean contains(IPAddress<?> ip) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 90 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 91 | |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 92 | if(ip.getIpVersion() == IPVersion.IPv6) { |
| 93 | IPv6Address ipv6 = (IPv6Address) ip; |
| 94 | return this.matches(ipv6); |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 99 | } |