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