pybatmesh/naxalnet

90 lines
2.6 KiB
Plaintext
Raw Normal View History

2021-05-21 20:26:00 +05:30
#!/usr/bin/env python3
2021-05-10 15:15:42 +05:30
2021-05-21 20:26:00 +05:30
"""
Setup a working BATMAN Advanced network
with systemd-networkd and iwd
"""
2021-05-10 15:15:42 +05:30
# Copyright (C) 2021 The 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/>.
import sys
2021-05-21 20:26:00 +05:30
from pathlib import Path
from shutil import copy
2021-05-21 20:26:00 +05:30
from dasbus.connection import SystemMessageBus
2021-05-10 15:15:42 +05:30
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf"
2021-05-21 20:26:00 +05:30
RESOLV_CONF = "/etc/resolv.conf"
2021-05-10 15:15:42 +05:30
# Copy networkd configs to volatile dir.
# See man:systemd.networkm(5)
try:
print("Copying network config files")
dest = Path(NETWORKD_VOLATILE_DIR)
src = Path(NETWORKD_CONFIGS)
2021-05-10 15:15:42 +05:30
# Create the volatile directory if it doesn't exist
dest.mkdir(parents=True, exist_ok=True)
2021-05-21 20:26:00 +05:30
# Copy all files in src to dest
for i in src.iterdir():
copy(i, dest)
except:
sys.exit("An error occured while copying network files")
2021-05-21 20:26:00 +05:30
# Symlink resolvd.conf to systemd's stub-resolvd.conf
# This is needed for DNS resolution to work.
# see https://wiki.archlinux.org/title/Systemd-resolved#DNS
2021-05-21 20:26:00 +05:30
try:
print("Checking resolv.conf")
r = Path(RESOLV_CONF)
if r.exists():
print(r, "already exists. Removing it")
r.unlink()
print("Linking resolv.conf")
r.symlink_to(RESOLVED_STUB_RESOLVE)
except:
sys.exit("An error occured while linking resolv.conf")
2021-05-21 20:26:00 +05:30
# Now, the iwd part
try:
# connect to the System bus
bus = SystemMessageBus()
# iwd proxy
iwd = bus.get_proxy("net.connman.iwd", "/")
# Get list of all devices
print("Finding connected devices")
objects = iwd.GetManagedObjects()
devices = []
for name, obj in objects.items():
if "net.connman.iwd.Device" in obj:
# add all devices to the list
print("Found device:", obj["net.connman.iwd.Device"]["Name"])
devices.append(name)
# TODO: On first devices, start ad-hoc
# If there is a second device, start AP
# in it
except:
sys.exit("An error occured while communicating with iwd")
2021-05-21 20:26:00 +05:30
print("Bye")