
/*
 *
 *     Module Name: nainet.c
 *	       Version: 1.00
 *   Original Date: 09/15/02	
 *	        Author: Charles Gordon
 *	      Language: Ansi C
 * Compile Options:
 * Compile defines:
 *	     Libraries:
 *    Link Options:
 *
 *    Entry Points: NAinet_addr()
 *
 *  Copyright (c) Digi International Inc., All Rights Reserved
 *  
 *  This software contains proprietary and confidential information of Digi
 *  International Inc.  By accepting transfer of this copy, Recipient agrees
 *  to retain this software in confidence, to prevent disclosure to others,
 *  and to make no use of this software other than that for which it was
 *  delivered.  This is an unpublished copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *  
 *  Restricted Rights Legend
 * 
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 * 
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 * Description.
 * =======================================================================
 *  Created to make copy of NAinet_addr() for bootloader which does not
 *  want the other stuff inside of narmsock.c.
 *
 *
 *
 * Edit Date/Ver   Edit Description
 * ==============  =======================================================
 *
 *
 */
#include <narmapi.h>
#include <sockapi.h>
#include <netdev.h>
#include <Npttypes.h>

/** 
 * Converts an IP address from a string into an unsigned long, in network
 *
 * @external
 * @category general_purpose
 * @since 6.0
 *
 * @param   addrp     Pointer to a NULL-terminated string that represents an IP address. 
 *		      (for example, 7.92.186.198)
 *
 * @return  0          Invalid IP address.
 * @return  otherwise  Unsigned long representation of the IP address defined by the
 *		       addrp argument.
 */

unsigned long NAInet_addr(char * addrp)
{
      unsigned long l;
      unsigned int val;
      int i;
      l = 0;

      NA_ASSERT(addrp != NULL);
      
      for (i = 0; i < 4; i++)
      {
           l <<= 8;
           if (*addrp != '\0') 
           {
               val = 0;
               while (*addrp != '\0' && *addrp != '.') 
               {  
                    val *= 10;
                    val += *addrp - '0';
                    addrp++;
               }
               if (val > 255)
               {
                   return 0;
               }
               l |= val;
               if (*addrp != '\0' && *addrp != '.') 
               {
                   return 0;
               }
               if (*addrp != '\0') 
                   addrp++;
           } 
       }
        #ifdef BOOT_LOADER
            return(l);
        #else
            return(htonl(l));
        #endif
}

/**
 * Converts an IP address from an unsigned long into dotted string notation (a.b.c.d).
 *
 * @external
 * @category general_purpose
 * @since 6.0
 *
 * @param   ulipaddr    Long format of the IP address to convert.
 * @param   destp       Pointer to the destination of the character representation. This
 *                      argument must point to user specified storage of at least 16 or 
 *                      more characters long (15 characters plus the NULL terminator).
 *
 * @note    This function provides similar functionality to inet_toa(), however not identical.
 *          Most importantly, the destp argument is user storage, as opposed to static 
 *          memory defined by the funcion.
 */
void NAInet_toa(unsigned long ulipaddr, char * destp)
{
    unsigned long ltemp;
    
    NA_ASSERT(destp != NULL);

    ltemp = (0xFF000000 & ulipaddr) >> 24; 
    sprintf (destp, "%ld.", ltemp);
    ltemp = (0x00FF0000 & ulipaddr) >> 16;
    sprintf (destp, "%s%ld.", destp, ltemp);
    ltemp = (0x0000FF00 & ulipaddr) >> 8;
    sprintf (destp, "%s%ld.", destp, ltemp);
    ltemp = (0x000000FF & ulipaddr);
    sprintf (destp, "%s%ld", destp, ltemp);
}
/*
 * NAIpAddress
 *
 * This routine returns the IP address being used by the network
 * TCP/UDP/IP protocol stacks.
 *
 * RETURNS
 *
 *    Unit IP address
 */

unsigned long
NAIpAddress ()
{
 
 
    netdev 			*ndp;
	extern   netdev  *ll_dev_2_ndp (char * devname);

    unsigned long iplong = 0;
    if ( (ndp = ll_dev_2_ndp("eth0")) != (netdev *)0 )
    {
		iplong = ndp->nd_ipas[0].nd_ipaddr;
    }
    return (ntohl(iplong));
}

