2021-07-21 12:06:11 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Copyright (C) 2021 The naxalnet Authors
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Setup a working BATMAN Advanced network
|
|
|
|
with systemd-networkd and iwd
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
from shutil import copy
|
2021-08-09 09:24:41 +02:00
|
|
|
from dasbus.error import DBusError
|
2021-07-21 12:06:11 +02:00
|
|
|
from naxalnet.iwd import IWD, Device, Adapter
|
2021-08-13 07:05:13 +02:00
|
|
|
from naxalnet.config import parse_args
|
2021-07-21 12:06:11 +02:00
|
|
|
|
|
|
|
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
|
|
|
|
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
|
2021-07-30 14:26:33 +02:00
|
|
|
# default values
|
|
|
|
ADHOC_NAME = "HelloWorld"
|
2021-07-21 12:06:11 +02:00
|
|
|
AP_SSID = "NaxalNet"
|
|
|
|
AP_PASSWD = "naxalnet256"
|
|
|
|
|
|
|
|
|
2021-08-13 07:05:13 +02:00
|
|
|
def copy_files(args):
|
2021-07-25 10:36:17 +02:00
|
|
|
"""
|
|
|
|
Copy networkd configs to volatile dir.
|
|
|
|
The D-Bus API does not support creating new interfaces
|
|
|
|
or linking to bridges. So we use config files.
|
|
|
|
See man:systemd.network(5)
|
|
|
|
"""
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
print("Copying network config files")
|
2021-08-13 07:05:13 +02:00
|
|
|
dest = Path(args.networkd_runtime_dir)
|
|
|
|
src = Path(args.networkd_config_dir)
|
2021-07-25 10:36:17 +02:00
|
|
|
|
|
|
|
# Create the volatile directory if it doesn't exist
|
|
|
|
dest.mkdir(parents=True, exist_ok=True)
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
# Copy all files in src to dest
|
|
|
|
for i in src.iterdir():
|
|
|
|
copy(i, dest)
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
|
2021-08-05 08:41:31 +02:00
|
|
|
def setup_devices(args):
|
2021-08-09 09:24:41 +02:00
|
|
|
"""
|
|
|
|
Setup wifi interfaces using iwd
|
|
|
|
This function should be called every time an interface
|
|
|
|
is connected or removed
|
|
|
|
"""
|
2021-08-05 08:41:31 +02:00
|
|
|
iwd = IWD()
|
|
|
|
devices = iwd.get_devices()
|
|
|
|
adhoc_devices = []
|
|
|
|
ap_devices = []
|
|
|
|
|
|
|
|
# Find devices supporting ad-hoc and ap
|
|
|
|
for i in devices:
|
|
|
|
device = Device(i)
|
|
|
|
adapter = Adapter(device.adapter)
|
|
|
|
if adapter.supports_mode("ad-hoc"):
|
|
|
|
adhoc_devices.append(i)
|
|
|
|
if adapter.supports_mode("ap"):
|
|
|
|
ap_devices.append(i)
|
|
|
|
|
|
|
|
if len(adhoc_devices) != 0:
|
|
|
|
# Start ad-hoc on first device supporting ad-hoc
|
|
|
|
adhoc_device = Device(adhoc_devices.pop())
|
|
|
|
# The same device is likely to have ap support too.
|
|
|
|
# But we can't start ad-hoc and ap on the same interface.
|
|
|
|
# Remove adhoc_device from ap_devices if it exists there
|
|
|
|
if adhoc_device.name in ap_devices:
|
|
|
|
ap_devices.remove(adhoc_device.name)
|
|
|
|
print("Working on ad-hoc")
|
|
|
|
# Turn on adapter if it is off
|
|
|
|
# See issue #9
|
|
|
|
adhoc_adapter = Adapter(adhoc_device.adapter)
|
|
|
|
if not adhoc_adapter.is_powered_on():
|
|
|
|
adhoc_adapter.power_on()
|
|
|
|
adhoc_device.reload()
|
|
|
|
adhoc_device.start_adhoc_open(args.adhoc_name)
|
|
|
|
# Start Access point if ap_device is not empty,
|
|
|
|
# ie, we have more devices
|
|
|
|
if len(ap_devices) != 0:
|
|
|
|
print("Working on AP")
|
|
|
|
ap_device = Device(ap_devices.pop())
|
|
|
|
# Turn on adapter if it is off
|
|
|
|
# See issue #9
|
|
|
|
ap_adapter = Adapter(ap_device.adapter)
|
|
|
|
if not ap_adapter.is_powered_on():
|
|
|
|
ap_adapter.power_on()
|
|
|
|
ap_adapter.reload()
|
|
|
|
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
|
|
|
|
|
|
|
|
|
2021-08-13 09:00:30 +02:00
|
|
|
def print_wifi(args):
|
|
|
|
print("Mesh name:", args.adhoc_name)
|
|
|
|
print("SSID:", args.ap_ssid)
|
|
|
|
print("Password:", args.ap_passwd)
|
|
|
|
|
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
def here_be_dragons():
|
|
|
|
"""
|
|
|
|
This function is run every time you
|
|
|
|
execute naxalnet from commandline
|
|
|
|
"""
|
2021-07-30 14:26:33 +02:00
|
|
|
args = parse_args()
|
2021-08-13 07:05:13 +02:00
|
|
|
|
2021-08-13 09:00:30 +02:00
|
|
|
if args.print_wifi:
|
|
|
|
print_wifi(args)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
try:
|
2021-08-13 07:05:13 +02:00
|
|
|
copy_files(args)
|
2021-07-21 12:06:11 +02:00
|
|
|
except PermissionError as error:
|
|
|
|
print(error)
|
|
|
|
sys.exit("Make sure you are root")
|
|
|
|
|
|
|
|
try:
|
2021-08-05 08:41:31 +02:00
|
|
|
setup_devices(args)
|
2021-07-21 12:06:11 +02:00
|
|
|
except DBusError as error:
|
|
|
|
print(error)
|
|
|
|
sys.exit("An error occured while communicating with iwd")
|
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
# naxalnet will print Bye if no errors occured
|
2021-07-21 12:06:11 +02:00
|
|
|
print("Bye")
|