Blame resolv/inet_addr.c

Packit Service e404fa
/* Legacy IPv4 text-to-address functions.
Packit Service e404fa
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service e404fa
   This file is part of the GNU C Library.
Packit Service e404fa
Packit Service e404fa
   The GNU C Library is free software; you can redistribute it and/or
Packit Service e404fa
   modify it under the terms of the GNU Lesser General Public
Packit Service e404fa
   License as published by the Free Software Foundation; either
Packit Service e404fa
   version 2.1 of the License, or (at your option) any later version.
Packit Service e404fa
Packit Service e404fa
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service e404fa
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service e404fa
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service e404fa
   Lesser General Public License for more details.
Packit Service e404fa
Packit Service e404fa
   You should have received a copy of the GNU Lesser General Public
Packit Service e404fa
   License along with the GNU C Library; if not, see
Packit Service e404fa
   <http://www.gnu.org/licenses/>.  */
Packit Service e404fa
Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1983, 1990, 1993
Packit 6c4009
 *    The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies, and that
Packit 6c4009
 * the name of Digital Equipment Corporation not be used in advertising or
Packit 6c4009
 * publicity pertaining to distribution of the document or software without
Packit 6c4009
 * specific, written prior permission.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
Packit 6c4009
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
Packit 6c4009
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
Packit 6c4009
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit 6c4009
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit 6c4009
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit 6c4009
 * SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
Packit 6c4009
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
Packit 6c4009
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
Packit 6c4009
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit 6c4009
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit 6c4009
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit 6c4009
 * SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
Packit 6c4009
#include <endian.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit Service e404fa
/* Check whether "cp" is a valid ASCII representation of an IPv4
Packit Service e404fa
   Internet address and convert it to a binary address.  Returns 1 if
Packit Service e404fa
   the address is valid, 0 if not.  This replaces inet_addr, the
Packit Service e404fa
   return value from which cannot distinguish between failure and a
Packit Service 50534c
   local broadcast address.  Write a pointer to the first
Packit Service 50534c
   non-converted character to *endp.  */
