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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13 #if defined(LIBC_SCCS) && !defined(lint) 14 static char sccsid[] = "@(#)inet_network.c 5.4 (Berkeley) 03/07/88"; 15 #endif /* LIBC_SCCS and not lint */ 16 17 #include <sys/types.h> 18 #include <netinet/in.h> 19 #include <ctype.h> 20 21 /* 22 * Internet network address interpretation routine. 23 * The library routines call this routine to interpret 24 * network numbers. 25 */ 26 u_long 27 inet_network(cp) 28 register char *cp; 29 { 30 register u_long val, base, n; 31 register char c; 32 u_long parts[4], *pp = parts; 33 register int i; 34 35 again: 36 val = 0; base = 10; 37 if (*cp == '0') 38 base = 8, cp++; 39 if (*cp == 'x' || *cp == 'X') 40 base = 16, cp++; 41 while (c = *cp) { 42 if (isdigit(c)) { 43 val = (val * base) + (c - '0'); 44 cp++; 45 continue; 46 } 47 if (base == 16 && isxdigit(c)) { 48 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 49 cp++; 50 continue; 51 } 52 break; 53 } 54 if (*cp == '.') { 55 if (pp >= parts + 4) 56 return (INADDR_NONE); 57 *pp++ = val, cp++; 58 goto again; 59 } 60 if (*cp && !isspace(*cp)) 61 return (INADDR_NONE); 62 *pp++ = val; 63 n = pp - parts; 64 if (n > 4) 65 return (INADDR_NONE); 66 for (val = 0, i = 0; i < n; i++) { 67 val <<= 8; 68 val |= parts[i] & 0xff; 69 } 70 return (val); 71 } 72