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 | |
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 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 36 | * 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 41 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 42 | private Ip4Address(byte[] value) { |
| 43 | super(VERSION, value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 47 | * Returns the integer value of this IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 48 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 49 | * @return the IPv4 address's value as an integer |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 50 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 51 | public int toInt() { |
| 52 | ByteBuffer bb = ByteBuffer.wrap(super.toOctets()); |
| 53 | return bb.getInt(); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 57 | * Converts an integer into an IPv4 address. |
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 | * @param value an integer representing an IPv4 address value |
| 60 | * @return an IPv4 address |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 61 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 62 | public static Ip4Address valueOf(int value) { |
| 63 | byte[] bytes = |
| 64 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); |
| 65 | return new Ip4Address(bytes); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 69 | * Converts a byte array into an IPv4 address. |
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 | * @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 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 | public static Ip4Address valueOf(byte[] value) { |
| 77 | return new Ip4Address(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 81 | * 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 87 | * @param value the value to use |
| 88 | * @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] | 89 | * @return an IPv4 address |
| 90 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 91 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 92 | 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 99 | * Converts an InetAddress into an IPv4 address. |
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 | * @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 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 | 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 115 | throw new IllegalArgumentException(msg); |
| 116 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 117 | // Use the number of bytes as a hint |
| 118 | if (bytes.length == INET_BYTE_LENGTH) { |
| 119 | return new Ip4Address(bytes); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 120 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 121 | final String msg = "Unrecognized IP version address string: " + |
| 122 | inetAddress.toString(); |
| 123 | throw new IllegalArgumentException(msg); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 127 | * 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] | 128 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 129 | * @param value an IPv4 address value in string form |
| 130 | * @return an IPv4 address |
| 131 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 132 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 133 | 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 Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Creates an IPv4 network mask prefix. |
| 146 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 147 | * @param prefixLength the length of the mask prefix. Must be in the |
| 148 | * interval [0, 32] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 149 | * @return a new IPv4 address that contains a mask prefix of the |
| 150 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 151 | * @throws IllegalArgumentException if the argument is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 152 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 153 | public static Ip4Address makeMaskPrefix(int prefixLength) { |
| 154 | byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); |
| 155 | return new Ip4Address(mask); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Creates an IPv4 address by masking it with a network mask of given |
| 160 | * mask length. |
| 161 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 162 | * @param address the address to mask |
| 163 | * @param prefixLength the length of the mask prefix. Must be in the |
| 164 | * interval [0, 32] |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 165 | * @return a new IPv4 address that is masked with a mask prefix of the |
| 166 | * specified length |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 167 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 168 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 169 | public static Ip4Address makeMaskedAddress(final Ip4Address address, |
| 170 | int prefixLength) { |
| 171 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 172 | return Ip4Address.valueOf(net); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 173 | } |
| 174 | } |