Skip to content

Commit

Permalink
[FEATURE] multidomain-domainnames (#59)
Browse files Browse the repository at this point in the history
* Implemented util.read_domainfile

This reads a given json file containing a dict with bat_device names as
keys and domain_names as values.

* Provided an example domainfile.

* Added a new parameter to spcify where to read the domainfile from.

It's optional.

* 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.

* updated util.read_dumainfile

removed unnecessary return variable

Co-Authored-By: Martin Weinelt <[email protected]>

* updated domainfile example to reflect domaincodes

* util: removed redundant line

* README: add domain_code-file documentation

* Fixed type in 'return', however it got there.

* Added per batman iface domaincode overrides

* readme: reflects per batinterface domaincode

* 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.

Co-authored-by: Martin Weinelt <[email protected]>
  • Loading branch information
AiyionPrime and mweinelt authored Apr 9, 2020
1 parent 5395852 commit acee436
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ optional arguments:
-b <iface> batman-adv interface to answer for (default: bat0).
Specify once per domain
-m <mesh_ipv4> mesh ipv4 address
-n <domain code> Gateway domain_code for nodeinfo/system/domain_code
-n <domain code> (default) domain code for system/domain_code
-c <domain code_file>
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:
Expand Down
20 changes: 20 additions & 0 deletions domainfile.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"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"
}
}
9 changes: 6 additions & 3 deletions providers/nodeinfo/system/domain_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 21 additions & 8 deletions respondd.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def handle(self):
if __name__ == "__main__":
parser = argparse.ArgumentParser(usage="""
%(prog)s -h
%(prog)s [-p <port>] [-g <group>] [-i [<group>%%]<if0>] [-i [<group>%%]<if1> ..] [-d <dir>] [-b <batman_iface>[:<mesh_ipv4>] [-n <domain code>] ..]""")
%(prog)s [-p <port>] [-g <group>] [-i [<group>%%]<if0>] [-i [<group>%%]<if1> ..] [-d <dir>] [-b <batman_iface>[:<mesh_ipv4>][:<domain code>] [-n <domain code>] [-c <domain_code_file>] ..]""")
parser.add_argument('-p', dest='port',
default=1001, type=int, metavar='<port>',
help='port number to listen on (default 1001)')
Expand All @@ -79,21 +79,34 @@ def handle(self):
metavar='<mesh_ipv4>',
help='mesh ipv4 address')
parser.add_argument('-n', dest='domain_code', metavar='<domain code>',
help='Domain Code for system/domain_code')
help='(default) domain code for system/domain_code')
parser.add_argument('-c', dest='domain_code_file', metavar='<domain code_file>',
help='domain_code.json path (if info is not in file, fallback to -n\'s value)')

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]

global_handler_env = { 'domain_code': args.domain_code, 'mesh_ipv4': args.mesh_ipv4 }
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 }

metasocketserver.MetadataUDPServer.address_family = socket.AF_INET6
metasocketserver.MetadataUDPServer.allow_reuse_address = True
Expand Down
15 changes: 15 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

def _file_name_filter(fname):
Expand Down Expand Up @@ -72,3 +73,17 @@ 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 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:
try:
return json.load(dc_file)["domaincodes"]
except KeyError:
return {}

0 comments on commit acee436

Please sign in to comment.