blob: aab5e7bb2dd53b48bdd77cb6b82152fe7b7157a9 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.nio.ByteBuffer;
21import java.util.Arrays;
22import java.util.HashMap;
23import java.util.Map;
24
25import net.floodlightcontroller.util.MACAddress;
Jonathan Harta99ec672014-04-03 11:30:34 -070026
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import org.openflow.util.HexString;
28
29/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 * @author David Erickson (daviderickson@cs.stanford.edu)
31 */
32public class Ethernet extends BasePacket {
Ray Milkey94b41b52014-04-10 11:13:06 -070033 private static final String HEXES = "0123456789ABCDEF";
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034 public static final short TYPE_ARP = 0x0806;
35 public static final short TYPE_RARP = (short) 0x8035;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070036 public static final short TYPE_IPV4 = 0x0800;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080037 public static final short TYPE_LLDP = (short) 0x88cc;
38 public static final short TYPE_BSN = (short) 0x8942;
Ray Milkey269ffb92014-04-03 14:43:30 -070039 public static final short VLAN_UNTAGGED = (short) 0xffff;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080040 public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070041 public static final Map<Short, Class<? extends IPacket>> ETHER_TYPE_CLASS_MAP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042
43 static {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070044 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 Krishnaswamy345ee992012-12-13 20:29:48 -080050 }
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 Milkeyb41100a2014-04-10 10:42:15 -070060 * By default, set Ethernet to untagged.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080061 */
62 public Ethernet() {
63 super();
64 this.vlanID = VLAN_UNTAGGED;
65 }
Ray Milkey269ffb92014-04-03 14:43:30 -070066
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067 /**
68 * @return the destination MAC as a byte array
69 */
70 public byte[] getDestinationMACAddress() {
71 return destinationMACAddress.toBytes();
72 }
Ray Milkey269ffb92014-04-03 14:43:30 -070073
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 /**
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 Milkey269ffb92014-04-03 14:43:30 -0700103
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 /**
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 Milkey269ffb92014-04-03 14:43:30 -0700171
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800172 /**
173 * @return True if the Ethernet frame is broadcast, false otherwise
174 */
175 public boolean isBroadcast() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 assert (destinationMACAddress.length() == 6);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800177 return destinationMACAddress.isBroadcast();
178 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700179
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 /**
181 * @return True is the Ethernet frame is multicast, False otherwise
182 */
183 public boolean isMulticast() {
184 return destinationMACAddress.isMulticast();
185 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700186
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800187 /**
188 * Pad this packet to 60 bytes minimum, filling with zeros?
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 * @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 Milkey269ffb92014-04-03 14:43:30 -0700198 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 * @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 Milkey269ffb92014-04-03 14:43:30 -0700213 ((payloadData == null) ? 0 : payloadData.length);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800214 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 Milkeyb29e6262014-04-09 16:02:14 -0700226 if (payloadData != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800227 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700228 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800229 if (pad) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700230 Arrays.fill(data, bb.position(), data.length, (byte) 0x0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800231 }
232 return data;
233 }
234
235 @Override
236 public IPacket deserialize(byte[] data, int offset, int length) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700237 if (length <= 0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800238 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700239 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800240 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700241 if (this.destinationMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800242 this.destinationMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700243 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800244 byte[] dstAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
245 bb.get(dstAddr);
246 this.destinationMACAddress = MACAddress.valueOf(dstAddr);
247
Ray Milkeyb29e6262014-04-09 16:02:14 -0700248 if (this.sourceMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800249 this.sourceMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700250 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800251 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 Milkey269ffb92014-04-03 14:43:30 -0700265
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800266 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700267 if (Ethernet.ETHER_TYPE_CLASS_MAP.containsKey(this.etherType)) {
268 Class<? extends IPacket> clazz = Ethernet.ETHER_TYPE_CLASS_MAP.get(this.etherType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800269 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 Milkey269ffb92014-04-03 14:43:30 -0700277 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800278 this.payload.setParent(this);
279 return this;
280 }
281
282 /**
283 * Checks to see if a string is a valid MAC address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700284 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800285 * @param macAddress
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 Milkeyb29e6262014-04-09 16:02:14 -0700290 if (macBytes.length != 6) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800291 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700292 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800293 for (int i = 0; i < 6; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700294 if (HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 ||
295 HEXES.indexOf(macBytes[i].toUpperCase().charAt(1)) == -1) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800296 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 Milkey269ffb92014-04-03 14:43:30 -0700305 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800306 * @param macAddress The MAC address to convert into a bye array
Ray Milkey269ffb92014-04-03 14:43:30 -0700307 * @return The macAddress as a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800308 */
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 Milkey269ffb92014-04-03 14:43:30 -0700317 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800318 * @param macAddress
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 Milkeyb41100a2014-04-10 10:42:15 -0700326 * Convert a long MAC address to a byte array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700327 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800328 * @param macAddress
329 * @return the bytes of the mac address
330 */
331 public static byte[] toByteArray(long macAddress) {
332 return MACAddress.valueOf(macAddress).toBytes();
333 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700334
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800335 /* (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 Milkeyb29e6262014-04-09 16:02:14 -0700356 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800357 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700358 }
359 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800360 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700361 }
362 if (!(obj instanceof Ethernet)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800363 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700364 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800365 Ethernet other = (Ethernet) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700366 if (!destinationMACAddress.equals(other.destinationMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800367 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700368 }
369 if (priorityCode != other.priorityCode) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800370 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700371 }
372 if (vlanID != other.vlanID) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800373 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700374 }
375 if (etherType != other.etherType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800376 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700377 }
378 if (pad != other.pad) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800379 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700380 }
381 if (!sourceMACAddress.equals(other.sourceMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800382 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700383 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800384 return true;
385 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700386
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800387 /* (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 Milkey149693c2014-05-20 14:58:53 -0700395 IPacket pkt = this.getPayload();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800396
Ray Milkeyb29e6262014-04-09 16:02:14 -0700397 if (pkt instanceof ARP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800398 sb.append("arp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700399 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800400 sb.append("lldp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700401 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800402 sb.append("icmp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700403 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800404 sb.append("ip");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700405 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800406 sb.append("dhcp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700407 } else {
408 sb.append(this.getEtherType());
409 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800410
411 sb.append("\ndl_vlan: ");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700412 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800413 sb.append("untagged");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700414 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800415 sb.append(this.getVlanID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700416 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800417 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 Milkey269ffb92014-04-03 14:43:30 -0700431 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800432 sb.append("lldp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700433 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800434 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 Milkey269ffb92014-04-03 14:43:30 -0700439 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800440 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 Milkey149693c2014-05-20 14:58:53 -0700450 IPacket payload = pkt.getPayload();
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700451 if (payload instanceof TCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800452 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700453 sb.append(((TCP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800454 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700455 sb.append(((TCP) payload).getDestinationPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800456
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700457 } else if (payload instanceof UDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800458 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700459 sb.append(((UDP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800460 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700461 sb.append(((UDP) payload).getDestinationPort());
462 } else if (payload instanceof ICMP) {
463 ICMP icmp = (ICMP) payload;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800464 sb.append("\nicmp_type: ");
465 sb.append(icmp.getIcmpType());
466 sb.append("\nicmp_code: ");
467 sb.append(icmp.getIcmpCode());
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700468 } else {
469 sb.append("\nunknown IPv4 packet");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800470 }
471
Ray Milkey269ffb92014-04-03 14:43:30 -0700472 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800473 sb.append("\ndhcp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700474 } else if (pkt instanceof Data) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800475 sb.append("\ndata packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 } else if (pkt instanceof LLC) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800477 sb.append("\nllc packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700478 } else if (pkt instanceof BPDU) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800479 sb.append("\nbpdu packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700480 } else {
Ray Milkeyb41100a2014-04-10 10:42:15 -0700481 sb.append("\nunknown packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700482 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800483
484 return sb.toString();
485 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800486}