home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / acld-1.11.tar.gz / acld-1.11.tar / acld-1.11 / inet_aton.c < prev    next >
C/C++ Source or Header  |  2009-01-28  |  5KB  |  169 lines

  1. /*
  2.  * ++Copyright++ 1983, 1990, 1993
  3.  * -
  4.  * Copyright (c) 1983, 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  *
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  *
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #include <sys/param.h>
  57.  
  58. #include <netinet/in.h>
  59. #include <arpa/inet.h>
  60.  
  61. #include <ctype.h>
  62. #include <errno.h>
  63. #include <string.h>
  64. #include <stdlib.h>
  65.  
  66. /*
  67.  * Check whether "cp" is a valid ASCII representation
  68.  * of an Internet address and convert to a binary address.
  69.  * Returns 1 if the address is valid, 0 if not.
  70.  * This replaces inet_addr, the return value from which
  71.  * cannot distinguish between failure and a local broadcast address.
  72.  */
  73. int
  74. inet_aton(cp, addr)
  75.     register const char *cp;
  76.     struct in_addr *addr;
  77. {
  78.     u_long parts[4];
  79.     in_addr_t val;
  80.     char *c;
  81.     char *endptr;
  82.     int gotend, n;
  83.  
  84.     c = (char *)cp;
  85.     n = 0;
  86.     /*
  87.      * Run through the string, grabbing numbers until
  88.      * the end of the string, or some error
  89.      */
  90.     gotend = 0;
  91.     while (!gotend) {
  92.         errno = 0;
  93.         val = strtoul(c, &endptr, 0);
  94.  
  95.         if (errno == ERANGE)    /* Fail completely if it overflowed. */
  96.             return (0);
  97.  
  98.         /*
  99.          * If the whole string is invalid, endptr will equal
  100.          * c.. this way we can make sure someone hasn't
  101.          * gone '.12' or something which would get past
  102.          * the next check.
  103.          */
  104.         if (endptr == c)
  105.             return (0);
  106.         parts[n] = val;
  107.         c = endptr;
  108.  
  109.         /* Check the next character past the previous number's end */
  110.         switch (*c) {
  111.         case '.' :
  112.             /* Make sure we only do 3 dots .. */
  113.             if (n == 3)    /* Whoops. Quit. */
  114.                 return (0);
  115.             n++;
  116.             c++;
  117.             break;
  118.  
  119.         case '\0':
  120.             gotend = 1;
  121.             break;
  122.  
  123.         default:
  124.             if (isspace((unsigned char)*c)) {
  125.                 gotend = 1;
  126.                 break;
  127.             } else
  128.                 return (0);    /* Invalid character, so fail */
  129.         }
  130.  
  131.     }
  132.  
  133.     /*
  134.      * Concoct the address according to
  135.      * the number of parts specified.
  136.      */
  137.  
  138.     switch (n) {
  139.     case 0:                /* a -- 32 bits */
  140.         /*
  141.          * Nothing is necessary here.  Overflow checking was
  142.          * already done in strtoul().
  143.          */
  144.         break;
  145.     case 1:                /* a.b -- 8.24 bits */
  146.         if (val > 0xffffff || parts[0] > 0xff)
  147.             return (0);
  148.         val |= parts[0] << 24;
  149.         break;
  150.  
  151.     case 2:                /* a.b.c -- 8.8.16 bits */
  152.         if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
  153.             return (0);
  154.         val |= (parts[0] << 24) | (parts[1] << 16);
  155.         break;
  156.  
  157.     case 3:                /* a.b.c.d -- 8.8.8.8 bits */
  158.         if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
  159.             parts[2] > 0xff)
  160.             return (0);
  161.         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  162.         break;
  163.     }
  164.  
  165.     if (addr != NULL)
  166.         addr->s_addr = htonl(val);
  167.     return (1);
  168. }
  169.