Skip to content
Snippets Groups Projects
Commit c87ae730 authored by Shudong Zhou's avatar Shudong Zhou
Browse files

Mechanical change to use PacketParsingException

parent c9cfdee1
No related branches found
No related tags found
No related merge requests found
......@@ -96,7 +96,8 @@ public class BSN extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
int magic = bb.getInt();
......
......@@ -125,7 +125,8 @@ public class BSNPROBE extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
controllerId = bb.getLong();
......
......@@ -113,7 +113,12 @@ public abstract class BasePacket implements IPacket {
// cloning. Not the most efficient way but simple. We can revisit
// if we hit performance problems.
byte[] data = this.serialize();
pkt.deserialize(this.serialize(), 0, data.length);
try {
pkt.deserialize(this.serialize(), 0, data.length);
} catch (PacketParsingException e) {
// This shouldn't happen here, since we already deserialized it once
return new Data(data);
}
pkt.setParent(this.parent);
return pkt;
}
......
......@@ -262,7 +262,7 @@ public class Ethernet extends BasePacket {
try {
payload = clazz.newInstance();
this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
} catch (Exception e) {
} catch (PacketParsingException e) {
if (log.isTraceEnabled()) {
log.trace("Failed to parse ethernet packet {}->{}" +
" payload as {}, treat as plain ethernet packet",
......@@ -271,12 +271,27 @@ public class Ethernet extends BasePacket {
clazz.getClass().getName()});
log.trace("Exception from parsing {}", e);
}
payload = new Data();
this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
this.payload = new Data(data);
} catch (InstantiationException e) {
if (log.isTraceEnabled()) {
log.trace("Fail to instantiate class {}, {}",
clazz.getClass().getName(), e);
}
this.payload = new Data(data);
} catch (IllegalAccessException e) {
if (log.isTraceEnabled()) {
log.trace("Fail to access class for instantiation {}, {}",
clazz.getClass().getName(), e);
}
this.payload = new Data(data);
} catch (RuntimeException e) {
if (log.isTraceEnabled()) {
log.trace("Runtime exception during packet parsing {}", e);
}
this.payload = new Data(data);
}
} else {
payload = new Data();
this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
this.payload = new Data(data);
}
this.payload.setParent(this);
return this;
......
......@@ -184,7 +184,8 @@ public class ICMP extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
this.icmpType = bb.get();
this.icmpCode = bb.get();
......
......@@ -67,7 +67,8 @@ public interface IPacket {
* @param length length of the data to deserialize
* @return the deserialized data
*/
public IPacket deserialize(byte[] data, int offset, int length);
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException;
/** Clone this packet and its payload packet but not its parent.
*
......
......@@ -353,7 +353,8 @@ public class IPv4 extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
short sscratch;
......
......@@ -258,7 +258,8 @@ public class TCP extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
this.sourcePort = bb.getShort();
this.destinationPort = bb.getShort();
......
......@@ -202,7 +202,8 @@ public class UDP extends BasePacket {
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
this.sourcePort = bb.getShort();
this.destinationPort = bb.getShort();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment