Blame inet/inet_net.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1983, 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
/* Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Internet network address interpretation routine.
Packit 6c4009
 * The library routines call this routine to interpret
Packit 6c4009
 * network numbers.
Packit 6c4009
 */
Packit 6c4009
uint32_t
Packit 6c4009
inet_network (const char *cp)
Packit 6c4009
{
Packit 6c4009
	uint32_t val, base, n, i;
Packit 6c4009
	char c;
Packit 6c4009
	uint32_t parts[4], *pp = parts;
Packit 6c4009
	int digit;
Packit 6c4009
Packit 6c4009
again:
Packit 6c4009
	val = 0; base = 10; digit = 0;
Packit 6c4009
	if (*cp == '0')
Packit 6c4009
		digit = 1, base = 8, cp++;
Packit 6c4009
	if (*cp == 'x' || *cp == 'X')
Packit 6c4009
		digit = 0, base = 16, cp++;
Packit 6c4009
	while ((c = *cp) != 0) {
Packit 6c4009
		if (isdigit(c)) {
Packit 6c4009
			if (base == 8 && (c == '8' || c == '9'))
Packit 6c4009
				return (INADDR_NONE);
Packit 6c4009
			val = (val * base) + (c - '0');
Packit 6c4009
			cp++;
Packit 6c4009
			digit = 1;
Packit 6c4009
			continue;
Packit 6c4009
		}
Packit 6c4009
		if (base == 16 && isxdigit(c)) {
Packit 6c4009
			val = (val << 4) + (tolower (c) + 10 - 'a');
Packit 6c4009
			cp++;
Packit 6c4009
			digit = 1;
Packit 6c4009
			continue;
Packit 6c4009
		}
Packit 6c4009
		break;
Packit 6c4009
	}
Packit 6c4009
	if (!digit)
Packit 6c4009
		return (INADDR_NONE);
Packit 6c4009
	if (pp >= parts + 4 || val > 0xff)
Packit 6c4009
		return (INADDR_NONE);
Packit 6c4009
	if (*cp == '.') {
Packit 6c4009
		*pp++ = val, cp++;
Packit 6c4009
		goto again;
Packit 6c4009
	}
Packit 6c4009
	while (isspace(*cp))
Packit 6c4009
		cp++;
Packit 6c4009
	if (*cp)
Packit 6c4009
		return (INADDR_NONE);
Packit 6c4009
	if (pp >= parts + 4 || val > 0xff)
Packit 6c4009
		return (INADDR_NONE);
Packit 6c4009
	*pp++ = val;
Packit 6c4009
	n = pp - parts;
Packit 6c4009
	for (val = 0, i = 0; i < n; i++) {
Packit 6c4009
		val <<= 8;
Packit 6c4009
		val |= parts[i] & 0xff;
Packit 6c4009
	}
Packit 6c4009
	return (val);
Packit 6c4009
}