Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.Arrays; |
| 22 | |
| 23 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 25 | */ |
| 26 | public class ARP extends BasePacket { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 27 | public static final short HW_TYPE_ETHERNET = 0x1; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 29 | public static final short PROTO_TYPE_IP = 0x800; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 31 | public static final short OP_REQUEST = 0x1; |
| 32 | public static final short OP_REPLY = 0x2; |
| 33 | public static final short OP_RARP_REQUEST = 0x3; |
| 34 | public static final short OP_RARP_REPLY = 0x4; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 35 | |
| 36 | protected short hardwareType; |
| 37 | protected short protocolType; |
| 38 | protected byte hardwareAddressLength; |
| 39 | protected byte protocolAddressLength; |
| 40 | protected short opCode; |
| 41 | protected byte[] senderHardwareAddress; |
| 42 | protected byte[] senderProtocolAddress; |
| 43 | protected byte[] targetHardwareAddress; |
| 44 | protected byte[] targetProtocolAddress; |
| 45 | |
| 46 | /** |
| 47 | * @return the hardwareType |
| 48 | */ |
| 49 | public short getHardwareType() { |
| 50 | return hardwareType; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param hardwareType the hardwareType to set |
| 55 | */ |
| 56 | public ARP setHardwareType(short hardwareType) { |
| 57 | this.hardwareType = hardwareType; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return the protocolType |
| 63 | */ |
| 64 | public short getProtocolType() { |
| 65 | return protocolType; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param protocolType the protocolType to set |
| 70 | */ |
| 71 | public ARP setProtocolType(short protocolType) { |
| 72 | this.protocolType = protocolType; |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return the hardwareAddressLength |
| 78 | */ |
| 79 | public byte getHardwareAddressLength() { |
| 80 | return hardwareAddressLength; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param hardwareAddressLength the hardwareAddressLength to set |
| 85 | */ |
| 86 | public ARP setHardwareAddressLength(byte hardwareAddressLength) { |
| 87 | this.hardwareAddressLength = hardwareAddressLength; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return the protocolAddressLength |
| 93 | */ |
| 94 | public byte getProtocolAddressLength() { |
| 95 | return protocolAddressLength; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param protocolAddressLength the protocolAddressLength to set |
| 100 | */ |
| 101 | public ARP setProtocolAddressLength(byte protocolAddressLength) { |
| 102 | this.protocolAddressLength = protocolAddressLength; |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return the opCode |
| 108 | */ |
| 109 | public short getOpCode() { |
| 110 | return opCode; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param opCode the opCode to set |
| 115 | */ |
| 116 | public ARP setOpCode(short opCode) { |
| 117 | this.opCode = opCode; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return the senderHardwareAddress |
| 123 | */ |
| 124 | public byte[] getSenderHardwareAddress() { |
| 125 | return senderHardwareAddress; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param senderHardwareAddress the senderHardwareAddress to set |
| 130 | */ |
| 131 | public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) { |
| 132 | this.senderHardwareAddress = senderHardwareAddress; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return the senderProtocolAddress |
| 138 | */ |
| 139 | public byte[] getSenderProtocolAddress() { |
| 140 | return senderProtocolAddress; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param senderProtocolAddress the senderProtocolAddress to set |
| 145 | */ |
| 146 | public ARP setSenderProtocolAddress(byte[] senderProtocolAddress) { |
| 147 | this.senderProtocolAddress = senderProtocolAddress; |
| 148 | return this; |
| 149 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 151 | public ARP setSenderProtocolAddress(int address) { |
| 152 | this.senderProtocolAddress = ByteBuffer.allocate(4).putInt(address).array(); |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return the targetHardwareAddress |
| 158 | */ |
| 159 | public byte[] getTargetHardwareAddress() { |
| 160 | return targetHardwareAddress; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @param targetHardwareAddress the targetHardwareAddress to set |
| 165 | */ |
| 166 | public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) { |
| 167 | this.targetHardwareAddress = targetHardwareAddress; |
| 168 | return this; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return the targetProtocolAddress |
| 173 | */ |
| 174 | public byte[] getTargetProtocolAddress() { |
| 175 | return targetProtocolAddress; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @return True if gratuitous ARP (SPA = TPA), false otherwise |
| 180 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | public boolean isGratuitous() { |
| 182 | assert (senderProtocolAddress.length == targetProtocolAddress.length); |
| 183 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 184 | int indx = 0; |
| 185 | while (indx < senderProtocolAddress.length) { |
| 186 | if (senderProtocolAddress[indx] != targetProtocolAddress[indx]) { |
| 187 | return false; |
| 188 | } |
| 189 | indx++; |
| 190 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 192 | return true; |
| 193 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 195 | /** |
| 196 | * @param targetProtocolAddress the targetProtocolAddress to set |
| 197 | */ |
| 198 | public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) { |
| 199 | this.targetProtocolAddress = targetProtocolAddress; |
| 200 | return this; |
| 201 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 203 | public ARP setTargetProtocolAddress(int address) { |
| 204 | this.targetProtocolAddress = ByteBuffer.allocate(4).putInt(address).array(); |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public byte[] serialize() { |
| 210 | int length = 8 + (2 * (0xff & this.hardwareAddressLength)) |
| 211 | + (2 * (0xff & this.protocolAddressLength)); |
| 212 | byte[] data = new byte[length]; |
| 213 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 214 | bb.putShort(this.hardwareType); |
| 215 | bb.putShort(this.protocolType); |
| 216 | bb.put(this.hardwareAddressLength); |
| 217 | bb.put(this.protocolAddressLength); |
| 218 | bb.putShort(this.opCode); |
| 219 | bb.put(this.senderHardwareAddress, 0, 0xff & this.hardwareAddressLength); |
| 220 | bb.put(this.senderProtocolAddress, 0, 0xff & this.protocolAddressLength); |
| 221 | bb.put(this.targetHardwareAddress, 0, 0xff & this.hardwareAddressLength); |
| 222 | bb.put(this.targetProtocolAddress, 0, 0xff & this.protocolAddressLength); |
| 223 | return data; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 228 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 229 | this.hardwareType = bb.getShort(); |
| 230 | this.protocolType = bb.getShort(); |
| 231 | this.hardwareAddressLength = bb.get(); |
| 232 | this.protocolAddressLength = bb.get(); |
| 233 | this.opCode = bb.getShort(); |
| 234 | this.senderHardwareAddress = new byte[0xff & this.hardwareAddressLength]; |
| 235 | bb.get(this.senderHardwareAddress, 0, this.senderHardwareAddress.length); |
| 236 | this.senderProtocolAddress = new byte[0xff & this.protocolAddressLength]; |
| 237 | bb.get(this.senderProtocolAddress, 0, this.senderProtocolAddress.length); |
| 238 | this.targetHardwareAddress = new byte[0xff & this.hardwareAddressLength]; |
| 239 | bb.get(this.targetHardwareAddress, 0, this.targetHardwareAddress.length); |
| 240 | this.targetProtocolAddress = new byte[0xff & this.protocolAddressLength]; |
| 241 | bb.get(this.targetProtocolAddress, 0, this.targetProtocolAddress.length); |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | /* (non-Javadoc) |
| 246 | * @see java.lang.Object#hashCode() |
| 247 | */ |
| 248 | @Override |
| 249 | public int hashCode() { |
| 250 | final int prime = 13121; |
| 251 | int result = super.hashCode(); |
| 252 | result = prime * result + hardwareAddressLength; |
| 253 | result = prime * result + hardwareType; |
| 254 | result = prime * result + opCode; |
| 255 | result = prime * result + protocolAddressLength; |
| 256 | result = prime * result + protocolType; |
| 257 | result = prime * result + Arrays.hashCode(senderHardwareAddress); |
| 258 | result = prime * result + Arrays.hashCode(senderProtocolAddress); |
| 259 | result = prime * result + Arrays.hashCode(targetHardwareAddress); |
| 260 | result = prime * result + Arrays.hashCode(targetProtocolAddress); |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | /* (non-Javadoc) |
| 265 | * @see java.lang.Object#equals(java.lang.Object) |
| 266 | */ |
| 267 | @Override |
| 268 | public boolean equals(Object obj) { |
| 269 | if (this == obj) |
| 270 | return true; |
| 271 | if (!super.equals(obj)) |
| 272 | return false; |
| 273 | if (!(obj instanceof ARP)) |
| 274 | return false; |
| 275 | ARP other = (ARP) obj; |
| 276 | if (hardwareAddressLength != other.hardwareAddressLength) |
| 277 | return false; |
| 278 | if (hardwareType != other.hardwareType) |
| 279 | return false; |
| 280 | if (opCode != other.opCode) |
| 281 | return false; |
| 282 | if (protocolAddressLength != other.protocolAddressLength) |
| 283 | return false; |
| 284 | if (protocolType != other.protocolType) |
| 285 | return false; |
| 286 | if (!Arrays.equals(senderHardwareAddress, other.senderHardwareAddress)) |
| 287 | return false; |
| 288 | if (!Arrays.equals(senderProtocolAddress, other.senderProtocolAddress)) |
| 289 | return false; |
| 290 | if (!Arrays.equals(targetHardwareAddress, other.targetHardwareAddress)) |
| 291 | return false; |
| 292 | if (!Arrays.equals(targetProtocolAddress, other.targetProtocolAddress)) |
| 293 | return false; |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | /* (non-Javadoc) |
| 298 | * @see java.lang.Object#toString() |
| 299 | */ |
| 300 | @Override |
| 301 | public String toString() { |
| 302 | return "ARP [hardwareType=" + hardwareType + ", protocolType=" |
| 303 | + protocolType + ", hardwareAddressLength=" |
| 304 | + hardwareAddressLength + ", protocolAddressLength=" |
| 305 | + protocolAddressLength + ", opCode=" + opCode |
| 306 | + ", senderHardwareAddress=" |
| 307 | + Arrays.toString(senderHardwareAddress) |
| 308 | + ", senderProtocolAddress=" |
| 309 | + Arrays.toString(senderProtocolAddress) |
| 310 | + ", targetHardwareAddress=" |
| 311 | + Arrays.toString(targetHardwareAddress) |
| 312 | + ", targetProtocolAddress=" |
| 313 | + Arrays.toString(targetProtocolAddress) + "]"; |
| 314 | } |
| 315 | } |