blob: 9dd97c3162e60a1f66c5621980637880278fd756 [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 */
Jonathan Hartdec62d42014-09-22 15:59:04 -070016package org.onlab.packet;
17
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070018import java.nio.ByteBuffer;
Jonathan Hartdec62d42014-09-22 15:59:04 -070019import java.util.Arrays;
20
21/**
22 * A class representing an IPv4 address.
Jonathan Hartdec62d42014-09-22 15:59:04 -070023 */
Jonathan Hartab63aac2014-10-16 08:52:55 -070024public final class IpAddress implements Comparable<IpAddress> {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070025 // IP Versions
Jonathan Hartdec62d42014-09-22 15:59:04 -070026 public enum Version { INET, INET6 };
27
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070028 // lengths of address, in bytes
29 public static final int INET_BYTE_LENGTH = 4;
30 public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE;
31 public static final int INET6_BYTE_LENGTH = 16;
32 public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE;
Jonathan Hartdec62d42014-09-22 15:59:04 -070033
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070034 private final Version version;
35 private final byte[] octets;
Jonathan Hartdec62d42014-09-22 15:59:04 -070036
37 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070038 * Constructor for given IP address version and address octets.
39 *
40 * @param ver the IP address version
41 * @param octets the IP address octets
Jonathan Hartdec62d42014-09-22 15:59:04 -070042 */
Jonathan Hartdec62d42014-09-22 15:59:04 -070043 private IpAddress(Version ver, byte[] octets) {
44 this.version = ver;
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070045 this.octets = Arrays.copyOf(octets, INET_BYTE_LENGTH);
Jonathan Hartdec62d42014-09-22 15:59:04 -070046 }
47
48 /**
49 * Converts a byte array into an IP address.
50 *
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070051 * @param address the IP address value stored in network byte order
52 * (i.e., the most significant byte first)
Jonathan Hartdec62d42014-09-22 15:59:04 -070053 * @return an IP address
54 */
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070055 public static IpAddress valueOf(byte[] address) {
Jonathan Hartdec62d42014-09-22 15:59:04 -070056 return new IpAddress(Version.INET, address);
57 }
58
59 /**
Jonathan Hartdec62d42014-09-22 15:59:04 -070060 * Converts an integer into an IPv4 address.
61 *
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070062 * @param address an integer representing an IPv4 value
Jonathan Hartdec62d42014-09-22 15:59:04 -070063 * @return an IP address
64 */
65 public static IpAddress valueOf(int address) {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070066 byte[] bytes =
67 ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(address).array();
68 return new IpAddress(Version.INET, bytes);
Jonathan Hartdec62d42014-09-22 15:59:04 -070069 }
70
71 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070072 * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address.
Jonathan Hartdec62d42014-09-22 15:59:04 -070073 *
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070074 * @param address a IP address in string form, e.g. "10.0.0.1".
Jonathan Hartdec62d42014-09-22 15:59:04 -070075 * @return an IP address
76 */
77 public static IpAddress valueOf(String address) {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070078 final String[] net = address.split("\\.");
79 if (net.length != INET_BYTE_LENGTH) {
Jonathan Hartdec62d42014-09-22 15:59:04 -070080 throw new IllegalArgumentException("Malformed IP address string; "
81 + "Address must have four decimal values separated by dots (.)");
82 }
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070083 final byte[] bytes = new byte[INET_BYTE_LENGTH];
84 for (int i = 0; i < INET_BYTE_LENGTH; i++) {
Jonathan Hartdec62d42014-09-22 15:59:04 -070085 bytes[i] = (byte) Short.parseShort(net[i], 10);
86 }
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070087 return new IpAddress(Version.INET, bytes);
Jonathan Hartdec62d42014-09-22 15:59:04 -070088 }
89
90 /**
91 * Returns the IP version of this address.
92 *
93 * @return the version
94 */
95 public Version version() {
96 return this.version;
97 }
98
99 /**
100 * Returns the IP address as a byte array.
101 *
102 * @return a byte array
103 */
104 public byte[] toOctets() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700105 return Arrays.copyOf(this.octets, INET_BYTE_LENGTH);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700106 }
107
108 /**
109 * Returns the integral value of this IP address.
110 *
111 * @return the IP address's value as an integer
112 */
113 public int toInt() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700114 ByteBuffer bb = ByteBuffer.wrap(octets);
115 return bb.getInt();
Jonathan Hartdec62d42014-09-22 15:59:04 -0700116 }
117
118 @Override
Jonathan Hartab63aac2014-10-16 08:52:55 -0700119 public int compareTo(IpAddress o) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700120 Long lv = ((long) this.toInt()) & 0xffffffffL;
121 Long rv = ((long) o.toInt()) & 0xffffffffL;
Jonathan Hartab63aac2014-10-16 08:52:55 -0700122 return lv.compareTo(rv);
123 }
124
125 @Override
Jonathan Hartdec62d42014-09-22 15:59:04 -0700126 public int hashCode() {
127 final int prime = 31;
128 int result = 1;
Jonathan Hartdec62d42014-09-22 15:59:04 -0700129 result = prime * result + Arrays.hashCode(octets);
130 result = prime * result + ((version == null) ? 0 : version.hashCode());
131 return result;
132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
138 }
139 if (obj == null) {
140 return false;
141 }
142 if (getClass() != obj.getClass()) {
143 return false;
144 }
145 IpAddress other = (IpAddress) obj;
Jonathan Hartdec62d42014-09-22 15:59:04 -0700146 if (!Arrays.equals(octets, other.octets)) {
147 return false;
148 }
149 if (version != other.version) {
150 return false;
151 }
152 return true;
153 }
154
155 @Override
156 /*
157 * (non-Javadoc)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700158 * format is "x.x.x.x" for IPv4 addresses.
Jonathan Hartdec62d42014-09-22 15:59:04 -0700159 *
160 * @see java.lang.Object#toString()
161 */
162 public String toString() {
163 final StringBuilder builder = new StringBuilder();
164 for (final byte b : this.octets) {
165 if (builder.length() > 0) {
166 builder.append(".");
167 }
168 builder.append(String.format("%d", b & 0xff));
169 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700170 return builder.toString();
171 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700172}