#!/usr/bin/env bash
#
#  =================================================================
#           #     #                 #     #
#           ##    #   ####   #####  ##    #  ######   #####
#           # #   #  #    #  #    # # #   #  #          #
#           #  #  #  #    #  #    # #  #  #  #####      #
#           #   # #  #    #  #####  #   # #  #          #
#           #    ##  #    #  #   #  #    ##  #          #
#           #     #   ####   #    # #     #  ######     #
#
#        ---   The NorNet Testbed for Multi-Homed Systems  ---
#                        https://www.nntb.no
#  =================================================================
#
#  High-Performance Connectivity Tracer (HiPerConTracer)
#  Copyright (C) 2015-2022 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 -e


# ###### Find all interfaces with configured default route ##################
OS=`uname`
if [ "${OS}" == "Linux" ] ; then
   INTERFACES_WITH_DEFAULT_ROUTE_IPv4=`( /bin/ip -4 route show default ) | (
      while read 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=`( /bin/ip -6 route show default ) | (
      while read 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 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 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 line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
                  address="${BASH_REMATCH[4]}"
                  if [[ ! "${line}" =~ scopeid ]] ; then   # FIXME! Is this sufficient in all cases?
                     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 line ; do
               if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
                  address="${BASH_REMATCH[4]}"
                  if [[ ! "${line}" =~ scopeid ]] ; then   # FIXME! Is this sufficient in all cases?
                     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}"
