Changed processing nftables from three lists to one list of list of list.

This commit is contained in:
WickedJack99
2023-12-30 17:34:30 +01:00
parent 1c2559aa6d
commit 5989d4d19c
4 changed files with 140 additions and 24 deletions

View File

@@ -80,9 +80,8 @@ public final class DataProcessorThread extends Thread {
model.getNetworkModel().accessConnections(AccessModifier.WriteEntry, conInf);
}break;
case NFTablesConfiguration: {
NFTablesProcessor.processDataString(data);
//NFTableInformation nftConfig =
//System.out.println(data);
NFTableInformation nftConf = NFTablesProcessor.processDataString(data);
model.getNFTablesModel().accessNFTConfiguration(AccessModifier.WriteEntry, nftConf);
}break;
case Undefined: // intended fall through
default: {

View File

@@ -5,22 +5,50 @@
package controller.src.DataProcessing.nftablesProcessing;
import java.util.LinkedList;
import java.util.List;
public class NFTableInformation {
private List<String> tables = new LinkedList<String>();
private List<String> chains = new LinkedList<String>();
private List<String> rules = new LinkedList<String>();
// Outer list: tables
// Middle lists: chains
// Inner lists: rules
private LinkedList<LinkedList<LinkedList<String>>> tables;
public List<String> getTables() {
public NFTableInformation() {}
public NFTableInformation(NFTableInformation nftInf) {
this.tables = new LinkedList<LinkedList<LinkedList<String>>>(nftInf.tables);
}
// list of list of list<string>
// -> table1
// -> chain1
// -> tablename
// -> chainname
// -> rule1
// -> rule2
// -> table2
/*
for (LinkedList<LinkedList<String>> table : tables) {
for (LinkedList<String> chain : table) {
for (String rule : chain) {
if (rule.startsWith("{\"table")) {
System.out.println(rule);
} else if (rule.startsWith("{\"chain")) {
System.out.println("\t" + rule);
} else {
System.out.println("\t\t" + rule);
}
}
}
}
*/
public LinkedList<LinkedList<LinkedList<String>>>getTables() {
return tables;
}
public List<String> getChains() {
return chains;
}
public List<String> getRules() {
return rules;
public void setTables(LinkedList<LinkedList<LinkedList<String>>> tables) {
this.tables = tables;
}
}

View File

@@ -1,9 +1,12 @@
/**
* @author Aaron Moser
* @date 29.12.2023
* @lastChange 30.12.2023
*/
package controller.src.DataProcessing.nftablesProcessing;
import java.util.LinkedList;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
@@ -18,38 +21,113 @@ public final class NFTablesProcessor {
private static final String RULE_FIELD = "rule";
private static final String NAME_FIELD = "name";
private enum FieldType {
TABLE,
CHAIN,
RULE,
NONE
}
public static NFTableInformation processDataString(String data) {
NFTableInformation nfTableInformation = new NFTableInformation();
ObjectMapper objectMapper = new ObjectMapper();
LinkedList<LinkedList<LinkedList<String>>> tables = new LinkedList<LinkedList<LinkedList<String>>>();
LinkedList<LinkedList<String>> tableChains = null;
LinkedList<String> tableChainRules = null;
try {
JsonNode nfttree = objectMapper.readTree(data);
JsonNode mainRoot = getField(nfttree, NFT_MAIN_FIELD);
String lastTable = null;
FieldType lastNodeType = FieldType.NONE;
for (JsonNode nftObject : mainRoot) {
// If next node is of type table
if (isTableEntry(nftObject)) {
nfTableInformation.getTables().add(nftObject.toString());
switch (lastNodeType) {
// Last node was of type table. Normally chain cares
// about storing the table entry at the beginning of
// the chains rules list but since table is empty,
// the next iteration has to care about storing the
// table at the list.
case FieldType.TABLE: {
tableChainRules = new LinkedList<String>();
tableChainRules.add(lastTable);
tableChains = new LinkedList<LinkedList<String>>();
tableChains.add(new LinkedList<String>(tableChainRules));
tables.add(new LinkedList<LinkedList<String>>(tableChains));
} // Intended fall through
case FieldType.CHAIN: // Intended fall through
case FieldType.RULE: // Intended fall through
case FieldType.NONE: // Intended fall through
default: {
// Reset all lists except tables list
tableChainRules = new LinkedList<String>();
tableChains = new LinkedList<LinkedList<String>>();
}
}
lastNodeType = FieldType.TABLE;
// Store current table description entry
lastTable = nftObject.toString();
// If next node is chain, the node before must have been of type table, chain or rule
} else if (isChainEntry(nftObject)) {
nfTableInformation.getChains().add(nftObject.toString());
switch (lastNodeType) {
case FieldType.TABLE: {
// Add table and chain to new table chain rules list
tableChainRules.add(lastTable);
tableChainRules.add(nftObject.toString());
}break;
case FieldType.CHAIN: // Intended fall through
case FieldType.RULE: {
// Add rules of last chain to chain and reset list
tableChains.add(new LinkedList<String>(tableChainRules));
tableChainRules = new LinkedList<String>();
// Then add this chains description to list
tableChainRules.add(nftObject.toString());
}break;
case FieldType.NONE: // Intended fall through
default: {
System.out.println("Error, missing table before chain.");
}break;
}
lastNodeType = FieldType.CHAIN;
} else if (isRuleEntry(nftObject)) {
nfTableInformation.getChains().add(nftObject.toString());
switch (lastNodeType) {
case FieldType.CHAIN: // Intended fall through
case FieldType.RULE: {
tableChainRules.add(nftObject.toString());
}break;
case FieldType.TABLE:// Intended fall through
case FieldType.NONE: // Intended fall through
default: {
System.out.println("Error, missing chain before rule.");
}break;
}
lastNodeType = FieldType.RULE;
} else {
// Do nothing
}
}
if (tableChains != null && !tableChains.isEmpty()) {
tables.add(new LinkedList<LinkedList<String>>(tableChains));
}
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
nfTableInformation.setTables(tables);
return nfTableInformation;
}
private static boolean isTableEntry(JsonNode node) {
return isSpecifiedEntry(node, TABLE_FIELD);
}
private static boolean isChainEntry(JsonNode node) {

View File

@@ -5,17 +5,28 @@
package model.src;
import controller.src.DataProcessing.nftablesProcessing.NFTableInformation;
import model.src.ModelConstants.AccessModifier;
public class NFTablesModel {
public final class NFTablesModel {
private NFTableInformation nfTableInformation = null;
public NFTablesModel() {}
public void setNFTableInformation(NFTableInformation newInformation) {
this.nfTableInformation = newInformation;
}
public NFTableInformation getNFTableInformation() {
return this.nfTableInformation;
public synchronized NFTableInformation accessNFTConfiguration(AccessModifier accessModifier, NFTableInformation nftInf) {
switch (accessModifier) {
case ReadAllEntries: {
return new NFTableInformation(nfTableInformation);
}//break;
case WriteEntry: {
this.nfTableInformation = new NFTableInformation(nftInf);
}break;
case DeleteAllEntries: {
this.nfTableInformation = new NFTableInformation();
}break;
case DeleteEntry: // Intended fall through
default: {
}break;
}
return null;
}
}