Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
| 18 | import java.net.InetAddress; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 19 | import java.net.Inet4Address; |
| 20 | import java.net.Inet6Address; |
| 21 | import java.util.Arrays; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 22 | |
| 23 | import com.google.common.net.InetAddresses; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 26 | * A class representing an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 27 | * This class is immutable. |
| 28 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 29 | public final class Ip6Address extends IpAddress { |
| 30 | public static final IpAddress.Version VERSION = IpAddress.Version.INET6; |
| 31 | public static final int BYTE_LENGTH = IpAddress.INET6_BYTE_LENGTH; |
| 32 | public static final int BIT_LENGTH = IpAddress.INET6_BIT_LENGTH; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Charles Chan | 1cf4ca3 | 2017-05-09 10:33:04 -0700 | [diff] [blame] | 35 | * All-zero unspecified IPv6 address. |
| 36 | */ |
| 37 | public static final Ip6Address ZERO = Ip6Address.valueOf("::"); |
| 38 | |
| 39 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 40 | * Constructor for given IP address version and address octets. |
| 41 | * |
| 42 | * @param value the IP address value stored in network byte order |
| 43 | * (i.e., the most significant byte first) |
| 44 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 45 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 46 | private Ip6Address(byte[] value) { |
| 47 | super(VERSION, value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 51 | * Converts a byte array into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 52 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 53 | * @param value the IPv6 address value stored in network byte order |
| 54 | * (i.e., the most significant byte first) |
| 55 | * @return an IPv6 address |
| 56 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 57 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 58 | public static Ip6Address valueOf(byte[] value) { |
| 59 | return new Ip6Address(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 63 | * Converts a byte array and a given offset from the beginning of the |
| 64 | * array into an IPv6 address. |
| 65 | * <p> |
| 66 | * The IP address is stored in network byte order (i.e., the most |
| 67 | * significant byte first). |
| 68 | * </p> |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 69 | * @param value the value to use |
| 70 | * @param offset the offset in bytes from the beginning of the byte array |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 71 | * @return an IPv6 address |
| 72 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 73 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 74 | public static Ip6Address valueOf(byte[] value, int offset) { |
| 75 | IpAddress.checkArguments(VERSION, value, offset); |
| 76 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 77 | return Ip6Address.valueOf(bc); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 81 | * Converts an InetAddress into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 82 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 83 | * @param inetAddress the InetAddress value to use. It must contain an IPv6 |
| 84 | * address |
| 85 | * @return an IPv6 address |
| 86 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 87 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 88 | public static Ip6Address valueOf(InetAddress inetAddress) { |
| 89 | byte[] bytes = inetAddress.getAddress(); |
| 90 | if (inetAddress instanceof Inet6Address) { |
| 91 | return new Ip6Address(bytes); |
| 92 | } |
| 93 | if ((inetAddress instanceof Inet4Address) || |
| 94 | (bytes.length == INET_BYTE_LENGTH)) { |
| 95 | final String msg = "Invalid IPv6 version address string: " + |
| 96 | inetAddress.toString(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 97 | throw new IllegalArgumentException(msg); |
| 98 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 99 | // Use the number of bytes as a hint |
| 100 | if (bytes.length == INET6_BYTE_LENGTH) { |
| 101 | return new Ip6Address(bytes); |
| 102 | } |
| 103 | final String msg = "Unrecognized IP version address string: " + |
| 104 | inetAddress.toString(); |
| 105 | throw new IllegalArgumentException(msg); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP |
| 110 | * address. |
| 111 | * |
| 112 | * @param value an IPv6 address value in string form |
| 113 | * @return an IPv6 address |
| 114 | * @throws IllegalArgumentException if the argument is invalid |
| 115 | */ |
| 116 | public static Ip6Address valueOf(String value) { |
| 117 | InetAddress inetAddress = null; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 118 | try { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 119 | inetAddress = InetAddresses.forString(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 120 | } catch (IllegalArgumentException e) { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 121 | final String msg = "Invalid IP address string: " + value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 122 | throw new IllegalArgumentException(msg); |
| 123 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 124 | return valueOf(inetAddress); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates an IPv6 network mask prefix. |
| 129 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 130 | * @param prefixLength the length of the mask prefix. Must be in the |
| 131 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 132 | * @return a new IPv6 address that contains a mask prefix of the |
| 133 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 134 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 135 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 136 | public static Ip6Address makeMaskPrefix(int prefixLength) { |
| 137 | byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); |
| 138 | return new Ip6Address(mask); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Creates an IPv6 address by masking it with a network mask of given |
| 143 | * mask length. |
| 144 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 145 | * @param address the address to mask |
| 146 | * @param prefixLength the length of the mask prefix. Must be in the |
| 147 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 148 | * @return a new IPv6 address that is masked with a mask prefix of the |
| 149 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 150 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 151 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 152 | public static Ip6Address makeMaskedAddress(final Ip6Address address, |
| 153 | int prefixLength) { |
| 154 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 155 | return Ip6Address.valueOf(net); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 156 | } |
| 157 | } |