Prechádzať zdrojové kódy

Adds PacketType Attribute

Andreas T. Meyer-Berg 3 rokov pred
rodič
commit
a445a49a95

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Packet.java

@@ -116,4 +116,12 @@ public abstract class Packet {
 	public void setLabel(short label) {
 		this.label = label;
 	}
+	
+	/**
+	 * Returns more specific Package type, or default if not implemented
+	 * @return PackageType
+	 */
+	public String getPackageType() {
+		return "default";
+	}
 }

+ 263 - 210
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/packets/MQTT_packet.java

@@ -1,210 +1,263 @@
-package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
-
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
-
-/**
- * Packet generated by the MQTT Protocol
- * 
- *
- * @author Andreas T. Meyer-Berg
- */
-public class MQTT_packet extends Packet {
-
-	/*
-	 * List of the different MQTT packet types, which are the bits 7 to 4 of the
-	 * first byte of the packet as specified in MQTT Specification 2.2.1 (MQTT
-	 * Control Packet type)<br> The first 4 bits contain the values 0 to F.
-	 */
-	/*
-	 * The second part of the first byte are the Flag Bits. In most control
-	 * packets they are 0. The Publish Packet contains DUP, QoS, QoS, RETAIN
-	 * bits. PUBREL, SUBSCRIBE and UNSUBSCRIBE contain 0x2 as lower nibble. See
-	 * MQTT Section 2.2.2 for further infos.
-	 */
-
-	/**
-	 * Reserved (forbidden)
-	 */
-	public static final byte RESERVED = (byte) 0x00;
-	/**
-	 * Client request to connect to Server (Client-{@literal >}Server)
-	 */
-	public static final byte CONNECT = (byte) 0x10;
-	/**
-	 * Connect acknowledgement (Client{@literal <}-Server)
-	 */
-	public static final byte CONNACK = (byte) 0x20;
-	/**
-	 * Publish message (Client{@literal <}-{@literal >}Server)
-	 */
-	public static final byte PUBLISH = (byte) 0x30;
-	/**
-	 * Publish acknowledgement (Client{@literal <}-{@literal >}Server)
-	 */
-	public static final byte PUBACK = (byte) 0x40;
-	/**
-	 * Publish received (assured delivery part 1) (Client{@literal <}-{@literal >}Server)
-	 */
-	public static final byte PUBREC = (byte) 0x50;
-	/**
-	 * Publish received (assured delivery part 2) (Client{@literal <}-{@literal >}Server)
-	 */
-	public static final byte PUBREL = (byte) 0x62;
-	/**
-	 * Publish received (assured delivery part 3) (Client{@literal <}-{@literal >}Server)
-	 */
-	public static final byte PUBCOMP = (byte) 0x70;
-	/**
-	 * Client subscribe request (Client-{@literal >}Server)
-	 */
-	public static final byte SUBSCRIBE = (byte) 0x82;
-	/**
-	 * Subscribe acknowledgement (Client{@literal <}-Server)
-	 */
-	public static final byte SUBACK = (byte) 0x90;
-	/**
-	 * Unsubscribe request (Client-{@literal >}Server)
-	 */
-	public static final byte UNSUBSCRIBE = (byte) 0xA2;
-	/**
-	 * Unsubscribe acknowledgement (Client{@literal <}-Server)
-	 */
-	public static final byte UNSUBACK = (byte) 0xB0;
-	/**
-	 * PING request (Client-{@literal >}Server)
-	 */
-	public static final byte PINGREQ = (byte) 0xC0;
-	/**
-	 * PING response (Client{@literal <}-Server)
-	 */
-	public static final byte PINGRESP = (byte) 0xD0;
-	/**
-	 * Client is disconnecting (Client-{@literal >}Server)
-	 */
-	public static final byte DISCONNECT = (byte) 0xE0;
-	/**
-	 * Reserved - forbidden
-	 */
-	public static final byte RESERVED2 = (byte) 0xF0;
-
-	/*
-	 * Packet Content
-	 */
-	/**
-	 * First byte of the MQTT:Packet
-	 */
-	protected byte controlByte;
-
-	/**
-	 * Payload message
-	 */
-	protected String message = "";
-
-	/**
-	 * Creates a new MQTT Packet
-	 * 
-	 * @param controlType
-	 *            specifies the packetType. one of CONNECT, CONNACK, ...
-	 * @param timestamp
-	 *            time the packet was sent
-	 * @param source
-	 *            source port of the package
-	 * @param destination
-	 *            destination port of the packet
-	 */
-	public MQTT_packet(byte controlType, long timestamp, Port source, Port destination) {
-		super(timestamp, source, destination);
-		controlByte = controlType;
-	}
-	
-	/**
-	 * Creates a new MQTT Packet
-	 * 
-	 * @param controlType
-	 *            specifies the packetType. one of CONNECT, CONNACK, ...
-	 * @param timestamp
-	 *            time the packet was sent
-	 * @param source
-	 *            source port of the package
-	 * @param destination
-	 *            destination port of the packet
-	 * @param msg Message of the packet
-	 */
-	public MQTT_packet(byte controlType, long timestamp, Port source, Port destination, String msg) {
-		super(timestamp, source, destination);
-		controlByte = controlType;
-		message = msg;
-	}
-
-	@Override
-	public byte[] dumpBytes() {
-		return "not implemented".getBytes();
-	}
-
-	@Override
-	public String getTextualRepresentation() {
-		/**
-		 * Control type of the MQTT Packet as String
-		 */
-		String packetType;
-		switch (controlByte) {
-		case CONNECT:
-			packetType="CONNECT";
-			break;
-		case CONNACK:
-			packetType="CONNACK";
-			break;
-		case PUBLISH:
-			packetType="PUBLISH";
-			break;
-		case PUBACK:
-			packetType="PUBACK";
-			break;
-		case PUBREC:
-			packetType="PUBREC";
-			break;
-		case PUBREL:
-			packetType="PUBREL";
-			break;
-		case PUBCOMP:
-			packetType="PUBCOMP";
-			break;
-		case SUBSCRIBE:
-			packetType="SUBSCRIBE";
-			break;
-		case SUBACK:
-			packetType="SUBACK";
-			break;
-		case UNSUBSCRIBE:
-			packetType="UNSUBSCRIBE";
-			break;
-		case UNSUBACK:
-			packetType="UNSUBACK";
-			break;
-		case PINGREQ:
-			packetType="PINGREQ";
-			break;
-		case PINGRESP:
-			packetType="PINGRESP";
-			break;
-		case DISCONNECT:
-			packetType="DISCONNECT";
-			break;
-		default:
-			packetType="RESERVED";
-			break;
-		}
-		return "[MQTT: "+packetType+"; time:"+timestamp+"; source:"+source.getOwner().getName()+":"+source.getPortNumber()+"; destination:"+destination.getOwner().getName()+":"+destination.getPortNumber()+(message==""?"":"; "+message)+"]";
-	}
-
-	@Override
-	public String getPayload() {
-		return "null";
-	}
-
-	@Override
-	public String getProtocolName() {
-		return "MQTT";
-	}
-}
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+
+/**
+ * Packet generated by the MQTT Protocol
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MQTT_packet extends Packet {
+
+	/*
+	 * List of the different MQTT packet types, which are the bits 7 to 4 of the
+	 * first byte of the packet as specified in MQTT Specification 2.2.1 (MQTT
+	 * Control Packet type)<br> The first 4 bits contain the values 0 to F.
+	 */
+	/*
+	 * The second part of the first byte are the Flag Bits. In most control
+	 * packets they are 0. The Publish Packet contains DUP, QoS, QoS, RETAIN
+	 * bits. PUBREL, SUBSCRIBE and UNSUBSCRIBE contain 0x2 as lower nibble. See
+	 * MQTT Section 2.2.2 for further infos.
+	 */
+
+	/**
+	 * Reserved (forbidden)
+	 */
+	public static final byte RESERVED = (byte) 0x00;
+	/**
+	 * Client request to connect to Server (Client-{@literal >}Server)
+	 */
+	public static final byte CONNECT = (byte) 0x10;
+	/**
+	 * Connect acknowledgement (Client{@literal <}-Server)
+	 */
+	public static final byte CONNACK = (byte) 0x20;
+	/**
+	 * Publish message (Client{@literal <}-{@literal >}Server)
+	 */
+	public static final byte PUBLISH = (byte) 0x30;
+	/**
+	 * Publish acknowledgement (Client{@literal <}-{@literal >}Server)
+	 */
+	public static final byte PUBACK = (byte) 0x40;
+	/**
+	 * Publish received (assured delivery part 1) (Client{@literal <}-{@literal >}Server)
+	 */
+	public static final byte PUBREC = (byte) 0x50;
+	/**
+	 * Publish received (assured delivery part 2) (Client{@literal <}-{@literal >}Server)
+	 */
+	public static final byte PUBREL = (byte) 0x62;
+	/**
+	 * Publish received (assured delivery part 3) (Client{@literal <}-{@literal >}Server)
+	 */
+	public static final byte PUBCOMP = (byte) 0x70;
+	/**
+	 * Client subscribe request (Client-{@literal >}Server)
+	 */
+	public static final byte SUBSCRIBE = (byte) 0x82;
+	/**
+	 * Subscribe acknowledgement (Client{@literal <}-Server)
+	 */
+	public static final byte SUBACK = (byte) 0x90;
+	/**
+	 * Unsubscribe request (Client-{@literal >}Server)
+	 */
+	public static final byte UNSUBSCRIBE = (byte) 0xA2;
+	/**
+	 * Unsubscribe acknowledgement (Client{@literal <}-Server)
+	 */
+	public static final byte UNSUBACK = (byte) 0xB0;
+	/**
+	 * PING request (Client-{@literal >}Server)
+	 */
+	public static final byte PINGREQ = (byte) 0xC0;
+	/**
+	 * PING response (Client{@literal <}-Server)
+	 */
+	public static final byte PINGRESP = (byte) 0xD0;
+	/**
+	 * Client is disconnecting (Client-{@literal >}Server)
+	 */
+	public static final byte DISCONNECT = (byte) 0xE0;
+	/**
+	 * Reserved - forbidden
+	 */
+	public static final byte RESERVED2 = (byte) 0xF0;
+
+	/*
+	 * Packet Content
+	 */
+	/**
+	 * First byte of the MQTT:Packet
+	 */
+	protected byte controlByte;
+
+	/**
+	 * Payload message
+	 */
+	protected String message = "";
+
+	/**
+	 * Creates a new MQTT Packet
+	 * 
+	 * @param controlType
+	 *            specifies the packetType. one of CONNECT, CONNACK, ...
+	 * @param timestamp
+	 *            time the packet was sent
+	 * @param source
+	 *            source port of the package
+	 * @param destination
+	 *            destination port of the packet
+	 */
+	public MQTT_packet(byte controlType, long timestamp, Port source, Port destination) {
+		super(timestamp, source, destination);
+		controlByte = controlType;
+	}
+	
+	/**
+	 * Creates a new MQTT Packet
+	 * 
+	 * @param controlType
+	 *            specifies the packetType. one of CONNECT, CONNACK, ...
+	 * @param timestamp
+	 *            time the packet was sent
+	 * @param source
+	 *            source port of the package
+	 * @param destination
+	 *            destination port of the packet
+	 * @param msg Message of the packet
+	 */
+	public MQTT_packet(byte controlType, long timestamp, Port source, Port destination, String msg) {
+		super(timestamp, source, destination);
+		controlByte = controlType;
+		message = msg;
+	}
+
+	@Override
+	public byte[] dumpBytes() {
+		return "not implemented".getBytes();
+	}
+
+	@Override
+	public String getTextualRepresentation() {
+		/**
+		 * Control type of the MQTT Packet as String
+		 */
+		String packetType;
+		switch (controlByte) {
+		case CONNECT:
+			packetType="CONNECT";
+			break;
+		case CONNACK:
+			packetType="CONNACK";
+			break;
+		case PUBLISH:
+			packetType="PUBLISH";
+			break;
+		case PUBACK:
+			packetType="PUBACK";
+			break;
+		case PUBREC:
+			packetType="PUBREC";
+			break;
+		case PUBREL:
+			packetType="PUBREL";
+			break;
+		case PUBCOMP:
+			packetType="PUBCOMP";
+			break;
+		case SUBSCRIBE:
+			packetType="SUBSCRIBE";
+			break;
+		case SUBACK:
+			packetType="SUBACK";
+			break;
+		case UNSUBSCRIBE:
+			packetType="UNSUBSCRIBE";
+			break;
+		case UNSUBACK:
+			packetType="UNSUBACK";
+			break;
+		case PINGREQ:
+			packetType="PINGREQ";
+			break;
+		case PINGRESP:
+			packetType="PINGRESP";
+			break;
+		case DISCONNECT:
+			packetType="DISCONNECT";
+			break;
+		default:
+			packetType="RESERVED";
+			break;
+		}
+		return "[MQTT: "+packetType+"; time:"+timestamp+"; source:"+source.getOwner().getName()+":"+source.getPortNumber()+"; destination:"+destination.getOwner().getName()+":"+destination.getPortNumber()+(message==""?"":"; "+message)+"]";
+	}
+
+	@Override
+	public String getPayload() {
+		return "null";
+	}
+
+	@Override
+	public String getProtocolName() {
+		return "MQTT";
+	}
+	
+	@Override
+	public String getPackageType() {
+		String packetType;
+		switch (controlByte) {
+		case CONNECT:
+			packetType="CONNECT";
+			break;
+		case CONNACK:
+			packetType="CONNACK";
+			break;
+		case PUBLISH:
+			packetType="PUBLISH";
+			break;
+		case PUBACK:
+			packetType="PUBACK";
+			break;
+		case PUBREC:
+			packetType="PUBREC";
+			break;
+		case PUBREL:
+			packetType="PUBREL";
+			break;
+		case PUBCOMP:
+			packetType="PUBCOMP";
+			break;
+		case SUBSCRIBE:
+			packetType="SUBSCRIBE";
+			break;
+		case SUBACK:
+			packetType="SUBACK";
+			break;
+		case UNSUBSCRIBE:
+			packetType="UNSUBSCRIBE";
+			break;
+		case UNSUBACK:
+			packetType="UNSUBACK";
+			break;
+		case PINGREQ:
+			packetType="PINGREQ";
+			break;
+		case PINGRESP:
+			packetType="PINGRESP";
+			break;
+		case DISCONNECT:
+			packetType="DISCONNECT";
+			break;
+		default:
+			packetType="RESERVED";
+			break;
+		}
+		return packetType;
+	}
+}

