Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import java.math.BigInteger; |
| 4 | import java.util.Arrays; |
| 5 | |
| 6 | public class IPv6WithMask extends Masked<IPv6> { |
| 7 | |
| 8 | private IPv6WithMask(IPv6 value, IPv6 mask) { |
| 9 | super(value, mask); |
| 10 | } |
| 11 | |
| 12 | public static IPv6WithMask of(IPv6 value, IPv6 mask) { |
| 13 | return new IPv6WithMask(value, mask); |
| 14 | } |
| 15 | |
| 16 | @Override |
| 17 | public String toString() { |
| 18 | StringBuilder res = new StringBuilder(); |
| 19 | res.append(((IPv6)value).toString()); |
| 20 | res.append('/'); |
| 21 | |
| 22 | BigInteger maskint = new BigInteger(((IPv6)mask).getBytes()); |
| 23 | if (maskint.not().add(BigInteger.ONE).bitCount() == 1) { |
| 24 | // CIDR notation |
| 25 | res.append(maskint.bitCount()); |
| 26 | } else { |
| 27 | // Full address mask |
| 28 | res.append(((IPv6)mask).toString()); |
| 29 | } |
| 30 | |
| 31 | return res.toString(); |
| 32 | } |
| 33 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 34 | public static OFValueType<?> ofPossiblyMasked(final String string) { |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 35 | int slashPos; |
| 36 | String ip = string; |
| 37 | int maskBits = 0; |
| 38 | IPv6 maskAddress = null; |
| 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 |
| 49 | maskAddress = IPv6.of(suffix); |
| 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 |
| 63 | IPv6 ipv6 = IPv6.of(ip); |
| 64 | |
| 65 | if (maskAddress != null) { |
| 66 | // Full address mask |
| 67 | return IPv6WithMask.of(ipv6, maskAddress); |
| 68 | } else if (maskBits == 0) { |
| 69 | // No mask |
| 70 | return ipv6; |
| 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 | } |
| 86 | return IPv6WithMask.of(ipv6, IPv6.of(maskBytes)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | } |