1 /* $NetBSD: inet_addr.c,v 1.2 2018/04/07 22:37:30 christos Exp $ */ 2 3 /* NetBSD: inet_addr.c,v 1.6 1996/02/02 15:22:23 mrg Exp */ 4 5 /* 6 * Copyright (c) 1983, 1990, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __RCSID("$NetBSD: inet_addr.c,v 1.2 2018/04/07 22:37:30 christos Exp $"); 36 37 38 #include "dhcpd.h" 39 40 #include "omapip/omapip_p.h" 41 42 #ifdef NEED_INET_ATON 43 /* 44 * Check whether "cp" is a valid ascii representation 45 * of an Internet address and convert to a binary address. 46 * Returns 1 if the address is valid, 0 if not. 47 * This replaces inet_addr, the return value from which 48 * cannot distinguish between failure and a local broadcast address. 49 */ 50 int 51 inet_aton(cp, addr) 52 const char *cp; 53 struct in_addr *addr; 54 { 55 register u_long val; 56 register int base, n; 57 register char c; 58 u_int parts[4]; 59 register u_int *pp = parts; 60 61 for (;;) { 62 /* 63 * Collect number up to ``.''. 64 * Values are specified as for C: 65 * 0x=hex, 0=octal, other=decimal. 66 */ 67 val = 0; base = 10; 68 if (*cp == '0') { 69 if (*++cp == 'x' || *cp == 'X') 70 base = 16, cp++; 71 else 72 base = 8; 73 } 74 while ((c = *cp) != '\0') { 75 if (isascii(c) && isdigit((int)c)) { 76 val = (val * base) + (c - '0'); 77 cp++; 78 continue; 79 } 80 if (base == 16 && isascii(c) && isxdigit((int)c)) { 81 val = (val << 4) + 82 (c + 10 - (islower((int)c) ? 'a' : 'A')); 83 cp++; 84 continue; 85 } 86 break; 87 } 88 if (*cp == '.') { 89 /* 90 * Internet format: 91 * a.b.c.d 92 * a.b.c (with c treated as 16-bits) 93 * a.b (with b treated as 24 bits) 94 */ 95 if (pp >= parts + 3 || val > 0xff) 96 return (0); 97 *pp++ = val, cp++; 98 } else 99 break; 100 } 101 /* 102 * Check for trailing characters. 103 */ 104 if (*cp && (!isascii(*cp) || !isspace((int)*cp))) 105 return (0); 106 /* 107 * Concoct the address according to 108 * the number of parts specified. 109 */ 110 n = pp - parts + 1; 111 switch (n) { 112 113 case 0: 114 return (0); /* initial nondigit */ 115 116 case 1: /* a -- 32 bits */ 117 break; 118 119 case 2: /* a.b -- 8.24 bits */ 120 if (val > 0xffffff) 121 return (0); 122 val |= parts[0] << 24; 123 break; 124 125 case 3: /* a.b.c -- 8.8.16 bits */ 126 if (val > 0xffff) 127 return (0); 128 val |= (parts[0] << 24) | (parts[1] << 16); 129 break; 130 131 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 132 if (val > 0xff) 133 return (0); 134 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 135 break; 136 } 137 if (addr) 138 addr->s_addr = htonl(val); 139 return (1); 140 } 141 #endif 142