+ 10 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/BasicPacketClassifier.java

@@ -70,6 +70,11 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	 * Map for the protocol names
 	 */
 	protected HashSet<String> protocol_mappings = new HashSet<String>();
+	
+	/**
+	 * Map for the protocol names
+	 */
+	protected HashSet<String> package_mappings = new HashSet<String>();
 
 	/**
 	 * Number of packets which are used to calculate the current transmission speed
@@ -87,6 +92,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		link_mappings.add("unknown");
 		destination_mappings.add("unknown");
 		protocol_mappings.add("unknown");
+		package_mappings.add("unknown");
 	}
 	
 	@Override
@@ -207,6 +213,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		} else {
 			instance.setValue(6, -100);
 		}
+		instance.setValue(7, stringToNominal(destination_mappings, packet.getPackageType()));
 		return instance;
 	}
 	
@@ -253,6 +260,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 				insertNominalIntoMap(source_mappings, pac.getSource().getOwner().getName());
 				insertNominalIntoMap(source_mappings, pac.getDestination().getOwner().getName());
 				insertNominalIntoMap(protocol_mappings, pac.getProtocolName());
+				insertNominalIntoMap(package_mappings, pac.getPackageType());
 			}
 			//TODO: Add packet/Link/Names etc. to mappings
 		}
@@ -282,6 +290,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		//pps.setWeight(20);
 		//atts.add(pps);
 		atts.add(new Attribute("PacketValue", false));
+		atts.add(new Attribute("PacketType", new LinkedList<String>(protocol_mappings)));
 		//atts.add(new Attribute("Anomaly", false));
 		
 		// TODO: Sensor Attribute, given as side channel information
@@ -390,7 +399,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 				
 				try {
 					double dist = classifyInstance(packet_instance, packet);	
-					if(dist<=Settings.DECISION_THRESHOLD) {
+					if(dist<=Settings.PRECISION_ERROR) {
 						if(packet.getLabel()==0)
 							tn++;
 						else {

+ 10 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/BasicPacketClassifierWitLabels.java

@@ -71,6 +71,11 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 	 */
 	protected HashSet<String> protocol_mappings = new HashSet<String>();
 
