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 | /** |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 35 | * A link-scoped multicast address used by a DHCP client to communicate with |
| 36 | * neighboring DHCP relay agents and servers. (RFC 3315) |
| 37 | */ |
| 38 | public static final Ip6Address ALL_DHCP_RELAY_AGENTS_AND_SERVERS = |
| 39 | Ip6Address.valueOf("ff02::1:2"); |
| 40 | /** |
| 41 | * A site-scoped multicast address used by a DHCP relay agent to |
| 42 | * communicate with DHCP servers. (RFC 3315) |
| 43 | */ |
| 44 | public static final Ip6Address ALL_DHCP_SERVERS = |
| 45 | Ip6Address.valueOf("ff05::1:3"); |
| 46 | |
| 47 | /** |
Charles Chan | 1cf4ca3 | 2017-05-09 10:33:04 -0700 | [diff] [blame] | 48 | * All-zero unspecified IPv6 address. |
| 49 | */ |
| 50 | public static final Ip6Address ZERO = Ip6Address.valueOf("::"); |
| 51 | |
| 52 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 53 | * Constructor for given IP address version and address octets. |
| 54 | * |
| 55 | * @param value the IP address value stored in network byte order |
| 56 | * (i.e., the most significant byte first) |
| 57 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 58 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 59 | private Ip6Address(byte[] value) { |
| 60 | super(VERSION, value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 64 | * Converts a byte array into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 65 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 66 | * @param value the IPv6 address value stored in network byte order |
| 67 | * (i.e., the most significant byte first) |
| 68 | * @return an IPv6 address |
| 69 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 70 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 71 | public static Ip6Address valueOf(byte[] value) { |
| 72 | return new Ip6Address(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 76 | * Converts a byte array and a given offset from the beginning of the |
| 77 | * array into an IPv6 address. |
| 78 | * <p> |
| 79 | * The IP address is stored in network byte order (i.e., the most |
| 80 | * significant byte first). |
| 81 | * </p> |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 82 | * @param value the value to use |
| 83 | * @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] | 84 | * @return an IPv6 address |
| 85 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 86 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 87 | public static Ip6Address valueOf(byte[] value, int offset) { |
| 88 | IpAddress.checkArguments(VERSION, value, offset); |
| 89 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 90 | return Ip6Address.valueOf(bc); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 94 | * Converts an InetAddress into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 95 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 96 | * @param inetAddress the InetAddress value to use. It must contain an IPv6 |
| 97 | * address |
| 98 | * @return an IPv6 address |
| 99 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 100 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 101 | public static Ip6Address valueOf(InetAddress inetAddress) { |
| 102 | byte[] bytes = inetAddress.getAddress(); |
| 103 | if (inetAddress instanceof Inet6Address) { |
| 104 | return new Ip6Address(bytes); |
| 105 | } |
| 106 | if ((inetAddress instanceof Inet4Address) || |
| 107 | (bytes.length == INET_BYTE_LENGTH)) { |
| 108 | final String msg = "Invalid IPv6 version address string: " + |
| 109 | inetAddress.toString(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 110 | throw new IllegalArgumentException(msg); |
| 111 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 112 | // Use the number of bytes as a hint |
| 113 | if (bytes.length == INET6_BYTE_LENGTH) { |
| 114 | return new Ip6Address(bytes); |
| 115 | } |
| 116 | final String msg = "Unrecognized IP version address string: " + |
| 117 | inetAddress.toString(); |
| 118 | throw new IllegalArgumentException(msg); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP |
| 123 | * address. |
| 124 | * |
| 125 | * @param value an IPv6 address value in string form |
| 126 | * @return an IPv6 address |
| 127 | * @throws IllegalArgumentException if the argument is invalid |
| 128 | */ |
| 129 | public static Ip6Address valueOf(String value) { |
| 130 | InetAddress inetAddress = null; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 131 | try { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 132 | inetAddress = InetAddresses.forString(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 133 | } catch (IllegalArgumentException e) { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 134 | final String msg = "Invalid IP address string: " + value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 135 | throw new IllegalArgumentException(msg); |
| 136 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 137 | return valueOf(inetAddress); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Creates an IPv6 network mask prefix. |
| 142 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 143 | * @param prefixLength the length of the mask prefix. Must be in the |
| 144 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 145 | * @return a new IPv6 address that contains a mask prefix of the |
| 146 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 147 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 148 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 149 | public static Ip6Address makeMaskPrefix(int prefixLength) { |
| 150 | byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); |
| 151 | return new Ip6Address(mask); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Creates an IPv6 address by masking it with a network mask of given |
| 156 | * mask length. |
| 157 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 158 | * @param address the address to mask |
| 159 | * @param prefixLength the length of the mask prefix. Must be in the |
| 160 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 161 | * @return a new IPv6 address that is masked with a mask prefix of the |
| 162 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 163 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 164 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 165 | public static Ip6Address makeMaskedAddress(final Ip6Address address, |
| 166 | int prefixLength) { |
| 167 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 168 | return Ip6Address.valueOf(net); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 169 | } |
| 170 | } |