diff --git a/Makefile b/Makefile index 04482a8..62373c1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,14 @@ PREFIX := /usr install: naxalnet install -d $(DESTDIR)$(PREFIX)/bin install -d $(DESTDIR)$(PREFIX)/lib/systemd/system/ - install naxalnet@.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ + install naxalnet.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ install naxalnet $(DESTDIR)$(PREFIX)/bin/ - install -d $(DESTDIR)$(PREFIX)/share/naxalnet - install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/ + install -d $(DESTDIR)$(PREFIX)/share/naxalnet/networkd + install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/networkd + +testdeps: + @for i in networkctl systemctl python3; do \ + echo "Checking for $$i"; \ + which $$i > /dev/null && echo " $$i found" || \ + (echo " $$i not found"; exit 1); \ + done diff --git a/README.md b/README.md index c2b5ae4..5c95037 100644 --- a/README.md +++ b/README.md @@ -31,17 +31,18 @@ into networkd's runtime configuration directory. It uses iwctl to start an ad-hoc network named "Hello World". The wireless interface is linked to `bat0`, the batman interface. A bridge `bridge0` is created so that other devices such as wired connections and wireless ap can be bridged. `bat0` is -added to the bridge, and the bridge gets an ip address (link-local, or DHCP if -any of the computers have a DHCP server configured). If these were successful, -an intranet is created. You can now use services like [IPFS](https://ipfs.io), -[Jami](https://jami.net), [Secure Scuttlebutt](https://scuttlebutt.nz) -and others which can work without internet access. +added to the bridge, and the bridge gets an ip address (link-local, or +DHCP if any of the computers have a DHCP server configured). If these +were successful, an intranet is created. You can now use services +like [IPFS](https://ipfs.io), [Jami](https://jami.net), +[Secure Scuttlebutt](https://scuttlebutt.nz) and others which can work +without internet access. ## Requirements - systemd v248 or more (for batman support) -- Linux kernel with batman-adv module (if `modinfo batman-adv` shows no error - then you already have it) +- Linux kernel with batman-adv module (if `modinfo batman-adv` shows + no error then you already have it) - iwd (for starting ad-hoc network) - wifi adapter with ad-hoc support - two or more computers with wifi adapter @@ -55,12 +56,10 @@ Clone the repo and cd into it. Run `sudo make install` to install naxalnet. This will install naxalnet in `/usr/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@.service}`. - ## TODO @@ -101,4 +104,4 @@ sudo rm -r /usr/local/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@ Add list of things here. -This project is in pre-alpha stage. Documentation is incomplete. \ No newline at end of file +This project is in alpha stage. Documentation is incomplete. diff --git a/naxalnet b/naxalnet index 80977d8..52c6f21 100755 --- a/naxalnet +++ b/naxalnet @@ -5,44 +5,67 @@ Setup a working BATMAN Advanced network with systemd-networkd and iwd """ -# for linking resolv.conf +import sys from pathlib import Path +from shutil import copy from dasbus.connection import SystemMessageBus -SYSTEMD_RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf" +NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd" +NETWORKD_VOLATILE_DIR = "/run/systemd/network" +RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf" RESOLV_CONF = "/etc/resolv.conf" -def get_all_devices(proxy): - """ - Returns d-bus object path of all devices as a list of strings. - proxy: usually what is returned by bus.get_proxy('net.connman.iwd','/') - """ - # Gets all D-Bus objects in the proxy. - objects = proxy.GetManagedObjects() - - # Now find out which of them are devices - devices = [] - for name, obj in objects.items(): - if "net.connman.iwd.Device" in obj: - # add all devices to the list - devices.append(name) - - return devices - - -# Copy networkd configs - - -# syslink resolvd +# Copy networkd configs to volatile dir. +# See man:systemd.networkm(5) try: - Path(RESOLV_CONF).symlink_to(SYSTEMD_RESOLVED_STUB_RESOLVE) -except FileExistsError: - print("Resolv.conf already exists") + print("Copying network config files") + dest = Path(NETWORKD_VOLATILE_DIR) + src = Path(NETWORKD_CONFIGS) + + # Create the volatile directory if it doesn't exist + dest.mkdir(parents=True, exist_ok=True) + + # Copy all files in src to dest + for i in src.iterdir(): + copy(i, dest) +except: + sys.exit("An error occured") + + +# 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 +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") + # connect to the System bus bus = SystemMessageBus() # iwd proxy -proxy = bus.get_proxy("net.connman.iwd", "/") +iwd = bus.get_proxy("net.connman.iwd", "/") + +# Get list of all devices +print("Finding connected devices") +objects = iwd.GetManagedObjects() +device_paths = [] +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"]) + device_paths.append(name) + + +# TODO: On first devices, start ad-hoc +# If there is a second device, start AP +# in it print("Bye")