#!/usr/bin/env bash
# ==========================================================================
#     _   _ _ ____            ____          _____
#    | | | (_)  _ \ ___ _ __ / ___|___  _ _|_   _| __ __ _  ___ ___ _ __
#    | |_| | | |_) / _ \ '__| |   / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
#    |  _  | |  __/  __/ |  | |__| (_) | | | | || | | (_| | (_|  __/ |
#    |_| |_|_|_|   \___|_|   \____\___/|_| |_|_||_|  \__,_|\___\___|_|
#
#       ---  High-Performance Connectivity Tracer (HiPerConTracer)  ---
#                 https://www.nntb.no/~dreibh/hipercontracer/
# ==========================================================================
#
# High-Performance Connectivity Tracer (HiPerConTracer)
# Copyright (C) 2015-2025 by Thomas Dreibholz
#
# 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/>.
#
# Contact: dreibh@simula.no

# Bash options:
set -eu


# ###### Find all interfaces with configured default route ##################
OS=$(uname)
if [ "${OS}" == "Linux" ] ; then
   INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(/sbin/ip -4 route show default | (
      while read -r line ; do
         if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
            echo "${BASH_REMATCH[6]}"
         fi
      done
   ) | sort -u | xargs)
   INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(/sbin/ip -6 route show default | (
      while read -r line ; do
         if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
            echo "${BASH_REMATCH[6]}"
         fi
      done
   ) | sort -u | xargs)
elif [ "${OS}" == "FreeBSD" ] ; then
   INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(netstat -rn -f inet | grep ^default | awk '{ print $4 }' | sort -u | xargs)
   INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(netstat -rn -f inet6 | grep ^default | awk '{ print $4 }' | sort -u | xargs)
else
   echo >&2 "ERROR: Unsupported operating system ${OS}!"
   exit 1
fi

# echo "I: ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6}"


# ###### Get addresses of these interfaces with global scope ################
if [ "${OS}" == "Linux" ] ; then
   ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
    ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
         ip -4 addr show dev "${interface}" scope global | (
            while read -r line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)/ ]] ; then
                  address="${BASH_REMATCH[4]}"
                  echo "${address}"
               fi
            done
         )
      done
   ) | sort -u | xargs)
   ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
    ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
         ip -6 addr show dev "${interface}" scope global | (
            while read -r line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)/ ]] ; then
                  address="${BASH_REMATCH[4]}"
                  echo "${address}"
               fi
            done
         )
      done
   ) | sort -u | xargs)
elif [ "${OS}" == "FreeBSD" ] ; then
   ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
    ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
         ifconfig "${interface}" | (
            while read -r line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
                  address="${BASH_REMATCH[4]}"
                  if [[ ! "${line}" =~ scopeid ]] ; then
                     echo "${address}"
                  fi
               fi
            done
         )
      done
   ) | sort -u | xargs)
   ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
    ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
         ifconfig "${interface}" | (
            while read -r line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
                  address="${BASH_REMATCH[4]}"
                  if [[ ! "${line}" =~ scopeid ]] ; then
                     echo "${address}"
                  fi
               fi
            done
         )
      done
   ) | sort -u | xargs)
fi

# echo "A: ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"

echo "${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"
