From 9d128d6c521bdd19e44999c1f8ecf34db567a786 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Sat, 4 Apr 2020 22:56:52 +0200 Subject: [PATCH 01/12] Implemented util.read_domainfile This reads a given json file containing a dict with bat_device names as keys and domain_names as values. --- util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/util.py b/util.py index dcc6359..5811678 100644 --- a/util.py +++ b/util.py @@ -1,3 +1,4 @@ +import json import os def _file_name_filter(fname): @@ -72,3 +73,14 @@ def ifindex_to_batiface(if_index, batman_ifaces): if iface in batman_ifaces or iface == None: return iface return iface_match_recursive(iface, batman_ifaces) + +def read_domainfile(dcf_path): + """Read a json file which holds all currently known assignments of + bat interfaces to domains as a dictionary and return it as python dict. + Return an empty dict, if the given path was None. + """ + if dcf_path is None: + return {} + with open(dcf_path, "r") as dc_file: + known = json.load(dc_file) + return known \ No newline at end of file From 045962f7dd3f0545afe468d501a030ef7cdfb4d7 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Sat, 4 Apr 2020 23:00:23 +0200 Subject: [PATCH 02/12] Provided an example domainfile. --- domainfile.json.example | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 domainfile.json.example diff --git a/domainfile.json.example b/domainfile.json.example new file mode 100644 index 0000000..aea9788 --- /dev/null +++ b/domainfile.json.example @@ -0,0 +1,18 @@ +{ + "bat0": "dom0-gw", + "bat10": "dom10-gw", + "bat11": "dom11-gw", + "bat12": "dom12-gw", + "bat13": "dom13-gw", + "bat14": "dom14-gw", + "bat15": "dom15-gw", + "bat16": "dom16-gw", + "bat17": "dom17-gw", + "bat18": "dom18-gw", + "bat19": "dom19-gw", + "bat20": "dom20-gw", + "bat21": "dom21-gw", + "bat22": "dom22-gw", + "bat23": "dom23-gw", + "bat99": "dom99-gw" +} From 0b4d856c990e6649b8e4e79ae7d7415874b1d842 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Sat, 4 Apr 2020 23:00:47 +0200 Subject: [PATCH 03/12] Added a new parameter to spcify where to read the domainfile from. It's optional. --- respondd.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/respondd.py b/respondd.py index d5bf4fc..18aa6e7 100755 --- a/respondd.py +++ b/respondd.py @@ -56,7 +56,7 @@ def handle(self): if __name__ == "__main__": parser = argparse.ArgumentParser(usage=""" %(prog)s -h - %(prog)s [-p ] [-g ] [-i [%%]] [-i [%%] ..] [-d ] [-b [:] [-n ] ..]""") + %(prog)s [-p ] [-g ] [-i [%%]] [-i [%%] ..] [-d ] [-b [:] [-n ] [-c ] ..]""") parser.add_argument('-p', dest='port', default=1001, type=int, metavar='', help='port number to listen on (default 1001)') @@ -79,7 +79,9 @@ def handle(self): metavar='', help='mesh ipv4 address') parser.add_argument('-n', dest='domain_code', metavar='', - help='Domain Code for system/domain_code') + help='(default) domain code for system/domain_code') + parser.add_argument('-c', dest='domain_code_file', metavar='', + help='domain_code.json path (if info is not in file, fallback to -n\'s value)') args = parser.parse_args() @@ -93,7 +95,9 @@ def handle(self): # mesh_ipv4 list is not empty, there is an override address batadv_mesh_ipv4_overrides[iface] = mesh_ipv4[0] - global_handler_env = { 'domain_code': args.domain_code, 'mesh_ipv4': args.mesh_ipv4 } + known_codes = util.read_domainfile(args.domain_code_file) + + global_handler_env = { 'domain_code': args.domain_code, 'known_codes': known_codes, 'mesh_ipv4': args.mesh_ipv4 } metasocketserver.MetadataUDPServer.address_family = socket.AF_INET6 metasocketserver.MetadataUDPServer.allow_reuse_address = True From 5e2754b3699ce551b205b27c7c3826bc2c254cdb Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Sat, 4 Apr 2020 23:02:38 +0200 Subject: [PATCH 04/12] Altered the domain_code-provider in order to support domaincode-files. Given a dcf-path, the provider tries to return the domaincode related to a batadv_dev, like trier did earlier in their code. If it cannot find a match, it returns whatever was provided as 'domaincode' via '-n' in the commandline, which makes it effectively a default value for unkown domains and a nice fallback. --- providers/nodeinfo/system/domain_code.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/providers/nodeinfo/system/domain_code.py b/providers/nodeinfo/system/domain_code.py index 31074ae..28b2181 100644 --- a/providers/nodeinfo/system/domain_code.py +++ b/providers/nodeinfo/system/domain_code.py @@ -2,7 +2,10 @@ class Source(providers.DataSource): def required_args(self): - return ['domain_code'] + return ['batadv_dev', 'domain_code', 'known_codes'] - def call(self, domain_code): - return domain_code + def call(self, batadv_dev, domain_code, known_codes): + try: + return known_codes[batadv_dev] + except KeyError: + return domain_code From 4f2b141845074daeb1dad6e867a985d7836b9e0f Mon Sep 17 00:00:00 2001 From: "J. Burfeind" Date: Tue, 7 Apr 2020 09:22:29 +0200 Subject: [PATCH 05/12] updated util.read_dumainfile removed unnecessary return variable Co-Authored-By: Martin Weinelt --- util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.py b/util.py index 5811678..2d0e239 100644 --- a/util.py +++ b/util.py @@ -83,4 +83,4 @@ def read_domainfile(dcf_path): return {} with open(dcf_path, "r") as dc_file: known = json.load(dc_file) - return known \ No newline at end of file + retorn json.load(dc_file) From 67ca3411d7ed86ebe5b88d9421d2b2654336dba5 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Tue, 7 Apr 2020 09:38:53 +0200 Subject: [PATCH 06/12] updated domainfile example to reflect domaincodes --- domainfile.json.example | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/domainfile.json.example b/domainfile.json.example index aea9788..6a3e686 100644 --- a/domainfile.json.example +++ b/domainfile.json.example @@ -1,18 +1,18 @@ { - "bat0": "dom0-gw", - "bat10": "dom10-gw", - "bat11": "dom11-gw", - "bat12": "dom12-gw", - "bat13": "dom13-gw", - "bat14": "dom14-gw", - "bat15": "dom15-gw", - "bat16": "dom16-gw", - "bat17": "dom17-gw", - "bat18": "dom18-gw", - "bat19": "dom19-gw", - "bat20": "dom20-gw", - "bat21": "dom21-gw", - "bat22": "dom22-gw", - "bat23": "dom23-gw", - "bat99": "dom99-gw" + "bat0": "dom0", + "bat10": "dom10", + "bat11": "dom11", + "bat12": "dom12", + "bat13": "dom13", + "bat14": "dom14", + "bat15": "dom15", + "bat16": "dom16", + "bat17": "dom17", + "bat18": "dom18", + "bat19": "dom19", + "bat20": "dom20", + "bat21": "dom21", + "bat22": "dom22", + "bat23": "dom23", + "bat99": "dom99" } From ceb489190ea754e0e0b4068f416e9a0dff84c30e Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Tue, 7 Apr 2020 15:44:03 +0200 Subject: [PATCH 07/12] util: removed redundant line --- util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/util.py b/util.py index 2d0e239..9262ef5 100644 --- a/util.py +++ b/util.py @@ -82,5 +82,4 @@ def read_domainfile(dcf_path): if dcf_path is None: return {} with open(dcf_path, "r") as dc_file: - known = json.load(dc_file) retorn json.load(dc_file) From 041807140cb6a77f8583be325c70272ea7590053 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Tue, 7 Apr 2020 15:52:45 +0200 Subject: [PATCH 08/12] README: add domain_code-file documentation --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a62f0d..83a39dc 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,10 @@ optional arguments: -b batman-adv interface to answer for (default: bat0). Specify once per domain -m mesh ipv4 address - -n Gateway domain_code for nodeinfo/system/domain_code + -n (default) domain code for system/domain_code + -c + domain_code.json path (if info is not in file, + fallback to -n's value) This is a possible configuration for a site with a single domain: From 75ce7055e1ae7a9b2121804e555703acbb09f0fe Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Tue, 7 Apr 2020 15:59:24 +0200 Subject: [PATCH 09/12] Fixed type in 'return', however it got there. --- util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.py b/util.py index 9262ef5..5d33e5b 100644 --- a/util.py +++ b/util.py @@ -82,4 +82,4 @@ def read_domainfile(dcf_path): if dcf_path is None: return {} with open(dcf_path, "r") as dc_file: - retorn json.load(dc_file) + return json.load(dc_file) From 96a42c2ce468578bfa4be2e6886c1b4ac5163f67 Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Wed, 8 Apr 2020 18:20:15 +0200 Subject: [PATCH 10/12] Added per batman iface domaincode overrides --- respondd.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/respondd.py b/respondd.py index 18aa6e7..a5444dc 100755 --- a/respondd.py +++ b/respondd.py @@ -85,17 +85,26 @@ def handle(self): args = parser.parse_args() + # Read domain-codes from file + known_codes = util.read_domainfile(args.domain_code_file) + # Extract batman interfaces from commandline parameters + # and overwrite domain-codes from file with commandline arguments batadv_mesh_ipv4_overrides = { } batadv_ifaces = [ ] for ifspec in args.batadv_ifaces: - iface, *mesh_ipv4 = ifspec.split(':') + iface, *left_over = ifspec.split(':') batadv_ifaces.append(iface) - if mesh_ipv4: - # mesh_ipv4 list is not empty, there is an override address - batadv_mesh_ipv4_overrides[iface] = mesh_ipv4[0] - - known_codes = util.read_domainfile(args.domain_code_file) + try: + # if left_over list is not empty, there is at least an override address + possible_override = left_over.pop(0) + # this clause is necessary in case one does not specify an ipv4 override, but a domain-code + if '' != possible_override: + batadv_mesh_ipv4_overrides[iface] = possible_override + # if left_over list is not empty, there is a domain_code + known_codes[iface] = left_over.pop(0) + except IndexError: + continue global_handler_env = { 'domain_code': args.domain_code, 'known_codes': known_codes, 'mesh_ipv4': args.mesh_ipv4 } From 4208e9fbf4b00fe55b41b00efafd4edfb6a0c03c Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Wed, 8 Apr 2020 18:49:44 +0200 Subject: [PATCH 11/12] readme: reflects per batinterface domaincode --- respondd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/respondd.py b/respondd.py index a5444dc..e27ef05 100755 --- a/respondd.py +++ b/respondd.py @@ -56,7 +56,7 @@ def handle(self): if __name__ == "__main__": parser = argparse.ArgumentParser(usage=""" %(prog)s -h - %(prog)s [-p ] [-g ] [-i [%%]] [-i [%%] ..] [-d ] [-b [:] [-n ] [-c ] ..]""") + %(prog)s [-p ] [-g ] [-i [%%]] [-i [%%] ..] [-d ] [-b [:][:] [-n ] [-c ] ..]""") parser.add_argument('-p', dest='port', default=1001, type=int, metavar='', help='port number to listen on (default 1001)') From db0321c56f3277fbb42e8682d37caab47cd3b7fd Mon Sep 17 00:00:00 2001 From: "aiyion.prime" Date: Wed, 8 Apr 2020 19:18:54 +0200 Subject: [PATCH 12/12] domaincodefile: changed format to one-key-dict The file now holds a key called 'domaincodes' and below the assignments. This allows a transition from domaincodefile to configfile in the future, if wanted. --- domainfile.json.example | 34 ++++++++++++++++++---------------- util.py | 8 ++++++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/domainfile.json.example b/domainfile.json.example index 6a3e686..27ddec9 100644 --- a/domainfile.json.example +++ b/domainfile.json.example @@ -1,18 +1,20 @@ { - "bat0": "dom0", - "bat10": "dom10", - "bat11": "dom11", - "bat12": "dom12", - "bat13": "dom13", - "bat14": "dom14", - "bat15": "dom15", - "bat16": "dom16", - "bat17": "dom17", - "bat18": "dom18", - "bat19": "dom19", - "bat20": "dom20", - "bat21": "dom21", - "bat22": "dom22", - "bat23": "dom23", - "bat99": "dom99" + "domaincodes": { + "bat0": "dom0", + "bat10": "dom10", + "bat11": "dom11", + "bat12": "dom12", + "bat13": "dom13", + "bat14": "dom14", + "bat15": "dom15", + "bat16": "dom16", + "bat17": "dom17", + "bat18": "dom18", + "bat19": "dom19", + "bat20": "dom20", + "bat21": "dom21", + "bat22": "dom22", + "bat23": "dom23", + "bat99": "dom99" + } } diff --git a/util.py b/util.py index 5d33e5b..a8e386c 100644 --- a/util.py +++ b/util.py @@ -76,10 +76,14 @@ def ifindex_to_batiface(if_index, batman_ifaces): def read_domainfile(dcf_path): """Read a json file which holds all currently known assignments of - bat interfaces to domains as a dictionary and return it as python dict. + bat interfaces to domains as a dictionary within a dict and below its key 'domaincodes' + and return it as python dict. Return an empty dict, if the given path was None. """ if dcf_path is None: return {} with open(dcf_path, "r") as dc_file: - return json.load(dc_file) + try: + return json.load(dc_file)["domaincodes"] + except KeyError: + return {}