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) { |
| 19 | return new IPv6AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 20 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 21 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 22 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 23 | public static IPv6AddressWithMask of(final String string) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 24 | int slashPos; |
| 25 | String ip = string; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 26 | int maskBits = 128; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 27 | IPv6Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | |
| 29 | // Read mask suffix |
| 30 | if ((slashPos = string.indexOf('/')) != -1) { |
| 31 | ip = string.substring(0, slashPos); |
| 32 | try { |
| 33 | String suffix = string.substring(slashPos + 1); |
| 34 | if (suffix.length() == 0) |
| 35 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 36 | if (suffix.indexOf(':') != -1) { |
| 37 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 38 | maskAddress = IPv6Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 39 | } else { |
| 40 | // CIDR Suffix |
| 41 | maskBits = Integer.parseInt(suffix); |
| 42 | } |
| 43 | } catch (NumberFormatException e) { |
| 44 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 45 | } |
| 46 | if (maskBits < 0 || maskBits > 128) { |
| 47 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 48 | } |
| 49 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 50 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 51 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 52 | IPv6Address ipv6 = IPv6Address.of(ip); |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 53 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 54 | if (maskAddress != null) { |
| 55 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 56 | return IPv6AddressWithMask.of(ipv6, maskAddress); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 57 | } else if (maskBits == 128) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 58 | // No mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 59 | return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 60 | } else if (maskBits == 0) { |
| 61 | // Entirely masked out |
| 62 | return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK); |
| 63 | }else { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 64 | // With mask |
| 65 | BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits); |
| 66 | byte[] maskBytesTemp = mask.toByteArray(); |
| 67 | byte[] maskBytes; |
| 68 | if (maskBytesTemp.length < 16) { |
| 69 | maskBytes = new byte[16]; |
| 70 | System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length); |
| 71 | Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF)); |
| 72 | } else if (maskBytesTemp.length > 16) { |
| 73 | maskBytes = new byte[16]; |
| 74 | System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length); |
| 75 | } else { |
| 76 | maskBytes = maskBytesTemp; |
| 77 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 78 | return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | } |