|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1983 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that the above copyright notice and this paragraph are ! 7: * duplicated in all such forms and that any documentation, ! 8: * advertising materials, and other materials related to such ! 9: * distribution and use acknowledge that the software was developed ! 10: * by the University of California, Berkeley. The name of the ! 11: * University may not be used to endorse or promote products derived ! 12: * from this software without specific prior written permission. ! 13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 16: */ ! 17: ! 18: #if defined(LIBC_SCCS) && !defined(lint) ! 19: static char sccsid[] = "@(#)inet_addr.c 5.6 (Berkeley) 6/27/88"; ! 20: #endif /* LIBC_SCCS and not lint */ ! 21: ! 22: #include <sys/types.h> ! 23: #include <ctype.h> ! 24: #include <netinet/in.h> ! 25: ! 26: /* ! 27: * Internet address interpretation routine. ! 28: * All the network library routines call this ! 29: * routine to interpret entries in the data bases ! 30: * which are expected to be an address. ! 31: * The value returned is in network order. ! 32: */ ! 33: u_long ! 34: inet_addr(cp) ! 35: register char *cp; ! 36: { ! 37: register u_long val, base, n; ! 38: register char c; ! 39: u_long parts[4], *pp = parts; ! 40: ! 41: again: ! 42: /* ! 43: * Collect number up to ``.''. ! 44: * Values are specified as for C: ! 45: * 0x=hex, 0=octal, other=decimal. ! 46: */ ! 47: val = 0; base = 10; ! 48: if (*cp == '0') { ! 49: if (*++cp == 'x' || *cp == 'X') ! 50: base = 16, cp++; ! 51: else ! 52: base = 8; ! 53: } ! 54: while (c = *cp) { ! 55: if (isdigit(c)) { ! 56: val = (val * base) + (c - '0'); ! 57: cp++; ! 58: continue; ! 59: } ! 60: if (base == 16 && isxdigit(c)) { ! 61: val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); ! 62: cp++; ! 63: continue; ! 64: } ! 65: break; ! 66: } ! 67: if (*cp == '.') { ! 68: /* ! 69: * Internet format: ! 70: * a.b.c.d ! 71: * a.b.c (with c treated as 16-bits) ! 72: * a.b (with b treated as 24 bits) ! 73: */ ! 74: if (pp >= parts + 4) ! 75: return (INADDR_NONE); ! 76: *pp++ = val, cp++; ! 77: goto again; ! 78: } ! 79: /* ! 80: * Check for trailing characters. ! 81: */ ! 82: if (*cp && !isspace(*cp)) ! 83: return (INADDR_NONE); ! 84: *pp++ = val; ! 85: /* ! 86: * Concoct the address according to ! 87: * the number of parts specified. ! 88: */ ! 89: n = pp - parts; ! 90: switch (n) { ! 91: ! 92: case 1: /* a -- 32 bits */ ! 93: val = parts[0]; ! 94: break; ! 95: ! 96: case 2: /* a.b -- 8.24 bits */ ! 97: val = (parts[0] << 24) | (parts[1] & 0xffffff); ! 98: break; ! 99: ! 100: case 3: /* a.b.c -- 8.8.16 bits */ ! 101: val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | ! 102: (parts[2] & 0xffff); ! 103: break; ! 104: ! 105: case 4: /* a.b.c.d -- 8.8.8.8 bits */ ! 106: val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | ! 107: ((parts[2] & 0xff) << 8) | (parts[3] & 0xff); ! 108: break; ! 109: ! 110: default: ! 111: return (INADDR_NONE); ! 112: } ! 113: val = htonl(val); ! 114: return (val); ! 115: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.