home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / unixlib_1 / !UnixLib37_netlib_netlib_c_inet_aton0 < prev    next >
Encoding:
Text File  |  1996-07-28  |  4.2 KB  |  155 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source$
  4.  * $Date$
  5.  * $Revision$
  6.  * $State$
  7.  * $Author$
  8.  *
  9.  * $Log$
  10.  ***************************************************************************/
  11.  
  12. static const char rcs_id[] = "$Id$";
  13.  
  14. /*
  15.  * Copyright (c) 1983, 1990, 1993
  16.  *      The Regents of the University of California.  All rights reserved.
  17.  *
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the above copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *      This product includes software developed by the University of
  29.  *      California, Berkeley and its contributors.
  30.  * 4. Neither the name of the University nor the names of its contributors
  31.  *    may be used to endorse or promote products derived from this software
  32.  *    without specific prior written permission.
  33.  *
  34.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  35.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  38.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44.  * SUCH DAMAGE.
  45.  */
  46.  
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <sys/byteorder.h>
  50. #include <ctype.h>
  51.  
  52. /*
  53.  * Check whether "cp" is a valid ascii representation
  54.  * of an Internet address and convert to a binary address.
  55.  * Returns 1 if the address is valid, 0 if not.
  56.  * This replaces inet_addr, the return value from which
  57.  * cannot distinguish between failure and a local broadcast address.
  58.  */
  59.  
  60. u_long
  61. inet_aton (const char *cp, struct in_addr *addr)
  62. {
  63.   u_long val;
  64.   int base, n;
  65.   char c;
  66.   u_int parts[4];
  67.   u_int *pp = parts;
  68.  
  69.   for (;;)
  70.     {
  71.       /*
  72.        * Collect number up to ``.''.
  73.        * Values are specified as for C:
  74.        * 0x=hex, 0=octal, other=decimal.
  75.        */
  76.       val = 0;
  77.       base = 10;
  78.       if (*cp == '0')
  79.     {
  80.       if (*++cp == 'x' || *cp == 'X')
  81.         base = 16, cp++;
  82.       else
  83.         base = 8;
  84.     }
  85.       while ((c = *cp) != '\0')
  86.     {
  87.       if (isascii (c) && isdigit (c))
  88.         {
  89.           val = (val * base) + (int) (c - '0');
  90.           cp++;
  91.           continue;
  92.         }
  93.       if (base == 16 && isascii (c) && isxdigit (c))
  94.         {
  95.           val = (val << 4) +
  96.         (c + 10 - (islower (c) ? 'a' : 'A'));
  97.           cp++;
  98.           continue;
  99.         }
  100.       break;
  101.     }
  102.       if (*cp == '.')
  103.     {
  104.       /*
  105.        * Internet format:
  106.        *      a.b.c.d
  107.        *      a.b.c   (with c treated as 16-bits)
  108.        *      a.b     (with b treated as 24 bits)
  109.        */
  110.       if (pp >= parts + 3 || val > 0xff)
  111.         return (0);
  112.       *pp++ = (u_int) val, cp++;
  113.     }
  114.       else
  115.     break;
  116.     }
  117.   /*
  118.    * Check for trailing characters.
  119.    */
  120.   if (*cp && (!isascii (*cp) || !isspace (*cp)))
  121.     return (0);
  122.   /*
  123.    * Concoct the address according to
  124.    * the number of parts specified.
  125.    */
  126.   n = pp - parts + 1;
  127.   switch (n)
  128.     {
  129.  
  130.     case 1:            /* a -- 32 bits */
  131.       break;
  132.  
  133.     case 2:            /* a.b -- 8.24 bits */
  134.       if (val > 0xffffff)
  135.     return (0);
  136.       val |= (u_long) parts[0] << 24;
  137.       break;
  138.  
  139.     case 3:            /* a.b.c -- 8.8.16 bits */
  140.       if (val > 0xffff)
  141.     return (0);
  142.       val |= (parts[0] << 24) | (parts[1] << 16);
  143.       break;
  144.  
  145.     case 4:            /* a.b.c.d -- 8.8.8.8 bits */
  146.       if (val > 0xff)
  147.     return (0);
  148.       val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  149.       break;
  150.     }
  151.   if (addr)
  152.     addr->s_addr = htonl (val);
  153.   return (1);
  154. }
  155.