Implement the missing queries

This commit is contained in:
Jan Philipp Timme 2016-12-13 12:00:36 +01:00
parent 687df96022
commit cdb36b39d2
Signed by untrusted user: JPT
GPG Key ID: 5F2C85EC6F3754B7
5 changed files with 57 additions and 34 deletions

View File

@ -4,9 +4,11 @@ import java.util.List;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import de.hsh.inform.orientdb_project.model.EthernetFrameModel;
import de.hsh.inform.orientdb_project.model.HostModel;
import de.hsh.inform.orientdb_project.model.TcpConnectionModel;
import de.hsh.inform.orientdb_project.orientdb.OrientDbHelperService;
import de.hsh.inform.orientdb_project.repository.EthernetFrameRepository;
import de.hsh.inform.orientdb_project.repository.HostRepository;
import de.hsh.inform.orientdb_project.repository.TcpConnectionRepository;
import de.hsh.inform.orientdb_project.util.ConfigPropertiesReader;
@ -36,6 +38,18 @@ public class Main {
System.out.println(hm);
}
for(HostModel hm : hr.findAllByConnectionsToOutsideHosts()) {
System.out.println(hm);
}
EthernetFrameRepository efr = new EthernetFrameRepository(odhs.getDatabaseDocument());
List<EthernetFrameModel> efrbyteResult = efr.findAllByRawData(new byte[] {
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
});
for(EthernetFrameModel em : efrbyteResult) {
System.out.println(em);
}
// Done
odhs.close();
}

View File

@ -3,6 +3,7 @@ package de.hsh.inform.orientdb_project.model;
import org.pcap4j.packet.EthernetPacket;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
@ -47,14 +48,14 @@ public class EthernetFrameModel {
this.microseconds = ms;
}
public EthernetFrameModel(Vertex v) {
this.ts = v.getProperty("timestamp");
this.ms = v.getProperty("microseconds");
this.sourceMac = v.getProperty("sourceMac");
this.targetMac = v.getProperty("targetMac");
this.rawData = v.getProperty("rawData");
this.size = v.getProperty("size");
this.payloadSize = v.getProperty("payloadSize");
public EthernetFrameModel(ODocument doc) {
this.ts = doc.field("timestamp");
this.ms = doc.field("microseconds");
this.sourceMac = doc.field("sourceMac");
this.targetMac = doc.field("targetMac");
this.rawData = doc.field("rawData");
this.size = doc.field("size");
this.payloadSize = doc.field("payloadSize");
this.timestamp = ts;
this.microseconds = ms;
}

View File

@ -1,9 +1,9 @@
package de.hsh.inform.orientdb_project.orientdb;
import java.io.IOException;
import java.util.List;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.intent.OIntentMassiveInsert;
import com.tinkerpop.blueprints.impls.orient.OrientConfigurableGraph.THREAD_MODE;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
@ -38,6 +38,10 @@ public class OrientDbHelperService {
this.factory = null;
}
public ODatabaseDocumentTx getDatabaseDocument() {
return this.factory.getDatabase();
}
public OrientGraphFactory getOrientGraphFactory() {
if(this.factory == null) {

View File

@ -3,30 +3,32 @@ package de.hsh.inform.orientdb_project.repository;
import java.util.ArrayList;
import java.util.List;
import com.tinkerpop.blueprints.GraphQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.iterator.ORecordIteratorClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
import de.hsh.inform.orientdb_project.model.EthernetFrameModel;
public class EthernetFrameRepository {
private OrientGraphNoTx ogf;
private ODatabaseDocumentTx db;
public EthernetFrameRepository(OrientGraphNoTx ogf) {
this.ogf = ogf;
public EthernetFrameRepository(ODatabaseDocumentTx oDatabaseDocumentTx) {
this.db = oDatabaseDocumentTx;
}
public List<EthernetFrameModel> findAllByRawData(byte[] content) {
GraphQuery gq = this.ogf.query();
gq = gq.has("@class", "EthernetFrame");
// TODO
return this.getListFromVertices(gq.vertices());
}
private List<EthernetFrameModel> getListFromVertices(Iterable<Vertex> vertices) {
public List<EthernetFrameModel> findAllByRawData(byte[] needle) {
ORecordIteratorClass<ODocument> resultIterator = db.browseClass("EthernetFrame");
List<EthernetFrameModel> result = new ArrayList<EthernetFrameModel>();
for(Vertex v : vertices) {
result.add(new EthernetFrameModel(v));
for(ODocument doc : resultIterator) {
int found = -1;
byte[] rawData = (byte[]) doc.field("rawData");
// Manually compare bytes... yay! \o/
String bigStr = new String(rawData);
String smallStr = new String(needle);
found = bigStr.indexOf(smallStr);
if(found != -1) {
result.add(new EthernetFrameModel(doc));
}
}
return result;
}

View File

@ -29,18 +29,20 @@ public class HostRepository {
}
public List<HostModel> findAllByConnectionsToOutsideHosts() {
GraphQuery gq = this.ogf.query();
gq = gq.has("@class", "Host");
// TODO
return this.getListFromVertices(gq.vertices());
String sql = "" +
"SELECT EXPAND(DISTINCT(out)) FROM (SELECT out('hasSourceHost') AS out FROM TcpConnection WHERE out('hasTargetHost').internal = false);";
@SuppressWarnings("unchecked") // We know.
Iterable<Vertex> vertices = (Iterable<Vertex>) this.ogf.command(new OCommandSQL(sql)).execute();
return this.getListFromVertices(vertices);
}
public List<HostModel> findByIncomingConnectionOnPort(int port) {
GraphQuery gq = this.ogf.query();
gq = gq.has("@class", "Host");
// TODO
return this.getListFromVertices(gq.vertices());
public List<HostModel> findAllByIncomingConnectionOnWellKnownPort() {
String sql = "" +
"SELECT EXPAND(DISTINCT(out)) FROM (SELECT out('hasTargetHost') FROM TcpConnection WHERE targetPort IN (SELECT port FROM WellKnownPort))";
@SuppressWarnings("unchecked") // We know.
Iterable<Vertex> vertices = (Iterable<Vertex>) this.ogf.command(new OCommandSQL(sql)).execute();
return this.getListFromVertices(vertices);
}
private List<HostModel> getListFromVertices(Iterable<Vertex> vertices) {