1*9da1e81cSryo /* $NetBSD: inet_network.c,v 1.5 2022/10/06 06:03:06 ryo Exp $ */
23fa54233Schristos
33fa54233Schristos /*
43fa54233Schristos * Copyright (c) 1983, 1993
53fa54233Schristos * The Regents of the University of California. All rights reserved.
63fa54233Schristos *
73fa54233Schristos * Redistribution and use in source and binary forms, with or without
83fa54233Schristos * modification, are permitted provided that the following conditions
93fa54233Schristos * are met:
103fa54233Schristos * 1. Redistributions of source code must retain the above copyright
113fa54233Schristos * notice, this list of conditions and the following disclaimer.
123fa54233Schristos * 2. Redistributions in binary form must reproduce the above copyright
133fa54233Schristos * notice, this list of conditions and the following disclaimer in the
143fa54233Schristos * documentation and/or other materials provided with the distribution.
153fa54233Schristos * 3. Neither the name of the University nor the names of its contributors
163fa54233Schristos * may be used to endorse or promote products derived from this software
173fa54233Schristos * without specific prior written permission.
183fa54233Schristos *
193fa54233Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
203fa54233Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213fa54233Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223fa54233Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
233fa54233Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243fa54233Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253fa54233Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263fa54233Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273fa54233Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283fa54233Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293fa54233Schristos * SUCH DAMAGE.
303fa54233Schristos */
313fa54233Schristos
323fa54233Schristos #include <sys/cdefs.h>
333fa54233Schristos #if defined(LIBC_SCCS) && !defined(lint)
343fa54233Schristos #if 0
353fa54233Schristos static char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93";
363fa54233Schristos #else
37*9da1e81cSryo __RCSID("$NetBSD: inet_network.c,v 1.5 2022/10/06 06:03:06 ryo Exp $");
383fa54233Schristos #endif
393fa54233Schristos #endif /* LIBC_SCCS and not lint */
403fa54233Schristos
413fa54233Schristos #include "namespace.h"
423fa54233Schristos #include <sys/types.h>
433fa54233Schristos #include <netinet/in.h>
443fa54233Schristos #include <arpa/inet.h>
453fa54233Schristos
463fa54233Schristos #include <assert.h>
473fa54233Schristos #include <ctype.h>
483fa54233Schristos #ifdef _DIAGNOSTIC
493fa54233Schristos #include <stddef.h> /* for NULL */
503fa54233Schristos #endif
513fa54233Schristos
523fa54233Schristos #ifdef __weak_alias
__weak_alias(inet_network,_inet_network)533fa54233Schristos __weak_alias(inet_network,_inet_network)
543fa54233Schristos #endif
553fa54233Schristos
563fa54233Schristos /*
573fa54233Schristos * Internet network address interpretation routine.
583fa54233Schristos * The library routines call this routine to interpret
593fa54233Schristos * network numbers.
603fa54233Schristos */
613fa54233Schristos in_addr_t
623fa54233Schristos inet_network(const char *cp)
633fa54233Schristos {
643fa54233Schristos in_addr_t val;
653fa54233Schristos size_t i, n;
663fa54233Schristos u_char c;
673fa54233Schristos in_addr_t parts[4], *pp = parts;
683fa54233Schristos int digit, base;
693fa54233Schristos
703fa54233Schristos _DIAGASSERT(cp != NULL);
713fa54233Schristos
723fa54233Schristos again:
733fa54233Schristos val = 0; base = 10; digit = 0;
74*9da1e81cSryo if (*cp == '0') {
753fa54233Schristos digit = 1, base = 8, cp++;
763fa54233Schristos if (*cp == 'x' || *cp == 'X')
770d03389cSginsbach digit = 0, base = 16, cp++;
78*9da1e81cSryo }
793fa54233Schristos while ((c = *cp) != 0) {
803fa54233Schristos if (isdigit(c)) {
813fa54233Schristos if (base == 8 && (c == '8' || c == '9'))
823fa54233Schristos return (INADDR_NONE);
833fa54233Schristos val = (val * base) + (c - '0');
843fa54233Schristos cp++;
853fa54233Schristos digit = 1;
863fa54233Schristos continue;
873fa54233Schristos }
883fa54233Schristos if (base == 16 && isxdigit(c)) {
893fa54233Schristos val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
903fa54233Schristos cp++;
913fa54233Schristos digit = 1;
923fa54233Schristos continue;
933fa54233Schristos }
943fa54233Schristos break;
953fa54233Schristos }
963fa54233Schristos if (!digit)
973fa54233Schristos return (INADDR_NONE);
983fa54233Schristos if (pp >= parts + 4 || val > 0xff)
993fa54233Schristos return (INADDR_NONE);
1004e783482Schristos if (*cp == '.') {
1013fa54233Schristos *pp++ = val, cp++;
1023fa54233Schristos goto again;
1033fa54233Schristos }
1043fa54233Schristos if (*cp && !isspace((u_char) *cp))
1053fa54233Schristos return (INADDR_NONE);
1063fa54233Schristos *pp++ = val;
1073fa54233Schristos n = pp - parts;
1083fa54233Schristos if (n > 4)
1093fa54233Schristos return (INADDR_NONE);
1103fa54233Schristos for (val = 0, i = 0; i < n; i++) {
1113fa54233Schristos val <<= 8;
1123fa54233Schristos val |= parts[i] & 0xff;
1133fa54233Schristos }
1143fa54233Schristos return (val);
1153fa54233Schristos }
116