blob: f0158710dd3d4ab402ceb990d1cac47aa39c5690 [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
Sovietaced9dfc1ef2014-06-27 11:13:57 -07006import com.google.common.base.Preconditions;
7
Yotam Harchol4d634682013-09-26 13:21:06 -07008public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07009 public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -070010
Yotam Harchol9a617aa2013-09-16 14:10:17 -070011 private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 super(value, mask);
13 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070014
Yotam Harchol4d634682013-09-26 13:21:06 -070015 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070016 public IPVersion getIpVersion() {
17 return IPVersion.IPv6;
Yotam Harchol4d634682013-09-26 13:21:06 -070018 }
19
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070021 Preconditions.checkNotNull(value, "value must not be null");
22 Preconditions.checkNotNull(mask, "mask must not be null");
Yotam Harchol9a617aa2013-09-16 14:10:17 -070023 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070025
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070026
Yotam Harchol9a617aa2013-09-16 14:10:17 -070027 public static IPv6AddressWithMask of(final String string) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070028 Preconditions.checkNotNull(string, "string must not be null");
29
Yotam Harcholf3f11152013-09-05 16:47:16 -070030 int slashPos;
31 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080032 int maskBits = 128;
Yotam Harchola289d552013-09-16 10:10:40 -070033 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070034
35 // Read mask suffix
36 if ((slashPos = string.indexOf('/')) != -1) {
37 ip = string.substring(0, slashPos);
38 try {
39 String suffix = string.substring(slashPos + 1);
40 if (suffix.length() == 0)
41 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
42 if (suffix.indexOf(':') != -1) {
43 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070044 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070045 } else {
46 // CIDR Suffix
47 maskBits = Integer.parseInt(suffix);
48 }
49 } catch (NumberFormatException e) {
50 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
51 }
52 if (maskBits < 0 || maskBits > 128) {
53 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
54 }
55 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070056
Yotam Harcholf3f11152013-09-05 16:47:16 -070057 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070058 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070059
Yotam Harcholf3f11152013-09-05 16:47:16 -070060 if (maskAddress != null) {
61 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070062 return IPv6AddressWithMask.of(ipv6, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080063 } else if (maskBits == 128) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070064 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070065 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080066 } else if (maskBits == 0) {
67 // Entirely masked out
68 return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK);
69 }else {
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 // With mask
71 BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
72 byte[] maskBytesTemp = mask.toByteArray();
73 byte[] maskBytes;
74 if (maskBytesTemp.length < 16) {
75 maskBytes = new byte[16];
76 System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length);
77 Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
78 } else if (maskBytesTemp.length > 16) {
79 maskBytes = new byte[16];
80 System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
81 } else {
82 maskBytes = maskBytesTemp;
83 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070084 return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070085 }
86 }
87
Sovietaced07cc8eb2014-06-24 11:55:26 -070088 @Override
89 public boolean contains(IPAddress<?> ip) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070090 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harcholf3f11152013-09-05 16:47:16 -070091
Sovietaced77d99492014-06-24 12:58:47 -070092 if(ip.getIpVersion() == IPVersion.IPv6) {
93 IPv6Address ipv6 = (IPv6Address) ip;
94 return this.matches(ipv6);
95 }
96
97 return false;
98 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070099}