#----------------------------------------------------------------------------
# /var/install/include/inetlib - library for eisfair scripts
#
# Copyright (c) 2005 Frank Meyer <frank@eisfair(dot)org>
#
# Creation:     19.02.2005 ansgar
# Last Update:  $Id$
#
# 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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# interface description
#----------------------------------------------------------------------------
#
# get_interfaces()       get list of interfaces
# get_interface()        get name of interface
# get_ipaddr()           get IP address of interface
# get_netmask()          get Netmask of interface
# get_broadcast()        get Boadcast address of interface
# get_network()          get Network of interface
#
#----------------------------------------------------------------------------

#============================================================================
# only include this file once
#============================================================================

if [ "$_INETLIB" != "true" ]
then
    _INETLIB='true'

    #
    # get_interfaces
    # ==============
    #
    #  Parameters:     -none-
    #
    #  Output:         list of interfaces
    #                  e.g. "eth0 eth1 eth1:0 ppp0"
    #
    get_interfaces()
    {
       _interfaces=''
       ifconfig -a | grep -E '^eth|^ppp' > /tmp/ifconfig.$$
       while read _line
       do
          set $_line
          [ "$_interfaces" = '' ] && _interfaces="$1" || _interfaces="$_interfaces $1"
       done < /tmp/ifconfig.$$
       rm -f /tmp/ifconfig.$$
       echo "$_interfaces"
    }
    #
    # get_interface
    # =============
    #
    #  Parameters:     $1    number <index>
    #                        Index of value IP_ETH_<index>_NAME
    #                        from /etc/config.d/base.
    #
    get_interface()
    {
       if echo "$1" | grep -q '^[0-9]*$'
       then
          _ipinterface=$1
          _interfaces=`get_interfaces`
          set $_interfaces
          eval echo \$$_ipinterface
       else
          echo ''
       fi
    }

    #
    # get_ipaddr
    # ==========
    #
    #  Parameters:     $1    number <index>
    #                        or
    #                        interface
    #                        Index of value IP_ETH_<index>_NAME
    #                        from /etc/config.d/base.
    #                        If IP_ETH_<index>_NAME is empty
    #                        eth<index - 1> is used instead.
    #
    #  Output:         IP address of interface
    #
    #                  Attention:
    #                  This value may differ from the value
    #                  given in /etc/config.d/base e.g.
    #                     IP_ETH_<index>_IPADDR
    #                  if a dynamic assignment was used
    #                  (eg. when using DHCP).
    #
    #  Returns:        0
    #
    get_ipaddr()
    {
       if echo "$1" | grep -q '^[0-9]*$'
       then
           # check if /etc/config.d/base is already included
           [ "$IP_ETH_N" = '' ] && . /etc/config.d/base
           # get interface
           eval _ipinterface=\${IP_ETH_${1}_NAME}
           [ "$_ipinterface" == "" ] && _ipinterface='eth'`/usr/bin/expr ${1} - 1`
       else
           _ipinterface=$1
       fi
       _ip=''
       # get ip address
       if ifconfig ${_ipinterface} 2>/dev/null | grep -q inet
       then
          set `ifconfig ${_ipinterface} | grep inet`
          _ip=`echo $2 | cut -d":" -f2`
          echo "$_ip"
       fi
       return 0
    }

    #
    # get_netmask
    # ===========
    #
    #   like get_ipaddr
    #
    #  Output:         Netmask of interface
    #
    get_netmask()
    {
       if echo "$1" | grep -q '^[0-9]*$'
       then
          # check if /etc/config.d/base is already included
          [ "$IP_ETH_N" = '' ] && . /etc/config.d/base
          # get interface
          eval _ipinterface=\${IP_ETH_${1}_NAME}
          [ "$_ipinterface" == "" ] && _ipinterface='eth'`/usr/bin/expr ${1} - 1`
       else
           _ipinterface=$1
       fi
       _mask=''
       # get netmask
       if ifconfig ${_ipinterface} 2>/dev/null | grep -q inet
       then
          set `ifconfig ${_ipinterface} | grep inet`
          _mask=`echo $4 | cut -d":" -f2`
          echo "$_mask"
       fi
       return 0
    }

    #
    # get_broacast
    # ============
    #
    #   like get_ipaddr
    #
    #  Output:         Boadcast address of interface
    #
    get_broadcast()
    {
       if echo "$1" | grep -q '^[0-9]*$'
       then
          # check if /etc/config.d/base is already included
          [ "$IP_ETH_N" = '' ] && . /etc/config.d/base
          # get interface
          eval _ipinterface=\${IP_ETH_${1}_NAME}
          [ "$_ipinterface" == "" ] && _ipinterface='eth'`/usr/bin/expr ${1} - 1`
       else
           _ipinterface=$1
       fi
       _ip=''
       _mask=''
       _broadcast=''
       # get ip address and netmask
       if ifconfig ${_ipinterface} 2>/dev/null | grep -q inet
       then
          set `ifconfig ${_ipinterface} | grep inet`
          _ip=`echo $2 | cut -d":" -f2`
          _mask=`echo $4 | cut -d":" -f2`
          # calculate broadcast
          _broadcast=`/usr/local/bin/netcalc broadcast $_ip $_mask`
          echo "$_broadcast"
       fi
       return 0
    }

    #
    # get_network
    # ===========
    #
    #   like get_ipaddr
    #
    #  Output:         Network of interface
    #
    get_network()
    {
       if echo "$1" | grep -q '^[0-9]*$'
       then
          # check if /etc/config.d/base is already included
          [ "$IP_ETH_N" = '' ] && . /etc/config.d/base
          # get interface
          eval _ipinterface=\${IP_ETH_${1}_NAME}
          [ "$_ipinterface" == "" ] && _ipinterface='eth'`/usr/bin/expr ${1} - 1`
       else
           _ipinterface=$1
       fi
       _ip=''
       _mask=''
       _network=''
       # get ip address and netmask
       if ifconfig ${_ipinterface} 2>/dev/null | grep -q inet
       then
          set `ifconfig ${_ipinterface} | grep inet`
          _ip=`echo $2 | cut -d":" -f2`
          _mask=`echo $4 | cut -d":" -f2`
          # calculate network
          _network=`/usr/local/bin/netcalc network $_ip $_mask`
          echo "$_network"
       fi
       return 0
    }

fi