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