Blame resolv/inet_addr.c

Packit Service d9f699
/* Legacy IPv4 text-to-address functions.
Packit Service d9f699
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service d9f699
   This file is part of the GNU C Library.
Packit Service d9f699
Packit Service d9f699
   The GNU C Library is free software; you can redistribute it and/or
Packit Service d9f699
   modify it under the terms of the GNU Lesser General Public
Packit Service d9f699
   License as published by the Free Software Foundation; either
Packit Service d9f699
   version 2.1 of the License, or (at your option) any later version.
Packit Service d9f699
Packit Service d9f699
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service d9f699
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d9f699
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service d9f699
   Lesser General Public License for more details.
Packit Service d9f699
Packit Service d9f699
   You should have received a copy of the GNU Lesser General Public
Packit Service d9f699
   License along with the GNU C Library; if not, see
Packit Service d9f699
   <http://www.gnu.org/licenses/>.  */
Packit Service d9f699
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 d9f699
/* ASCII IPv4 Internet address interpretation routine.  The value
Packit Service d9f699
   returned is in network order.  */
Packit Service 541783
in_addr_t
Packit Service d9f699
__inet_addr (const char *cp)
Packit Service d9f699
{
Packit Service d9f699
  struct in_addr val;
Packit Service 50534c
Packit Service d9f699
  if (__inet_aton (cp, &val))
Packit Service d9f699
    return val.s_addr;
Packit Service d9f699
  return INADDR_NONE;
Packit Service 50534c
}
Packit Service 541783
weak_alias (__inet_addr, inet_addr)
Packit Service 50534c
Packit Service d9f699
/* Check whether "cp" is a valid ASCII representation of an IPv4
Packit Service d9f699
   Internet address and convert it to a binary address.  Returns 1 if
Packit Service d9f699
   the address is valid, 0 if not.  This replaces inet_addr, the
Packit Service d9f699
   return value from which cannot distinguish between failure and a
Packit Service d9f699
   local broadcast address.  */
Packit Service 50534c
int
Packit Service d9f699
__inet_aton (const char *cp, struct in_addr *addr)
Packit Service 50534c
{
Packit Service d9f699
  static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
Packit Service d9f699
  in_addr_t val;
Packit Service d9f699
  char c;
Packit Service d9f699
  union iaddr
Packit Service d9f699
  {
Packit Service d9f699
    uint8_t bytes[4];
Packit Service d9f699
    uint32_t word;
Packit Service d9f699
  } res;
Packit Service d9f699
  uint8_t *pp = res.bytes;
Packit Service d9f699
  int digit;
Packit Service d9f699
Packit Service d9f699
  int saved_errno = errno;
Packit Service d9f699
  __set_errno (0);
Packit Service d9f699
Packit Service d9f699
  res.word = 0;
Packit Service d9f699
Packit Service d9f699
  c = *cp;
Packit Service d9f699
  for (;;)
Packit Service d9f699
    {
Packit Service d9f699
      /* Collect number up to ``.''.  Values are specified as for C:
Packit Service d9f699
	 0x=hex, 0=octal, isdigit=decimal.  */
Packit Service d9f699
      if (!isdigit (c))
Packit Service d9f699
	goto ret_0;
Packit Service d9f699
      {
Packit Service d9f699
	char *endp;
Packit Service d9f699
	unsigned long ul = strtoul (cp, &endp, 0);
Packit Service d9f699
	if (ul == ULONG_MAX && errno == ERANGE)
Packit Service 541783
	  goto ret_0;
Packit Service d9f699
	if (ul > 0xfffffffful)
Packit Service d9f699
	  goto ret_0;
Packit Service d9f699
	val = ul;
Packit Service d9f699
	digit = cp != endp;
Packit Service d9f699
	cp = endp;
Packit Service d9f699
      }
Packit Service d9f699
      c = *cp;
Packit Service d9f699
      if (c == '.')
Packit Service d9f699
	{
Packit Service d9f699
	  /* Internet format:
Packit Service d9f699
	     a.b.c.d
Packit Service d9f699
	     a.b.c	(with c treated as 16 bits)
Packit Service d9f699
	     a.b	(with b treated as 24 bits).  */
Packit Service d9f699
	  if (pp > res.bytes + 2 || val > 0xff)
Packit Service d9f699
	    goto ret_0;
Packit Service d9f699
	  *pp++ = val;
Packit Service d9f699
	  c = *++cp;
Packit Service d9f699
	}
Packit Service d9f699
      else
Packit Service d9f699
	break;
Packit Service d9f699
    }
Packit Service d9f699
  /* Check for trailing characters.  */
Packit Service d9f699
  if (c != '\0' && (!isascii (c) || !isspace (c)))
Packit Service d9f699
    goto ret_0;
Packit Service d9f699
  /*  Did we get a valid digit?  */
Packit Service d9f699
  if (!digit)
Packit Service d9f699
    goto ret_0;
Packit Service d9f699
Packit Service d9f699
  /* Check whether the last part is in its limits depending on the
Packit Service d9f699
     number of parts in total.  */
Packit Service d9f699
  if (val > max[pp - res.bytes])
Packit Service d9f699
    goto ret_0;
Packit Service d9f699
Packit Service d9f699
  if (addr != NULL)
Packit Service d9f699
    addr->s_addr = res.word | htonl (val);
Packit Service d9f699
Packit Service d9f699
  __set_errno (saved_errno);
Packit Service d9f699
  return 1;
Packit Service d9f699
Packit Service d9f699
 ret_0:
Packit Service d9f699
  __set_errno (saved_errno);
Packit Service d9f699
  return 0;
Packit Service 50534c
}
Packit Service 541783
weak_alias (__inet_aton, inet_aton)
Packit Service 541783
libc_hidden_def (__inet_aton)
Packit Service 541783
libc_hidden_weak (inet_aton)