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