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_network.c 5.5 (Berkeley) 06/27/88"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/types.h> 23 #include <netinet/in.h> 24 #include <ctype.h> 25 26 /* 27 * Internet network address interpretation routine. 28 * The library routines call this routine to interpret 29 * network numbers. 30 */ 31 u_long 32 inet_network(cp) 33 register char *cp; 34 { 35 register u_long val, base, n; 36 register char c; 37 u_long parts[4], *pp = parts; 38 register int i; 39 40 again: 41 val = 0; base = 10; 42 if (*cp == '0') 43 base = 8, cp++; 44 if (*cp == 'x' || *cp == 'X') 45 base = 16, cp++; 46 while (c = *cp) { 47 if (isdigit(c)) { 48 val = (val * base) + (c - '0'); 49 cp++; 50 continue; 51 } 52 if (base == 16 && isxdigit(c)) { 53 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 54 cp++; 55 continue; 56 } 57 break; 58 } 59 if (*cp == '.') { 60 if (pp >= parts + 4) 61 return (INADDR_NONE); 62 *pp++ = val, cp++; 63 goto again; 64 } 65 if (*cp && !isspace(*cp)) 66 return (INADDR_NONE); 67 *pp++ = val; 68 n = pp - parts; 69 if (n > 4) 70 return (INADDR_NONE); 71 for (val = 0, i = 0; i < n; i++) { 72 val <<= 8; 73 val |= parts[i] & 0xff; 74 } 75 return (val); 76 } 77