added initial pcap exporter for 6lowpan analyzer

This commit is contained in:
joxe 2010-11-15 12:00:54 +00:00
parent f4c1953563
commit c9a109dbc8
4 changed files with 101 additions and 14 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: RadioLogger.java,v 1.39 2010/10/13 11:31:10 fros4943 Exp $
* $Id: RadioLogger.java,v 1.40 2010/11/15 12:00:54 joxe Exp $
*/
package se.sics.cooja.plugins;
@ -140,9 +140,14 @@ public class RadioLogger extends VisPlugin {
radioMedium = simulation.getRadioMedium();
ArrayList<PacketAnalyzer> lowpanAnalyzers = new ArrayList<PacketAnalyzer>();
lowpanAnalyzers.add(new IEEE802154Analyzer());
lowpanAnalyzers.add(new IEEE802154Analyzer(false));
lowpanAnalyzers.add(new IPHCPacketAnalyzer());
lowpanAnalyzers.add(new ICMPv6Analyzer());
ArrayList<PacketAnalyzer> lowpanAnalyzersPcap = new ArrayList<PacketAnalyzer>();
lowpanAnalyzersPcap.add(new IEEE802154Analyzer(true));
lowpanAnalyzersPcap.add(new IPHCPacketAnalyzer());
lowpanAnalyzersPcap.add(new ICMPv6Analyzer());
model = new AbstractTableModel() {
private static final long serialVersionUID = 1692207305977527004L;
@ -333,6 +338,12 @@ public class RadioLogger extends VisPlugin {
group.add(rbMenuItem);
popupMenu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem(createAnalyzerAction(
"6LoWPAN Analyzer with PCAP", "6lowpan-pcap", lowpanAnalyzersPcap, false));
group.add(rbMenuItem);
popupMenu.add(rbMenuItem);
/* Load additional analyzers specified by projects (cooja.config) */
String[] projectAnalyzerSuites =
gui.getProjectConfig().getStringArrayValue(RadioLogger.class, "ANALYZERS");

View File

@ -14,9 +14,10 @@ public class ICMPv6Analyzer extends PacketAnalyzer {
public static final int NEIGHBOR_SOLICITATION = 135;
public static final int NEIGHBOR_ADVERTISEMENT = 136;
public static final int RPL_CODE_DIS = 1; /* DIS message */
public static final int RPL_CODE_DIO = 2; /* DIO message */
public static final int RPL_CODE_DAO = 4;/* DAO message */
public static final int RPL_CODE_DIS = 0; /* DIS message */
public static final int RPL_CODE_DIO = 1; /* DIO message */
public static final int RPL_CODE_DAO = 2;/* DAO message */
public static final int RPL_CODE_DAO_ACK = 3;/* DAO ACK message */
public static final int FLAG_ROUTER = 0x80;
public static final int FLAG_SOLICITED = 0x40;
@ -66,6 +67,10 @@ public class ICMPv6Analyzer extends PacketAnalyzer {
brief.append("DAO");
verbose.append("DAO");
break;
case RPL_CODE_DAO_ACK:
brief.append("DAO ACK");
verbose.append("DAO ACK");
break;
default:
brief.append(code);
verbose.append(code);
@ -81,5 +86,4 @@ public class ICMPv6Analyzer extends PacketAnalyzer {
public boolean matchPacket(Packet packet) {
return packet.level == NETWORK_LEVEL && packet.lastDispatch == ICMPv6_DISPATCH;
}
}

View File

@ -1,5 +1,7 @@
package se.sics.cooja.plugins.analyzers;
import java.io.IOException;
import se.sics.cooja.util.StringUtils;
public class IEEE802154Analyzer extends PacketAnalyzer {
@ -19,22 +21,25 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
private static final String[] typeS = {"-", "D", "A"};
private static final String[] typeVerbose = {"BEACON", "DATA", "ACK"};
private PcapExporter pcapExporter;
// private int defaultAddressMode = LONG_ADDRESS;
// private byte seqNo = 0;
// private int myPanID = 0xabcd;
public IEEE802154Analyzer(boolean pcap) {
if (pcap) try {
pcapExporter = new PcapExporter();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean matchPacket(Packet packet) {
return packet.level == MAC_LEVEL;
}
/* we need better model of this later... */
public boolean matchPacket(byte[] packet, int level) {
return false;
}
/* this protocol always have network level packets as payload */
public int nextLevel(byte[] packet, int level) {
return NETWORK_LEVEL;
@ -43,6 +48,11 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
* next handler
*/
public int analyzePacket(Packet packet, StringBuffer brief, StringBuffer verbose) {
if (pcapExporter != null) {
pcapExporter.exportPacketData(packet.getPayload());
}
int pos = packet.pos;
int type = packet.data[pos + 0] & 7;
// int security = (packet.data[pos + 0] >> 3) & 1;
@ -102,14 +112,15 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
brief.append("15.4 ");
brief.append(type < typeS.length ? typeS[type] : "?").append(' ');
printAddress(brief, srcAddrMode, sourceAddress);
brief.append(' ');
printAddress(brief, destAddrMode, destAddress);
verbose.append("<html><b>IEEE 802.15.4 ")
.append(type < typeVerbose.length ? typeVerbose[type] : "?")
.append(' ').append(seqNumber);
if (type != ACKFRAME) {
printAddress(brief, srcAddrMode, sourceAddress);
brief.append(' ');
printAddress(brief, destAddrMode, destAddress);
verbose.append("</b><br>From ");
if (srcPanID != 0) {
verbose.append(StringUtils.toHex((byte)(srcPanID >> 8)))
@ -124,6 +135,9 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
.append('/');
}
printAddress(verbose, destAddrMode, destAddress);
} else {
/* got ack - no more to do ... */
return ANALYSIS_OK_FINAL;
}
/* update packet */

View File

@ -0,0 +1,58 @@
package se.sics.cooja.plugins.analyzers;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class PcapExporter {
private static final byte[] ETH_DATA = {(byte)0xaf, (byte)0xab, (byte)0xac, (byte)0xad,
(byte)0xae, (byte)0xaf, 0x42, (byte)0xfb, (byte)0x9f, (byte)0x81, 0x5a,
(byte)0x81, (byte)0x80, (byte)0x9a};
DataOutputStream out;
public PcapExporter() throws IOException {
}
public void openPcap() throws IOException {
out = new DataOutputStream(new FileOutputStream("radiolog-" + System.currentTimeMillis() + ".pcap"));
/* pcap header */
out.writeInt(0xa1b2c3d4);
out.writeShort(0x0002);
out.writeShort(0x0004);
out.writeInt(0);
out.writeInt(0);
out.writeInt(4096);
out.writeInt(1); /* 1 for ethernet ? */
out.flush();
System.out.println("Opened pcap file!");
}
public void closePcap() throws IOException {
out.close();
}
public void exportPacketData(byte[] data) throws IOException {
if (out == null) {
openPcap();
}
try {
/* pcap packet header */
out.writeInt((int) System.currentTimeMillis() / 1000);
out.writeInt((int) ((System.currentTimeMillis() % 1000) * 1000));
out.writeInt(data.length + 14);
out.writeInt(data.length + 14);
out.write(ETH_DATA);
/* and the data */
out.write(data);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}