blob: 8376e5e121ebd3cb7f545ac5336f72fca6f69ae2 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import java.math.BigInteger;
4import java.util.Arrays;
5
Yotam Harchol4d634682013-09-26 13:21:06 -07006public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07007 public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07008
Yotam Harchol9a617aa2013-09-16 14:10:17 -07009 private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 super(value, mask);
11 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070012
Yotam Harchol4d634682013-09-26 13:21:06 -070013 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070014 public IPVersion getIpVersion() {
15 return IPVersion.IPv6;
Yotam Harchol4d634682013-09-26 13:21:06 -070016 }
17
Yotam Harchol9a617aa2013-09-16 14:10:17 -070018 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
19 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070020 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070021
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070022
Yotam Harchol9a617aa2013-09-16 14:10:17 -070023 public static IPv6AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 int slashPos;
25 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080026 int maskBits = 128;
Yotam Harchola289d552013-09-16 10:10:40 -070027 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070028
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 Harchola289d552013-09-16 10:10:40 -070038 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070039 } 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 Wundsamb75c4ad2013-09-23 14:45:35 -070050
Yotam Harcholf3f11152013-09-05 16:47:16 -070051 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070052 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070053
Yotam Harcholf3f11152013-09-05 16:47:16 -070054 if (maskAddress != null) {
55 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070056 return IPv6AddressWithMask.of(ipv6, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080057 } else if (maskBits == 128) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070058 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070059 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080060 } else if (maskBits == 0) {
61 // Entirely masked out
62 return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK);
63 }else {
Yotam Harcholf3f11152013-09-05 16:47:16 -070064 // 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 Harchol9a617aa2013-09-16 14:10:17 -070078 return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070079 }
80 }
81
82
83}