blob: 36b6f81b0ce849a2faa56cce952b2cbe92e09a56 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080018import java.net.InetAddress;
19import java.net.Inet4Address;
20import java.net.Inet6Address;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070021import java.nio.ByteBuffer;
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080022import java.util.Arrays;
23
24import com.google.common.net.InetAddresses;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070025
26/**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080027 * A class representing an IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070028 * This class is immutable.
29 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080030public final class Ip4Address extends IpAddress {
31 public static final IpAddress.Version VERSION = IpAddress.Version.INET;
32 public static final int BYTE_LENGTH = IpAddress.INET_BYTE_LENGTH;
33 public static final int BIT_LENGTH = IpAddress.INET_BIT_LENGTH;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070034
35 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080036 * Constructor for given IP address version and address octets.
37 *
38 * @param value the IP address value stored in network byte order
39 * (i.e., the most significant byte first)
40 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070041 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080042 private Ip4Address(byte[] value) {
43 super(VERSION, value);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070044 }
45
46 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080047 * Returns the integer value of this IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070048 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080049 * @return the IPv4 address's value as an integer
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070050 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080051 public int toInt() {
52 ByteBuffer bb = ByteBuffer.wrap(super.toOctets());
53 return bb.getInt();
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070054 }
55
56 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080057 * Converts an integer into an IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070058 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080059 * @param value an integer representing an IPv4 address value
60 * @return an IPv4 address
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070061 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080062 public static Ip4Address valueOf(int value) {
63 byte[] bytes =
64 ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array();
65 return new Ip4Address(bytes);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070066 }
67
68 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080069 * Converts a byte array into an IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070070 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080071 * @param value the IPv4 address value stored in network byte order
72 * (i.e., the most significant byte first)
73 * @return an IPv4 address
74 * @throws IllegalArgumentException if the argument is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070075 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080076 public static Ip4Address valueOf(byte[] value) {
77 return new Ip4Address(value);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070078 }
79
80 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080081 * Converts a byte array and a given offset from the beginning of the
82 * array into an IPv4 address.
83 * <p>
84 * The IP address is stored in network byte order (i.e., the most
85 * significant byte first).
86 * </p>
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070087 * @param value the value to use
88 * @param offset the offset in bytes from the beginning of the byte array
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080089 * @return an IPv4 address
90 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070091 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080092 public static Ip4Address valueOf(byte[] value, int offset) {
93 IpAddress.checkArguments(VERSION, value, offset);
94 byte[] bc = Arrays.copyOfRange(value, offset, value.length);
95 return Ip4Address.valueOf(bc);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070096 }
97
98 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080099 * Converts an InetAddress into an IPv4 address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700100 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800101 * @param inetAddress the InetAddress value to use. It must contain an IPv4
102 * address
103 * @return an IPv4 address
104 * @throws IllegalArgumentException if the argument is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700105 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800106 public static Ip4Address valueOf(InetAddress inetAddress) {
107 byte[] bytes = inetAddress.getAddress();
108 if (inetAddress instanceof Inet4Address) {
109 return new Ip4Address(bytes);
110 }
111 if ((inetAddress instanceof Inet6Address) ||
112 (bytes.length == INET6_BYTE_LENGTH)) {
113 final String msg = "Invalid IPv4 version address string: " +
114 inetAddress.toString();
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700115 throw new IllegalArgumentException(msg);
116 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800117 // Use the number of bytes as a hint
118 if (bytes.length == INET_BYTE_LENGTH) {
119 return new Ip4Address(bytes);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700120 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800121 final String msg = "Unrecognized IP version address string: " +
122 inetAddress.toString();
123 throw new IllegalArgumentException(msg);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700124 }
125
126 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800127 * Converts an IPv4 string literal (e.g., "10.2.3.4") into an IP address.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700128 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800129 * @param value an IPv4 address value in string form
130 * @return an IPv4 address
131 * @throws IllegalArgumentException if the argument is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700132 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800133 public static Ip4Address valueOf(String value) {
134 InetAddress inetAddress = null;
135 try {
136 inetAddress = InetAddresses.forString(value);
137 } catch (IllegalArgumentException e) {
138 final String msg = "Invalid IP address string: " + value;
139 throw new IllegalArgumentException(msg);
140 }
141 return valueOf(inetAddress);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700142 }
143
144 /**
145 * Creates an IPv4 network mask prefix.
146 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800147 * @param prefixLength the length of the mask prefix. Must be in the
148 * interval [0, 32]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700149 * @return a new IPv4 address that contains a mask prefix of the
150 * specified length
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800151 * @throws IllegalArgumentException if the argument is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700152 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800153 public static Ip4Address makeMaskPrefix(int prefixLength) {
154 byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength);
155 return new Ip4Address(mask);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700156 }
157
158 /**
159 * Creates an IPv4 address by masking it with a network mask of given
160 * mask length.
161 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800162 * @param address the address to mask
163 * @param prefixLength the length of the mask prefix. Must be in the
164 * interval [0, 32]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700165 * @return a new IPv4 address that is masked with a mask prefix of the
166 * specified length
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800167 * @throws IllegalArgumentException if the prefix length is invalid
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700168 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800169 public static Ip4Address makeMaskedAddress(final Ip4Address address,
170 int prefixLength) {
171 byte[] net = makeMaskedAddressArray(address, prefixLength);
172 return Ip4Address.valueOf(net);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700173 }
174}