inet_aton.c

Go to the documentation of this file.
00001 /*
00002  * Portions Copyright (C) 2004, 2005, 2007, 2008, 2012-2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Portions Copyright (C) 1996-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /*
00019  * Copyright (c) 1983, 1990, 1993
00020  *    The Regents of the University of California.  All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. Neither the name of the University nor the names of its contributors
00031  *    may be used to endorse or promote products derived from this software
00032  *    without specific prior written permission.
00033  *
00034  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00035  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00036  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00037  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00038  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00039  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00040  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00041  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00042  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00043  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00044  * SUCH DAMAGE.
00045  */
00046 
00047 /*
00048  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00049  *
00050  * Permission to use, copy, modify, and distribute this software for any
00051  * purpose with or without fee is hereby granted, provided that the above
00052  * copyright notice and this permission notice appear in all copies, and that
00053  * the name of Digital Equipment Corporation not be used in advertising or
00054  * publicity pertaining to distribution of the document or software without
00055  * specific, written prior permission.
00056  *
00057  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00058  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00059  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00060  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00061  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00062  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00063  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00064  * SOFTWARE.
00065  */
00066 /*! \file */
00067 
00068 #if defined(LIBC_SCCS) && !defined(lint)
00069 static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
00070 static char rcsid[] = "$Id: inet_aton.c,v 1.23 2008/12/01 23:47:45 tbox Exp $";
00071 #endif /* LIBC_SCCS and not lint */
00072 
00073 #include <config.h>
00074 
00075 #include <ctype.h>
00076 #include <stddef.h>             /* Required for NULL. */
00077 
00078 #include <isc/types.h>
00079 #include <isc/net.h>
00080 
00081 /*%
00082  * Check whether "cp" is a valid ascii representation
00083  * of an Internet address and convert to a binary address.
00084  * Returns 1 if the address is valid, 0 if not.
00085  * This replaces inet_addr, the return value from which
00086  * cannot distinguish between failure and a local broadcast address.
00087  */
00088 int
00089 isc_net_aton(const char *cp, struct in_addr *addr) {
00090         isc_uint32_t val;
00091         int base;
00092         ptrdiff_t n;
00093         unsigned char c;
00094         isc_uint8_t parts[4];
00095         isc_uint8_t *pp = parts;
00096         int digit;
00097 
00098         c = *cp;
00099         for (;;) {
00100                 /*
00101                  * Collect number up to ``.''.
00102                  * Values are specified as for C:
00103                  * 0x=hex, 0=octal, isdigit=decimal.
00104                  */
00105                 if (!isdigit(c & 0xff))
00106                         return (0);
00107                 val = 0; base = 10; digit = 0;
00108                 if (c == '0') {
00109                         c = *++cp;
00110                         if (c == 'x' || c == 'X')
00111                                 base = 16, c = *++cp;
00112                         else {
00113                                 base = 8;
00114                                 digit = 1;
00115                         }
00116                 }
00117                 for (;;) {
00118                         /*
00119                          * isascii() is valid for all integer values, and
00120                          * when it is true, c is known to be in scope
00121                          * for isdigit().  No cast necessary.  Similar
00122                          * comment applies for later ctype uses.
00123                          */
00124                         if (isascii(c) && isdigit(c)) {
00125                                 if (base == 8 && (c == '8' || c == '9'))
00126                                         return (0);
00127                                 val = (val * base) + (c - '0');
00128                                 c = *++cp;
00129                                 digit = 1;
00130                         } else if (base == 16 && isascii(c) && isxdigit(c)) {
00131                                 val = (val << 4) |
00132                                         (c + 10 - (islower(c) ? 'a' : 'A'));
00133                                 c = *++cp;
00134                                 digit = 1;
00135                         } else
00136                                 break;
00137                 }
00138                 if (c == '.') {
00139                         /*
00140                          * Internet format:
00141                          *      a.b.c.d
00142                          *      a.b.c   (with c treated as 16 bits)
00143                          *      a.b     (with b treated as 24 bits)
00144                          */
00145                         if (pp >= parts + 3 || val > 0xffU)
00146                                 return (0);
00147                         *pp++ = (isc_uint8_t)val;
00148                         c = *++cp;
00149                 } else
00150                         break;
00151         }
00152         /*
00153          * Check for trailing characters.
00154          */
00155         if (c != '\0' && (!isascii(c) || !isspace(c)))
00156                 return (0);
00157         /*
00158          * Did we get a valid digit?
00159          */
00160         if (!digit)
00161                 return (0);
00162         /*
00163          * Concoct the address according to
00164          * the number of parts specified.
00165          */
00166         n = pp - parts + 1;
00167         switch (n) {
00168         case 1:                         /* a -- 32 bits */
00169                 break;
00170 
00171         case 2:                         /* a.b -- 8.24 bits */
00172                 if (val > 0xffffffU)
00173                         return (0);
00174                 val |= parts[0] << 24;
00175                 break;
00176 
00177         case 3:                         /* a.b.c -- 8.8.16 bits */
00178                 if (val > 0xffffU)
00179                         return (0);
00180                 val |= (parts[0] << 24) | (parts[1] << 16);
00181                 break;
00182 
00183         case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
00184                 if (val > 0xffU)
00185                         return (0);
00186                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
00187                 break;
00188         }
00189         if (addr != NULL)
00190                 addr->s_addr = htonl(val);
00191 
00192         return (1);
00193 }

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha