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 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 18 | import java.net.InetAddress; |
| 19 | import java.net.Inet4Address; |
| 20 | import java.net.Inet6Address; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 22 | import java.util.Arrays; |
| 23 | |
| 24 | import com.google.common.net.InetAddresses; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 27 | * A class representing an IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 28 | * This class is immutable. |
| 29 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 30 | public 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Charles Chan | 1cf4ca3 | 2017-05-09 10:33:04 -0700 | [diff] [blame] | 36 | * All-zero unspecified IPv4 address. |
| 37 | */ |
| 38 | public static final Ip4Address ZERO = Ip4Address.valueOf("0.0.0.0"); |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 41 | * Constructor for given IP address version and address octets. |
| 42 | * |
| 43 | * @param value the IP address value stored in network byte order |
| 44 | * (i.e., the most significant byte first) |
| 45 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 46 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 47 | private Ip4Address(byte[] value) { |
| 48 | super(VERSION, value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 52 | * Returns the integer value of this IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 53 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 54 | * @return the IPv4 address's value as an integer |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 55 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 56 | public int toInt() { |
| 57 | ByteBuffer bb = ByteBuffer.wrap(super.toOctets()); |
| 58 | return bb.getInt(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 62 | * Converts an integer into an IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 63 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 64 | * @param value an integer representing an IPv4 address value |
| 65 | * @return an IPv4 address |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 66 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 67 | public static Ip4Address valueOf(int value) { |
| 68 | byte[] bytes = |
| 69 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); |
| 70 | return new Ip4Address(bytes); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 74 | * Converts a byte array into an IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 75 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 76 | * @param value the IPv4 address value stored in network byte order |
| 77 | * (i.e., the most significant byte first) |
| 78 | * @return an IPv4 address |
| 79 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 80 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 81 | public static Ip4Address valueOf(byte[] value) { |
| 82 | return new Ip4Address(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 86 | * Converts a byte array and a given offset from the beginning of the |
| 87 | * array into an IPv4 address. |
| 88 | * <p> |
| 89 | * The IP address is stored in network byte order (i.e., the most |
| 90 | * significant byte first). |
| 91 | * </p> |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 92 | * @param value the value to use |
| 93 | * @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] | 94 | * @return an IPv4 address |
| 95 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 96 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 97 | public static Ip4Address valueOf(byte[] value, int offset) { |
| 98 | IpAddress.checkArguments(VERSION, value, offset); |
| 99 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 100 | return Ip4Address.valueOf(bc); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 104 | * Converts an InetAddress into an IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 105 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 106 | * @param inetAddress the InetAddress value to use. It must contain an IPv4 |
| 107 | * address |
| 108 | * @return an IPv4 address |
| 109 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 110 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 111 | public static Ip4Address valueOf(InetAddress inetAddress) { |
| 112 | byte[] bytes = inetAddress.getAddress(); |
| 113 | if (inetAddress instanceof Inet4Address) { |
| 114 | return new Ip4Address(bytes); |
| 115 | } |
| 116 | if ((inetAddress instanceof Inet6Address) || |
| 117 | (bytes.length == INET6_BYTE_LENGTH)) { |
| 118 | final String msg = "Invalid IPv4 version address string: " + |
| 119 | inetAddress.toString(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 120 | throw new IllegalArgumentException(msg); |
| 121 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 122 | // Use the number of bytes as a hint |
| 123 | if (bytes.length == INET_BYTE_LENGTH) { |
| 124 | return new Ip4Address(bytes); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 125 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 126 | final String msg = "Unrecognized IP version address string: " + |
| 127 | inetAddress.toString(); |
| 128 | throw new IllegalArgumentException(msg); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 132 | * Converts an IPv4 string literal (e.g., "10.2.3.4") into an IP address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 133 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 134 | * @param value an IPv4 address value in string form |
| 135 | * @return an IPv4 address |
| 136 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 137 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 138 | public static Ip4Address valueOf(String value) { |
| 139 | InetAddress inetAddress = null; |
| 140 | try { |
| 141 | inetAddress = InetAddresses.forString(value); |
| 142 | } catch (IllegalArgumentException e) { |
| 143 | final String msg = "Invalid IP address string: " + value; |
| 144 | throw new IllegalArgumentException(msg); |
| 145 | } |
| 146 | return valueOf(inetAddress); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Creates an IPv4 network mask prefix. |
| 151 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 152 | * @param prefixLength the length of the mask prefix. Must be in the |
| 153 | * interval [0, 32] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 154 | * @return a new IPv4 address that contains a mask prefix of the |
| 155 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 156 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 157 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 158 | public static Ip4Address makeMaskPrefix(int prefixLength) { |
| 159 | byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); |
| 160 | return new Ip4Address(mask); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Creates an IPv4 address by masking it with a network mask of given |
| 165 | * mask length. |
| 166 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 167 | * @param address the address to mask |
| 168 | * @param prefixLength the length of the mask prefix. Must be in the |
| 169 | * interval [0, 32] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 170 | * @return a new IPv4 address that is masked with a mask prefix of the |
| 171 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 172 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 173 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 174 | public static Ip4Address makeMaskedAddress(final Ip4Address address, |
| 175 | int prefixLength) { |
| 176 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 177 | return Ip4Address.valueOf(net); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 178 | } |
| 179 | } |