Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 35 | * Constructor for given IP address version and address octets. |
| 36 | * |
| 37 | * @param value the IP address value stored in network byte order |
| 38 | * (i.e., the most significant byte first) |
| 39 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 40 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 41 | private Ip6Address(byte[] value) { |
| 42 | super(VERSION, value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 46 | * Converts a byte array into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 47 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 48 | * @param value the IPv6 address value stored in network byte order |
| 49 | * (i.e., the most significant byte first) |
| 50 | * @return an IPv6 address |
| 51 | * @throws IllegalArgumentException if the argument is invalid |
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 | public static Ip6Address valueOf(byte[] value) { |
| 54 | return new Ip6Address(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 58 | * Converts a byte array and a given offset from the beginning of the |
| 59 | * array into an IPv6 address. |
| 60 | * <p> |
| 61 | * The IP address is stored in network byte order (i.e., the most |
| 62 | * significant byte first). |
| 63 | * </p> |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 64 | * @param value the value to use |
| 65 | * @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] | 66 | * @return an IPv6 address |
| 67 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 68 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 69 | public static Ip6Address valueOf(byte[] value, int offset) { |
| 70 | IpAddress.checkArguments(VERSION, value, offset); |
| 71 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 72 | return Ip6Address.valueOf(bc); |
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 an InetAddress into an IPv6 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 77 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 78 | * @param inetAddress the InetAddress value to use. It must contain an IPv6 |
| 79 | * address |
| 80 | * @return an IPv6 address |
| 81 | * @throws IllegalArgumentException if the argument is invalid |
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 | public static Ip6Address valueOf(InetAddress inetAddress) { |
| 84 | byte[] bytes = inetAddress.getAddress(); |
| 85 | if (inetAddress instanceof Inet6Address) { |
| 86 | return new Ip6Address(bytes); |
| 87 | } |
| 88 | if ((inetAddress instanceof Inet4Address) || |
| 89 | (bytes.length == INET_BYTE_LENGTH)) { |
| 90 | final String msg = "Invalid IPv6 version address string: " + |
| 91 | inetAddress.toString(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 92 | throw new IllegalArgumentException(msg); |
| 93 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 94 | // Use the number of bytes as a hint |
| 95 | if (bytes.length == INET6_BYTE_LENGTH) { |
| 96 | return new Ip6Address(bytes); |
| 97 | } |
| 98 | final String msg = "Unrecognized IP version address string: " + |
| 99 | inetAddress.toString(); |
| 100 | throw new IllegalArgumentException(msg); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP |
| 105 | * address. |
| 106 | * |
| 107 | * @param value an IPv6 address value in string form |
| 108 | * @return an IPv6 address |
| 109 | * @throws IllegalArgumentException if the argument is invalid |
| 110 | */ |
| 111 | public static Ip6Address valueOf(String value) { |
| 112 | InetAddress inetAddress = null; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 113 | try { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 114 | inetAddress = InetAddresses.forString(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 115 | } catch (IllegalArgumentException e) { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 116 | final String msg = "Invalid IP address string: " + value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 117 | throw new IllegalArgumentException(msg); |
| 118 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 119 | return valueOf(inetAddress); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates an IPv6 network mask prefix. |
| 124 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 125 | * @param prefixLength the length of the mask prefix. Must be in the |
| 126 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 127 | * @return a new IPv6 address that contains a mask prefix of the |
| 128 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 129 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 130 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 131 | public static Ip6Address makeMaskPrefix(int prefixLength) { |
| 132 | byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); |
| 133 | return new Ip6Address(mask); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates an IPv6 address by masking it with a network mask of given |
| 138 | * mask length. |
| 139 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 140 | * @param address the address to mask |
| 141 | * @param prefixLength the length of the mask prefix. Must be in the |
| 142 | * interval [0, 128] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 143 | * @return a new IPv6 address that is masked with a mask prefix of the |
| 144 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 145 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 146 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 147 | public static Ip6Address makeMaskedAddress(final Ip6Address address, |
| 148 | int prefixLength) { |
| 149 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 150 | return Ip6Address.valueOf(net); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 151 | } |
| 152 | } |