# -- perl --

# Jun 14 2002
# modified by Suryadi Suryadi for distribution

$try=`netstat > ~/.garbage`;
&PrintOnScreen;
$try=`rm ~/.garbage`;


#*************************************************
#****************** PROCEDURES *******************
#*************************************************


sub PrintOnScreen
{
    $DIR = $ENV{"HOME"};
    open(MYFILE,"$DIR/.garbage") or die "Can't Open File  \n";
    


    while($Option[0] ne "Active")
    {
    	$line=<MYFILE>;
	@Option=split(/ /,$line);
    }
    
    printf("%s %s %s\n", $Option[0], $Option[1], $Option[2]);

    $line=<MYFILE>;
    $line=<MYFILE>;

    printf("%-23s %5s\n","Foreign Address","State");	

    while($line ne "")
    {
	@Option=split(' ',$line);
	printf("%-23s ",$Option[4]);
	if($Option[5] eq "ESTABLISHED")
	{
		printf("EST\n");
	}
        elsif($Option[5] eq "TIME_WAIT")
	{
		printf("TWA\n");
	}
        elsif($Option[5] eq "SYN_SENT")
        {
                printf("SYS\n");
        }
        elsif($Option[5] eq "SYS_RECV")
        {
                printf("SYR\n");
        }
        elsif($Option[5] eq "FIN_WAIT1")
        {
                printf("FW1\n");
        }
        elsif($Option[5] eq "FIN_WAIT2")
        {
                printf("FW2\n");
        }
        elsif($Option[5] eq "CLOSE_WAIT")
        {
                printf("CWA\n");
        }
        elsif($Option[5] eq "LAST_ACK")
        {
                printf("LTA\n");
        }
        elsif($Option[5] eq "LISTEN")
        {
                printf("LIS\n");
        }
        elsif($Option[5] eq "CLOSING")
        {
                printf("CLO\n");
        }
        elsif($Option[5] eq "UNKNOWN")  
        {
                printf("UNK\n");
        } 

	$line=<MYFILE>;
	@Option1=split(' ',$line);
        if($Option1[0] eq "Active")
	{
 		@Option=split(' ',$line);
		printf("\n");
		printf("%s %s %s\n", $Option[0], $Option[1], $Option[3]);
 		$line=<MYFILE>;
		$line=<MYFILE>;
 		printf("%-4s %-5s %-6s %-15s\n","Type","State", "I-Node", "Path");

    		while($line ne "")
    		{
        		@Option=split(' ',$line);
        		if($Option[4] eq "DGRAM")
        		{
                		printf("%-4s ","SKD");
        		}
        		elsif($Option[4] eq "STREAM")
        		{
                		printf("%-4s ","SKS"); 
        		}
        		elsif($Option[4] eq "RAW")
        		{
                		printf("%-4s ","SKR");
        		}       
        		elsif($Option[4] eq "RDM")
        		{
                		printf("%-4s ","SKD");
        		}
        		elsif($Option[4] eq "SEQPACKET")
        		{
                		printf("%-4s ","SKP");
        		}
        		elsif($Option[4] eq "PACKET")
        		{
    				printf("%-4s ","SKP");
			}
			elsif($Option[4] eq "UNKNOWN")
			{
				printf("%-4s ","UNK");
			}        	
			if($Option[5] eq "FREE") 
                        {
                                printf("%-5s ","FRE");
                        }
                        elsif($Option[5] eq "LISTENING")
                        {
                                printf("%-5s ","LSN");
                        }
                        elsif($Option[5] eq "CONNECTING")
                        {
                                printf("%-5s ","CNT");
                        }
                        elsif($Option[5] eq "CONNECTED")
                        {
                                printf("%-5s ","CND");
                        }
                        elsif($Option[5] eq "DISCONNECTING")
                        {
                                printf("%-5s ","DCD");
                        }
                        elsif($Option[5] eq "EMPTY")
                        {
                                printf("%-5s ","EMP");
                        }
                        elsif($Option[5] eq "UNKNOWN")
                        {
                                printf("%-5s ","UNK");
                        }
			printf("%-6s ",$Option[6]);
			printf("%-15s\n",$Option[7]);
			$line=<MYFILE>;

		}
	}
   }
    close(MYFILE);
}



sub Help
{
    	printf("\nUSAGE: netstat \n\n");
	printf("FOR ACTIVE INTERNET");
	printf("\nCONNECTIONS");
	printf("\nEST - ESTABLISHED");
	printf("\nTWA - TIME_WAIT");
	printf("\nSYS - SYN_SENT");
	printf("\nSYR - SYS_RECV");
	printf("\nFW1 - FIN_WAIT1");
	printf("\nFW2 - FIN_WAIT2");
	printf("\nCWA - CLOSE_WAIT");
	printf("\nLTA - LAST_ACK");
	printf("\nLIS - LISTEN");
	printf("\nCLO - CLOSING");
	printf("\nUNK - UNKNOWN");


	printf("\nACTIVE UNIX SOCKETS");
	printf("\nType:");
	printf("\nSKD - SOCK_DGRAM");
	printf("\nSKS - SOCK_STREAM");	
	printf("\nSKR - SOCK_RAW");
	printf("\nSKD - SOCK_RDM");
	printf("\nSKQ - SOCK_SEQPACKET");
	printf("\nSKP - SOCK_PACKET");
	printf("\nUNK - UNKNOWN");

	printf("\nState:");
	printf("\nFRE - FREE");
	printf("\nLSN - LISTENING");
	printf("\nCNT - CONNECTING");
	printf("\nCND - CONNECTED");
	printf("\nDCD - DISCONNECTING");
	printf("\nEMP - EMPTY");
	printf("\nUNK - UNKNOWN");
}
