* Copyright (c) 1983 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)inet_addr.c 5.6 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
* Internet address interpretation routine.
* All the network library routines call this
* routine to interpret entries in the data bases
* which are expected to be an address.
* The value returned is in network order.
register u_long val
, base
, n
;
u_long parts
[4], *pp
= parts
;
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, other=decimal.
if (*++cp
== 'x' || *cp
== 'X')
val
= (val
* base
) + (c
- '0');
if (base
== 16 && isxdigit(c
)) {
val
= (val
<< 4) + (c
+ 10 - (islower(c
) ? 'a' : 'A'));
* a.b.c (with c treated as 16-bits)
* a.b (with b treated as 24 bits)
* Check for trailing characters.
if (*cp
&& !isspace(*cp
))
* Concoct the address according to
* the number of parts specified.
case 1: /* a -- 32 bits */
case 2: /* a.b -- 8.24 bits */
val
= (parts
[0] << 24) | (parts
[1] & 0xffffff);
case 3: /* a.b.c -- 8.8.16 bits */
val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
case 4: /* a.b.c.d -- 8.8.8.8 bits */
val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
((parts
[2] & 0xff) << 8) | (parts
[3] & 0xff);