home *** CD-ROM | disk | FTP | other *** search
/ Steganos Hacker Tools / SHT151.iso / programme / scanner / nmapNTsp1 / Win_2000.exe / nmapNT-src / inet_aton.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-05  |  2.5 KB  |  107 lines

  1. /***** THIS CODE IS FROM GLIBC 2.0.6 AND IS INCLUDED IN NMAP BECAUSE SOLARIS
  2.        DOES NOT SEEM TO CONTAIN THE inet_aton(3C) FUNCTION (!).  IT
  3.        IS VERY SLIGHTLY MODIFIED.  SPECIFICALLY, THIS COMES FROM
  4.        resolv/inet_addr.c --fyodor@dhp.com   ******/
  5.  
  6. #include "nmap.h"
  7. /*
  8.  * Check whether "cp" is a valid ascii representation
  9.  * of an Internet address and convert to a binary address.
  10.  * Returns 1 if the address is valid, 0 if not.
  11.  * This replaces inet_addr, the return value from which
  12.  * cannot distinguish between failure and a local broadcast address.
  13.  */
  14. int
  15. inet_aton(cp, addr)
  16.     register const char *cp;
  17.     struct in_addr *addr;
  18. {
  19.     register unsigned int val;    /* changed from u_long --david */
  20.     register int base, n;
  21.     register char c;
  22.     u_int parts[4];
  23.     register u_int *pp = parts;
  24.  
  25.     c = *cp;
  26.     for (;;) {
  27.         /*
  28.          * Collect number up to ``.''.
  29.          * Values are specified as for C:
  30.          * 0x=hex, 0=octal, isdigit=decimal.
  31.          */
  32.         if (!isdigit((int)c))
  33.             return (0);
  34.         val = 0; base = 10;
  35.         if (c == '0') {
  36.             c = *++cp;
  37.             if (c == 'x' || c == 'X')
  38.                 base = 16, c = *++cp;
  39.             else
  40.                 base = 8;
  41.         }
  42.         for (;;) {
  43.             if (isascii((int)c) && isdigit((int)c)) {
  44.                 val = (val * base) + (c - '0');
  45.                 c = *++cp;
  46.             } else if (base == 16 && isascii((int)c) && isxdigit((int)c)) {
  47.                 val = (val << 4) |
  48.                     (c + 10 - (islower((int)c) ? 'a' : 'A'));
  49.                 c = *++cp;
  50.             } else
  51.                 break;
  52.         }
  53.         if (c == '.') {
  54.             /*
  55.              * Internet format:
  56.              *    a.b.c.d
  57.              *    a.b.c    (with c treated as 16 bits)
  58.              *    a.b    (with b treated as 24 bits)
  59.              */
  60.             if (pp >= parts + 3)
  61.                 return (0);
  62.             *pp++ = val;
  63.             c = *++cp;
  64.         } else
  65.             break;
  66.     }
  67.     /*
  68.      * Check for trailing characters.
  69.      */
  70.     if (c != '\0' && (!isascii((int)c) || !isspace((int)c)))
  71.         return (0);
  72.     /*
  73.      * Concoct the address according to
  74.      * the number of parts specified.
  75.      */
  76.     n = pp - parts + 1;
  77.     switch (n) {
  78.  
  79.     case 0:
  80.         return (0);        /* initial nondigit */
  81.  
  82.     case 1:                /* a -- 32 bits */
  83.         break;
  84.  
  85.     case 2:                /* a.b -- 8.24 bits */
  86.         if (val > 0xffffff)
  87.             return (0);
  88.         val |= parts[0] << 24;
  89.         break;
  90.  
  91.     case 3:                /* a.b.c -- 8.8.16 bits */
  92.         if (val > 0xffff)
  93.             return (0);
  94.         val |= (parts[0] << 24) | (parts[1] << 16);
  95.         break;
  96.  
  97.     case 4:                /* a.b.c.d -- 8.8.8.8 bits */
  98.         if (val > 0xff)
  99.             return (0);
  100.         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  101.         break;
  102.     }
  103.     if (addr)
  104.         addr->s_addr = htonl(val);
  105.     return (1);
  106. }
  107.