#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2026 Chester A. Unal <chester.a.unal@arinc9.com>

TIMEOUT_MS=900
COUNT=3
HOST_ADDR="1.1.1.1 8.8.4.4"
REACHABLE_HOST=1
INTERVAL=1
PERIOD_MS=10
PLPMTUD_NODE="plpmtu.bondingshouldbefree.com:32767"
IFACE="$1"

while true; do
	sleep "$INTERVAL"

	fping -q -p "$PERIOD_MS" -t "$TIMEOUT_MS" -c "$COUNT" -x "$REACHABLE_HOST" $HOST_ADDR >/dev/null 2>&1
	# Continue if there is no connectivity.
	[ $? -ne 0 ] && continue

	# Set the MTU to 1500 to allow plpmtu to start probing from 1500 bytes.
	ip l set mtu 1500 dev "$IFACE"

	# Set the MTU of the path to the found one.
	PMTU=$(plpmtu -p udp -s "$PLPMTUD_NODE" -i "$IFACE" | tail -n 1 | awk '{print $4}')
	ip l set mtu "$PMTU" dev "$IFACE"
	exit
done