+	/**
+	 * Map for the protocol names
+	 */
+	protected HashSet<String> packet_mappings = new HashSet<String>();
+	
 	/**
 	 * Number of packets which are used to calculate the current transmission speed
 	 */
@@ -87,6 +92,7 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 		link_mappings.add("unknown");
 		destination_mappings.add("unknown");
 		protocol_mappings.add("unknown");
+		packet_mappings.add("unknown");
 	}
 	
 	@Override
@@ -217,6 +223,7 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 			instance.setValue(6, -100);
 			instance.setValue(7, -100);
 		}
+		instance.setValue(8, stringToNominal(packet_mappings, packet.getPackageType()));
 		return instance;
 	}
 	
@@ -263,6 +270,7 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 				insertNominalIntoMap(source_mappings, pac.getSource().getOwner().getName());
 				insertNominalIntoMap(source_mappings, pac.getDestination().getOwner().getName());
 				insertNominalIntoMap(protocol_mappings, pac.getProtocolName());
+				insertNominalIntoMap(packet_mappings, pac.getPackageType());
 			}
 			//TODO: Add packet/Link/Names etc. to mappings
 		}
@@ -296,7 +304,7 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 		
 		// TODO: Sensor Attribute, given as side channel information
 		atts.add(new Attribute("SensorValue", false));
