blob: d353422b160fd948fd3f420a7e0c662da079ebf9 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070016package org.onlab.packet;
17
18import java.net.InetAddress;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080019import java.net.Inet4Address;
20import java.net.Inet6Address;
21import java.util.Arrays;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070022
23import com.google.common.net.InetAddresses;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070024
25/**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080026 * A class representing an IPv6 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070027 * This class is immutable.
28 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080029public 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 Radoslavov9de27722014-10-23 20:31:15 -070033
34 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080035 * 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 Radoslavov9de27722014-10-23 20:31:15 -070040 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080041 private Ip6Address(byte[] value) {
42 super(VERSION, value);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070043 }
44
45 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080046 * Converts a byte array into an IPv6 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070047 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080048 * @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 Radoslavov9de27722014-10-23 20:31:15 -070052 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080053 public static Ip6Address valueOf(byte[] value) {
54 return new Ip6Address(value);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070055 }
56
57 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080058 * 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 Radoslavov9de27722014-10-23 20:31:15 -070064 * @param value the value to use
65 * @param offset the offset in bytes from the beginning of the byte array
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080066 * @return an IPv6 address
67 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070068 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080069 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 Radoslavov9de27722014-10-23 20:31:15 -070073 }
74
75 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080076 * Converts an InetAddress into an IPv6 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070077 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080078 * @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 Radoslavov9de27722014-10-23 20:31:15 -070082 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080083 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 Radoslavov9de27722014-10-23 20:31:15 -070092 throw new IllegalArgumentException(msg);
93 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080094 // 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 Radoslavov9de27722014-10-23 20:31:15 -0700113 try {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800114 inetAddress = InetAddresses.forString(value);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700115 } catch (IllegalArgumentException e) {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800116 final String msg = "Invalid IP address string: " + value;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700117 throw new IllegalArgumentException(msg);
118 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800119 return valueOf(inetAddress);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700120 }
121
122 /**
123 * Creates an IPv6 network mask prefix.
124 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800125 * @param prefixLength the length of the mask prefix. Must be in the
126 * interval [0, 128]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700127 * @return a new IPv6 address that contains a mask prefix of the
128 * specified length
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800129 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700130 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800131 public static Ip6Address makeMaskPrefix(int prefixLength) {
132 byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength);
133 return new Ip6Address(mask);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700134 }
135
136 /**
137 * Creates an IPv6 address by masking it with a network mask of given
138 * mask length.
139 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800140 * @param address the address to mask
141 * @param prefixLength the length of the mask prefix. Must be in the
142 * interval [0, 128]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700143 * @return a new IPv6 address that is masked with a mask prefix of the
144 * specified length
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800145 * @throws IllegalArgumentException if the prefix length is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700146 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800147 public static Ip6Address makeMaskedAddress(final Ip6Address address,
148 int prefixLength) {
149 byte[] net = makeMaskedAddressArray(address, prefixLength);
150 return Ip6Address.valueOf(net);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700151 }
152}