Back to index

glibc  2.9
inet_addr.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1983, 1990, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies, and that
00036  * the name of Digital Equipment Corporation not be used in advertising or
00037  * publicity pertaining to distribution of the document or software without
00038  * specific, written prior permission.
00039  *
00040  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00041  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00042  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00043  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00044  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00045  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00046  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047  * SOFTWARE.
00048  */
00049 
00050 /*
00051  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00052  *
00053  * Permission to use, copy, modify, and distribute this software for any
00054  * purpose with or without fee is hereby granted, provided that the above
00055  * copyright notice and this permission notice appear in all copies.
00056  *
00057  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00058  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00059  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00060  * CONSORTIUM 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 
00067 #if defined(LIBC_SCCS) && !defined(lint)
00068 static const char sccsid[] = "@(#)inet_addr.c    8.1 (Berkeley) 6/17/93";
00069 static const char rcsid[] = "$BINDId: inet_addr.c,v 8.11 1999/10/13 16:39:25 vixie Exp $";
00070 #endif /* LIBC_SCCS and not lint */
00071 
00072 #include <sys/types.h>
00073 #include <sys/param.h>
00074 
00075 #include <netinet/in.h>
00076 #include <arpa/inet.h>
00077 
00078 #include <ctype.h>
00079 
00080 #ifdef _LIBC
00081 # include <endian.h>
00082 # include <stdint.h>
00083 # include <stdlib.h>
00084 # include <limits.h>
00085 # include <errno.h>
00086 #endif
00087 
00088 /*
00089  * Ascii internet address interpretation routine.
00090  * The value returned is in network order.
00091  */
00092 in_addr_t
00093 inet_addr(const char *cp) {
00094        struct in_addr val;
00095 
00096        if (__inet_aton(cp, &val))
00097               return (val.s_addr);
00098        return (INADDR_NONE);
00099 }
00100 
00101 /*
00102  * Check whether "cp" is a valid ascii representation
00103  * of an Internet address and convert to a binary address.
00104  * Returns 1 if the address is valid, 0 if not.
00105  * This replaces inet_addr, the return value from which
00106  * cannot distinguish between failure and a local broadcast address.
00107  */
00108 int
00109 __inet_aton(const char *cp, struct in_addr *addr)
00110 {
00111        static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
00112        in_addr_t val;
00113 #ifndef _LIBC
00114        int base;
00115 #endif
00116        char c;
00117        union iaddr {
00118          uint8_t bytes[4];
00119          uint32_t word;
00120        } res;
00121        uint8_t *pp = res.bytes;
00122        int digit;
00123 
00124 #ifdef _LIBC
00125        int saved_errno = errno;
00126        __set_errno (0);
00127 #endif
00128 
00129        res.word = 0;
00130 
00131        c = *cp;
00132        for (;;) {
00133               /*
00134                * Collect number up to ``.''.
00135                * Values are specified as for C:
00136                * 0x=hex, 0=octal, isdigit=decimal.
00137                */
00138               if (!isdigit(c))
00139                      goto ret_0;
00140 #ifdef _LIBC
00141               {
00142                      char *endp;
00143                      unsigned long ul = strtoul (cp, (char **) &endp, 0);
00144                      if (ul == ULONG_MAX && errno == ERANGE)
00145                             goto ret_0;
00146                      if (ul > 0xfffffffful)
00147                             goto ret_0;
00148                      val = ul;
00149                      digit = cp != endp;
00150                      cp = endp;
00151               }
00152               c = *cp;
00153 #else
00154               val = 0; base = 10; digit = 0;
00155               if (c == '0') {
00156                      c = *++cp;
00157                      if (c == 'x' || c == 'X')
00158                             base = 16, c = *++cp;
00159                      else {
00160                             base = 8;
00161                             digit = 1 ;
00162                      }
00163               }
00164               for (;;) {
00165                      if (isascii(c) && isdigit(c)) {
00166                             if (base == 8 && (c == '8' || c == '9'))
00167                                    return (0);
00168                             val = (val * base) + (c - '0');
00169                             c = *++cp;
00170                             digit = 1;
00171                      } else if (base == 16 && isascii(c) && isxdigit(c)) {
00172                             val = (val << 4) |
00173                                    (c + 10 - (islower(c) ? 'a' : 'A'));
00174                             c = *++cp;
00175                             digit = 1;
00176                      } else
00177                             break;
00178               }
00179 #endif
00180               if (c == '.') {
00181                      /*
00182                       * Internet format:
00183                       *     a.b.c.d
00184                       *     a.b.c  (with c treated as 16 bits)
00185                       *     a.b    (with b treated as 24 bits)
00186                       */
00187                      if (pp > res.bytes + 2 || val > 0xff)
00188                             goto ret_0;
00189                      *pp++ = val;
00190                      c = *++cp;
00191               } else
00192                      break;
00193        }
00194        /*
00195         * Check for trailing characters.
00196         */
00197        if (c != '\0' && (!isascii(c) || !isspace(c)))
00198               goto ret_0;
00199        /*
00200         * Did we get a valid digit?
00201         */
00202        if (!digit)
00203               goto ret_0;
00204 
00205        /* Check whether the last part is in its limits depending on
00206           the number of parts in total.  */
00207        if (val > max[pp - res.bytes])
00208          goto ret_0;
00209 
00210        if (addr != NULL)
00211               addr->s_addr = res.word | htonl (val);
00212 
00213 #ifdef _LIBC
00214        __set_errno (saved_errno);
00215 #endif
00216        return (1);
00217 
00218 ret_0:
00219 #ifdef _LIBC
00220        __set_errno (saved_errno);
00221 #endif
00222        return (0);
00223 }
00224 weak_alias (__inet_aton, inet_aton)
00225 libc_hidden_def (__inet_aton)
00226 libc_hidden_weak (inet_aton)