1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

dist/tools: provide DHCPv6 server support for network setup scripts

This commit is contained in:
Martine S. Lenders 2020-03-10 15:44:49 +01:00 committed by Martine S. Lenders
parent e6510cb89e
commit 0c51285b9b
No known key found for this signature in database
GPG Key ID: CCD317364F63286F
4 changed files with 81 additions and 14 deletions

View File

@ -63,7 +63,8 @@ class _SingletonMeta(type):
class DHCPv6Server(metaclass=_SingletonMeta):
"""
Inspired by Daemon class
https://web.archive.org/web/20160305151936/http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
https://web.archive.org/web/20160305151936/\
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
"""
PORT = 547
command = None
@ -118,7 +119,8 @@ class DHCPv6Server(metaclass=_SingletonMeta):
# exit from second parent
sys.exit(0)
except OSError as e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.stderr.write("fork #2 failed: %d (%s)\n" %
(e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors

View File

@ -18,20 +18,41 @@ cleanup() {
echo "Cleaning up..."
remove_tap
ip a d fd00:dead:beef::1/128 dev lo
if [ -n "${UHCPD_PID}" ]; then
kill ${UHCPD_PID}
fi
if [ -n "${DHCPD_PIDFILE}" ]; then
kill "$(cat ${DHCPD_PIDFILE})"
rm "${DHCPD_PIDFILE}"
fi
trap "" INT QUIT TERM EXIT
}
start_uhcpd() {
${UHCPD} ${TAP} ${PREFIX}
UHCPD_PID=$!
}
start_dhcpd() {
DHCPD_PIDFILE=$(mkfile)
${DHCPD} -d -p ${DHCPD_PIDFILE} ${TAP} ${PREFIX} 2> /dev/null
}
if [ "$1" = "-d" ] || [ "$1" = "--use-dhcp" ]; then
USE_DHCPV6=1
shift 1
else
USE_DHCPV6=0
fi
TAP=$1
PREFIX=$2
_USER=$3
: ${UHCPD:="$(readlink -f $(dirname $0)"/../uhcpd/bin")/uhcpd"}
: ${DHCPD:="$(readlink -f $(dirname $0)"/../dhcpdv6-pd_ia")/dhcpv6-pd_ia.py"}
[ -z "${TAP}" -o -z "${PREFIX}" ] && {
echo "usage: $0 <tap-device> <prefix> [<user>]"
echo "usage: $0 [-d|--use-dhcp] <tap-device> <prefix> [<user>]"
exit 1
}
@ -46,4 +67,8 @@ fi
trap "cleanup" INT QUIT TERM EXIT
create_tap && start_uhcpd
create_tap && if [ ${USE_DHCPV6} -eq 1 ]; then
start_dhcpd
else
start_uhcpd
fi

View File

@ -20,9 +20,13 @@ cleanup() {
echo "Cleaning up..."
remove_tap
ip a d fd00:dead:beef::1/128 dev lo
if [ ${ETHOS_ONLY} -ne 1 ]; then
if [ -n "${UHCPD_PID}" ]; then
kill ${UHCPD_PID}
fi
if [ -n "${DHCPD_PIDFILE}" ]; then
kill "$(cat ${DHCPD_PIDFILE})"
rm "${DHCPD_PIDFILE}"
fi
trap "" INT QUIT TERM EXIT
}
@ -31,6 +35,18 @@ start_uhcpd() {
UHCPD_PID=$!
}
start_dhcpd() {
DHCPD_PIDFILE=$(mktemp)
${DHCPD} -d -p ${DHCPD_PIDFILE} ${TAP} ${PREFIX} 2> /dev/null
}
if [ "$1" = "-d" ] || [ "$1" = "--use-dhcpv6" ]; then
USE_DHCPV6=1
shift 1
else
USE_DHCPV6=0
fi
if [ "$1" = "-e" ] || [ "$1" = "--ethos-only" ]; then
ETHOS_ONLY=1
shift 1
@ -44,7 +60,8 @@ PREFIX=$3
BAUDRATE=115200
[ -z "${PORT}" -o -z "${TAP}" -o -z "${PREFIX}" ] && {
echo "usage: $0 [-e|--ethos-only] <serial-port> <tap-device> <prefix> " \
echo "usage: $0 [-d|--use-dhcp] [-e|--ethos-only] " \
"<serial-port> <tap-device> <prefix> " \
"[baudrate]"
exit 1
}
@ -58,9 +75,15 @@ trap "cleanup" INT QUIT TERM EXIT
create_tap && \
if [ ${ETHOS_ONLY} -ne 1 ]; then
UHCPD="$(readlink -f "${ETHOS_DIR}/../uhcpd/bin")/uhcpd"
start_uhcpd
START_ETHOS=$?
if [ ${USE_DHCPV6} -eq 1 ]; then
DHCPD="$(readlink -f "${ETHOS_DIR}/../dhcpv6-pd_ia/")/dhcpv6-pd_ia.py"
start_dhcpd
START_ETHOS=$?
else
UHCPD="$(readlink -f "${ETHOS_DIR}/../uhcpd/bin")/uhcpd"
start_uhcpd
START_ETHOS=$?
fi
else
START_ETHOS=0
fi

View File

@ -2,6 +2,7 @@
SLIPTTY_DIR="$(cd "$(dirname "$0")" && pwd -P)"
UHCPD="$(cd "${SLIPTTY_DIR}/../uhcpd/bin" && pwd -P)/uhcpd"
DHCPD="$(cd "${SLIPTTY_DIR}/../dhcpv6-pd_ia/" && pwd -P)/dhcpv6-pd_ia.py"
TUN=sl0
TUN_GLB="fdea:dbee:f::1/64"
UHCPD_PID=
@ -92,6 +93,10 @@ cleanup() {
if [ -n "${UHCPD_PID}" ]; then
kill ${UHCPD_PID}
fi
if [ -n "${DHCPD_PIDFILE}" ]; then
kill "$(cat ${DHCPD_PIDFILE})"
rm "${DHCPD_PIDFILE}"
fi
trap "" INT QUIT TERM EXIT
}
@ -100,17 +105,24 @@ start_uhcpd() {
UHCPD_PID=$!
}
start_dhcpd() {
DHCPD_PIDFILE=$(mktemp)
${DHCPD} -d -p ${DHCPD_PIDFILE} ${TAP} ${PREFIX} 2> /dev/null
}
usage() {
echo "usage: $1 [-I <sl0>] [-e] [-g <addr>/<prefix_len>] <prefix> serial [baudrate]"
echo "usage: $1 [-I <sl0>] [-e] [-g <addr>/<prefix_len>] <prefix> tcp:host [port]"
echo "usage: $1 [-I <sl0>] [-d] [-e] [-g <addr>/<prefix_len>] <prefix> serial [baudrate]"
echo "usage: $1 [-I <sl0>] [-d] [-e] [-g <addr>/<prefix_len>] <prefix> tcp:host [port]"
}
trap "cleanup" INT QUIT TERM EXIT
SLIP_ONLY=0
USE_DHCPV6=1
while getopts ehI: opt; do
while getopts dehI: opt; do
case ${opt} in
d) USE_DHCPV6=1; shift 1;;
e) SLIP_ONLY=1; shift 1;;
I) TUN=${OPTARG}; shift 2;;
g) TUN_GLB=${OPTARG}; shift 2;;
@ -128,8 +140,13 @@ fi
create_tun && \
if [ ${SLIP_ONLY} -ne 1 ]; then
start_uhcpd
START_SLIP=$?
if [ ${USE_DHCPV6} -eq 1 ]; then
start_dhcpd
START_SLIP=$?
else
start_uhcpd
START_SLIP=$?
fi
else
START_SLIP=0
fi