-
+		atts.add(new Attribute("PackageType",new LinkedList<String>(packet_mappings)));
 		/*
 		atts = new ArrayList<Attribute>();
 		atts.add(new Attribute("LN", new LinkedList<String>(link_mappings)));//TODO:??
@@ -401,7 +409,7 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 				try {
 					double dist = classifyInstance(packet_instance, packet);
 					//System.out.println(packet.getTextualRepresentation()+": "+packet.getLabel() +":"+sensorLabel);	
-					if(dist<=Settings.DECISION_THRESHOLD) {
+					if(dist<=Settings.PRECISION_ERROR) {
 						if(packet.getLabel()==0) {
 							tn++;
 							writer.write(packet.getTextualRepresentation()+",TN"+sensorLabel+"\n");

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/SWCKMeansClustering.java

@@ -103,7 +103,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 			test = false;
 			System.out.println("");
 		}
-		if(dist < stdv[x] * Settings.ANOMALY_THRESHOLD)
+		if(dist < stdv[x] * Settings.ANOMALY_THRESHOLD + Settings.PRECISION_ERROR)
 			return 0;
 		else
 			return Double.MAX_VALUE;

+ 2 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/Settings.java

@@ -2,9 +2,9 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation;
 
 public class Settings {
 
-	public static int NUM_CLUSTERS = 20;
+	public static int NUM_CLUSTERS = 12;
 	public static double ANOMALY_THRESHOLD = 1;
-	public static double DECISION_THRESHOLD = 1;
+	public static double PRECISION_ERROR = 0.01;
 	
 	public Settings() {
 		// TODO Auto-generated constructor stub