blob: 1eb6d69a6743994f23d2b1d3b3fd353aa05ccdae [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);
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
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -0700206 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800207 public byte[] serialize() {
208 byte[] payloadData = null;
209 if (payload != null) {
210 payload.setParent(this);
211 payloadData = payload.serialize();
212 }
213 int length = 14 + ((vlanID == VLAN_UNTAGGED) ? 0 : 4) +
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 ((payloadData == null) ? 0 : payloadData.length);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800215 if (pad && length < 60) {
216 length = 60;
217 }
218 byte[] data = new byte[length];
219 ByteBuffer bb = ByteBuffer.wrap(data);
220 bb.put(destinationMACAddress.toBytes());
221 bb.put(sourceMACAddress.toBytes());
222 if (vlanID != VLAN_UNTAGGED) {
223 bb.putShort((short) 0x8100);
224 bb.putShort((short) ((priorityCode << 13) | (vlanID & 0x0fff)));
225 }
226 bb.putShort(etherType);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700227 if (payloadData != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800228 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700229 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800230 if (pad) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700231 Arrays.fill(data, bb.position(), data.length, (byte) 0x0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800232 }
233 return data;
234 }
235
236 @Override
237 public IPacket deserialize(byte[] data, int offset, int length) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700238 if (length <= 0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800239 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700240 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800241 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700242 if (this.destinationMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800243 this.destinationMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700244 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800245 byte[] dstAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
246 bb.get(dstAddr);
247 this.destinationMACAddress = MACAddress.valueOf(dstAddr);
248
Ray Milkeyb29e6262014-04-09 16:02:14 -0700249 if (this.sourceMACAddress == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800250 this.sourceMACAddress = MACAddress.valueOf(new byte[6]);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700251 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800252 byte[] srcAddr = new byte[MACAddress.MAC_ADDRESS_LENGTH];
253 bb.get(srcAddr);
254 this.sourceMACAddress = MACAddress.valueOf(srcAddr);
255
256 short etherType = bb.getShort();
257 if (etherType == (short) 0x8100) {
258 short tci = bb.getShort();
259 this.priorityCode = (byte) ((tci >> 13) & 0x07);
260 this.vlanID = (short) (tci & 0x0fff);
261 etherType = bb.getShort();
262 } else {
263 this.vlanID = VLAN_UNTAGGED;
264 }
265 this.etherType = etherType;
Ray Milkey269ffb92014-04-03 14:43:30 -0700266
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800267 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700268 if (Ethernet.ETHER_TYPE_CLASS_MAP.containsKey(this.etherType)) {
269 Class<? extends IPacket> clazz = Ethernet.ETHER_TYPE_CLASS_MAP.get(this.etherType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800270 try {
271 payload = clazz.newInstance();
272 } catch (Exception e) {
273 throw new RuntimeException("Error parsing payload for Ethernet packet", e);
274 }
275 } else {
276 payload = new Data();
277 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800279 this.payload.setParent(this);
280 return this;
281 }
282
283 /**
284 * Checks to see if a string is a valid MAC address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700285 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700286 * @param macAddress MAC address to be checked
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800287 * @return True if macAddress is a valid MAC, False otherwise
288 */
289 public static boolean isMACAddress(String macAddress) {
290 String[] macBytes = macAddress.split(":");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700291 if (macBytes.length != 6) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800292 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700293 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800294 for (int i = 0; i < 6; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700295 if (HEXES.indexOf(macBytes[i].toUpperCase().charAt(0)) == -1 ||
296 HEXES.indexOf(macBytes[i].toUpperCase().charAt(1)) == -1) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800297 return false;
298 }
299 }
300 return true;
301 }
302
303 /**
304 * Accepts a MAC address of the form 00:aa:11:bb:22:cc, case does not
305 * matter, and returns a corresponding byte[].
Ray Milkey269ffb92014-04-03 14:43:30 -0700306 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800307 * @param macAddress The MAC address to convert into a bye array
Ray Milkey269ffb92014-04-03 14:43:30 -0700308 * @return The macAddress as a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800309 */
310 public static byte[] toMACAddress(String macAddress) {
311 return MACAddress.valueOf(macAddress).toBytes();
312 }
313
314
315 /**
316 * Accepts a MAC address and returns the corresponding long, where the
317 * MAC bytes are set on the lower order bytes of the long.
Ray Milkey269ffb92014-04-03 14:43:30 -0700318 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700319 * @param macAddress MAC address to be converted to long value
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800320 * @return a long containing the mac address bytes
321 */
322 public static long toLong(byte[] macAddress) {
323 return MACAddress.valueOf(macAddress).toLong();
324 }
325
326 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700327 * Convert a long MAC address to a byte array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700328 *
Sho SHIMIZU69474452014-06-20 09:17:40 -0700329 * @param macAddress MAC address to be converted to a byte array
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800330 * @return the bytes of the mac address
331 */
332 public static byte[] toByteArray(long macAddress) {
333 return MACAddress.valueOf(macAddress).toBytes();
334 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700335
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800336 /* (non-Javadoc)
337 * @see java.lang.Object#hashCode()
338 */
339 @Override
340 public int hashCode() {
341 final int prime = 7867;
342 int result = super.hashCode();
343 result = prime * result + destinationMACAddress.hashCode();
344 result = prime * result + etherType;
345 result = prime * result + vlanID;
346 result = prime * result + priorityCode;
347 result = prime * result + (pad ? 1231 : 1237);
348 result = prime * result + sourceMACAddress.hashCode();
349 return result;
350 }
351
352 /* (non-Javadoc)
353 * @see java.lang.Object#equals(java.lang.Object)
354 */
355 @Override
356 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700357 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800358 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700359 }
360 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800361 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700362 }
363 if (!(obj instanceof Ethernet)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800364 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700365 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800366 Ethernet other = (Ethernet) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700367 if (!destinationMACAddress.equals(other.destinationMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800368 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700369 }
370 if (priorityCode != other.priorityCode) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800371 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700372 }
373 if (vlanID != other.vlanID) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800374 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700375 }
376 if (etherType != other.etherType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800377 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700378 }
379 if (pad != other.pad) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800380 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700381 }
382 if (!sourceMACAddress.equals(other.sourceMACAddress)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800383 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700384 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800385 return true;
386 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700387
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800388 /* (non-Javadoc)
389 * @see java.lang.Object#toString(java.lang.Object)
390 */
391 @Override
392 public String toString() {
393
394 StringBuffer sb = new StringBuffer("\n");
395
Ray Milkey149693c2014-05-20 14:58:53 -0700396 IPacket pkt = this.getPayload();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800397
Ray Milkeyb29e6262014-04-09 16:02:14 -0700398 if (pkt instanceof ARP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800399 sb.append("arp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700400 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800401 sb.append("lldp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700402 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800403 sb.append("icmp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700404 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800405 sb.append("ip");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700406 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800407 sb.append("dhcp");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700408 } else {
409 sb.append(this.getEtherType());
410 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800411
412 sb.append("\ndl_vlan: ");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700413 if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800414 sb.append("untagged");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700415 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800416 sb.append(this.getVlanID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700417 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800418 sb.append("\ndl_vlan_pcp: ");
419 sb.append(this.getPriorityCode());
420 sb.append("\ndl_src: ");
421 sb.append(HexString.toHexString(this.getSourceMACAddress()));
422 sb.append("\ndl_dst: ");
423 sb.append(HexString.toHexString(this.getDestinationMACAddress()));
424
425
426 if (pkt instanceof ARP) {
427 ARP p = (ARP) pkt;
428 sb.append("\nnw_src: ");
429 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getSenderProtocolAddress())));
430 sb.append("\nnw_dst: ");
431 sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getTargetProtocolAddress())));
Ray Milkey269ffb92014-04-03 14:43:30 -0700432 } else if (pkt instanceof LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800433 sb.append("lldp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700434 } else if (pkt instanceof ICMP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800435 ICMP icmp = (ICMP) pkt;
436 sb.append("\nicmp_type: ");
437 sb.append(icmp.getIcmpType());
438 sb.append("\nicmp_code: ");
439 sb.append(icmp.getIcmpCode());
Ray Milkey269ffb92014-04-03 14:43:30 -0700440 } else if (pkt instanceof IPv4) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800441 IPv4 p = (IPv4) pkt;
442 sb.append("\nnw_src: ");
443 sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
444 sb.append("\nnw_dst: ");
445 sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
446 sb.append("\nnw_tos: ");
447 sb.append(p.getDiffServ());
448 sb.append("\nnw_proto: ");
449 sb.append(p.getProtocol());
450
Ray Milkey149693c2014-05-20 14:58:53 -0700451 IPacket payload = pkt.getPayload();
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700452 if (payload instanceof TCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800453 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700454 sb.append(((TCP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800455 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700456 sb.append(((TCP) payload).getDestinationPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800457
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700458 } else if (payload instanceof UDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800459 sb.append("\ntp_src: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700460 sb.append(((UDP) payload).getSourcePort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800461 sb.append("\ntp_dst: ");
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700462 sb.append(((UDP) payload).getDestinationPort());
463 } else if (payload instanceof ICMP) {
464 ICMP icmp = (ICMP) payload;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800465 sb.append("\nicmp_type: ");
466 sb.append(icmp.getIcmpType());
467 sb.append("\nicmp_code: ");
468 sb.append(icmp.getIcmpCode());
Pavlin Radoslavovebc70512014-04-09 18:19:19 -0700469 } else {
470 sb.append("\nunknown IPv4 packet");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800471 }
472
Ray Milkey269ffb92014-04-03 14:43:30 -0700473 } else if (pkt instanceof DHCP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800474 sb.append("\ndhcp packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700475 } else if (pkt instanceof Data) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800476 sb.append("\ndata packet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700477 } else if (pkt instanceof LLC) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800478 sb.append("\nllc packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700479 } else {
Ray Milkeyb41100a2014-04-10 10:42:15 -0700480 sb.append("\nunknown packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700481 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800482
483 return sb.toString();
484 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800485}