Newer
Older
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class coap(Custom):
# Class variables
layer = 7 # Protocol OSI layer
protocol_name = "coap" # Protocol name
# Supported keys in YAML profile
supported_keys = [
"type",
"method",
"uri"
]
def parse(self, is_backward: bool = False, initiator: str = "src") -> dict:
"""
Parse the CoAP protocol.
:param is_backward (optional): Whether the protocol must be parsed for a backward rule.
Optional, default is `False`.
:param initiator (optional): Connection initiator (src or dst).
Optional, default is "src".
:return: Dictionary containing the (forward and backward) nftables and nfqueue rules for this policy.
"""
# Lambda functions to convert a CoAP type or method to its C representation (upper case and separated by underscores)
func_coap_type = lambda type: f"COAP_{type.upper().replace('-', '_')}"
func_coap_method = lambda method: f"HTTP_{method.upper().replace('-', '_')}"
# Handle CoAP message type
rule = {"forward": "coap_message.type == {}"}
self.add_field("type", rule, is_backward, func_coap_type)
# Handle CoAP method
rule = {"forward": "coap_message.method == {}"}
self.add_field("method", rule, is_backward, func_coap_method)
# Handle CoAP URI
rule = {"forward": "strcmp(coap_message.uri, \"{}\") == 0"}
self.add_field("uri", rule, is_backward)
return self.rules