Packit Service 50534c
static int
Packit Service 50534c
inet_aton_end (const char *cp, struct in_addr *addr, const char **endp)
Packit Service 894d4d
{
Packit Service e404fa
  static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
Packit Service e404fa
  in_addr_t val;
Packit Service e404fa
  char c;
Packit Service e404fa
  union iaddr
Packit Service e404fa
  {
Packit Service e404fa
    uint8_t bytes[4];
Packit Service e404fa
    uint32_t word;
Packit Service e404fa
  } res;
Packit Service e404fa
  uint8_t *pp = res.bytes;
Packit Service e404fa
  int digit;
Packit Service e404fa
Packit Service e404fa
  int saved_errno = errno;
Packit Service e404fa
  __set_errno (0);
Packit Service e404fa
Packit Service e404fa
  res.word = 0;
Packit Service e404fa
Packit Service e404fa
  c = *cp;
Packit Service e404fa
  for (;;)
Packit Service e404fa
    {
Packit Service e404fa
      /* Collect number up to ``.''.  Values are specified as for C:
Packit Service e404fa
	 0x=hex, 0=octal, isdigit=decimal.  */
Packit Service e404fa
      if (!isdigit (c))
Packit Service e404fa
	goto ret_0;
Packit Service e404fa
      {
Packit Service e404fa
	char *endp;
Packit Service e404fa
	unsigned long ul = strtoul (cp, &endp, 0);
Packit Service e404fa
	if (ul == ULONG_MAX && errno == ERANGE)
Packit Service 562438
	  goto ret_0;
Packit Service e404fa
	if (ul > 0xfffffffful)
Packit Service e404fa
	  goto ret_0;
Packit Service e404fa
	val = ul;
Packit Service e404fa
	digit = cp != endp;
Packit Service e404fa
	cp = endp;
Packit Service e404fa
      }
Packit Service e404fa
      c = *cp;
Packit Service e404fa
      if (c == '.')
Packit Service e404fa
	{
Packit Service e404fa
	  /* Internet format:
Packit Service e404fa
	     a.b.c.d
Packit Service e404fa
	     a.b.c	(with c treated as 16 bits)
Packit Service e404fa
	     a.b	(with b treated as 24 bits).  */
Packit Service e404fa
	  if (pp > res.bytes + 2 || val > 0xff)
Packit Service e404fa
	    goto ret_0;
Packit Service e404fa
	  *pp++ = val;
Packit Service e404fa
	  c = *++cp;
Packit Service e404fa
	}
Packit Service e404fa
      else
Packit Service e404fa
	break;
Packit Service e404fa
    }
Packit Service e404fa
  /* Check for trailing characters.  */
Packit Service e404fa
  if (c != '\0' && (!isascii (c) || !isspace (c)))
Packit Service e404fa
    goto ret_0;
Packit Service e404fa
  /*  Did we get a valid digit?  */
Packit Service e404fa
  if (!digit)
Packit Service e404fa
    goto ret_0;
Packit Service e404fa
Packit Service e404fa
  /* Check whether the last part is in its limits depending on the
Packit Service e404fa
     number of parts in total.  */
Packit Service e404fa
  if (val > max[pp - res.bytes])
Packit Service e404fa
    goto ret_0;
Packit Service e404fa
Packit Service e404fa
  if (addr != NULL)
Packit Service e404fa
    addr->s_addr = res.word | htonl (val);
Packit Service 50534c
  *endp = cp;
Packit Service e404fa
Packit Service e404fa
  __set_errno (saved_errno);
Packit Service e404fa
  return 1;
Packit Service e404fa
Packit Service e404fa
 ret_0:
Packit Service e404fa
  __set_errno (saved_errno);
Packit Service e404fa
  return 0;
Packit Service 894d4d
}
Packit Service 50534c
Packit Service 50534c
int
Packit Service 50534c
__inet_aton_exact (const char *cp, struct in_addr *addr)
Packit Service 50534c
{
Packit Service 50534c
  struct in_addr val;
Packit Service 50534c
  const char *endp;
Packit Service 50534c
  /* Check that inet_aton_end parsed the entire string.  */
Packit Service 50534c
  if (inet_aton_end (cp, &val, &endp) != 0 && *endp == 0)
Packit Service 50534c
    {
Packit Service 50534c
      *addr = val;
Packit Service 50534c
      return 1;
Packit Service 50534c
    }
Packit Service 50534c
  else
Packit Service 50534c
    return 0;
Packit Service 50534c
}
Packit Service 50534c
libc_hidden_def (__inet_aton_exact)
Packit Service 50534c
Packit Service 50534c
/* inet_aton ignores trailing garbage.  */
Packit Service 50534c
int
Packit Service 50534c
__inet_aton_ignore_trailing (const char *cp, struct in_addr *addr)
Packit Service 50534c
{
Packit Service 50534c
  const char *endp;
Packit Service 50534c
  return  inet_aton_end (cp, addr, &endp);
Packit Service 50534c
}
Packit Service 50534c
weak_alias (__inet_aton_ignore_trailing, inet_aton)
Packit Service 50534c
Packit Service 50534c
/* ASCII IPv4 Internet address interpretation routine.  The value
Packit Service 50534c
   returned is in network order.  */
Packit Service 50534c
in_addr_t
Packit Service 50534c
__inet_addr (const char *cp)
Packit Service 50534c
{
Packit Service 50534c
  struct in_addr val;
Packit Service 50534c
  const char *endp;
Packit Service 50534c
  if (inet_aton_end (cp, &val, &endp))
Packit Service 50534c
    return val.s_addr;
Packit Service 50534c
  return INADDR_NONE;
Packit Service 50534c
}
Packit Service 50534c
weak_alias (__inet_addr, inet_addr)