121367Sdist /* 221367Sdist * Copyright (c) 1983 Regents of the University of California. 321367Sdist * All rights reserved. The Berkeley software License Agreement 421367Sdist * specifies the terms and conditions for redistribution. 521367Sdist */ 68324Ssam 726612Sdonn #if defined(LIBC_SCCS) && !defined(lint) 8*32314Sbostic static char sccsid[] = "@(#)inet_addr.c 5.4 (Berkeley) 10/01/87"; 926612Sdonn #endif LIBC_SCCS and not lint 1021367Sdist 118324Ssam #include <sys/types.h> 128324Ssam #include <ctype.h> 139192Ssam #include <netinet/in.h> 148324Ssam 158309Ssam /* 168309Ssam * Internet address interpretation routine. 178309Ssam * All the network library routines call this 188309Ssam * routine to interpret entries in the data bases 198309Ssam * which are expected to be an address. 208357Ssam * The value returned is in network order. 218309Ssam */ 228361Ssam u_long 238309Ssam inet_addr(cp) 248309Ssam register char *cp; 258309Ssam { 268357Ssam register u_long val, base, n; 278309Ssam register char c; 288357Ssam u_long parts[4], *pp = parts; 298309Ssam 308309Ssam again: 318357Ssam /* 328357Ssam * Collect number up to ``.''. 338357Ssam * Values are specified as for C: 348357Ssam * 0x=hex, 0=octal, other=decimal. 358357Ssam */ 368309Ssam val = 0; base = 10; 3731352Sbostic if (*cp == '0') { 3831352Sbostic if (*++cp == 'x' || *cp == 'X') 3931352Sbostic base = 16, cp++; 4031352Sbostic else 4131352Sbostic base = 8; 4231352Sbostic } 438309Ssam while (c = *cp) { 448309Ssam if (isdigit(c)) { 458309Ssam val = (val * base) + (c - '0'); 468309Ssam cp++; 478309Ssam continue; 488309Ssam } 498309Ssam if (base == 16 && isxdigit(c)) { 508309Ssam val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 518309Ssam cp++; 528309Ssam continue; 538309Ssam } 548309Ssam break; 558309Ssam } 568309Ssam if (*cp == '.') { 578309Ssam /* 588309Ssam * Internet format: 598309Ssam * a.b.c.d 608309Ssam * a.b.c (with c treated as 16-bits) 618309Ssam * a.b (with b treated as 24 bits) 628309Ssam */ 638309Ssam if (pp >= parts + 4) 64*32314Sbostic return (INADDR_NONE); 658309Ssam *pp++ = val, cp++; 668309Ssam goto again; 678309Ssam } 688357Ssam /* 698357Ssam * Check for trailing characters. 708357Ssam */ 718309Ssam if (*cp && !isspace(*cp)) 72*32314Sbostic return (INADDR_NONE); 738357Ssam *pp++ = val; 748357Ssam /* 758357Ssam * Concoct the address according to 768357Ssam * the number of parts specified. 778357Ssam */ 788309Ssam n = pp - parts; 798357Ssam switch (n) { 808357Ssam 818357Ssam case 1: /* a -- 32 bits */ 828309Ssam val = parts[0]; 838357Ssam break; 848357Ssam 858357Ssam case 2: /* a.b -- 8.24 bits */ 868357Ssam val = (parts[0] << 24) | (parts[1] & 0xffffff); 878357Ssam break; 888357Ssam 898357Ssam case 3: /* a.b.c -- 8.8.16 bits */ 908357Ssam val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | 918357Ssam (parts[2] & 0xffff); 928357Ssam break; 938357Ssam 948357Ssam case 4: /* a.b.c.d -- 8.8.8.8 bits */ 958357Ssam val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | 968357Ssam ((parts[2] & 0xff) << 8) | (parts[3] & 0xff); 978357Ssam break; 988357Ssam 998357Ssam default: 100*32314Sbostic return (INADDR_NONE); 1018309Ssam } 1028357Ssam val = htonl(val); 1038309Ssam return (val); 1048309Ssam } 105