|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1983, 1990 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted provided ! 6: * that: (1) source distributions retain this entire copyright notice and ! 7: * comment, and (2) distributions including binaries display the following ! 8: * acknowledgement: ``This product includes software developed by the ! 9: * University of California, Berkeley and its contributors'' in the ! 10: * documentation or other materials provided with the distribution and in ! 11: * all advertising materials mentioning features or use of this software. ! 12: * Neither the name of the University nor the names of its contributors may ! 13: * be used to endorse or promote products derived from this software without ! 14: * specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #if defined(LIBC_SCCS) && !defined(lint) ! 21: static char sccsid[] = "@(#)inet_addr.c 5.8 (Berkeley) 6/23/90"; ! 22: #endif /* LIBC_SCCS and not lint */ ! 23: ! 24: #include <sys/types.h> ! 25: #include <ctype.h> ! 26: #include <netinet/in.h> ! 27: ! 28: /* ! 29: * Ascii internet address interpretation routine. ! 30: * The value returned is in network order. ! 31: */ ! 32: u_long ! 33: inet_addr(cp) ! 34: register char *cp; ! 35: { ! 36: struct in_addr val; ! 37: ! 38: if (inet_aton(cp, &val)) ! 39: return (val.s_addr); ! 40: return (INADDR_NONE); ! 41: } ! 42: ! 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: ! 51: inet_aton(cp, addr) ! 52: register char *cp; ! 53: struct in_addr *addr; ! 54: { ! 55: register u_long val, base, n; ! 56: register char c; ! 57: u_long parts[4], *pp = parts; ! 58: ! 59: for (;;) { ! 60: /* ! 61: * Collect number up to ``.''. ! 62: * Values are specified as for C: ! 63: * 0x=hex, 0=octal, other=decimal. ! 64: */ ! 65: val = 0; base = 10; ! 66: if (*cp == '0') { ! 67: if (*++cp == 'x' || *cp == 'X') ! 68: base = 16, cp++; ! 69: else ! 70: base = 8; ! 71: } ! 72: while ((c = *cp) != '\0') { ! 73: if (isascii(c) && isdigit(c)) { ! 74: val = (val * base) + (c - '0'); ! 75: cp++; ! 76: continue; ! 77: } ! 78: if (base == 16 && isascii(c) && isxdigit(c)) { ! 79: val = (val << 4) + ! 80: (c + 10 - (islower(c) ? 'a' : 'A')); ! 81: cp++; ! 82: continue; ! 83: } ! 84: break; ! 85: } ! 86: if (*cp == '.') { ! 87: /* ! 88: * Internet format: ! 89: * a.b.c.d ! 90: * a.b.c (with c treated as 16-bits) ! 91: * a.b (with b treated as 24 bits) ! 92: */ ! 93: if (pp >= parts + 3 || val > 0xff) ! 94: return (0); ! 95: *pp++ = val, cp++; ! 96: } else ! 97: break; ! 98: } ! 99: /* ! 100: * Check for trailing characters. ! 101: */ ! 102: if (*cp && (!isascii(*cp) || !isspace(*cp))) ! 103: return (0); ! 104: /* ! 105: * Concoct the address according to ! 106: * the number of parts specified. ! 107: */ ! 108: n = pp - parts + 1; ! 109: switch (n) { ! 110: ! 111: case 1: /* a -- 32 bits */ ! 112: break; ! 113: ! 114: case 2: /* a.b -- 8.24 bits */ ! 115: if (val > 0xffffff) ! 116: return (0); ! 117: val |= parts[0] << 24; ! 118: break; ! 119: ! 120: case 3: /* a.b.c -- 8.8.16 bits */ ! 121: if (val > 0xffff) ! 122: return (0); ! 123: val |= (parts[0] << 24) | (parts[1] << 16); ! 124: break; ! 125: ! 126: case 4: /* a.b.c.d -- 8.8.8.8 bits */ ! 127: if (val > 0xff) ! 128: return (0); ! 129: val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); ! 130: break; ! 131: } ! 132: if (addr) ! 133: addr->s_addr = htonl(val); ! 134: return (1); ! 135: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.