Commit f317ec9d by Dennis Suermann

parent e1c8fe32
This diff is collapsed.
 RelayModel.ConceptChange API documentation

Module RelayModel.ConceptChange

Expand source code
import math
import statistics

def calculate_windows(window_a, window_b):
"""Calculates a change rate of two given windows provided as lists.

The two lists should be of same size.

Args:
window_a (list): Holds the first window
window_b (list): Holds the second window

Returns:
int: A change rate of the both windows
"""
mean_a = statistics.mean(window_a)
mean_b = statistics.mean(window_b)

var_a = statistics.variance(window_a, mean_a)
var_b = statistics.variance(window_b, mean_b)

frac_a = 1.0/len(window_a)
frac_b = 1.0/len(window_b)

divider = math.sqrt(frac_a*var_a + frac_b*var_b)
if divider != 0:
result = mean_a - mean_b
result = result / divider
else:
result = 0

return result

Functions

def calculate_windows(window_a, window_b)

Calculates a change rate of two given windows provided as lists.

The two lists should be of same size.

Args

window_a : list
Holds the first window
window_b : list
Holds the second window

Returns

int
A change rate of the both windows
Expand source code
def calculate_windows(window_a, window_b):
"""Calculates a change rate of two given windows provided as lists.

The two lists should be of same size.

Args:
window_a (list): Holds the first window
window_b (list): Holds the second window

Returns:
int: A change rate of the both windows
"""
mean_a = statistics.mean(window_a)
mean_b = statistics.mean(window_b)

var_a = statistics.variance(window_a, mean_a)
var_b = statistics.variance(window_b, mean_b)

frac_a = 1.0/len(window_a)
frac_b = 1.0/len(window_b)

divider = math.sqrt(frac_a*var_a + frac_b*var_b)
if divider != 0:
result = mean_a - mean_b
result = result / divider
else:
result = 0

return result
\ No newline at end of file
This diff is collapsed.
 RelayModel.KeyGeneration API documentation

Module RelayModel.KeyGeneration

Expand source code
import hashlib
import uuid

def generate_prefix(layer_id):
"""Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key.

Args:
layer_id (RelayLayerId): The RelayLayerId of the RelayLayer that generates the key

Returns:
str: The key prefix as a string
"""
sha1 = hashlib.sha1()
sha1.update(str(layer_id).encode())

return sha1.hexdigest()

def generate_key(layer_id):
"""Generates a key for a given RelayLayerId

Args:
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that generates the key

Returns:
str: The generated key as a string in the format "prefix#uniqueId"
"""
prefix = generate_prefix(layer_id)
base = uuid.uuid4()

key = prefix + "#" + str(base)
return key

def check_key_origin(key: str, layer_id):
"""Checks if the given key contains the given RelayLayerId.

Args:
key (str): Holds the key that should be checked.
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that should be checked.

Returns:
bool: True if prefix of key matches given layer id, False otherwise.
"""
prefix = generate_prefix(layer_id)

if "#" in key:
key_prefix = key.split("#")[0]

if key_prefix == prefix:
return True

return False

Functions

def check_key_origin(key: str, layer_id)

Checks if the given key contains the given RelayLayerId.

Args

key : str
Holds the key that should be checked.
layer_id : RelayLayerId
Holds the RelayLayerId of the RelayLayer that should be checked.

Returns

bool
True if prefix of key matches given layer id, False otherwise.
Expand source code
def check_key_origin(key: str, layer_id):
"""Checks if the given key contains the given RelayLayerId.

Args:
key (str): Holds the key that should be checked.
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that should be checked.

Returns:
bool: True if prefix of key matches given layer id, False otherwise.
"""
prefix = generate_prefix(layer_id)

if "#" in key:
key_prefix = key.split("#")[0]

if key_prefix == prefix:
return True

return False
def generate_key(layer_id)

Generates a key for a given RelayLayerId

Args

layer_id : RelayLayerId
Holds the RelayLayerId of the RelayLayer that generates the key

Returns

str
The generated key as a string in the format "prefix#uniqueId"
Expand source code
def generate_key(layer_id):
"""Generates a key for a given RelayLayerId

Args:
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that generates the key

Returns:
str: The generated key as a string in the format "prefix#uniqueId"
"""
prefix = generate_prefix(layer_id)
base = uuid.uuid4()

key = prefix + "#" + str(base)
return key
def generate_prefix(layer_id)

Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key.

Args

layer_id : RelayLayerId
The RelayLayerId of the RelayLayer that generates the key

Returns

