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