"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/inet_aton.c" (7 Sep 2020, 5823 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "inet_aton.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*
   13  * Copyright (c) 1983, 1990, 1993
   14  *    The Regents of the University of California.  All rights reserved.
   15  *
   16  * Redistribution and use in source and binary forms, with or without
   17  * modification, are permitted provided that the following conditions
   18  * are met:
   19  * 1. Redistributions of source code must retain the above copyright
   20  *    notice, this list of conditions and the following disclaimer.
   21  * 2. Redistributions in binary form must reproduce the above copyright
   22  *    notice, this list of conditions and the following disclaimer in the
   23  *    documentation and/or other materials provided with the distribution.
   24  * 3. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  */
   40 
   41 /*
   42  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
   43  *
   44  * Permission to use, copy, modify, and distribute this software for any
   45  * purpose with or without fee is hereby granted, provided that the above
   46  * copyright notice and this permission notice appear in all copies, and that
   47  * the name of Digital Equipment Corporation not be used in advertising or
   48  * publicity pertaining to distribution of the document or software without
   49  * specific, written prior permission.
   50  *
   51  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
   52  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
   53  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
   54  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
   55  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
   56  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
   57  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
   58  * SOFTWARE.
   59  */
   60 /*! \file */
   61 
   62 #if defined(LIBC_SCCS) && !defined(lint)
   63 static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
   64 static char rcsid[] = "$Id: inet_aton.c,v 1.23 2008/12/01 23:47:45 tbox Exp $";
   65 #endif /* LIBC_SCCS and not lint */
   66 
   67 #include <config.h>
   68 
   69 #include <ctype.h>
   70 #include <stddef.h>     /* Required for NULL. */
   71 
   72 #include <isc/types.h>
   73 #include <isc/net.h>
   74 
   75 /*%
   76  * Check whether "cp" is a valid ascii representation
   77  * of an Internet address and convert to a binary address.
   78  * Returns 1 if the address is valid, 0 if not.
   79  * This replaces inet_addr, the return value from which
   80  * cannot distinguish between failure and a local broadcast address.
   81  */
   82 int
   83 isc_net_aton(const char *cp, struct in_addr *addr) {
   84     uint32_t val;
   85     int base;
   86     ptrdiff_t n;
   87     unsigned char c;
   88     uint32_t parts[4];
   89     uint32_t *pp = parts;
   90     int digit;
   91 
   92     c = *cp;
   93     for (;;) {
   94         /*
   95          * Collect number up to ``.''.
   96          * Values are specified as for C:
   97          * 0x=hex, 0=octal, isdigit=decimal.
   98          */
   99         if (!isdigit(c & 0xff))
  100             return (0);
  101         val = 0; base = 10; digit = 0;
  102         if (c == '0') {
  103             c = *++cp;
  104             if (c == 'x' || c == 'X')
  105                 base = 16, c = *++cp;
  106             else {
  107                 base = 8;
  108                 digit = 1;
  109             }
  110         }
  111         for (;;) {
  112             /*
  113              * isascii() is valid for all integer values, and
  114              * when it is true, c is known to be in scope
  115              * for isdigit().  No cast necessary.  Similar
  116              * comment applies for later ctype uses.
  117              */
  118             if (isascii(c) && isdigit(c)) {
  119                 if (base == 8 && (c == '8' || c == '9'))
  120                     return (0);
  121                 val = (val * base) + (c - '0');
  122                 c = *++cp;
  123                 digit = 1;
  124             } else if (base == 16 && isascii(c) && isxdigit(c)) {
  125                 val = (val << 4) |
  126                     (c + 10 - (islower(c) ? 'a' : 'A'));
  127                 c = *++cp;
  128                 digit = 1;
  129             } else
  130                 break;
  131         }
  132         if (c == '.') {
  133             /*
  134              * Internet format:
  135              *  a.b.c.d
  136              *  a.b.c   (with c treated as 16 bits)
  137              *  a.b (with b treated as 24 bits)
  138              */
  139             if (pp >= parts + 3 || val > 0xffU)
  140                 return (0);
  141             *pp++ = val;
  142             c = *++cp;
  143         } else
  144             break;
  145     }
  146     /*
  147      * Check for trailing characters.
  148      */
  149     if (c != '\0' && (!isascii(c) || !isspace(c)))
  150         return (0);
  151     /*
  152      * Did we get a valid digit?
  153      */
  154     if (!digit)
  155         return (0);
  156     /*
  157      * Concoct the address according to
  158      * the number of parts specified.
  159      */
  160     n = pp - parts + 1;
  161     switch (n) {
  162     case 1:             /* a -- 32 bits */
  163         break;
  164 
  165     case 2:             /* a.b -- 8.24 bits */
  166         if (val > 0xffffffU)
  167             return (0);
  168         val |= parts[0] << 24;
  169         break;
  170 
  171     case 3:             /* a.b.c -- 8.8.16 bits */
  172         if (val > 0xffffU)
  173             return (0);
  174         val |= (parts[0] << 24) | (parts[1] << 16);
  175         break;
  176 
  177     case 4:             /* a.b.c.d -- 8.8.8.8 bits */
  178         if (val > 0xffU)
  179             return (0);
  180         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  181         break;
  182     }
  183     if (addr != NULL)
  184         addr->s_addr = htonl(val);
  185 
  186     return (1);
  187 }