alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | ******************************************************************************/ |
| 16 | /** |
| 17 | * Copyright 2011, Big Switch Networks, Inc. |
| 18 | * Originally created by David Erickson, Stanford University |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 21 | * not use this file except in compliance with the License. You may obtain |
| 22 | * a copy of the License at |
| 23 | * |
| 24 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | * |
| 26 | * Unless required by applicable law or agreed to in writing, software |
| 27 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 28 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 29 | * License for the specific language governing permissions and limitations |
| 30 | * under the License. |
| 31 | **/ |
| 32 | |
| 33 | package org.onlab.packet; |
| 34 | |
| 35 | import java.nio.ByteBuffer; |
| 36 | import java.util.Arrays; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.Map; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 43 | */ |
| 44 | public class Ethernet extends BasePacket { |
| 45 | private static final String HEXES = "0123456789ABCDEF"; |
| 46 | public static final short TYPE_ARP = 0x0806; |
| 47 | public static final short TYPE_RARP = (short) 0x8035; |
| 48 | public static final short TYPE_IPV4 = 0x0800; |
| 49 | public static final short TYPE_LLDP = (short) 0x88cc; |
| 50 | public static final short TYPE_BSN = (short) 0x8942; |
| 51 | public static final short VLAN_UNTAGGED = (short) 0xffff; |
| 52 | public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes |
| 53 | public static Map<Short, Class<? extends IPacket>> etherTypeClassMap; |
| 54 | |
| 55 | static { |
| 56 | Ethernet.etherTypeClassMap = new HashMap<Short, Class<? extends IPacket>>(); |
| 57 | Ethernet.etherTypeClassMap.put(Ethernet.TYPE_ARP, ARP.class); |
| 58 | Ethernet.etherTypeClassMap.put(Ethernet.TYPE_RARP, ARP.class); |
| 59 | Ethernet.etherTypeClassMap.put(Ethernet.TYPE_IPV4, IPv4.class); |
| 60 | Ethernet.etherTypeClassMap.put(Ethernet.TYPE_LLDP, LLDP.class); |
| 61 | } |
| 62 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 63 | protected MacAddress destinationMACAddress; |
| 64 | protected MacAddress sourceMACAddress; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 65 | protected byte priorityCode; |
| 66 | protected short vlanID; |
| 67 | protected short etherType; |
| 68 | protected boolean pad = false; |
| 69 | |
| 70 | /** |
| 71 | * By default, set Ethernet to untagged. |
| 72 | */ |
| 73 | public Ethernet() { |
| 74 | super(); |
| 75 | this.vlanID = Ethernet.VLAN_UNTAGGED; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the destination MAC address. |
| 80 | * |
| 81 | * @return the destination MAC as a byte array |
| 82 | */ |
| 83 | public byte[] getDestinationMACAddress() { |
| 84 | return this.destinationMACAddress.toBytes(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gets the destination MAC address. |
| 89 | * |
| 90 | * @return the destination MAC |
| 91 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 92 | public MacAddress getDestinationMAC() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 93 | return this.destinationMACAddress; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the destination MAC address. |
| 98 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 99 | * @param destMac the destination MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 100 | * @return the Ethernet frame |
| 101 | */ |
| 102 | public Ethernet setDestinationMACAddress(final byte[] destMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 103 | this.destinationMACAddress = MacAddress.valueOf(destMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 104 | return this; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sets the destination MAC address. |
| 109 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 110 | * @param destMac the destination MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 111 | * @return the Ethernet frame |
| 112 | */ |
| 113 | public Ethernet setDestinationMACAddress(final String destMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 114 | this.destinationMACAddress = MacAddress.valueOf(destMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 115 | return this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Gets the source MAC address. |
| 120 | * |
| 121 | * @return the source MACAddress as a byte array |
| 122 | */ |
| 123 | public byte[] getSourceMACAddress() { |
| 124 | return this.sourceMACAddress.toBytes(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets the source MAC address. |
| 129 | * |
| 130 | * @return the source MACAddress |
| 131 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 132 | public MacAddress getSourceMAC() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 133 | return this.sourceMACAddress; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Sets the source MAC address. |
| 138 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 139 | * @param sourceMac the source MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 140 | * @return the Ethernet frame |
| 141 | */ |
| 142 | public Ethernet setSourceMACAddress(final byte[] sourceMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 143 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 144 | return this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets the source MAC address. |
| 149 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 150 | * @param sourceMac the source MAC to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 151 | * @return the Ethernet frame |
| 152 | */ |
| 153 | public Ethernet setSourceMACAddress(final String sourceMac) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 154 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Gets the priority code. |
| 160 | * |
| 161 | * @return the priorityCode |
| 162 | */ |
| 163 | public byte getPriorityCode() { |
| 164 | return this.priorityCode; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the priority code. |
| 169 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 170 | * @param priority the priorityCode to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 171 | * @return the Ethernet frame |
| 172 | */ |
| 173 | public Ethernet setPriorityCode(final byte priority) { |
| 174 | this.priorityCode = priority; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets the VLAN ID. |
| 180 | * |
| 181 | * @return the vlanID |
| 182 | */ |
| 183 | public short getVlanID() { |
| 184 | return this.vlanID; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets the VLAN ID. |
| 189 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 190 | * @param vlan the vlanID to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 191 | * @return the Ethernet frame |
| 192 | */ |
| 193 | public Ethernet setVlanID(final short vlan) { |
| 194 | this.vlanID = vlan; |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Gets the Ethernet type. |
| 200 | * |
| 201 | * @return the etherType |
| 202 | */ |
| 203 | public short getEtherType() { |
| 204 | return this.etherType; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sets the Ethernet type. |
| 209 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 210 | * @param ethType the etherType to set |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 211 | * @return the Ethernet frame |
| 212 | */ |
| 213 | public Ethernet setEtherType(final short ethType) { |
| 214 | this.etherType = ethType; |
| 215 | return this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @return True if the Ethernet frame is broadcast, false otherwise |
| 220 | */ |
| 221 | public boolean isBroadcast() { |
| 222 | assert this.destinationMACAddress.length() == 6; |
| 223 | return this.destinationMACAddress.isBroadcast(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return True is the Ethernet frame is multicast, False otherwise |
| 228 | */ |
| 229 | public boolean isMulticast() { |
| 230 | return this.destinationMACAddress.isMulticast(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Pad this packet to 60 bytes minimum, filling with zeros? |
| 235 | * |
| 236 | * @return the pad |
| 237 | */ |
| 238 | public boolean isPad() { |
| 239 | return this.pad; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Pad this packet to 60 bytes minimum, filling with zeros? |
| 244 | * |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 245 | * @param pd |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 246 | * the pad to set |
| 247 | */ |
| 248 | public Ethernet setPad(final boolean pd) { |
| 249 | this.pad = pd; |
| 250 | return this; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public byte[] serialize() { |
| 255 | byte[] payloadData = null; |
| 256 | if (this.payload != null) { |
| 257 | this.payload.setParent(this); |
| 258 | payloadData = this.payload.serialize(); |
| 259 | } |
| 260 | int length = 14 + (this.vlanID == Ethernet.VLAN_UNTAGGED ? 0 : 4) |
| 261 | + (payloadData == null ? 0 : payloadData.length); |
| 262 | if (this.pad && length < 60) { |
| 263 | length = 60; |
| 264 | } |
| 265 | final byte[] data = new byte[length]; |
| 266 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 267 | bb.put(this.destinationMACAddress.toBytes()); |
| 268 | bb.put(this.sourceMACAddress.toBytes()); |
| 269 | if (this.vlanID != Ethernet.VLAN_UNTAGGED) { |
| 270 | bb.putShort((short) 0x8100); |
| 271 | bb.putShort((short) (this.priorityCode << 13 | this.vlanID & 0x0fff)); |
| 272 | } |
| 273 | bb.putShort(this.etherType); |
| 274 | if (payloadData != null) { |
| 275 | bb.put(payloadData); |
| 276 | } |
| 277 | if (this.pad) { |
| 278 | Arrays.fill(data, bb.position(), data.length, (byte) 0x0); |
| 279 | } |
| 280 | return data; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public IPacket deserialize(final byte[] data, final int offset, |
| 285 | final int length) { |
| 286 | if (length <= 0) { |
| 287 | return null; |
| 288 | } |
| 289 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 290 | if (this.destinationMACAddress == null) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 291 | this.destinationMACAddress = MacAddress.valueOf(new byte[6]); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 292 | } |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 293 | final byte[] dstAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 294 | bb.get(dstAddr); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 295 | this.destinationMACAddress = MacAddress.valueOf(dstAddr); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 296 | |
| 297 | if (this.sourceMACAddress == null) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 298 | this.sourceMACAddress = MacAddress.valueOf(new byte[6]); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 299 | } |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 300 | final byte[] srcAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 301 | bb.get(srcAddr); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 302 | this.sourceMACAddress = MacAddress.valueOf(srcAddr); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 303 | |
| 304 | short ethType = bb.getShort(); |
| 305 | if (ethType == (short) 0x8100) { |
| 306 | final short tci = bb.getShort(); |
| 307 | this.priorityCode = (byte) (tci >> 13 & 0x07); |
| 308 | this.vlanID = (short) (tci & 0x0fff); |
| 309 | ethType = bb.getShort(); |
| 310 | } else { |
| 311 | this.vlanID = Ethernet.VLAN_UNTAGGED; |
| 312 | } |
| 313 | this.etherType = ethType; |
| 314 | |
| 315 | IPacket payload; |
| 316 | if (Ethernet.etherTypeClassMap.containsKey(this.etherType)) { |
| 317 | final Class<? extends IPacket> clazz = Ethernet.etherTypeClassMap |
| 318 | .get(this.etherType); |
| 319 | try { |
| 320 | payload = clazz.newInstance(); |
| 321 | } catch (final Exception e) { |
| 322 | throw new RuntimeException( |
| 323 | "Error parsing payload for Ethernet packet", e); |
| 324 | } |
| 325 | } else { |
| 326 | payload = new Data(); |
| 327 | } |
| 328 | this.payload = payload.deserialize(data, bb.position(), |
| 329 | bb.limit() - bb.position()); |
| 330 | this.payload.setParent(this); |
| 331 | return this; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Checks to see if a string is a valid MAC address. |
| 336 | * |
| 337 | * @param macAddress |
| 338 | * @return True if macAddress is a valid MAC, False otherwise |
| 339 | */ |
| 340 | public static boolean isMACAddress(final String macAddress) { |
| 341 | final String[] macBytes = macAddress.split(":"); |
| 342 | if (macBytes.length != 6) { |
| 343 | return false; |
| 344 | } |
| 345 | for (int i = 0; i < 6; ++i) { |
| 346 | if (Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 |
| 347 | || Ethernet.HEXES.indexOf(macBytes[i].toUpperCase().charAt( |
| 348 | 1)) == -1) { |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not |
| 357 | * matter, and returns a corresponding byte[]. |
| 358 | * |
| 359 | * @param macAddress |
| 360 | * The MAC address to convert into a bye array |
| 361 | * @return The macAddress as a byte array |
| 362 | */ |
| 363 | public static byte[] toMACAddress(final String macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 364 | return MacAddress.valueOf(macAddress).toBytes(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Accepts a MAC address and returns the corresponding long, where the MAC |
| 369 | * bytes are set on the lower order bytes of the long. |
| 370 | * |
| 371 | * @param macAddress |
| 372 | * @return a long containing the mac address bytes |
| 373 | */ |
| 374 | public static long toLong(final byte[] macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 375 | return MacAddress.valueOf(macAddress).toLong(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Converts a long MAC address to a byte array. |
| 380 | * |
| 381 | * @param macAddress |
| 382 | * @return the bytes of the mac address |
| 383 | */ |
| 384 | public static byte[] toByteArray(final long macAddress) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 385 | return MacAddress.valueOf(macAddress).toBytes(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
| 389 | * (non-Javadoc) |
| 390 | * |
| 391 | * @see java.lang.Object#hashCode() |
| 392 | */ |
| 393 | @Override |
| 394 | public int hashCode() { |
| 395 | final int prime = 7867; |
| 396 | int result = super.hashCode(); |
| 397 | result = prime * result + this.destinationMACAddress.hashCode(); |
| 398 | result = prime * result + this.etherType; |
| 399 | result = prime * result + this.vlanID; |
| 400 | result = prime * result + this.priorityCode; |
| 401 | result = prime * result + (this.pad ? 1231 : 1237); |
| 402 | result = prime * result + this.sourceMACAddress.hashCode(); |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * (non-Javadoc) |
| 408 | * |
| 409 | * @see java.lang.Object#equals(java.lang.Object) |
| 410 | */ |
| 411 | @Override |
| 412 | public boolean equals(final Object obj) { |
| 413 | if (this == obj) { |
| 414 | return true; |
| 415 | } |
| 416 | if (!super.equals(obj)) { |
| 417 | return false; |
| 418 | } |
| 419 | if (!(obj instanceof Ethernet)) { |
| 420 | return false; |
| 421 | } |
| 422 | final Ethernet other = (Ethernet) obj; |
| 423 | if (!this.destinationMACAddress.equals(other.destinationMACAddress)) { |
| 424 | return false; |
| 425 | } |
| 426 | if (this.priorityCode != other.priorityCode) { |
| 427 | return false; |
| 428 | } |
| 429 | if (this.vlanID != other.vlanID) { |
| 430 | return false; |
| 431 | } |
| 432 | if (this.etherType != other.etherType) { |
| 433 | return false; |
| 434 | } |
| 435 | if (this.pad != other.pad) { |
| 436 | return false; |
| 437 | } |
| 438 | if (!this.sourceMACAddress.equals(other.sourceMACAddress)) { |
| 439 | return false; |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * (non-Javadoc) |
| 446 | * |
| 447 | * @see java.lang.Object#toString(java.lang.Object) |
| 448 | */ |
| 449 | @Override |
| 450 | public String toString() { |
| 451 | |
| 452 | final StringBuffer sb = new StringBuffer("\n"); |
| 453 | |
| 454 | final IPacket pkt = this.getPayload(); |
| 455 | |
| 456 | if (pkt instanceof ARP) { |
| 457 | sb.append("arp"); |
| 458 | } else if (pkt instanceof LLDP) { |
| 459 | sb.append("lldp"); |
| 460 | } else if (pkt instanceof ICMP) { |
| 461 | sb.append("icmp"); |
| 462 | } else if (pkt instanceof IPv4) { |
| 463 | sb.append("ip"); |
| 464 | } else if (pkt instanceof DHCP) { |
| 465 | sb.append("dhcp"); |
| 466 | } else { |
| 467 | sb.append(this.getEtherType()); |
| 468 | } |
| 469 | |
| 470 | sb.append("\ndl_vlan: "); |
| 471 | if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) { |
| 472 | sb.append("untagged"); |
| 473 | } else { |
| 474 | sb.append(this.getVlanID()); |
| 475 | } |
| 476 | sb.append("\ndl_vlan_pcp: "); |
| 477 | sb.append(this.getPriorityCode()); |
| 478 | sb.append("\ndl_src: "); |
| 479 | sb.append(bytesToHex(this.getSourceMACAddress())); |
| 480 | sb.append("\ndl_dst: "); |
| 481 | sb.append(bytesToHex(this.getDestinationMACAddress())); |
| 482 | |
| 483 | if (pkt instanceof ARP) { |
| 484 | final ARP p = (ARP) pkt; |
| 485 | sb.append("\nnw_src: "); |
| 486 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p |
| 487 | .getSenderProtocolAddress()))); |
| 488 | sb.append("\nnw_dst: "); |
| 489 | sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p |
| 490 | .getTargetProtocolAddress()))); |
| 491 | } else if (pkt instanceof LLDP) { |
| 492 | sb.append("lldp packet"); |
| 493 | } else if (pkt instanceof ICMP) { |
| 494 | final ICMP icmp = (ICMP) pkt; |
| 495 | sb.append("\nicmp_type: "); |
| 496 | sb.append(icmp.getIcmpType()); |
| 497 | sb.append("\nicmp_code: "); |
| 498 | sb.append(icmp.getIcmpCode()); |
| 499 | } else if (pkt instanceof IPv4) { |
| 500 | final IPv4 p = (IPv4) pkt; |
| 501 | sb.append("\nnw_src: "); |
| 502 | sb.append(IPv4.fromIPv4Address(p.getSourceAddress())); |
| 503 | sb.append("\nnw_dst: "); |
| 504 | sb.append(IPv4.fromIPv4Address(p.getDestinationAddress())); |
| 505 | sb.append("\nnw_tos: "); |
| 506 | sb.append(p.getDiffServ()); |
| 507 | sb.append("\nnw_proto: "); |
| 508 | sb.append(p.getProtocol()); |
| 509 | |
| 510 | if (pkt instanceof TCP) { |
| 511 | sb.append("\ntp_src: "); |
| 512 | sb.append(((TCP) pkt).getSourcePort()); |
| 513 | sb.append("\ntp_dst: "); |
| 514 | sb.append(((TCP) pkt).getDestinationPort()); |
| 515 | |
| 516 | } else if (pkt instanceof UDP) { |
| 517 | sb.append("\ntp_src: "); |
| 518 | sb.append(((UDP) pkt).getSourcePort()); |
| 519 | sb.append("\ntp_dst: "); |
| 520 | sb.append(((UDP) pkt).getDestinationPort()); |
| 521 | } |
| 522 | |
| 523 | if (pkt instanceof ICMP) { |
| 524 | final ICMP icmp = (ICMP) pkt; |
| 525 | sb.append("\nicmp_type: "); |
| 526 | sb.append(icmp.getIcmpType()); |
| 527 | sb.append("\nicmp_code: "); |
| 528 | sb.append(icmp.getIcmpCode()); |
| 529 | } |
| 530 | |
| 531 | } else if (pkt instanceof DHCP) { |
| 532 | sb.append("\ndhcp packet"); |
| 533 | } else if (pkt instanceof Data) { |
| 534 | sb.append("\ndata packet"); |
| 535 | } else if (pkt instanceof LLC) { |
| 536 | sb.append("\nllc packet"); |
| 537 | } else { |
tom | 5f18cf3 | 2014-09-13 14:10:57 -0700 | [diff] [blame] | 538 | sb.append("\nunknown packet"); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | return sb.toString(); |
| 542 | } |
| 543 | |
| 544 | public static String bytesToHex(byte[] in) { |
| 545 | final StringBuilder builder = new StringBuilder(); |
| 546 | for (byte b : in) { |
| 547 | builder.append(String.format("%02x", b)); |
| 548 | } |
| 549 | return builder.toString(); |
| 550 | } |
| 551 | |
| 552 | } |