blob: 62e30f518ca079ab15a7247dc1a3e6d0556c88a2 [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
Jonathan Hartc78b8f62014-08-07 22:31:09 -070027import org.projectfloodlight.openflow.util.HexString;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070029// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 * @author David Erickson (daviderickson@cs.stanford.edu)
32 */
33public class Ethernet extends BasePacket {
Ray Milkey94b41b52014-04-10 11:13:06 -070034 private static final String HEXES = "0123456789ABCDEF";
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080035 public static final short TYPE_ARP = 0x0806;
36 public static final short TYPE_RARP = (short) 0x8035;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070037 public static final short TYPE_IPV4 = 0x0800;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080038 public static final short TYPE_LLDP = (short) 0x88cc;
39 public static final short TYPE_BSN = (short) 0x8942;
Ray Milkey269ffb92014-04-03 14:43:30 -070040 public static final short VLAN_UNTAGGED = (short) 0xffff;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041 public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070042 public static final Map<Short, Class<? extends IPacket>> ETHER_TYPE_CLASS_MAP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043
44 static {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070045 ETHER_TYPE_CLASS_MAP = new HashMap<Short, Class<? extends IPacket>>();
46 ETHER_TYPE_CLASS_MAP.put(TYPE_ARP, ARP.class);
47 ETHER_TYPE_CLASS_MAP.put(TYPE_RARP, ARP.class);
48 ETHER_TYPE_CLASS_MAP.put(TYPE_IPV4, IPv4.class);
49 ETHER_TYPE_CLASS_MAP.put(TYPE_LLDP, LLDP.class);
50 ETHER_TYPE_CLASS_MAP.put(TYPE_BSN, BSN.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 }
52
53 protected MACAddress destinationMACAddress;
54 protected MACAddress sourceMACAddress;
55 protected byte priorityCode;
56 protected short vlanID;
57 protected short etherType;
58 protected boolean pad = false;
59
60 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070061 * By default, set Ethernet to untagged.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 */
63 public Ethernet() {
64 super();
65 this.vlanID = VLAN_UNTAGGED;
66 }
Ray Milkey269ffb92014-04-03 14:43:30 -070067
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 /**
69 * @return the destination MAC as a byte array
70 */
71 public byte[] getDestinationMACAddress() {
72 return destinationMACAddress.toBytes();
73 }
Ray Milkey269ffb92014-04-03 14:43:30 -070074
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 /**
76 * @return the destination MAC
77 */
78 public MACAddress getDestinationMAC() {
79 return destinationMACAddress;
80 }
81
82 /**
83 * @param destinationMACAddress the destination MAC to set
84 */
85 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
86 this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress);
87 return this;
88 }
89
90 /**
91 * @param destinationMACAddress the destination MAC to set
92 */
93 public Ethernet setDestinationMACAddress(String destinationMACAddress) {
94 this.destinationMACAddress = MACAddress.valueOf(destinationMACAddress);
95 return this;
96 }
97
98 /**
99 * @return the source MACAddress as a byte array
100 */
101 public byte[] getSourceMACAddress() {
102 return sourceMACAddress.toBytes();
103 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700104
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 /**
106 * @return the source MACAddress
107 */
108 public MACAddress getSourceMAC() {
109 return sourceMACAddress;
110 }
111
112 /**
113 * @param sourceMACAddress the source MAC to set
114 */
115 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
116 this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress);
117 return this;
118 }
119
120 /**
121 * @param sourceMACAddress the source MAC to set
122 */
123 public Ethernet setSourceMACAddress(String sourceMACAddress) {
124 this.sourceMACAddress = MACAddress.valueOf(sourceMACAddress);
125 return this;
126 }
127
128 /**
129 * @return the priorityCode
130 */
131 public byte getPriorityCode() {
132 return priorityCode;
133 }
134
135 /**
136 * @param priorityCode the priorityCode to set
137 */
138 public Ethernet setPriorityCode(byte priorityCode) {
139 this.priorityCode = priorityCode;
140 return this;
141 }
142
143 /**
144 * @return the vlanID
145 */
146 public short getVlanID() {
147 return vlanID;
148 }
149
150 /**
151 * @param vlanID the vlanID to set
152 */
153 public Ethernet setVlanID(short vlanID) {
154 this.vlanID = vlanID;
155 return this;
156 }
157
158 /**
159 * @return the etherType
160 */
161 public short getEtherType() {
162 return etherType;
163 }
164
165 /**
166 * @param etherType the etherType to set
167 */
168 public Ethernet setEtherType(short etherType) {
169 this.etherType = etherType;
170 return this;
171 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700172
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800173 /**
174 * @return True if the Ethernet frame is broadcast, false otherwise
175 */
176 public boolean isBroadcast() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 assert (destinationMACAddress.length() == 6);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800178 return destinationMACAddress.isBroadcast();
179 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700180
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800181 /**
182 * @return True is the Ethernet frame is multicast, False otherwise
183 */
184 public boolean isMulticast() {
185 return destinationMACAddress.isMulticast();
186 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700187
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800188 /**
189 * Pad this packet to 60 bytes minimum, filling with zeros?
Ray Milkey269ffb92014-04-03 14:43:30 -0700190 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191 * @return the pad
192 */
193 public boolean isPad() {
194 return pad;
195 }
196
197 /**
198 * Pad this packet to 60 bytes minimum, filling with zeros?
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800200 * @param pad the pad to set
201 */
202 public Ethernet setPad(boolean pad) {
203 this.pad = pad;
204 return this;
205 }
206
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -0700207 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800208 public byte[] serialize() {
209 byte[] payloadData = null;
210 if (payload != null) {
211 payload.setParent(this);
212 payloadData = payload.serialize();
213 }
214 int length = 14 + ((vlanID == VLAN_UNTAGGED) ? 0 : 4) +
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 ((payloadData == null) ? 0 : payloadData.length);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800216 if (pad && length < 60) {
217 length = 60;
218 }
219 byte[] data = new byte[length];
220 ByteBuffer bb = ByteBuffer.wrap(data);
221 bb.put(destinationMACAddress.toBytes());
222 bb.put(sourceMACAddress.toBytes());
223 if (vlanID != VLAN_UNTAGGED) {
224 bb.putShort((short) 0x8100);
225 bb.putShort((short) ((priorityCode << 13) | (vlanID & 0x0fff)));
226 }
227 bb.putShort(etherType);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700228 if (payloadData != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800229 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700230 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800231 if (pad) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700232 Arrays.fill(data, bb.position(), data.length, (byte) 0x0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800233 }
234 return data;
235 }
236
237 @Override
238 public IPacket deserialize(byte[] data, int offset, int length) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700239 if (length <= 0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800240 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700241 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800242 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700243 if (this.destinationMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800244 this.destinationMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700245 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800246 byte[] dstAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
247 bb.get(dstAddr);
248 this.destinationMACAddress = MACAddress.valueOf(dstAddr);
249
Ray Milkeyb29e6262014-04-09 16:02:14 -0700250 if (this.sourceMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800251 this.sourceMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700252 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800253 byte[] srcAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
254 bb.get(srcAddr);
255 this.sourceMACAddress = MACAddress.valueOf(srcAddr);
256
257 short etherType = bb.getShort();
258 if (etherType == (short) 0x8100) {
259 short tci = bb.getShort();
260 this.priorityCode = (byte) ((tci >> 13) & 0x07);
261 this.vlanID = (short) (tci & 0x0fff);
262 etherType = bb.getShort();
263 } else {
264 this.vlanID = VLAN_UNTAGGED;
265 }
266 this.etherType = etherType;
Ray Milkey269ffb92014-04-03 14:43:30 -0700267
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800268 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700269 if (Ethernet.ETHER_TYPE_CLASS_MAP.containsKey(this.etherType)) {
270 Class<? extends IPacket> clazz = Ethernet.ETHER_TYPE_CLASS_MAP.get(this.etherType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800271 try {
272 payload = clazz.newInstance();
273 } catch (Exception e) {
274 throw new RuntimeException("Error parsing payload for Ethernet packet", e);
275 }
276 } else {
277 payload = new Data();
278 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700279 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800280 this.payload.setParent(this);
281 return this;
282 }
283
284 /**
285 * Checks to see if a string is a valid MAC address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700286 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700287 * @param macAddress MAC address to be checked
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800288 * @return True if macAddress is a valid MAC, False otherwise
289 */
290 public static boolean isMACAddress(String macAddress) {
291 String[] macBytes = macAddress.split(":");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700292 if (macBytes.length != 6) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800293 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700294 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800295 for (int i = 0; i < 6; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700296 if (HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 ||
297 HEXES.indexOf(macBytes[i].toUpperCase().charAt(1)) == -1) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800298 return false;
299 }
300 }
301 return true;
302 }
303
304 /**
305 * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
306 * matter, and returns a corresponding byte[].
Ray Milkey269ffb92014-04-03 14:43:30 -0700307 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800308 * @param macAddress The MAC address to convert into a bye array
Ray Milkey269ffb92014-04-03 14:43:30 -0700309 * @return The macAddress as a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800310 */
311 public static byte[] toMACAddress(String macAddress) {
312 return MACAddress.valueOf(macAddress).toBytes();
313 }
314
315
316 /**
317 * Accepts a MAC address and returns the corresponding long, where the
318 * MAC bytes are set on the lower order bytes of the long.
Ray Milkey269ffb92014-04-03 14:43:30 -0700319 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700320 * @param macAddress MAC address to be converted to long value
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800321 * @return a long containing the mac address bytes
322 */
323 public static long toLong(byte[] macAddress) {
324 return MACAddress.valueOf(macAddress).toLong();
325 }
326
327 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700328 * Convert a long MAC address to a byte array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700329 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700330 * @param macAddress MAC address to be converted to a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800331 * @return the bytes of the mac address
332 */
333 public static byte[] toByteArray(long macAddress) {
334 return MACAddress.valueOf(macAddress).toBytes();
335 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700336
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800337 /* (non-Javadoc)
338 * @see java.lang.Object#hashCode()
339 */
340 @Override
341 public int hashCode() {
342 final int prime = 7867;
343 int result = super.hashCode();
344 result = prime * result + destinationMACAddress.hashCode();
345 result = prime * result + etherType;
346 result = prime * result + vlanID;
347 result = prime * result + priorityCode;
348 result = prime * result + (pad ? 1231 : 1237);
349 result = prime * result + sourceMACAddress.hashCode();
350 return result;
351 }
352
353 /* (non-Javadoc)
354 * @see java.lang.Object#equals(java.lang.Object)
355 */
356 @Override
357 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700358 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800359 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700360 }
361 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800362 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700363 }
364 if (!(obj instanceof Ethernet)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800365 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700366 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800367 Ethernet other = (Ethernet) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700368 if (!destinationMACAddress.equals(other.destinationMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800369 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700370 }
371 if (priorityCode != other.priorityCode) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800372 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700373 }
374 if (vlanID != other.vlanID) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800375 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700376 }
377 if (etherType != other.etherType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800378 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700379 }
380 if (pad != other.pad) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800381 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700382 }
383 if (!sourceMACAddress.equals(other.sourceMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800384 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700385 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800386 return true;
387 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700388
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800389 /* (non-Javadoc)
390 * @see java.lang.Object#toString(java.lang.Object)
391 */
392 @Override
393 public String toString() {
394
395 StringBuffer sb = new StringBuffer("\n");
396
Ray Milkey149693c2014-05-20 14:58:53 -0700397 IPacket pkt = this.getPayload();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800398
Ray Milkeyb29e6262014-04-09 16:02:14 -0700399 if (pkt instanceof ARP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800400 sb.append("arp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700401 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800402 sb.append("lldp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700403 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800404 sb.append("icmp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700405 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800406 sb.append("ip");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700407 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800408 sb.append("dhcp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700409 } else {
410 sb.append(this.getEtherType());
411 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800412
413 sb.append("\ndl_vlan: ");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700414 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800415 sb.append("untagged");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700416 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800417 sb.append(this.getVlanID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700418 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800419 sb.append("\ndl_vlan_pcp: ");
420 sb.append(this.getPriorityCode());
421 sb.append("\ndl_src: ");
422 sb.append(HexString.toHexString(this.getSourceMACAddress()));
423 sb.append("\ndl_dst: ");
424 sb.append(HexString.toHexString(this.getDestinationMACAddress()));
425
426
427 if (pkt instanceof ARP) {
428 ARP p = (ARP) pkt;
429 sb.append("\nnw_src: ");
430 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getSenderProtocolAddress())));
431 sb.append("\nnw_dst: ");
432 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getTargetProtocolAddress())));
Ray Milkey269ffb92014-04-03 14:43:30 -0700433 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800434 sb.append("lldp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700435 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800436 ICMP icmp = (ICMP) pkt;
437 sb.append("\nicmp_type: ");
438 sb.append(icmp.getIcmpType());
439 sb.append("\nicmp_code: ");
440 sb.append(icmp.getIcmpCode());
Ray Milkey269ffb92014-04-03 14:43:30 -0700441 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800442 IPv4 p = (IPv4) pkt;
443 sb.append("\nnw_src: ");
444 sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
445 sb.append("\nnw_dst: ");
446 sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
447 sb.append("\nnw_tos: ");
448 sb.append(p.getDiffServ());
449 sb.append("\nnw_proto: ");
450 sb.append(p.getProtocol());
451
Ray Milkey149693c2014-05-20 14:58:53 -0700452 IPacket payload = pkt.getPayload();
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700453 if (payload instanceof TCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800454 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700455 sb.append(((TCP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800456 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700457 sb.append(((TCP) payload).getDestinationPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800458
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700459 } else if (payload instanceof UDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800460 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700461 sb.append(((UDP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800462 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700463 sb.append(((UDP) payload).getDestinationPort());
464 } else if (payload instanceof ICMP) {
465 ICMP icmp = (ICMP) payload;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800466 sb.append("\nicmp_type: ");
467 sb.append(icmp.getIcmpType());
468 sb.append("\nicmp_code: ");
469 sb.append(icmp.getIcmpCode());
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700470 } else {
471 sb.append("\nunknown IPv4 packet");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800472 }
473
Ray Milkey269ffb92014-04-03 14:43:30 -0700474 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800475 sb.append("\ndhcp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 } else if (pkt instanceof Data) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800477 sb.append("\ndata packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700478 } else if (pkt instanceof LLC) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800479 sb.append("\nllc packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700480 } else if (pkt instanceof BPDU) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800481 sb.append("\nbpdu packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700482 } else {
Ray Milkeyb41100a2014-04-10 10:42:15 -0700483 sb.append("\nunknown packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700484 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800485
486 return sb.toString();
487 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800488}