#!/bin/sh

# --------------------------------------
# get_aliases  "ppa"  "pid"
#
#   ppa    Physical point of attachment
#   pid    pid of the calling processes
#   
#   ppa is an old adapter "slot"    
#   identifier.  As in "hme0" or "hme1"
#   
#
# get_aliases is invoked by the "nbdaemon"
# program when it is setting up the netbios
# driver.  It is invoked once for each
# possible ppa value.  The default is 
# the range 0-15.
#   
# get_aliases first examines the "boards"
# file to get the adapter name which
# we are interested in.  Those adapters 
# are matched to the output of the ifconfig     
# command so that we can then parse for the     
# aliases of those adapters, but not the        
# base adapter itself.                          
#
# get_aliases creates an output file            
# /tmp/nbPID, where PID is the supplied         
# value.  The calling program deletes           
# the output file created by get_aliases.       
#
# The output file looks like:
# 10.0.0.1 ff000000 10.255.255.255
# 10.0.0.2 ff000000 10.255.255.255 
#  where the 10.0.. are aliases to hme0.
#                                               
# --------------------------------------

BRD=/etc/opt/SUNWlznb

#-----------------------------------
# This part checks the boards file for
# a single line matching the ppa value
# supplied on the command line.       
#
# A sample boards file line:           
# hme0:1 	 0 	up	-	csma/cd
# .......... 	^^^^   ....    ...      ......
#               ppa
#
#-----------------------------------

PID=$2
var=`cat $BRD/boards | nawk -v ppa=$1 '{
	if ( $2 == ppa) {
		print $1;
		} }' `

if [ -z "$var" ]; then
	exit 1;
fi

/usr/sbin/ifconfig -au inet | nawk -v ifname=$var -v pid=$PID '
BEGIN {
    correct_alias = 0; 
    filename="/tmp/nb"pid;
    split(ifname,pattern,":");
}
{
# check for down interfaces
    if (match ($0, "^[^ 	]")) {
	correct_alias = 0;
    }
# This line must begin with the adapter - "hme0:1:"
    if ( match ($0, pattern[1]":") && !match ($0, pattern[1]":[ 	]") ) {
# This line must have an adapter name which is an alias "hme0:1:"
# The key to array size being >2 is the trailing ":"
        array_size = split($1,alias_def,":") ;
        if ( array_size > 2) {
# The adapter must be up and public
    	    if (match($0, "UP") && !match($0, "PRIVATE") ) {
	        correct_alias = 1;
             }
        }
    }

#--------------------------------------------
# Now, get the network info line, which starts 
# with "inet"
# Take the IP address, mask ,etc from this line
# The output file looks like:
# 10.0.0.1 ff000000 10.255.255.255
# 10.0.0.2 ff000000 10.255.255.255 
# where the 10.0.. are aliases to hme0.
#--------------------------------------------
    if ( match ($0, "inet") ) {
	if ( correct_alias == 1 ) {
	    printf("%s %s %s\n", $2, $4, $6) > filename;
	    correct_alias = 0;
	}
    }
}
END {
}'
