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 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 26 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | import org.openflow.util.HexString; |
| 28 | |
| 29 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 31 | */ |
| 32 | public class Ethernet extends BasePacket { |
Ray Milkey | 94b41b5 | 2014-04-10 11:13:06 -0700 | [diff] [blame] | 33 | private static final String HEXES = "0123456789ABCDEF"; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 34 | public static final short TYPE_ARP = 0x0806; |
| 35 | public static final short TYPE_RARP = (short) 0x8035; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 36 | public static final short TYPE_IPV4 = 0x0800; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | public static final short TYPE_LLDP = (short) 0x88cc; |
| 38 | public static final short TYPE_BSN = (short) 0x8942; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | public static final short VLAN_UNTAGGED = (short) 0xffff; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 40 | public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 41 | public static final Map<Short, Class<? extends IPacket>> ETHER_TYPE_CLASS_MAP; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 42 | |
| 43 | static { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 44 | ETHER_TYPE_CLASS_MAP = new HashMap<Short, Class<? extends IPacket>>(); |
| 45 | ETHER_TYPE_CLASS_MAP.put(TYPE_ARP, ARP.class); |
| 46 | ETHER_TYPE_CLASS_MAP.put(TYPE_RARP, ARP.class); |
| 47 | ETHER_TYPE_CLASS_MAP.put(TYPE_IPV4, IPv4.class); |
| 48 | ETHER_TYPE_CLASS_MAP.put(TYPE_LLDP, LLDP.class); |
| 49 | ETHER_TYPE_CLASS_MAP.put(TYPE_BSN, BSN.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | protected MACAddress destinationMACAddress; |
| 53 | protected MACAddress sourceMACAddress; |
| 54 | protected byte priorityCode; |
| 55 | protected short vlanID; |
| 56 | protected short etherType; |
| 57 | protected boolean pad = false; |
| 58 | |
| 59 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 60 | * By default, set Ethernet to untagged. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 61 | */ |
| 62 | public Ethernet() { |
| 63 | super(); |
| 64 | this.vlanID = VLAN_UNTAGGED; |
| 65 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | /** |
| 68 | * @return the destination MAC as a byte array |
| 69 | */ |
| 70 | public byte[] getDestinationMACAddress() { |
| 71 | return destinationMACAddress.toBytes(); |
| 72 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 74 | /** |
| 75 | * @return the destination MAC |
| 76 | */ |
| 77 | public MACAddress getDestinationMAC() { |
| 78 | return destinationMACAddress; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param destinationMACAddress the destination MAC to set |
| 83 | */ |
| 84 | public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) { |
| 85 | this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param destinationMACAddress the destination MAC to set |
| 91 | */ |
| 92 | public Ethernet setDestinationMACAddress(String destinationMACAddress) { |
| 93 | this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress); |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @return the source MACAddress as a byte array |
| 99 | */ |
| 100 | public byte[] getSourceMACAddress() { |
| 101 | return sourceMACAddress.toBytes(); |
| 102 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | /** |
| 105 | * @return the source MACAddress |
| 106 | */ |
| 107 | public MACAddress getSourceMAC() { |
| 108 | return sourceMACAddress; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param sourceMACAddress the source MAC to set |
| 113 | */ |
| 114 | public Ethernet setSourceMACAddress(byte[] sourceMACAddress) { |
| 115 | this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress); |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param sourceMACAddress the source MAC to set |
| 121 | */ |
| 122 | public Ethernet setSourceMACAddress(String sourceMACAddress) { |
| 123 | this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return the priorityCode |
| 129 | */ |
| 130 | public byte getPriorityCode() { |
| 131 | return priorityCode; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param priorityCode the priorityCode to set |
| 136 | */ |
| 137 | public Ethernet setPriorityCode(byte priorityCode) { |
| 138 | this.priorityCode = priorityCode; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return the vlanID |
| 144 | */ |
| 145 | public short getVlanID() { |
| 146 | return vlanID; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param vlanID the vlanID to set |
| 151 | */ |
| 152 | public Ethernet setVlanID(short vlanID) { |
| 153 | this.vlanID = vlanID; |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return the etherType |
| 159 | */ |
| 160 | public short getEtherType() { |
| 161 | return etherType; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param etherType the etherType to set |
| 166 | */ |
| 167 | public Ethernet setEtherType(short etherType) { |
| 168 | this.etherType = etherType; |
| 169 | return this; |
| 170 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 172 | /** |
| 173 | * @return True if the Ethernet frame is broadcast, false otherwise |
| 174 | */ |
| 175 | public boolean isBroadcast() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | assert (destinationMACAddress.length() == 6); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 177 | return destinationMACAddress.isBroadcast(); |
| 178 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 179 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 180 | /** |
| 181 | * @return True is the Ethernet frame is multicast, False otherwise |
| 182 | */ |
| 183 | public boolean isMulticast() { |
| 184 | return destinationMACAddress.isMulticast(); |
| 185 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 187 | /** |
| 188 | * Pad this packet to 60 bytes minimum, filling with zeros? |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 190 | * @return the pad |
| 191 | */ |
| 192 | public boolean isPad() { |
| 193 | return pad; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Pad this packet to 60 bytes minimum, filling with zeros? |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 198 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 199 | * @param pad the pad to set |
| 200 | */ |
| 201 | public Ethernet setPad(boolean pad) { |
| 202 | this.pad = pad; |
| 203 | return this; |
| 204 | } |
| 205 | |
| 206 | public byte[] serialize() { |
| 207 | byte[] payloadData = null; |
| 208 | if (payload != null) { |
| 209 | payload.setParent(this); |
| 210 | payloadData = payload.serialize(); |
| 211 | } |
| 212 | int length = 14 + ((vlanID == VLAN_UNTAGGED) ? 0 : 4) + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | ((payloadData == null) ? 0 : payloadData.length); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 214 | if (pad && length < 60) { |
| 215 | length = 60; |
| 216 | } |
| 217 | byte[] data = new byte[length]; |
| 218 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 219 | bb.put(destinationMACAddress.toBytes()); |
| 220 | bb.put(sourceMACAddress.toBytes()); |
| 221 | if (vlanID != VLAN_UNTAGGED) { |
| 222 | bb.putShort((short) 0x8100); |
| 223 | bb.putShort((short) ((priorityCode << 13) | (vlanID & 0x0fff))); |
| 224 | } |
| 225 | bb.putShort(etherType); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 226 | if (payloadData != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 227 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 228 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 229 | if (pad) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | Arrays.fill(data, bb.position(), data.length, (byte) 0x0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 231 | } |
| 232 | return data; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public IPacket deserialize(byte[] data, int offset, int length) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 237 | if (length <= 0) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 238 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 239 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 240 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 241 | if (this.destinationMACAddress == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 242 | this.destinationMACAddress = MACAddress.valueOf(new byte[6]); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 243 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 244 | byte[] dstAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH]; |
| 245 | bb.get(dstAddr); |
| 246 | this.destinationMACAddress = MACAddress.valueOf(dstAddr); |
| 247 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 248 | if (this.sourceMACAddress == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 249 | this.sourceMACAddress = MACAddress.valueOf(new byte[6]); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 250 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 251 | byte[] srcAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH]; |
| 252 | bb.get(srcAddr); |
| 253 | this.sourceMACAddress = MACAddress.valueOf(srcAddr); |
| 254 | |
| 255 | short etherType = bb.getShort(); |
| 256 | if (etherType == (short) 0x8100) { |
| 257 | short tci = bb.getShort(); |
| 258 | this.priorityCode = (byte) ((tci >> 13) & 0x07); |
| 259 | this.vlanID = (short) (tci & 0x0fff); |
| 260 | etherType = bb.getShort(); |
| 261 | } else { |
| 262 | this.vlanID = VLAN_UNTAGGED; |
| 263 | } |
| 264 | this.etherType = etherType; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 265 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 266 | IPacket payload; |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 267 | if (Ethernet.ETHER_TYPE_CLASS_MAP.containsKey(this.etherType)) { |
| 268 | Class<? extends IPacket> clazz = Ethernet.ETHER_TYPE_CLASS_MAP.get(this.etherType); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 269 | try { |
| 270 | payload = clazz.newInstance(); |
| 271 | } catch (Exception e) { |
| 272 | throw new RuntimeException("Error parsing payload for Ethernet packet", e); |
| 273 | } |
| 274 | } else { |
| 275 | payload = new Data(); |
| 276 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 277 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 278 | this.payload.setParent(this); |
| 279 | return this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Checks to see if a string is a valid MAC address. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 284 | * |
Sho SHIMIZU | 6947445 | 2014-06-20 09:17:40 -0700 | [diff] [blame] | 285 | * @param macAddress MAC address to be checked |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 286 | * @return True if macAddress is a valid MAC, False otherwise |
| 287 | */ |
| 288 | public static boolean isMACAddress(String macAddress) { |
| 289 | String[] macBytes = macAddress.split(":"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 290 | if (macBytes.length != 6) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 291 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 292 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 293 | for (int i = 0; i < 6; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 294 | if (HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 || |
| 295 | HEXES.indexOf(macBytes[i].toUpperCase().charAt(1)) == -1) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not |
| 304 | * matter, and returns a corresponding byte[]. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 305 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 306 | * @param macAddress The MAC address to convert into a bye array |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 307 | * @return The macAddress as a byte array |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 308 | */ |
| 309 | public static byte[] toMACAddress(String macAddress) { |
| 310 | return MACAddress.valueOf(macAddress).toBytes(); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * Accepts a MAC address and returns the corresponding long, where the |
| 316 | * MAC bytes are set on the lower order bytes of the long. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | * |
Sho SHIMIZU | 6947445 | 2014-06-20 09:17:40 -0700 | [diff] [blame] | 318 | * @param macAddress MAC address to be converted to long value |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 319 | * @return a long containing the mac address bytes |
| 320 | */ |
| 321 | public static long toLong(byte[] macAddress) { |
| 322 | return MACAddress.valueOf(macAddress).toLong(); |
| 323 | } |
| 324 | |
| 325 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 326 | * Convert a long MAC address to a byte array. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 327 | * |
Sho SHIMIZU | 6947445 | 2014-06-20 09:17:40 -0700 | [diff] [blame] | 328 | * @param macAddress MAC address to be converted to a byte array |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 329 | * @return the bytes of the mac address |
| 330 | */ |
| 331 | public static byte[] toByteArray(long macAddress) { |
| 332 | return MACAddress.valueOf(macAddress).toBytes(); |
| 333 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 334 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 335 | /* (non-Javadoc) |
| 336 | * @see java.lang.Object#hashCode() |
| 337 | */ |
| 338 | @Override |
| 339 | public int hashCode() { |
| 340 | final int prime = 7867; |
| 341 | int result = super.hashCode(); |
| 342 | result = prime * result + destinationMACAddress.hashCode(); |
| 343 | result = prime * result + etherType; |
| 344 | result = prime * result + vlanID; |
| 345 | result = prime * result + priorityCode; |
| 346 | result = prime * result + (pad ? 1231 : 1237); |
| 347 | result = prime * result + sourceMACAddress.hashCode(); |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | /* (non-Javadoc) |
| 352 | * @see java.lang.Object#equals(java.lang.Object) |
| 353 | */ |
| 354 | @Override |
| 355 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 356 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 357 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 358 | } |
| 359 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 360 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 361 | } |
| 362 | if (!(obj instanceof Ethernet)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 363 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 364 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 365 | Ethernet other = (Ethernet) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 366 | if (!destinationMACAddress.equals(other.destinationMACAddress)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 367 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 368 | } |
| 369 | if (priorityCode != other.priorityCode) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 370 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 371 | } |
| 372 | if (vlanID != other.vlanID) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 373 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 374 | } |
| 375 | if (etherType != other.etherType) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 376 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 377 | } |
| 378 | if (pad != other.pad) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 379 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 380 | } |
| 381 | if (!sourceMACAddress.equals(other.sourceMACAddress)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 382 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 383 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 384 | return true; |
| 385 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 386 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 387 | /* (non-Javadoc) |
| 388 | * @see java.lang.Object#toString(java.lang.Object) |
| 389 | */ |
| 390 | @Override |
| 391 | public String toString() { |
| 392 | |
| 393 | StringBuffer sb = new StringBuffer("\n"); |
| 394 | |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 395 | IPacket pkt = this.getPayload(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 396 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 397 | if (pkt instanceof ARP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 398 | sb.append("arp"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 399 | } else if (pkt instanceof LLDP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 400 | sb.append("lldp"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 401 | } else if (pkt instanceof ICMP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 402 | sb.append("icmp"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 403 | } else if (pkt instanceof IPv4) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 404 | sb.append("ip"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 405 | } else if (pkt instanceof DHCP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 406 | sb.append("dhcp"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 407 | } else { |
| 408 | sb.append(this.getEtherType()); |
| 409 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 410 | |
| 411 | sb.append("\ndl_vlan: "); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 412 | if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 413 | sb.append("untagged"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 414 | } else { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 415 | sb.append(this.getVlanID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 416 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 417 | sb.append("\ndl_vlan_pcp: "); |
| 418 | sb.append(this.getPriorityCode()); |
| 419 | sb.append("\ndl_src: "); |
| 420 | sb.append(HexString.toHexString(this.getSourceMACAddress())); |
| 421 | sb.append("\ndl_dst: "); |
| 422 | sb.append(HexString.toHexString(this.getDestinationMACAddress())); |
| 423 | |
| 424 | |
| 425 | if (pkt instanceof ARP) { |
| 426 | ARP p = (ARP) pkt; |
| 427 | sb.append("\nnw_src: "); |
| 428 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getSenderProtocolAddress()))); |
| 429 | sb.append("\nnw_dst: "); |
| 430 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getTargetProtocolAddress()))); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 431 | } else if (pkt instanceof LLDP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 432 | sb.append("lldp packet"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 433 | } else if (pkt instanceof ICMP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 434 | ICMP icmp = (ICMP) pkt; |
| 435 | sb.append("\nicmp_type: "); |
| 436 | sb.append(icmp.getIcmpType()); |
| 437 | sb.append("\nicmp_code: "); |
| 438 | sb.append(icmp.getIcmpCode()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 439 | } else if (pkt instanceof IPv4) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 440 | IPv4 p = (IPv4) pkt; |
| 441 | sb.append("\nnw_src: "); |
| 442 | sb.append(IPv4.fromIPv4Address(p.getSourceAddress())); |
| 443 | sb.append("\nnw_dst: "); |
| 444 | sb.append(IPv4.fromIPv4Address(p.getDestinationAddress())); |
| 445 | sb.append("\nnw_tos: "); |
| 446 | sb.append(p.getDiffServ()); |
| 447 | sb.append("\nnw_proto: "); |
| 448 | sb.append(p.getProtocol()); |
| 449 | |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 450 | IPacket payload = pkt.getPayload(); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 451 | if (payload instanceof TCP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 452 | sb.append("\ntp_src: "); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 453 | sb.append(((TCP) payload).getSourcePort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 454 | sb.append("\ntp_dst: "); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 455 | sb.append(((TCP) payload).getDestinationPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 456 | |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 457 | } else if (payload instanceof UDP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 458 | sb.append("\ntp_src: "); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 459 | sb.append(((UDP) payload).getSourcePort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 460 | sb.append("\ntp_dst: "); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 461 | sb.append(((UDP) payload).getDestinationPort()); |
| 462 | } else if (payload instanceof ICMP) { |
| 463 | ICMP icmp = (ICMP) payload; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 464 | sb.append("\nicmp_type: "); |
| 465 | sb.append(icmp.getIcmpType()); |
| 466 | sb.append("\nicmp_code: "); |
| 467 | sb.append(icmp.getIcmpCode()); |
Pavlin Radoslavov | ebc7051 | 2014-04-09 18:19:19 -0700 | [diff] [blame] | 468 | } else { |
| 469 | sb.append("\nunknown IPv4 packet"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 472 | } else if (pkt instanceof DHCP) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 473 | sb.append("\ndhcp packet"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 474 | } else if (pkt instanceof Data) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 475 | sb.append("\ndata packet"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 476 | } else if (pkt instanceof LLC) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 477 | sb.append("\nllc packet"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 478 | } else if (pkt instanceof BPDU) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 479 | sb.append("\nbpdu packet"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 480 | } else { |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 481 | sb.append("\nunknown packet"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 482 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 483 | |
| 484 | return sb.toString(); |
| 485 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 486 | } |