str
The key prefix as a string
Expand source code
def generate_prefix(layer_id):
"""Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key.

Args:
layer_id (RelayLayerId): The RelayLayerId of the RelayLayer that generates the key

Returns:
str: The key prefix as a string
"""
sha1 = hashlib.sha1()
sha1.update(str(layer_id).encode())

return sha1.hexdigest()
\ No newline at end of file
This diff is collapsed.
 RelayModel.ModuleConfig API documentation

Module RelayModel.ModuleConfig

Expand source code
import logging

# Defines the level of the logger
RELAY_LOG_LEVEL = logging.WARNING
"""int: Holds the logging level of all loggers in this module.

Normally this is set to logging.WARNING
"""

# Defines the timeout period of a relay layer
RELAY_LAYER_TIMEOUT_PERIOD = 1
"""float: Defines the timeout period of a relay layer.

Normally this is set to 1 second.
"""

# Defines the timeout period of a node
NODE_TIMEOUT_PERIOD = 1
"""float: Defines the timeout period of a node.

Normally this is set to 1 second.
"""

# Timeout for polling receiving messages when sending in link layer
POLL_TIMEOUT = 300
"""int: Timeout in milliseconds for polling receiving messages when sending in link layer.

Normally this is set to 300 ms.
"""

# Tries until the receiving sockets is considered as closed
POLL_TRIES = 3
"""int: Tries until the receiving socket is considered as closed.

Normally this is set to 3 tries.
"""

CONSIDER_AS_CLOSED = False
"""bool: Stores the flag if the link layer should send closed messages if one link layer is not reachable.

If this is set to False it will only discard the messages after the retries. Otherwise it will send In- and
OutRelayClosed Actions.
Normally this is set to False.
"""

STATE_MONITOR_PORT = 1999
"""Defines the port of the StateMonitor

Normally this is set to port 1999.
"""

"""Defines the full address of the StateMonitor including ip and port.

Normally the ip of the address is localhost.
"""

# Activate DoS detection
DOS_DETECTION_ACTIVATED = True
"""Stores the flag if the dos detection should be used.

If this is set to True the RelayLayer is watching transmit rates and try to detect dos attacks.
Normally this is set to True.
"""

# DoS Window Size
WINDOW_SIZE = 20
"""Defines the window size for the dos detection.

This should be a value that is dividable by 2.
Normally this is set to 20.
"""

# DoS change detection alpha
CHANGE_ALPHA = -10
"""Defines the change rate threshold for a dos attack detection.

This value should always be lower than 0. Otherwise falling transmit rates get detected as dos attacks.
Normally this is set to -10.
"""

# Actions that are not tracked and should only be used for relay layer messages
NO_MONITOR_ACTIONS = ['Probe', 'ProbeFail', 'NotAuthorized', 'Ping', 'InRelayClosed', 'OutRelayClosed']
"""Stores the actions that are standard actions from the RelayLayer.

This actions are not monitored on transmission because a node should not have access to this actions.
It is stored in a list where each entry has the action name set.
Normally this is set to ['Probe', 'ProbeFail', 'NotAuthorized', 'Ping', 'InRelayClosed', 'OutRelayClosed']
"""

# Results folder
RESULTS_FOLDER = "results/"
"""Defines the folder where the StateMonitor should write result files to.

Normally this is set to the folder named "results/".
"""

Global variables

var CHANGE_ALPHA

Defines the change rate threshold for a dos attack detection.

This value should always be lower than 0. Otherwise falling transmit rates get detected as dos attacks. Normally this is set to -10.

var CONSIDER_AS_CLOSED

bool: Stores the flag if the link layer should send closed messages if one link layer is not reachable.

If this is set to False it will only discard the messages after the retries. Otherwise it will send In- and OutRelayClosed Actions. Normally this is set to False.

var DOS_DETECTION_ACTIVATED

Stores the flag if the dos detection should be used.

If this is set to True the RelayLayer is watching transmit rates and try to detect dos attacks. Normally this is set to True.

var NODE_TIMEOUT_PERIOD

float: Defines the timeout period of a node.

Normally this is set to 1 second.

var NO_MONITOR_ACTIONS

Stores the actions that are standard actions from the RelayLayer.

This actions are not monitored on transmission because a node should not have access to this actions. It is stored in a list where each entry has the action name set. Normally this is set to ['Probe', 'ProbeFail', 'NotAuthorized', 'Ping', 'InRelayClosed', 'OutRelayClosed']

var POLL_TIMEOUT

int: Timeout in milliseconds for polling receiving messages when sending in link layer.

Normally this is set to 300 ms.